From b9167334eed22def23b33f04b3854285df953a6e Mon Sep 17 00:00:00 2001 From: Guoliang Wang Date: Wed, 9 Oct 2019 14:29:38 +0800 Subject: [PATCH] [migration phase 1] PodFitsHost as filter plugin --- .../api/compatibility/compatibility_test.go | 93 ++++++++++++------- pkg/scheduler/framework/plugins/BUILD | 2 + .../framework/plugins/default_registry.go | 7 ++ .../framework/plugins/nodename/BUILD | 43 +++++++++ .../framework/plugins/nodename/node_name.go | 50 ++++++++++ .../plugins/nodename/node_name_test.go | 87 +++++++++++++++++ 6 files changed, 250 insertions(+), 32 deletions(-) create mode 100644 pkg/scheduler/framework/plugins/nodename/BUILD create mode 100644 pkg/scheduler/framework/plugins/nodename/node_name.go create mode 100644 pkg/scheduler/framework/plugins/nodename/node_name_test.go diff --git a/pkg/scheduler/api/compatibility/compatibility_test.go b/pkg/scheduler/api/compatibility/compatibility_test.go index 8c73c223fbe..a7921b31b30 100644 --- a/pkg/scheduler/api/compatibility/compatibility_test.go +++ b/pkg/scheduler/api/compatibility/compatibility_test.go @@ -109,7 +109,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "PodFitsHostPorts", "PodFitsResources", "NoDiskConflict", - "HostName", "TestServiceAffinity", "TestLabelsPresence", ), @@ -121,6 +120,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "TestServiceAntiAffinity", "TestLabelPreference", ), + wantPlugins: map[string][]kubeschedulerconfig.Plugin{ + "FilterPlugin": { + {Name: "NodeName"}, + }, + }, }, // Do not change this JSON after the corresponding release has been tagged. @@ -156,7 +160,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "MaxEBSVolumeCount", @@ -175,6 +178,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "TestServiceAntiAffinity", "TestLabelPreference", ), + wantPlugins: map[string][]kubeschedulerconfig.Plugin{ + "FilterPlugin": { + {Name: "NodeName"}, + }, + }, }, // Do not change this JSON after the corresponding release has been tagged. @@ -214,7 +222,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -236,8 +243,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "InterPodAffinityPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, }, @@ -281,7 +291,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -306,8 +315,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MostRequestedPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, }, // Do not change this JSON after the corresponding release has been tagged. @@ -360,7 +372,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -385,8 +396,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MostRequestedPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, wantExtenders: []schedulerapi.ExtenderConfig{{ URLPrefix: "/prefix", @@ -451,7 +465,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -477,8 +490,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MostRequestedPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, wantExtenders: []schedulerapi.ExtenderConfig{{ URLPrefix: "/prefix", @@ -544,7 +560,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -571,8 +586,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MostRequestedPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, wantExtenders: []schedulerapi.ExtenderConfig{{ URLPrefix: "/prefix", @@ -642,7 +660,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -670,8 +687,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MostRequestedPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, wantExtenders: []schedulerapi.ExtenderConfig{{ URLPrefix: "/prefix", @@ -753,7 +773,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -782,8 +801,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "RequestedToCapacityRatioPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, wantExtenders: []schedulerapi.ExtenderConfig{{ URLPrefix: "/prefix", @@ -866,7 +888,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -896,8 +917,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "RequestedToCapacityRatioPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, wantExtenders: []schedulerapi.ExtenderConfig{{ URLPrefix: "/prefix", @@ -979,7 +1003,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -1010,8 +1033,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "RequestedToCapacityRatioPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, wantExtenders: []schedulerapi.ExtenderConfig{{ URLPrefix: "/prefix", @@ -1097,7 +1123,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "MatchNodeSelector", "PodFitsResources", "PodFitsHostPorts", - "HostName", "NoDiskConflict", "NoVolumeZoneConflict", "CheckNodeMemoryPressure", @@ -1128,8 +1153,11 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { "RequestedToCapacityRatioPriority", ), wantPlugins: map[string][]kubeschedulerconfig.Plugin{ - "FilterPlugin": {{Name: "TaintToleration"}}, - "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, + "FilterPlugin": { + {Name: "NodeName"}, + {Name: "TaintToleration"}, + }, + "ScorePlugin": {{Name: "TaintToleration", Weight: 2}}, }, wantExtenders: []schedulerapi.ExtenderConfig{{ URLPrefix: "/prefix", @@ -1153,6 +1181,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) { mandatoryPredicates := sets.NewString("CheckNodeCondition") filterToPredicateMap := map[string]string{ "TaintToleration": "PodToleratesNodeTaints", + "NodeName": "HostName", } scoreToPriorityMap := map[string]string{ "TaintToleration": "TaintTolerationPriority", diff --git a/pkg/scheduler/framework/plugins/BUILD b/pkg/scheduler/framework/plugins/BUILD index 296e8605ee6..db79debe551 100644 --- a/pkg/scheduler/framework/plugins/BUILD +++ b/pkg/scheduler/framework/plugins/BUILD @@ -10,6 +10,7 @@ go_library( "//pkg/scheduler/algorithm/predicates:go_default_library", "//pkg/scheduler/algorithm/priorities:go_default_library", "//pkg/scheduler/apis/config:go_default_library", + "//pkg/scheduler/framework/plugins/nodename:go_default_library", "//pkg/scheduler/framework/plugins/tainttoleration:go_default_library", "//pkg/scheduler/framework/v1alpha1:go_default_library", "//pkg/scheduler/internal/cache:go_default_library", @@ -32,6 +33,7 @@ filegroup( ":package-srcs", "//pkg/scheduler/framework/plugins/examples:all-srcs", "//pkg/scheduler/framework/plugins/migration:all-srcs", + "//pkg/scheduler/framework/plugins/nodename:all-srcs", "//pkg/scheduler/framework/plugins/tainttoleration:all-srcs", ], tags = ["automanaged"], diff --git a/pkg/scheduler/framework/plugins/default_registry.go b/pkg/scheduler/framework/plugins/default_registry.go index fade46ba5c2..43f392c53f5 100644 --- a/pkg/scheduler/framework/plugins/default_registry.go +++ b/pkg/scheduler/framework/plugins/default_registry.go @@ -25,6 +25,7 @@ import ( "k8s.io/kubernetes/pkg/scheduler/algorithm/predicates" "k8s.io/kubernetes/pkg/scheduler/algorithm/priorities" "k8s.io/kubernetes/pkg/scheduler/apis/config" + "k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodename" "k8s.io/kubernetes/pkg/scheduler/framework/plugins/tainttoleration" framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1" internalcache "k8s.io/kubernetes/pkg/scheduler/internal/cache" @@ -51,6 +52,7 @@ type RegistryArgs struct { func NewDefaultRegistry(args *RegistryArgs) framework.Registry { return framework.Registry{ tainttoleration.Name: tainttoleration.New, + nodename.Name: nodename.New, } } @@ -83,6 +85,11 @@ func NewDefaultConfigProducerRegistry() *ConfigProducerRegistry { plugins.Filter = appendToPluginSet(plugins.Filter, tainttoleration.Name, nil) return }) + registry.RegisterPredicate(predicates.HostNamePred, + func(_ ConfigProducerArgs) (plugins config.Plugins, pluginConfig []config.PluginConfig) { + plugins.Filter = appendToPluginSet(plugins.Filter, nodename.Name, nil) + return + }) registry.RegisterPriority(priorities.TaintTolerationPriority, func(args ConfigProducerArgs) (plugins config.Plugins, pluginConfig []config.PluginConfig) { diff --git a/pkg/scheduler/framework/plugins/nodename/BUILD b/pkg/scheduler/framework/plugins/nodename/BUILD new file mode 100644 index 00000000000..71eca63f553 --- /dev/null +++ b/pkg/scheduler/framework/plugins/nodename/BUILD @@ -0,0 +1,43 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = ["node_name.go"], + importpath = "k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodename", + visibility = ["//visibility:public"], + deps = [ + "//pkg/scheduler/algorithm/predicates:go_default_library", + "//pkg/scheduler/framework/plugins/migration:go_default_library", + "//pkg/scheduler/framework/v1alpha1:go_default_library", + "//pkg/scheduler/nodeinfo:go_default_library", + "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) + +go_test( + name = "go_default_test", + srcs = ["node_name_test.go"], + embed = [":go_default_library"], + deps = [ + "//pkg/scheduler/algorithm/predicates:go_default_library", + "//pkg/scheduler/framework/v1alpha1:go_default_library", + "//pkg/scheduler/nodeinfo:go_default_library", + "//staging/src/k8s.io/api/core/v1:go_default_library", + "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", + ], +) diff --git a/pkg/scheduler/framework/plugins/nodename/node_name.go b/pkg/scheduler/framework/plugins/nodename/node_name.go new file mode 100644 index 00000000000..95cf20122ec --- /dev/null +++ b/pkg/scheduler/framework/plugins/nodename/node_name.go @@ -0,0 +1,50 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package nodename + +import ( + "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/kubernetes/pkg/scheduler/algorithm/predicates" + "k8s.io/kubernetes/pkg/scheduler/framework/plugins/migration" + framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1" + "k8s.io/kubernetes/pkg/scheduler/nodeinfo" +) + +// NodeName is a plugin that checks if a pod spec node name matches the current node. +type NodeName struct{} + +var _ = framework.FilterPlugin(&NodeName{}) + +// Name is the name of the plugin used in the plugin registry and configurations. +const Name = "NodeName" + +// Name returns name of the plugin. It is used in logs, etc. +func (pl *NodeName) Name() string { + return Name +} + +// Filter invoked at the filter extension point. +func (pl *NodeName) Filter(_ *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status { + _, reasons, err := predicates.PodFitsHost(pod, nil, nodeInfo) + return migration.PredicateResultToFrameworkStatus(reasons, err) +} + +// New initializes a new plugin and returns it. +func New(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) { + return &NodeName{}, nil +} diff --git a/pkg/scheduler/framework/plugins/nodename/node_name_test.go b/pkg/scheduler/framework/plugins/nodename/node_name_test.go new file mode 100644 index 00000000000..579e4771398 --- /dev/null +++ b/pkg/scheduler/framework/plugins/nodename/node_name_test.go @@ -0,0 +1,87 @@ +/* +Copyright 2019 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package nodename + +import ( + "reflect" + "testing" + + "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/kubernetes/pkg/scheduler/algorithm/predicates" + framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1" + schedulernodeinfo "k8s.io/kubernetes/pkg/scheduler/nodeinfo" +) + +func TestNodeName(t *testing.T) { + tests := []struct { + pod *v1.Pod + node *v1.Node + fits bool + name string + wantStatus *framework.Status + }{ + { + pod: &v1.Pod{}, + node: &v1.Node{}, + fits: true, + name: "no host specified", + }, + { + pod: &v1.Pod{ + Spec: v1.PodSpec{ + NodeName: "foo", + }, + }, + node: &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + }, + }, + fits: true, + name: "host matches", + }, + { + pod: &v1.Pod{ + Spec: v1.PodSpec{ + NodeName: "bar", + }, + }, + node: &v1.Node{ + ObjectMeta: metav1.ObjectMeta{ + Name: "foo", + }, + }, + fits: false, + name: "host doesn't match", + wantStatus: framework.NewStatus(framework.UnschedulableAndUnresolvable, predicates.ErrPodNotMatchHostName.GetReason()), + }, + } + + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + nodeInfo := schedulernodeinfo.NewNodeInfo() + nodeInfo.SetNode(test.node) + + p, _ := New(nil, nil) + gotStatus := p.(framework.FilterPlugin).Filter(nil, test.pod, nodeInfo) + if !reflect.DeepEqual(gotStatus, test.wantStatus) { + t.Errorf("status does not match: %v, want: %v", gotStatus, test.wantStatus) + } + }) + } +}