Skip to content

Commit a783404

Browse files
authored
Merge pull request kubernetes#83650 from wgliang/scheduler-v2/pod-fits-resources
[migration phase 1] PodFitsResources as framework plugin
2 parents ceceaca + 4ca3d9c commit a783404

File tree

7 files changed

+607
-13
lines changed

7 files changed

+607
-13
lines changed

pkg/scheduler/api/compatibility/compatibility_test.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
6565
]
6666
}`,
6767
wantPredicates: sets.NewString(
68-
"PodFitsResources",
6968
"PodFitsPorts",
7069
"TestServiceAffinity",
7170
"TestLabelsPresence",
@@ -79,6 +78,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
7978
wantPlugins: map[string][]kubeschedulerconfig.Plugin{
8079
"FilterPlugin": {
8180
{Name: "NodeAffinity"},
81+
{Name: "NodeResources"},
8282
{Name: "VolumeRestrictions"},
8383
},
8484
},
@@ -109,7 +109,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
109109
}`,
110110
wantPredicates: sets.NewString(
111111
"PodFitsHostPorts",
112-
"PodFitsResources",
113112
"TestServiceAffinity",
114113
"TestLabelsPresence",
115114
),
@@ -125,6 +124,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
125124
"FilterPlugin": {
126125
{Name: "NodeName"},
127126
{Name: "NodeAffinity"},
127+
{Name: "NodeResources"},
128128
{Name: "VolumeRestrictions"},
129129
},
130130
},
@@ -160,7 +160,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
160160
]
161161
}`,
162162
wantPredicates: sets.NewString(
163-
"PodFitsResources",
164163
"PodFitsHostPorts",
165164
"NoVolumeZoneConflict",
166165
"MaxEBSVolumeCount",
@@ -183,6 +182,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
183182
"FilterPlugin": {
184183
{Name: "NodeName"},
185184
{Name: "NodeAffinity"},
185+
{Name: "NodeResources"},
186186
{Name: "VolumeRestrictions"},
187187
},
188188
},
@@ -222,7 +222,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
222222
]
223223
}`,
224224
wantPredicates: sets.NewString(
225-
"PodFitsResources",
226225
"PodFitsHostPorts",
227226
"NoVolumeZoneConflict",
228227
"CheckNodeMemoryPressure",
@@ -247,6 +246,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
247246
"FilterPlugin": {
248247
{Name: "NodeName"},
249248
{Name: "NodeAffinity"},
249+
{Name: "NodeResources"},
250250
{Name: "VolumeRestrictions"},
251251
{Name: "TaintToleration"},
252252
},
@@ -291,7 +291,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
291291
]
292292
}`,
293293
wantPredicates: sets.NewString(
294-
"PodFitsResources",
295294
"PodFitsHostPorts",
296295
"NoVolumeZoneConflict",
297296
"CheckNodeMemoryPressure",
@@ -319,6 +318,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
319318
"FilterPlugin": {
320319
{Name: "NodeName"},
321320
{Name: "NodeAffinity"},
321+
{Name: "NodeResources"},
322322
{Name: "VolumeRestrictions"},
323323
{Name: "TaintToleration"},
324324
},
@@ -372,7 +372,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
372372
}]
373373
}`,
374374
wantPredicates: sets.NewString(
375-
"PodFitsResources",
376375
"PodFitsHostPorts",
377376
"NoVolumeZoneConflict",
378377
"CheckNodeMemoryPressure",
@@ -400,6 +399,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
400399
"FilterPlugin": {
401400
{Name: "NodeName"},
402401
{Name: "NodeAffinity"},
402+
{Name: "NodeResources"},
403403
{Name: "VolumeRestrictions"},
404404
{Name: "TaintToleration"},
405405
},
@@ -465,7 +465,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
465465
}]
466466
}`,
467467
wantPredicates: sets.NewString(
468-
"PodFitsResources",
469468
"PodFitsHostPorts",
470469
"NoVolumeZoneConflict",
471470
"CheckNodeMemoryPressure",
@@ -494,6 +493,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
494493
"FilterPlugin": {
495494
{Name: "NodeName"},
496495
{Name: "NodeAffinity"},
496+
{Name: "NodeResources"},
497497
{Name: "VolumeRestrictions"},
498498
{Name: "TaintToleration"},
499499
},
@@ -560,7 +560,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
560560
}]
561561
}`,
562562
wantPredicates: sets.NewString(
563-
"PodFitsResources",
564563
"PodFitsHostPorts",
565564
"NoVolumeZoneConflict",
566565
"CheckNodeMemoryPressure",
@@ -589,6 +588,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
589588
"FilterPlugin": {
590589
{Name: "NodeName"},
591590
{Name: "NodeAffinity"},
591+
{Name: "NodeResources"},
592592
{Name: "VolumeRestrictions"},
593593
{Name: "TaintToleration"},
594594
{Name: "VolumeBinding"},
@@ -660,7 +660,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
660660
}]
661661
}`,
662662
wantPredicates: sets.NewString(
663-
"PodFitsResources",
664663
"PodFitsHostPorts",
665664
"NoVolumeZoneConflict",
666665
"CheckNodeMemoryPressure",
@@ -690,6 +689,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
690689
"FilterPlugin": {
691690
{Name: "NodeName"},
692691
{Name: "NodeAffinity"},
692+
{Name: "NodeResources"},
693693
{Name: "VolumeRestrictions"},
694694
{Name: "TaintToleration"},
695695
{Name: "VolumeBinding"},
@@ -773,7 +773,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
773773
}]
774774
}`,
775775
wantPredicates: sets.NewString(
776-
"PodFitsResources",
777776
"PodFitsHostPorts",
778777
"NoVolumeZoneConflict",
779778
"CheckNodeMemoryPressure",
@@ -804,6 +803,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
804803
"FilterPlugin": {
805804
{Name: "NodeName"},
806805
{Name: "NodeAffinity"},
806+
{Name: "NodeResources"},
807807
{Name: "VolumeRestrictions"},
808808
{Name: "TaintToleration"},
809809
{Name: "VolumeBinding"},
@@ -888,7 +888,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
888888
}]
889889
}`,
890890
wantPredicates: sets.NewString(
891-
"PodFitsResources",
892891
"PodFitsHostPorts",
893892
"NoVolumeZoneConflict",
894893
"CheckNodeMemoryPressure",
@@ -920,6 +919,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
920919
"FilterPlugin": {
921920
{Name: "NodeName"},
922921
{Name: "NodeAffinity"},
922+
{Name: "NodeResources"},
923923
{Name: "VolumeRestrictions"},
924924
{Name: "TaintToleration"},
925925
{Name: "VolumeBinding"},
@@ -1003,7 +1003,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
10031003
}]
10041004
}`,
10051005
wantPredicates: sets.NewString(
1006-
"PodFitsResources",
10071006
"PodFitsHostPorts",
10081007
"NoVolumeZoneConflict",
10091008
"CheckNodeMemoryPressure",
@@ -1036,6 +1035,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
10361035
"FilterPlugin": {
10371036
{Name: "NodeName"},
10381037
{Name: "NodeAffinity"},
1038+
{Name: "NodeResources"},
10391039
{Name: "VolumeRestrictions"},
10401040
{Name: "TaintToleration"},
10411041
{Name: "VolumeBinding"},
@@ -1123,7 +1123,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
11231123
}]
11241124
}`,
11251125
wantPredicates: sets.NewString(
1126-
"PodFitsResources",
11271126
"PodFitsHostPorts",
11281127
"NoVolumeZoneConflict",
11291128
"CheckNodeMemoryPressure",
@@ -1156,6 +1155,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
11561155
"FilterPlugin": {
11571156
{Name: "NodeName"},
11581157
{Name: "NodeAffinity"},
1158+
{Name: "NodeResources"},
11591159
{Name: "VolumeRestrictions"},
11601160
{Name: "TaintToleration"},
11611161
{Name: "VolumeBinding"},
@@ -1185,6 +1185,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
11851185
filterToPredicateMap := map[string]string{
11861186
"TaintToleration": "PodToleratesNodeTaints",
11871187
"NodeName": "HostName",
1188+
"NodeResources": "PodFitsResources",
11881189
"NodeAffinity": "MatchNodeSelector",
11891190
"VolumeBinding": "CheckVolumeBinding",
11901191
"VolumeRestrictions": "NoDiskConflict",

pkg/scheduler/framework/plugins/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ go_library(
1212
"//pkg/scheduler/apis/config:go_default_library",
1313
"//pkg/scheduler/framework/plugins/nodeaffinity:go_default_library",
1414
"//pkg/scheduler/framework/plugins/nodename:go_default_library",
15+
"//pkg/scheduler/framework/plugins/noderesources:go_default_library",
1516
"//pkg/scheduler/framework/plugins/tainttoleration:go_default_library",
1617
"//pkg/scheduler/framework/plugins/volumebinding:go_default_library",
1718
"//pkg/scheduler/framework/plugins/volumerestrictions:go_default_library",
@@ -39,6 +40,7 @@ filegroup(
3940
"//pkg/scheduler/framework/plugins/migration:all-srcs",
4041
"//pkg/scheduler/framework/plugins/nodeaffinity:all-srcs",
4142
"//pkg/scheduler/framework/plugins/nodename:all-srcs",
43+
"//pkg/scheduler/framework/plugins/noderesources:all-srcs",
4244
"//pkg/scheduler/framework/plugins/tainttoleration:all-srcs",
4345
"//pkg/scheduler/framework/plugins/volumebinding:all-srcs",
4446
"//pkg/scheduler/framework/plugins/volumerestrictions:all-srcs",

pkg/scheduler/framework/plugins/default_registry.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/kubernetes/pkg/scheduler/apis/config"
2929
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodeaffinity"
3030
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodename"
31+
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources"
3132
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/tainttoleration"
3233
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumebinding"
3334
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumerestrictions"
@@ -56,6 +57,7 @@ type RegistryArgs struct {
5657
func NewDefaultRegistry(args *RegistryArgs) framework.Registry {
5758
return framework.Registry{
5859
tainttoleration.Name: tainttoleration.New,
60+
noderesources.Name: noderesources.New,
5961
nodename.Name: nodename.New,
6062
nodeaffinity.Name: nodeaffinity.New,
6163
volumebinding.Name: func(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
@@ -94,6 +96,11 @@ func NewDefaultConfigProducerRegistry() *ConfigProducerRegistry {
9496
plugins.Filter = appendToPluginSet(plugins.Filter, tainttoleration.Name, nil)
9597
return
9698
})
99+
registry.RegisterPredicate(predicates.PodFitsResourcesPred,
100+
func(_ ConfigProducerArgs) (plugins config.Plugins, pluginConfig []config.PluginConfig) {
101+
plugins.Filter = appendToPluginSet(plugins.Filter, noderesources.Name, nil)
102+
return
103+
})
97104
registry.RegisterPredicate(predicates.HostNamePred,
98105
func(_ ConfigProducerArgs) (plugins config.Plugins, pluginConfig []config.PluginConfig) {
99106
plugins.Filter = appendToPluginSet(plugins.Filter, nodename.Name, nil)

pkg/scheduler/framework/plugins/migration/utils.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,18 @@ func PriorityMetadata(state *framework.CycleState) interface{} {
104104
}
105105
return meta
106106
}
107+
108+
// PredicateMetadata returns predicate metadata stored in CycleState.
109+
func PredicateMetadata(state *framework.CycleState) interface{} {
110+
if state == nil {
111+
return nil
112+
}
113+
114+
var meta interface{}
115+
if s, err := state.Read(PredicatesStateKey); err == nil {
116+
meta = s.(*PredicatesStateData).Reference
117+
} else {
118+
klog.Errorf("reading key %q from CycleState, continuing without metadata: %v", PredicatesStateKey, err)
119+
}
120+
return meta
121+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = ["node_resources.go"],
6+
importpath = "k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources",
7+
visibility = ["//visibility:public"],
8+
deps = [
9+
"//pkg/scheduler/algorithm/predicates:go_default_library",
10+
"//pkg/scheduler/framework/plugins/migration:go_default_library",
11+
"//pkg/scheduler/framework/v1alpha1:go_default_library",
12+
"//pkg/scheduler/nodeinfo:go_default_library",
13+
"//staging/src/k8s.io/api/core/v1:go_default_library",
14+
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
15+
],
16+
)
17+
18+
filegroup(
19+
name = "package-srcs",
20+
srcs = glob(["**"]),
21+
tags = ["automanaged"],
22+
visibility = ["//visibility:private"],
23+
)
24+
25+
filegroup(
26+
name = "all-srcs",
27+
srcs = [":package-srcs"],
28+
tags = ["automanaged"],
29+
visibility = ["//visibility:public"],
30+
)
31+
32+
go_test(
33+
name = "go_default_test",
34+
srcs = ["node_resources_test.go"],
35+
embed = [":go_default_library"],
36+
deps = [
37+
"//pkg/apis/core/v1/helper:go_default_library",
38+
"//pkg/features:go_default_library",
39+
"//pkg/scheduler/algorithm/predicates:go_default_library",
40+
"//pkg/scheduler/framework/plugins/migration:go_default_library",
41+
"//pkg/scheduler/framework/v1alpha1:go_default_library",
42+
"//pkg/scheduler/nodeinfo:go_default_library",
43+
"//staging/src/k8s.io/api/core/v1:go_default_library",
44+
"//staging/src/k8s.io/apimachinery/pkg/api/resource:go_default_library",
45+
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
46+
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
47+
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
48+
],
49+
)
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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 noderesources
18+
19+
import (
20+
"fmt"
21+
22+
"k8s.io/api/core/v1"
23+
"k8s.io/apimachinery/pkg/runtime"
24+
"k8s.io/kubernetes/pkg/scheduler/algorithm/predicates"
25+
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/migration"
26+
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
27+
"k8s.io/kubernetes/pkg/scheduler/nodeinfo"
28+
)
29+
30+
// NodeResources is a plugin that checks if a node has sufficient resources.
31+
type NodeResources struct{}
32+
33+
var _ = framework.FilterPlugin(&NodeResources{})
34+
35+
// Name is the name of the plugin used in the plugin registry and configurations.
36+
const Name = "NodeResources"
37+
38+
// Name returns name of the plugin. It is used in logs, etc.
39+
func (pl *NodeResources) Name() string {
40+
return Name
41+
}
42+
43+
// Filter invoked at the filter extension point.
44+
func (pl *NodeResources) Filter(cycleState *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
45+
meta, ok := migration.PredicateMetadata(cycleState).(predicates.PredicateMetadata)
46+
if !ok {
47+
return migration.ErrorToFrameworkStatus(fmt.Errorf("%+v convert to predicates.PredicateMetadata error", cycleState))
48+
}
49+
_, reasons, err := predicates.PodFitsResources(pod, meta, nodeInfo)
50+
return migration.PredicateResultToFrameworkStatus(reasons, err)
51+
}
52+
53+
// New initializes a new plugin and returns it.
54+
func New(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
55+
return &NodeResources{}, nil
56+
}

0 commit comments

Comments
 (0)