k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/scheduler/apis/config/v1/default_plugins.go (about) 1 /* 2 Copyright 2022 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 v1 18 19 import ( 20 "k8s.io/apimachinery/pkg/util/sets" 21 utilfeature "k8s.io/apiserver/pkg/util/feature" 22 "k8s.io/klog/v2" 23 v1 "k8s.io/kube-scheduler/config/v1" 24 "k8s.io/kubernetes/pkg/features" 25 "k8s.io/kubernetes/pkg/scheduler/framework/plugins/names" 26 "k8s.io/utils/ptr" 27 ) 28 29 // getDefaultPlugins returns the default set of plugins. 30 func getDefaultPlugins() *v1.Plugins { 31 plugins := &v1.Plugins{ 32 MultiPoint: v1.PluginSet{ 33 Enabled: []v1.Plugin{ 34 {Name: names.SchedulingGates}, 35 {Name: names.PrioritySort}, 36 {Name: names.NodeUnschedulable}, 37 {Name: names.NodeName}, 38 {Name: names.TaintToleration, Weight: ptr.To[int32](3)}, 39 {Name: names.NodeAffinity, Weight: ptr.To[int32](2)}, 40 {Name: names.NodePorts}, 41 {Name: names.NodeResourcesFit, Weight: ptr.To[int32](1)}, 42 {Name: names.VolumeRestrictions}, 43 {Name: names.NodeVolumeLimits}, 44 {Name: names.VolumeBinding}, 45 {Name: names.VolumeZone}, 46 {Name: names.PodTopologySpread, Weight: ptr.To[int32](2)}, 47 {Name: names.InterPodAffinity, Weight: ptr.To[int32](2)}, 48 {Name: names.DefaultPreemption}, 49 {Name: names.NodeResourcesBalancedAllocation, Weight: ptr.To[int32](1)}, 50 {Name: names.ImageLocality, Weight: ptr.To[int32](1)}, 51 {Name: names.DefaultBinder}, 52 }, 53 }, 54 } 55 applyFeatureGates(plugins) 56 57 return plugins 58 } 59 60 func applyFeatureGates(config *v1.Plugins) { 61 if utilfeature.DefaultFeatureGate.Enabled(features.DynamicResourceAllocation) { 62 // This plugin should come before DefaultPreemption because if 63 // there is a problem with a Pod and PostFilter gets called to 64 // resolve the problem, it is better to first deallocate an 65 // idle ResourceClaim than it is to evict some Pod that might 66 // be doing useful work. 67 for i := range config.MultiPoint.Enabled { 68 if config.MultiPoint.Enabled[i].Name == names.DefaultPreemption { 69 extended := make([]v1.Plugin, 0, len(config.MultiPoint.Enabled)+1) 70 extended = append(extended, config.MultiPoint.Enabled[:i]...) 71 extended = append(extended, v1.Plugin{Name: names.DynamicResources}) 72 extended = append(extended, config.MultiPoint.Enabled[i:]...) 73 config.MultiPoint.Enabled = extended 74 break 75 } 76 } 77 } 78 } 79 80 // mergePlugins merges the custom set into the given default one, handling disabled sets. 81 func mergePlugins(logger klog.Logger, defaultPlugins, customPlugins *v1.Plugins) *v1.Plugins { 82 if customPlugins == nil { 83 return defaultPlugins 84 } 85 86 defaultPlugins.MultiPoint = mergePluginSet(logger, defaultPlugins.MultiPoint, customPlugins.MultiPoint) 87 defaultPlugins.PreEnqueue = mergePluginSet(logger, defaultPlugins.PreEnqueue, customPlugins.PreEnqueue) 88 defaultPlugins.QueueSort = mergePluginSet(logger, defaultPlugins.QueueSort, customPlugins.QueueSort) 89 defaultPlugins.PreFilter = mergePluginSet(logger, defaultPlugins.PreFilter, customPlugins.PreFilter) 90 defaultPlugins.Filter = mergePluginSet(logger, defaultPlugins.Filter, customPlugins.Filter) 91 defaultPlugins.PostFilter = mergePluginSet(logger, defaultPlugins.PostFilter, customPlugins.PostFilter) 92 defaultPlugins.PreScore = mergePluginSet(logger, defaultPlugins.PreScore, customPlugins.PreScore) 93 defaultPlugins.Score = mergePluginSet(logger, defaultPlugins.Score, customPlugins.Score) 94 defaultPlugins.Reserve = mergePluginSet(logger, defaultPlugins.Reserve, customPlugins.Reserve) 95 defaultPlugins.Permit = mergePluginSet(logger, defaultPlugins.Permit, customPlugins.Permit) 96 defaultPlugins.PreBind = mergePluginSet(logger, defaultPlugins.PreBind, customPlugins.PreBind) 97 defaultPlugins.Bind = mergePluginSet(logger, defaultPlugins.Bind, customPlugins.Bind) 98 defaultPlugins.PostBind = mergePluginSet(logger, defaultPlugins.PostBind, customPlugins.PostBind) 99 return defaultPlugins 100 } 101 102 type pluginIndex struct { 103 index int 104 plugin v1.Plugin 105 } 106 107 func mergePluginSet(logger klog.Logger, defaultPluginSet, customPluginSet v1.PluginSet) v1.PluginSet { 108 disabledPlugins := sets.New[string]() 109 enabledCustomPlugins := make(map[string]pluginIndex) 110 // replacedPluginIndex is a set of index of plugins, which have replaced the default plugins. 111 replacedPluginIndex := sets.New[int]() 112 var disabled []v1.Plugin 113 for _, disabledPlugin := range customPluginSet.Disabled { 114 // if the user is manually disabling any (or all, with "*") default plugins for an extension point, 115 // we need to track that so that the MultiPoint extension logic in the framework can know to skip 116 // inserting unspecified default plugins to this point. 117 disabled = append(disabled, v1.Plugin{Name: disabledPlugin.Name}) 118 disabledPlugins.Insert(disabledPlugin.Name) 119 } 120 121 // With MultiPoint, we may now have some disabledPlugins in the default registry 122 // For example, we enable PluginX with Filter+Score through MultiPoint but disable its Score plugin by default. 123 for _, disabledPlugin := range defaultPluginSet.Disabled { 124 disabled = append(disabled, v1.Plugin{Name: disabledPlugin.Name}) 125 disabledPlugins.Insert(disabledPlugin.Name) 126 } 127 128 for index, enabledPlugin := range customPluginSet.Enabled { 129 enabledCustomPlugins[enabledPlugin.Name] = pluginIndex{index, enabledPlugin} 130 } 131 var enabledPlugins []v1.Plugin 132 if !disabledPlugins.Has("*") { 133 for _, defaultEnabledPlugin := range defaultPluginSet.Enabled { 134 if disabledPlugins.Has(defaultEnabledPlugin.Name) { 135 continue 136 } 137 // The default plugin is explicitly re-configured, update the default plugin accordingly. 138 if customPlugin, ok := enabledCustomPlugins[defaultEnabledPlugin.Name]; ok { 139 logger.Info("Default plugin is explicitly re-configured; overriding", "plugin", defaultEnabledPlugin.Name) 140 // Update the default plugin in place to preserve order. 141 defaultEnabledPlugin = customPlugin.plugin 142 replacedPluginIndex.Insert(customPlugin.index) 143 } 144 enabledPlugins = append(enabledPlugins, defaultEnabledPlugin) 145 } 146 } 147 148 // Append all the custom plugins which haven't replaced any default plugins. 149 // Note: duplicated custom plugins will still be appended here. 150 // If so, the instantiation of scheduler framework will detect it and abort. 151 for index, plugin := range customPluginSet.Enabled { 152 if !replacedPluginIndex.Has(index) { 153 enabledPlugins = append(enabledPlugins, plugin) 154 } 155 } 156 return v1.PluginSet{Enabled: enabledPlugins, Disabled: disabled} 157 }