...
1
16
17 package runtime
18
19 import (
20 "context"
21 "fmt"
22
23 "k8s.io/apimachinery/pkg/runtime"
24 "k8s.io/apimachinery/pkg/util/json"
25 "k8s.io/kubernetes/pkg/scheduler/framework"
26 plfeature "k8s.io/kubernetes/pkg/scheduler/framework/plugins/feature"
27 "sigs.k8s.io/yaml"
28 )
29
30
31 type PluginFactory = func(ctx context.Context, configuration runtime.Object, f framework.Handle) (framework.Plugin, error)
32
33
34 type PluginFactoryWithFts func(context.Context, runtime.Object, framework.Handle, plfeature.Features) (framework.Plugin, error)
35
36
37
38 func FactoryAdapter(fts plfeature.Features, withFts PluginFactoryWithFts) PluginFactory {
39 return func(ctx context.Context, plArgs runtime.Object, fh framework.Handle) (framework.Plugin, error) {
40 return withFts(ctx, plArgs, fh, fts)
41 }
42 }
43
44
45 func DecodeInto(obj runtime.Object, into interface{}) error {
46 if obj == nil {
47 return nil
48 }
49 configuration, ok := obj.(*runtime.Unknown)
50 if !ok {
51 return fmt.Errorf("want args of type runtime.Unknown, got %T", obj)
52 }
53 if configuration.Raw == nil {
54 return nil
55 }
56
57 switch configuration.ContentType {
58
59 case runtime.ContentTypeJSON, "":
60 return json.Unmarshal(configuration.Raw, into)
61 case runtime.ContentTypeYAML:
62 return yaml.Unmarshal(configuration.Raw, into)
63 default:
64 return fmt.Errorf("not supported content type %s", configuration.ContentType)
65 }
66 }
67
68
69
70
71 type Registry map[string]PluginFactory
72
73
74
75 func (r Registry) Register(name string, factory PluginFactory) error {
76 if _, ok := r[name]; ok {
77 return fmt.Errorf("a plugin named %v already exists", name)
78 }
79 r[name] = factory
80 return nil
81 }
82
83
84
85 func (r Registry) Unregister(name string) error {
86 if _, ok := r[name]; !ok {
87 return fmt.Errorf("no plugin named %v exists", name)
88 }
89 delete(r, name)
90 return nil
91 }
92
93
94 func (r Registry) Merge(in Registry) error {
95 for name, factory := range in {
96 if err := r.Register(name, factory); err != nil {
97 return err
98 }
99 }
100 return nil
101 }
102
View as plain text