k8s.io/kubernetes@v1.29.3/pkg/scheduler/testing/framework/framework_helpers.go (about) 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 framework 18 19 import ( 20 "context" 21 22 "k8s.io/apimachinery/pkg/runtime/schema" 23 kubeschedulerconfigv1 "k8s.io/kube-scheduler/config/v1" 24 schedulerapi "k8s.io/kubernetes/pkg/scheduler/apis/config" 25 "k8s.io/kubernetes/pkg/scheduler/apis/config/scheme" 26 "k8s.io/kubernetes/pkg/scheduler/framework" 27 "k8s.io/kubernetes/pkg/scheduler/framework/runtime" 28 ) 29 30 var configDecoder = scheme.Codecs.UniversalDecoder() 31 32 // NewFramework creates a Framework from the register functions and options. 33 func NewFramework(ctx context.Context, fns []RegisterPluginFunc, profileName string, opts ...runtime.Option) (framework.Framework, error) { 34 registry := runtime.Registry{} 35 profile := &schedulerapi.KubeSchedulerProfile{ 36 SchedulerName: profileName, 37 Plugins: &schedulerapi.Plugins{}, 38 } 39 for _, f := range fns { 40 f(®istry, profile) 41 } 42 return runtime.NewFramework(ctx, registry, profile, opts...) 43 } 44 45 // RegisterPluginFunc is a function signature used in method RegisterFilterPlugin() 46 // to register a Filter Plugin to a given registry. 47 type RegisterPluginFunc func(reg *runtime.Registry, profile *schedulerapi.KubeSchedulerProfile) 48 49 // RegisterQueueSortPlugin returns a function to register a QueueSort Plugin to a given registry. 50 func RegisterQueueSortPlugin(pluginName string, pluginNewFunc runtime.PluginFactory) RegisterPluginFunc { 51 return RegisterPluginAsExtensions(pluginName, pluginNewFunc, "QueueSort") 52 } 53 54 // RegisterPreFilterPlugin returns a function to register a PreFilter Plugin to a given registry. 55 func RegisterPreFilterPlugin(pluginName string, pluginNewFunc runtime.PluginFactory) RegisterPluginFunc { 56 return RegisterPluginAsExtensions(pluginName, pluginNewFunc, "PreFilter") 57 } 58 59 // RegisterFilterPlugin returns a function to register a Filter Plugin to a given registry. 60 func RegisterFilterPlugin(pluginName string, pluginNewFunc runtime.PluginFactory) RegisterPluginFunc { 61 return RegisterPluginAsExtensions(pluginName, pluginNewFunc, "Filter") 62 } 63 64 // RegisterReservePlugin returns a function to register a Reserve Plugin to a given registry. 65 func RegisterReservePlugin(pluginName string, pluginNewFunc runtime.PluginFactory) RegisterPluginFunc { 66 return RegisterPluginAsExtensions(pluginName, pluginNewFunc, "Reserve") 67 } 68 69 // RegisterPermitPlugin returns a function to register a Permit Plugin to a given registry. 70 func RegisterPermitPlugin(pluginName string, pluginNewFunc runtime.PluginFactory) RegisterPluginFunc { 71 return RegisterPluginAsExtensions(pluginName, pluginNewFunc, "Permit") 72 } 73 74 // RegisterPreBindPlugin returns a function to register a PreBind Plugin to a given registry. 75 func RegisterPreBindPlugin(pluginName string, pluginNewFunc runtime.PluginFactory) RegisterPluginFunc { 76 return RegisterPluginAsExtensions(pluginName, pluginNewFunc, "PreBind") 77 } 78 79 // RegisterScorePlugin returns a function to register a Score Plugin to a given registry. 80 func RegisterScorePlugin(pluginName string, pluginNewFunc runtime.PluginFactory, weight int32) RegisterPluginFunc { 81 return RegisterPluginAsExtensionsWithWeight(pluginName, weight, pluginNewFunc, "Score") 82 } 83 84 // RegisterPreScorePlugin returns a function to register a Score Plugin to a given registry. 85 func RegisterPreScorePlugin(pluginName string, pluginNewFunc runtime.PluginFactory) RegisterPluginFunc { 86 return RegisterPluginAsExtensions(pluginName, pluginNewFunc, "PreScore") 87 } 88 89 // RegisterBindPlugin returns a function to register a Bind Plugin to a given registry. 90 func RegisterBindPlugin(pluginName string, pluginNewFunc runtime.PluginFactory) RegisterPluginFunc { 91 return RegisterPluginAsExtensions(pluginName, pluginNewFunc, "Bind") 92 } 93 94 // RegisterPluginAsExtensions returns a function to register a Plugin as given extensionPoints to a given registry. 95 func RegisterPluginAsExtensions(pluginName string, pluginNewFunc runtime.PluginFactory, extensions ...string) RegisterPluginFunc { 96 return RegisterPluginAsExtensionsWithWeight(pluginName, 1, pluginNewFunc, extensions...) 97 } 98 99 // RegisterPluginAsExtensionsWithWeight returns a function to register a Plugin as given extensionPoints with weight to a given registry. 100 func RegisterPluginAsExtensionsWithWeight(pluginName string, weight int32, pluginNewFunc runtime.PluginFactory, extensions ...string) RegisterPluginFunc { 101 return func(reg *runtime.Registry, profile *schedulerapi.KubeSchedulerProfile) { 102 reg.Register(pluginName, pluginNewFunc) 103 for _, extension := range extensions { 104 ps := getPluginSetByExtension(profile.Plugins, extension) 105 if ps == nil { 106 continue 107 } 108 ps.Enabled = append(ps.Enabled, schedulerapi.Plugin{Name: pluginName, Weight: weight}) 109 } 110 // Use defaults from latest config API version. 111 var gvk schema.GroupVersionKind 112 gvk = kubeschedulerconfigv1.SchemeGroupVersion.WithKind(pluginName + "Args") 113 if args, _, err := configDecoder.Decode(nil, &gvk, nil); err == nil { 114 profile.PluginConfig = append(profile.PluginConfig, schedulerapi.PluginConfig{ 115 Name: pluginName, 116 Args: args, 117 }) 118 } 119 } 120 } 121 122 func getPluginSetByExtension(plugins *schedulerapi.Plugins, extension string) *schedulerapi.PluginSet { 123 switch extension { 124 case "QueueSort": 125 return &plugins.QueueSort 126 case "Filter": 127 return &plugins.Filter 128 case "PreFilter": 129 return &plugins.PreFilter 130 case "PreScore": 131 return &plugins.PreScore 132 case "Score": 133 return &plugins.Score 134 case "Bind": 135 return &plugins.Bind 136 case "Reserve": 137 return &plugins.Reserve 138 case "Permit": 139 return &plugins.Permit 140 case "PreBind": 141 return &plugins.PreBind 142 case "PostBind": 143 return &plugins.PostBind 144 default: 145 return nil 146 } 147 }