Skip to content

Commit ca46c1d

Browse files
committed
[scheduler] Move predicate & priority registration to separate file
- Maintain list of default predicates and priorities in defaults.go and move the registration to separate files Signed-off-by: Bhavin Gandhi <[email protected]>
1 parent 190f6d8 commit ca46c1d

File tree

7 files changed

+317
-182
lines changed

7 files changed

+317
-182
lines changed

pkg/scheduler/algorithm/priorities/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ go_library(
1818
"node_affinity.go",
1919
"node_label.go",
2020
"node_prefer_avoid_pods.go",
21+
"priorities.go",
2122
"reduce.go",
2223
"requested_to_capacity_ratio.go",
2324
"resource_allocation.go",
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
Copyright 2018 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 priorities
18+
19+
const (
20+
// EqualPriority defines the name of prioritizer function that gives an equal weight of one to all nodes.
21+
EqualPriority = "EqualPriority"
22+
// MostRequestedPriority defines the name of prioritizer function that gives used nodes higher priority.
23+
MostRequestedPriority = "MostRequestedPriority"
24+
// RequestedToCapacityRatioPriority defines the name of RequestedToCapacityRatioPriority.
25+
RequestedToCapacityRatioPriority = "RequestedToCapacityRatioPriority"
26+
// SelectorSpreadPriority defines the name of prioritizer function that spreads pods by minimizing
27+
// the number of pods (belonging to the same service or replication controller) on the same node.
28+
SelectorSpreadPriority = "SelectorSpreadPriority"
29+
// ServiceSpreadingPriority is largely replaced by "SelectorSpreadPriority".
30+
ServiceSpreadingPriority = "ServiceSpreadingPriority"
31+
// InterPodAffinityPriority defines the name of prioritizer function that decides which pods should or
32+
// should not be placed in the same topological domain as some other pods.
33+
InterPodAffinityPriority = "InterPodAffinityPriority"
34+
// LeastRequestedPriority defines the name of prioritizer function that prioritize nodes by least
35+
// requested utilization.
36+
LeastRequestedPriority = "LeastRequestedPriority"
37+
// BalancedResourceAllocation defines the name of prioritizer function that prioritizes nodes
38+
// to help achieve balanced resource usage.
39+
BalancedResourceAllocation = "BalancedResourceAllocation"
40+
// NodePreferAvoidPodsPriority defines the name of prioritizer function that priorities nodes according to
41+
// the node annotation "scheduler.alpha.kubernetes.io/preferAvoidPods".
42+
NodePreferAvoidPodsPriority = "NodePreferAvoidPodsPriority"
43+
// NodeAffinityPriority defines the name of prioritizer function that prioritizes nodes which have labels
44+
// matching NodeAffinity.
45+
NodeAffinityPriority = "NodeAffinityPriority"
46+
// TaintTolerationPriority defines the name of prioritizer function that prioritizes nodes that marked
47+
// with taint which pod can tolerate.
48+
TaintTolerationPriority = "TaintTolerationPriority"
49+
// ImageLocalityPriority defines the name of prioritizer function that prioritizes nodes that have images
50+
// requested by the pod present.
51+
ImageLocalityPriority = "ImageLocalityPriority"
52+
// ResourceLimitsPriority defines the nodes of prioritizer function ResourceLimitsPriority.
53+
ResourceLimitsPriority = "ResourceLimitsPriority"
54+
)

pkg/scheduler/algorithmprovider/defaults/BUILD

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ load(
88

99
go_library(
1010
name = "go_default_library",
11-
srcs = ["defaults.go"],
11+
srcs = [
12+
"defaults.go",
13+
"register_predicates.go",
14+
"register_priorities.go",
15+
],
1216
importpath = "k8s.io/kubernetes/pkg/scheduler/algorithmprovider/defaults",
1317
deps = [
1418
"//pkg/features:go_default_library",
@@ -29,6 +33,7 @@ go_test(
2933
embed = [":go_default_library"],
3034
deps = [
3135
"//pkg/scheduler/algorithm/predicates:go_default_library",
36+
"//pkg/scheduler/algorithm/priorities:go_default_library",
3237
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
3338
],
3439
)

pkg/scheduler/algorithmprovider/defaults/defaults.go

Lines changed: 25 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@ import (
2323
utilfeature "k8s.io/apiserver/pkg/util/feature"
2424

2525
"k8s.io/kubernetes/pkg/features"
26-
"k8s.io/kubernetes/pkg/scheduler/algorithm"
2726
"k8s.io/kubernetes/pkg/scheduler/algorithm/predicates"
2827
"k8s.io/kubernetes/pkg/scheduler/algorithm/priorities"
29-
"k8s.io/kubernetes/pkg/scheduler/core"
3028
"k8s.io/kubernetes/pkg/scheduler/factory"
3129
)
3230

@@ -36,139 +34,25 @@ const (
3634
)
3735

3836
func init() {
39-
// Register functions that extract metadata used by predicates and priorities computations.
40-
factory.RegisterPredicateMetadataProducerFactory(
41-
func(args factory.PluginFactoryArgs) predicates.PredicateMetadataProducer {
42-
return predicates.NewPredicateMetadataFactory(args.PodLister)
43-
})
44-
factory.RegisterPriorityMetadataProducerFactory(
45-
func(args factory.PluginFactoryArgs) algorithm.PriorityMetadataProducer {
46-
return priorities.NewPriorityMetadataFactory(args.ServiceLister, args.ControllerLister, args.ReplicaSetLister, args.StatefulSetLister)
47-
})
48-
4937
registerAlgorithmProvider(defaultPredicates(), defaultPriorities())
50-
51-
// IMPORTANT NOTES for predicate developers:
52-
// Registers predicates and priorities that are not enabled by default, but user can pick when creating their
53-
// own set of priorities/predicates.
54-
55-
// PodFitsPorts has been replaced by PodFitsHostPorts for better user understanding.
56-
// For backwards compatibility with 1.0, PodFitsPorts is registered as well.
57-
factory.RegisterFitPredicate("PodFitsPorts", predicates.PodFitsHostPorts)
58-
// Fit is defined based on the absence of port conflicts.
59-
// This predicate is actually a default predicate, because it is invoked from
60-
// predicates.GeneralPredicates()
61-
factory.RegisterFitPredicate(predicates.PodFitsHostPortsPred, predicates.PodFitsHostPorts)
62-
// Fit is determined by resource availability.
63-
// This predicate is actually a default predicate, because it is invoked from
64-
// predicates.GeneralPredicates()
65-
factory.RegisterFitPredicate(predicates.PodFitsResourcesPred, predicates.PodFitsResources)
66-
// Fit is determined by the presence of the Host parameter and a string match
67-
// This predicate is actually a default predicate, because it is invoked from
68-
// predicates.GeneralPredicates()
69-
factory.RegisterFitPredicate(predicates.HostNamePred, predicates.PodFitsHost)
70-
// Fit is determined by node selector query.
71-
factory.RegisterFitPredicate(predicates.MatchNodeSelectorPred, predicates.PodMatchNodeSelector)
72-
73-
// ServiceSpreadingPriority is a priority config factory that spreads pods by minimizing
74-
// the number of pods (belonging to the same service) on the same node.
75-
// Register the factory so that it's available, but do not include it as part of the default priorities
76-
// Largely replaced by "SelectorSpreadPriority", but registered for backward compatibility with 1.0
77-
factory.RegisterPriorityConfigFactory(
78-
"ServiceSpreadingPriority",
79-
factory.PriorityConfigFactory{
80-
MapReduceFunction: func(args factory.PluginFactoryArgs) (algorithm.PriorityMapFunction, algorithm.PriorityReduceFunction) {
81-
return priorities.NewSelectorSpreadPriority(args.ServiceLister, algorithm.EmptyControllerLister{}, algorithm.EmptyReplicaSetLister{}, algorithm.EmptyStatefulSetLister{})
82-
},
83-
Weight: 1,
84-
},
85-
)
86-
// EqualPriority is a prioritizer function that gives an equal weight of one to all nodes
87-
// Register the priority function so that its available
88-
// but do not include it as part of the default priorities
89-
factory.RegisterPriorityFunction2("EqualPriority", core.EqualPriorityMap, nil, 1)
90-
// Optional, cluster-autoscaler friendly priority function - give used nodes higher priority.
91-
factory.RegisterPriorityFunction2("MostRequestedPriority", priorities.MostRequestedPriorityMap, nil, 1)
92-
factory.RegisterPriorityFunction2(
93-
"RequestedToCapacityRatioPriority",
94-
priorities.RequestedToCapacityRatioResourceAllocationPriorityDefault().PriorityMap,
95-
nil,
96-
1)
9738
}
9839

9940
func defaultPredicates() sets.String {
10041
return sets.NewString(
101-
// Fit is determined by volume zone requirements.
102-
factory.RegisterFitPredicateFactory(
103-
predicates.NoVolumeZoneConflictPred,
104-
func(args factory.PluginFactoryArgs) predicates.FitPredicate {
105-
return predicates.NewVolumeZonePredicate(args.PVInfo, args.PVCInfo, args.StorageClassInfo)
106-
},
107-
),
108-
// Fit is determined by whether or not there would be too many AWS EBS volumes attached to the node
109-
factory.RegisterFitPredicateFactory(
110-
predicates.MaxEBSVolumeCountPred,
111-
func(args factory.PluginFactoryArgs) predicates.FitPredicate {
112-
return predicates.NewMaxPDVolumeCountPredicate(predicates.EBSVolumeFilterType, args.PVInfo, args.PVCInfo)
113-
},
114-
),
115-
// Fit is determined by whether or not there would be too many GCE PD volumes attached to the node
116-
factory.RegisterFitPredicateFactory(
117-
predicates.MaxGCEPDVolumeCountPred,
118-
func(args factory.PluginFactoryArgs) predicates.FitPredicate {
119-
return predicates.NewMaxPDVolumeCountPredicate(predicates.GCEPDVolumeFilterType, args.PVInfo, args.PVCInfo)
120-
},
121-
),
122-
// Fit is determined by whether or not there would be too many Azure Disk volumes attached to the node
123-
factory.RegisterFitPredicateFactory(
124-
predicates.MaxAzureDiskVolumeCountPred,
125-
func(args factory.PluginFactoryArgs) predicates.FitPredicate {
126-
return predicates.NewMaxPDVolumeCountPredicate(predicates.AzureDiskVolumeFilterType, args.PVInfo, args.PVCInfo)
127-
},
128-
),
129-
factory.RegisterFitPredicateFactory(
130-
predicates.MaxCSIVolumeCountPred,
131-
func(args factory.PluginFactoryArgs) predicates.FitPredicate {
132-
return predicates.NewCSIMaxVolumeLimitPredicate(args.PVInfo, args.PVCInfo)
133-
},
134-
),
135-
// Fit is determined by inter-pod affinity.
136-
factory.RegisterFitPredicateFactory(
137-
predicates.MatchInterPodAffinityPred,
138-
func(args factory.PluginFactoryArgs) predicates.FitPredicate {
139-
return predicates.NewPodAffinityPredicate(args.NodeInfo, args.PodLister)
140-
},
141-
),
142-
143-
// Fit is determined by non-conflicting disk volumes.
144-
factory.RegisterFitPredicate(predicates.NoDiskConflictPred, predicates.NoDiskConflict),
145-
146-
// GeneralPredicates are the predicates that are enforced by all Kubernetes components
147-
// (e.g. kubelet and all schedulers)
148-
factory.RegisterFitPredicate(predicates.GeneralPred, predicates.GeneralPredicates),
149-
150-
// Fit is determined by node memory pressure condition.
151-
factory.RegisterFitPredicate(predicates.CheckNodeMemoryPressurePred, predicates.CheckNodeMemoryPressurePredicate),
152-
153-
// Fit is determined by node disk pressure condition.
154-
factory.RegisterFitPredicate(predicates.CheckNodeDiskPressurePred, predicates.CheckNodeDiskPressurePredicate),
155-
156-
// Fit is determined by node pid pressure condition.
157-
factory.RegisterFitPredicate(predicates.CheckNodePIDPressurePred, predicates.CheckNodePIDPressurePredicate),
158-
159-
// Fit is determined by node conditions: not ready, network unavailable or out of disk.
160-
factory.RegisterMandatoryFitPredicate(predicates.CheckNodeConditionPred, predicates.CheckNodeConditionPredicate),
161-
162-
// Fit is determined based on whether a pod can tolerate all of the node's taints
163-
factory.RegisterFitPredicate(predicates.PodToleratesNodeTaintsPred, predicates.PodToleratesNodeTaints),
164-
165-
// Fit is determined by volume topology requirements.
166-
factory.RegisterFitPredicateFactory(
167-
predicates.CheckVolumeBindingPred,
168-
func(args factory.PluginFactoryArgs) predicates.FitPredicate {
169-
return predicates.NewVolumeBindingPredicate(args.VolumeBinder)
170-
},
171-
),
42+
predicates.NoVolumeZoneConflictPred,
43+
predicates.MaxEBSVolumeCountPred,
44+
predicates.MaxGCEPDVolumeCountPred,
45+
predicates.MaxAzureDiskVolumeCountPred,
46+
predicates.MaxCSIVolumeCountPred,
47+
predicates.MatchInterPodAffinityPred,
48+
predicates.NoDiskConflictPred,
49+
predicates.GeneralPred,
50+
predicates.CheckNodeMemoryPressurePred,
51+
predicates.CheckNodeDiskPressurePred,
52+
predicates.CheckNodePIDPressurePred,
53+
predicates.CheckNodeConditionPred,
54+
predicates.PodToleratesNodeTaintsPred,
55+
predicates.CheckVolumeBindingPred,
17256
)
17357
}
17458

@@ -206,9 +90,9 @@ func ApplyFeatureGates() {
20690
// Prioritizes nodes that satisfy pod's resource limits
20791
if utilfeature.DefaultFeatureGate.Enabled(features.ResourceLimitsPriorityFunction) {
20892
klog.Infof("Registering resourcelimits priority function")
209-
factory.RegisterPriorityFunction2("ResourceLimitsPriority", priorities.ResourceLimitsPriorityMap, nil, 1)
93+
factory.RegisterPriorityFunction2(priorities.ResourceLimitsPriority, priorities.ResourceLimitsPriorityMap, nil, 1)
21094
// Register the priority function to specific provider too.
211-
factory.InsertPriorityKeyToAlgorithmProviderMap(factory.RegisterPriorityFunction2("ResourceLimitsPriority", priorities.ResourceLimitsPriorityMap, nil, 1))
95+
factory.InsertPriorityKeyToAlgorithmProviderMap(factory.RegisterPriorityFunction2(priorities.ResourceLimitsPriority, priorities.ResourceLimitsPriorityMap, nil, 1))
21296
}
21397
}
21498

@@ -218,51 +102,19 @@ func registerAlgorithmProvider(predSet, priSet sets.String) {
218102
factory.RegisterAlgorithmProvider(factory.DefaultProvider, predSet, priSet)
219103
// Cluster autoscaler friendly scheduling algorithm.
220104
factory.RegisterAlgorithmProvider(ClusterAutoscalerProvider, predSet,
221-
copyAndReplace(priSet, "LeastRequestedPriority", "MostRequestedPriority"))
105+
copyAndReplace(priSet, priorities.LeastRequestedPriority, priorities.MostRequestedPriority))
222106
}
223107

224108
func defaultPriorities() sets.String {
225109
return sets.NewString(
226-
// spreads pods by minimizing the number of pods (belonging to the same service or replication controller) on the same node.
227-
factory.RegisterPriorityConfigFactory(
228-
"SelectorSpreadPriority",
229-
factory.PriorityConfigFactory{
230-
MapReduceFunction: func(args factory.PluginFactoryArgs) (algorithm.PriorityMapFunction, algorithm.PriorityReduceFunction) {
231-
return priorities.NewSelectorSpreadPriority(args.ServiceLister, args.ControllerLister, args.ReplicaSetLister, args.StatefulSetLister)
232-
},
233-
Weight: 1,
234-
},
235-
),
236-
// pods should be placed in the same topological domain (e.g. same node, same rack, same zone, same power domain, etc.)
237-
// as some other pods, or, conversely, should not be placed in the same topological domain as some other pods.
238-
factory.RegisterPriorityConfigFactory(
239-
"InterPodAffinityPriority",
240-
factory.PriorityConfigFactory{
241-
Function: func(args factory.PluginFactoryArgs) algorithm.PriorityFunction {
242-
return priorities.NewInterPodAffinityPriority(args.NodeInfo, args.NodeLister, args.PodLister, args.HardPodAffinitySymmetricWeight)
243-
},
244-
Weight: 1,
245-
},
246-
),
247-
248-
// Prioritize nodes by least requested utilization.
249-
factory.RegisterPriorityFunction2("LeastRequestedPriority", priorities.LeastRequestedPriorityMap, nil, 1),
250-
251-
// Prioritizes nodes to help achieve balanced resource usage
252-
factory.RegisterPriorityFunction2("BalancedResourceAllocation", priorities.BalancedResourceAllocationMap, nil, 1),
253-
254-
// Set this weight large enough to override all other priority functions.
255-
// TODO: Figure out a better way to do this, maybe at same time as fixing #24720.
256-
factory.RegisterPriorityFunction2("NodePreferAvoidPodsPriority", priorities.CalculateNodePreferAvoidPodsPriorityMap, nil, 10000),
257-
258-
// Prioritizes nodes that have labels matching NodeAffinity
259-
factory.RegisterPriorityFunction2("NodeAffinityPriority", priorities.CalculateNodeAffinityPriorityMap, priorities.CalculateNodeAffinityPriorityReduce, 1),
260-
261-
// Prioritizes nodes that marked with taint which pod can tolerate.
262-
factory.RegisterPriorityFunction2("TaintTolerationPriority", priorities.ComputeTaintTolerationPriorityMap, priorities.ComputeTaintTolerationPriorityReduce, 1),
263-
264-
// ImageLocalityPriority prioritizes nodes that have images requested by the pod present.
265-
factory.RegisterPriorityFunction2("ImageLocalityPriority", priorities.ImageLocalityPriorityMap, nil, 1),
110+
priorities.SelectorSpreadPriority,
111+
priorities.InterPodAffinityPriority,
112+
priorities.LeastRequestedPriority,
113+
priorities.BalancedResourceAllocation,
114+
priorities.NodePreferAvoidPodsPriority,
115+
priorities.NodeAffinityPriority,
116+
priorities.TaintTolerationPriority,
117+
priorities.ImageLocalityPriority,
266118
)
267119
}
268120

pkg/scheduler/algorithmprovider/defaults/defaults_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121

2222
"k8s.io/apimachinery/pkg/util/sets"
2323
"k8s.io/kubernetes/pkg/scheduler/algorithm/predicates"
24+
"k8s.io/kubernetes/pkg/scheduler/algorithm/priorities"
2425
)
2526

2627
func TestCopyAndReplace(t *testing.T) {
@@ -53,14 +54,15 @@ func TestCopyAndReplace(t *testing.T) {
5354

5455
func TestDefaultPriorities(t *testing.T) {
5556
result := sets.NewString(
56-
"SelectorSpreadPriority",
57-
"InterPodAffinityPriority",
58-
"LeastRequestedPriority",
59-
"BalancedResourceAllocation",
60-
"NodePreferAvoidPodsPriority",
61-
"NodeAffinityPriority",
62-
"TaintTolerationPriority",
63-
"ImageLocalityPriority")
57+
priorities.SelectorSpreadPriority,
58+
priorities.InterPodAffinityPriority,
59+
priorities.LeastRequestedPriority,
60+
priorities.BalancedResourceAllocation,
61+
priorities.NodePreferAvoidPodsPriority,
62+
priorities.NodeAffinityPriority,
63+
priorities.TaintTolerationPriority,
64+
priorities.ImageLocalityPriority,
65+
)
6466
if expected := defaultPriorities(); !result.Equal(expected) {
6567
t.Errorf("expected %v got %v", expected, result)
6668
}

0 commit comments

Comments
 (0)