Skip to content

Commit 4ca3d9c

Browse files
committed
[migration phase 1] PodFitsResources as framework plugin
1 parent aee99ce commit 4ca3d9c

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
@@ -66,7 +66,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
6666
]
6767
}`,
6868
wantPredicates: sets.NewString(
69-
"PodFitsResources",
7069
"PodFitsPorts",
7170
"TestServiceAffinity",
7271
"TestLabelsPresence",
@@ -80,6 +79,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
8079
wantPlugins: map[string][]kubeschedulerconfig.Plugin{
8180
"FilterPlugin": {
8281
{Name: "NodeAffinity"},
82+
{Name: "NodeResources"},
8383
{Name: "VolumeRestrictions"},
8484
},
8585
},
@@ -110,7 +110,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
110110
}`,
111111
wantPredicates: sets.NewString(
112112
"PodFitsHostPorts",
113-
"PodFitsResources",
114113
"TestServiceAffinity",
115114
"TestLabelsPresence",
116115
),
@@ -126,6 +125,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
126125
"FilterPlugin": {
127126
{Name: "NodeName"},
128127
{Name: "NodeAffinity"},
128+
{Name: "NodeResources"},
129129
{Name: "VolumeRestrictions"},
130130
},
131131
},
@@ -161,7 +161,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
161161
]
162162
}`,
163163
wantPredicates: sets.NewString(
164-
"PodFitsResources",
165164
"PodFitsHostPorts",
166165
"NoVolumeZoneConflict",
167166
"MaxEBSVolumeCount",
@@ -184,6 +183,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
184183
"FilterPlugin": {
185184
{Name: "NodeName"},
186185
{Name: "NodeAffinity"},
186+
{Name: "NodeResources"},
187187
{Name: "VolumeRestrictions"},
188188
},
189189
},
@@ -223,7 +223,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
223223
]
224224
}`,
225225
wantPredicates: sets.NewString(
226-
"PodFitsResources",
227226
"PodFitsHostPorts",
228227
"NoVolumeZoneConflict",
229228
"CheckNodeMemoryPressure",
@@ -248,6 +247,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
248247
"FilterPlugin": {
249248
{Name: "NodeName"},
250249
{Name: "NodeAffinity"},
250+
{Name: "NodeResources"},
251251
{Name: "VolumeRestrictions"},
252252
{Name: "TaintToleration"},
253253
},
@@ -292,7 +292,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
292292
]
293293
}`,
294294
wantPredicates: sets.NewString(
295-
"PodFitsResources",
296295
"PodFitsHostPorts",
297296
"NoVolumeZoneConflict",
298297
"CheckNodeMemoryPressure",
@@ -320,6 +319,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
320319
"FilterPlugin": {
321320
{Name: "NodeName"},
322321
{Name: "NodeAffinity"},
322+
{Name: "NodeResources"},
323323
{Name: "VolumeRestrictions"},
324324
{Name: "TaintToleration"},
325325
},
@@ -373,7 +373,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
373373
}]
374374
}`,
375375
wantPredicates: sets.NewString(
376-
"PodFitsResources",
377376
"PodFitsHostPorts",
378377
"NoVolumeZoneConflict",
379378
"CheckNodeMemoryPressure",
@@ -401,6 +400,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
401400
"FilterPlugin": {
402401
{Name: "NodeName"},
403402
{Name: "NodeAffinity"},
403+
{Name: "NodeResources"},
404404
{Name: "VolumeRestrictions"},
405405
{Name: "TaintToleration"},
406406
},
@@ -466,7 +466,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
466466
}]
467467
}`,
468468
wantPredicates: sets.NewString(
469-
"PodFitsResources",
470469
"PodFitsHostPorts",
471470
"NoVolumeZoneConflict",
472471
"CheckNodeMemoryPressure",
@@ -495,6 +494,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
495494
"FilterPlugin": {
496495
{Name: "NodeName"},
497496
{Name: "NodeAffinity"},
497+
{Name: "NodeResources"},
498498
{Name: "VolumeRestrictions"},
499499
{Name: "TaintToleration"},
500500
},
@@ -561,7 +561,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
561561
}]
562562
}`,
563563
wantPredicates: sets.NewString(
564-
"PodFitsResources",
565564
"PodFitsHostPorts",
566565
"NoVolumeZoneConflict",
567566
"CheckNodeMemoryPressure",
@@ -590,6 +589,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
590589
"FilterPlugin": {
591590
{Name: "NodeName"},
592591
{Name: "NodeAffinity"},
592+
{Name: "NodeResources"},
593593
{Name: "VolumeRestrictions"},
594594
{Name: "TaintToleration"},
595595
{Name: "VolumeBinding"},
@@ -661,7 +661,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
661661
}]
662662
}`,
663663
wantPredicates: sets.NewString(
664-
"PodFitsResources",
665664
"PodFitsHostPorts",
666665
"NoVolumeZoneConflict",
667666
"CheckNodeMemoryPressure",
@@ -691,6 +690,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
691690
"FilterPlugin": {
692691
{Name: "NodeName"},
693692
{Name: "NodeAffinity"},
693+
{Name: "NodeResources"},
694694
{Name: "VolumeRestrictions"},
695695
{Name: "TaintToleration"},
696696
{Name: "VolumeBinding"},
@@ -774,7 +774,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
774774
}]
775775
}`,
776776
wantPredicates: sets.NewString(
777-
"PodFitsResources",
778777
"PodFitsHostPorts",
779778
"NoVolumeZoneConflict",
780779
"CheckNodeMemoryPressure",
@@ -805,6 +804,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
805804
"FilterPlugin": {
806805
{Name: "NodeName"},
807806
{Name: "NodeAffinity"},
807+
{Name: "NodeResources"},
808808
{Name: "VolumeRestrictions"},
809809
{Name: "TaintToleration"},
810810
{Name: "VolumeBinding"},
@@ -889,7 +889,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
889889
}]
890890
}`,
891891
wantPredicates: sets.NewString(
892-
"PodFitsResources",
893892
"PodFitsHostPorts",
894893
"NoVolumeZoneConflict",
895894
"CheckNodeMemoryPressure",
@@ -921,6 +920,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
921920
"FilterPlugin": {
922921
{Name: "NodeName"},
923922
{Name: "NodeAffinity"},
923+
{Name: "NodeResources"},
924924
{Name: "VolumeRestrictions"},
925925
{Name: "TaintToleration"},
926926
{Name: "VolumeBinding"},
@@ -1004,7 +1004,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
10041004
}]
10051005
}`,
10061006
wantPredicates: sets.NewString(
1007-
"PodFitsResources",
10081007
"PodFitsHostPorts",
10091008
"NoVolumeZoneConflict",
10101009
"CheckNodeMemoryPressure",
@@ -1037,6 +1036,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
10371036
"FilterPlugin": {
10381037
{Name: "NodeName"},
10391038
{Name: "NodeAffinity"},
1039+
{Name: "NodeResources"},
10401040
{Name: "VolumeRestrictions"},
10411041
{Name: "TaintToleration"},
10421042
{Name: "VolumeBinding"},
@@ -1124,7 +1124,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
11241124
}]
11251125
}`,
11261126
wantPredicates: sets.NewString(
1127-
"PodFitsResources",
11281127
"PodFitsHostPorts",
11291128
"NoVolumeZoneConflict",
11301129
"CheckNodeMemoryPressure",
@@ -1157,6 +1156,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
11571156
"FilterPlugin": {
11581157
{Name: "NodeName"},
11591158
{Name: "NodeAffinity"},
1159+
{Name: "NodeResources"},
11601160
{Name: "VolumeRestrictions"},
11611161
{Name: "TaintToleration"},
11621162
{Name: "VolumeBinding"},
@@ -1186,6 +1186,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
11861186
filterToPredicateMap := map[string]string{
11871187
"TaintToleration": "PodToleratesNodeTaints",
11881188
"NodeName": "HostName",
1189+
"NodeResources": "PodFitsResources",
11891190
"NodeAffinity": "MatchNodeSelector",
11901191
"VolumeBinding": "CheckVolumeBinding",
11911192
"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)