generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 135
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support the wasm extension in the debuggable scheduler (#393)
- Loading branch information
1 parent
70484f1
commit b3c7a44
Showing
5 changed files
with
194 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package config | ||
|
||
import ( | ||
"golang.org/x/xerrors" | ||
"k8s.io/apimachinery/pkg/util/sets" | ||
configv1 "k8s.io/kube-scheduler/config/v1" | ||
"k8s.io/kubernetes/pkg/scheduler/apis/config" | ||
"k8s.io/kubernetes/pkg/scheduler/apis/config/scheme" | ||
"k8s.io/kubernetes/pkg/scheduler/framework/runtime" | ||
wasm "sigs.k8s.io/kube-scheduler-wasm-extension/scheduler/plugin" | ||
) | ||
|
||
// RegisterWasmPlugins registers wasm plugins from the given configuration. | ||
func RegisterWasmPlugins(versionedCfg *configv1.KubeSchedulerConfiguration) error { | ||
cfg := config.KubeSchedulerConfiguration{} | ||
if err := scheme.Scheme.Convert(versionedCfg, &cfg, nil); err != nil { | ||
return xerrors.Errorf("convert configuration: %w", err) | ||
} | ||
|
||
registry, err := getWasmRegistryFromUnversionedConfig(&cfg) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
SetOutOfTreeRegistries(registry) | ||
|
||
return nil | ||
} | ||
|
||
// getWasmRegistryFromUnversionedConfig registers wasm plugins from the given unversioned configuration. | ||
func getWasmRegistryFromUnversionedConfig(cfg *config.KubeSchedulerConfiguration) (runtime.Registry, error) { | ||
registry := runtime.Registry{} | ||
|
||
for _, profile := range cfg.Profiles { | ||
wasmplugins := sets.New[string]() | ||
// look for the wasm plugin in the plugin config. | ||
for _, config := range profile.PluginConfig { | ||
if err := runtime.DecodeInto(config.Args, &wasm.PluginConfig{}); err != nil { | ||
// not wasm plugin. | ||
continue | ||
} | ||
|
||
wasmplugins.Insert(config.Name) | ||
} | ||
|
||
// look for the wasm plugin in the enabled plugins. | ||
// (assuming that the wasm plugin is specified as a multi-point plugin.) | ||
for _, plugin := range profile.Plugins.MultiPoint.Enabled { | ||
if wasmplugins.Has(plugin.Name) { | ||
if err := registry.Register(plugin.Name, wasm.PluginFactory(plugin.Name)); err != nil { | ||
return nil, xerrors.Errorf("register plugin %s: %w", plugin.Name, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return registry, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package config | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/kubernetes/pkg/scheduler/apis/config" | ||
) | ||
|
||
func TestGetWasmRegistryFromUnversionedConfig(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
cfg *config.KubeSchedulerConfiguration | ||
expected int | ||
}{ | ||
{ | ||
name: "no profiles", | ||
cfg: &config.KubeSchedulerConfiguration{}, | ||
expected: 0, | ||
}, | ||
{ | ||
name: "no wasm plugins", | ||
cfg: &config.KubeSchedulerConfiguration{ | ||
Profiles: []config.KubeSchedulerProfile{ | ||
{ | ||
PluginConfig: []config.PluginConfig{ | ||
{ | ||
Name: "DefaultPreemption", | ||
Args: &config.DefaultPreemptionArgs{}, | ||
}, | ||
}, | ||
Plugins: &config.Plugins{ | ||
MultiPoint: config.PluginSet{ | ||
Enabled: []config.Plugin{ | ||
{Name: "DefaultPreemption"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: 0, | ||
}, | ||
{ | ||
name: "one wasm plugin", | ||
cfg: &config.KubeSchedulerConfiguration{ | ||
Profiles: []config.KubeSchedulerProfile{ | ||
{ | ||
PluginConfig: []config.PluginConfig{ | ||
{ | ||
Name: "DefaultPreemption", | ||
Args: &config.DefaultPreemptionArgs{}, | ||
}, | ||
{Name: "wasmPlugin", Args: &runtime.Unknown{ | ||
ContentType: runtime.ContentTypeJSON, | ||
Raw: []byte(`{"guestURL":"http://example.com/plugin.wasm"}`), | ||
}}, | ||
}, | ||
Plugins: &config.Plugins{ | ||
MultiPoint: config.PluginSet{ | ||
Enabled: []config.Plugin{ | ||
{Name: "DefaultPreemption"}, | ||
{Name: "wasmPlugin"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: 1, | ||
}, | ||
{ | ||
name: "multiple wasm plugins", | ||
cfg: &config.KubeSchedulerConfiguration{ | ||
Profiles: []config.KubeSchedulerProfile{ | ||
{ | ||
PluginConfig: []config.PluginConfig{ | ||
{ | ||
Name: "DefaultPreemption", | ||
Args: &config.DefaultPreemptionArgs{}, | ||
}, | ||
{Name: "wasmPlugin1", Args: &runtime.Unknown{ | ||
ContentType: runtime.ContentTypeJSON, | ||
Raw: []byte(`{"guestURL":"http://example.com/plugin1.wasm"}`), | ||
}}, | ||
{Name: "wasmPlugin2", Args: &runtime.Unknown{ | ||
ContentType: runtime.ContentTypeJSON, | ||
Raw: []byte(`{"guestURL":"http://example.com/plugin2.wasm"}`), | ||
}}, | ||
}, | ||
Plugins: &config.Plugins{ | ||
MultiPoint: config.PluginSet{ | ||
Enabled: []config.Plugin{ | ||
{Name: "DefaultPreemption"}, | ||
{Name: "wasmPlugin1"}, | ||
{Name: "wasmPlugin2"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expected: 2, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
registry, err := getWasmRegistryFromUnversionedConfig(tt.cfg) | ||
require.NoError(t, err, "check error") | ||
if len(registry) != tt.expected { | ||
t.Errorf("expected %d plugins, got %d", tt.expected, len(registry)) | ||
} | ||
}) | ||
} | ||
} |