Skip to content

Commit 6324285

Browse files
committed
Add a helper function to decode scheduler plugin args
1 parent 1afcd7d commit 6324285

File tree

3 files changed

+129
-0
lines changed

3 files changed

+129
-0
lines changed

pkg/scheduler/framework/v1alpha1/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ go_library(
1919
"//staging/src/k8s.io/api/core/v1:go_default_library",
2020
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
2121
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
22+
"//staging/src/k8s.io/apimachinery/pkg/util/json:go_default_library",
2223
"//staging/src/k8s.io/client-go/util/workqueue:go_default_library",
2324
"//vendor/k8s.io/klog:go_default_library",
25+
"//vendor/sigs.k8s.io/yaml:go_default_library",
2426
],
2527
)
2628

@@ -43,10 +45,12 @@ go_test(
4345
srcs = [
4446
"framework_test.go",
4547
"interface_test.go",
48+
"registry_test.go",
4649
],
4750
embed = [":go_default_library"],
4851
deps = [
4952
"//pkg/scheduler/apis/config:go_default_library",
53+
"//pkg/scheduler/apis/config/scheme:go_default_library",
5054
"//staging/src/k8s.io/api/core/v1:go_default_library",
5155
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
5256
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",

pkg/scheduler/framework/v1alpha1/registry.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,30 @@ import (
2020
"fmt"
2121

2222
"k8s.io/apimachinery/pkg/runtime"
23+
"k8s.io/apimachinery/pkg/util/json"
24+
"sigs.k8s.io/yaml"
2325
)
2426

2527
// PluginFactory is a function that builds a plugin.
2628
type PluginFactory = func(configuration *runtime.Unknown, f FrameworkHandle) (Plugin, error)
2729

30+
// DecodeInto decodes configuration whose type is *runtime.Unknown to the interface into.
31+
func DecodeInto(configuration *runtime.Unknown, into interface{}) error {
32+
if configuration == nil {
33+
return nil
34+
}
35+
36+
switch configuration.ContentType {
37+
// If ContentType is empty, it means ContentTypeJSON by default.
38+
case runtime.ContentTypeJSON, "":
39+
return json.Unmarshal(configuration.Raw, into)
40+
case runtime.ContentTypeYAML:
41+
return yaml.Unmarshal(configuration.Raw, into)
42+
default:
43+
return fmt.Errorf("not supported content type %s", configuration.ContentType)
44+
}
45+
}
46+
2847
// Registry is a collection of all available plugins. The framework uses a
2948
// registry to enable and initialize configured plugins.
3049
// All plugins must be in the registry before initializing the framework.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/kubernetes/pkg/scheduler/apis/config"
25+
"k8s.io/kubernetes/pkg/scheduler/apis/config/scheme"
26+
)
27+
28+
func TestDecodeInto(t *testing.T) {
29+
type PluginFooConfig struct {
30+
FooTest string `json:"foo_test,omitempty"`
31+
}
32+
tests := []struct {
33+
name string
34+
schedulerConfig string
35+
expeted PluginFooConfig
36+
}{
37+
{
38+
name: "test decode for JSON config",
39+
schedulerConfig: `{
40+
"kind": "KubeSchedulerConfiguration",
41+
"apiVersion": "kubescheduler.config.k8s.io/v1alpha1",
42+
"plugins": {
43+
"permit": {
44+
"enabled": [
45+
{
46+
"name": "foo"
47+
}
48+
]
49+
}
50+
},
51+
"pluginConfig": [
52+
{
53+
"name": "foo",
54+
"args": {
55+
"foo_test": "test decode"
56+
}
57+
}
58+
]
59+
}`,
60+
expeted: PluginFooConfig{
61+
FooTest: "test decode",
62+
},
63+
},
64+
{
65+
name: "test decode for YAML config",
66+
schedulerConfig: `
67+
apiVersion: kubescheduler.config.k8s.io/v1alpha1
68+
kind: KubeSchedulerConfiguration
69+
plugins:
70+
permit:
71+
enabled:
72+
- name: foo
73+
pluginConfig:
74+
- name: foo
75+
args:
76+
foo_test: "test decode"`,
77+
expeted: PluginFooConfig{
78+
FooTest: "test decode",
79+
},
80+
},
81+
}
82+
for i, test := range tests {
83+
schedulerConf, err := loadConfig([]byte(test.schedulerConfig))
84+
if err != nil {
85+
t.Errorf("Test #%v(%s): failed to load scheduler config: %v", i, test.name, err)
86+
}
87+
var pluginFooConf PluginFooConfig
88+
if err := DecodeInto(&schedulerConf.PluginConfig[0].Args, &pluginFooConf); err != nil {
89+
t.Errorf("Test #%v(%s): failed to decode args %+v: %v",
90+
i, test.name, schedulerConf.PluginConfig[0].Args, err)
91+
}
92+
if !reflect.DeepEqual(pluginFooConf, test.expeted) {
93+
t.Errorf("Test #%v(%s): failed to decode plugin config, expected: %+v, got: %+v",
94+
i, test.name, test.expeted, pluginFooConf)
95+
}
96+
}
97+
}
98+
99+
func loadConfig(data []byte) (*config.KubeSchedulerConfiguration, error) {
100+
configObj := &config.KubeSchedulerConfiguration{}
101+
if err := runtime.DecodeInto(scheme.Codecs.UniversalDecoder(), data, configObj); err != nil {
102+
return nil, err
103+
}
104+
105+
return configObj, nil
106+
}

0 commit comments

Comments
 (0)