Skip to content

Commit

Permalink
[migration phase 1] PodFitsResources as framework plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
wgliang committed Oct 13, 2019
1 parent aee99ce commit 4ca3d9c
Show file tree
Hide file tree
Showing 7 changed files with 607 additions and 13 deletions.
27 changes: 14 additions & 13 deletions pkg/scheduler/api/compatibility/compatibility_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsPorts",
"TestServiceAffinity",
"TestLabelsPresence",
Expand All @@ -80,6 +79,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
wantPlugins: map[string][]kubeschedulerconfig.Plugin{
"FilterPlugin": {
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
},
},
Expand Down Expand Up @@ -110,7 +110,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
}`,
wantPredicates: sets.NewString(
"PodFitsHostPorts",
"PodFitsResources",
"TestServiceAffinity",
"TestLabelsPresence",
),
Expand All @@ -126,6 +125,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
},
},
Expand Down Expand Up @@ -161,7 +161,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"MaxEBSVolumeCount",
Expand All @@ -184,6 +183,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
},
},
Expand Down Expand Up @@ -223,7 +223,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand All @@ -248,6 +247,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
},
Expand Down Expand Up @@ -292,7 +292,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand Down Expand Up @@ -320,6 +319,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
},
Expand Down Expand Up @@ -373,7 +373,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
}]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand Down Expand Up @@ -401,6 +400,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
},
Expand Down Expand Up @@ -466,7 +466,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
}]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand Down Expand Up @@ -495,6 +494,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
},
Expand Down Expand Up @@ -561,7 +561,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
}]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand Down Expand Up @@ -590,6 +589,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
{Name: "VolumeBinding"},
Expand Down Expand Up @@ -661,7 +661,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
}]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand Down Expand Up @@ -691,6 +690,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
{Name: "VolumeBinding"},
Expand Down Expand Up @@ -774,7 +774,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
}]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand Down Expand Up @@ -805,6 +804,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
{Name: "VolumeBinding"},
Expand Down Expand Up @@ -889,7 +889,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
}]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand Down Expand Up @@ -921,6 +920,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
{Name: "VolumeBinding"},
Expand Down Expand Up @@ -1004,7 +1004,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
}]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand Down Expand Up @@ -1037,6 +1036,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
{Name: "VolumeBinding"},
Expand Down Expand Up @@ -1124,7 +1124,6 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
}]
}`,
wantPredicates: sets.NewString(
"PodFitsResources",
"PodFitsHostPorts",
"NoVolumeZoneConflict",
"CheckNodeMemoryPressure",
Expand Down Expand Up @@ -1157,6 +1156,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
"FilterPlugin": {
{Name: "NodeName"},
{Name: "NodeAffinity"},
{Name: "NodeResources"},
{Name: "VolumeRestrictions"},
{Name: "TaintToleration"},
{Name: "VolumeBinding"},
Expand Down Expand Up @@ -1186,6 +1186,7 @@ func TestCompatibility_v1_Scheduler(t *testing.T) {
filterToPredicateMap := map[string]string{
"TaintToleration": "PodToleratesNodeTaints",
"NodeName": "HostName",
"NodeResources": "PodFitsResources",
"NodeAffinity": "MatchNodeSelector",
"VolumeBinding": "CheckVolumeBinding",
"VolumeRestrictions": "NoDiskConflict",
Expand Down
2 changes: 2 additions & 0 deletions pkg/scheduler/framework/plugins/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"//pkg/scheduler/apis/config:go_default_library",
"//pkg/scheduler/framework/plugins/nodeaffinity:go_default_library",
"//pkg/scheduler/framework/plugins/nodename:go_default_library",
"//pkg/scheduler/framework/plugins/noderesources:go_default_library",
"//pkg/scheduler/framework/plugins/tainttoleration:go_default_library",
"//pkg/scheduler/framework/plugins/volumebinding:go_default_library",
"//pkg/scheduler/framework/plugins/volumerestrictions:go_default_library",
Expand Down Expand Up @@ -39,6 +40,7 @@ filegroup(
"//pkg/scheduler/framework/plugins/migration:all-srcs",
"//pkg/scheduler/framework/plugins/nodeaffinity:all-srcs",
"//pkg/scheduler/framework/plugins/nodename:all-srcs",
"//pkg/scheduler/framework/plugins/noderesources:all-srcs",
"//pkg/scheduler/framework/plugins/tainttoleration:all-srcs",
"//pkg/scheduler/framework/plugins/volumebinding:all-srcs",
"//pkg/scheduler/framework/plugins/volumerestrictions:all-srcs",
Expand Down
7 changes: 7 additions & 0 deletions pkg/scheduler/framework/plugins/default_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"k8s.io/kubernetes/pkg/scheduler/apis/config"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodeaffinity"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/nodename"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/tainttoleration"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumebinding"
"k8s.io/kubernetes/pkg/scheduler/framework/plugins/volumerestrictions"
Expand Down Expand Up @@ -56,6 +57,7 @@ type RegistryArgs struct {
func NewDefaultRegistry(args *RegistryArgs) framework.Registry {
return framework.Registry{
tainttoleration.Name: tainttoleration.New,
noderesources.Name: noderesources.New,
nodename.Name: nodename.New,
nodeaffinity.Name: nodeaffinity.New,
volumebinding.Name: func(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
Expand Down Expand Up @@ -94,6 +96,11 @@ func NewDefaultConfigProducerRegistry() *ConfigProducerRegistry {
plugins.Filter = appendToPluginSet(plugins.Filter, tainttoleration.Name, nil)
return
})
registry.RegisterPredicate(predicates.PodFitsResourcesPred,
func(_ ConfigProducerArgs) (plugins config.Plugins, pluginConfig []config.PluginConfig) {
plugins.Filter = appendToPluginSet(plugins.Filter, noderesources.Name, nil)
return
})
registry.RegisterPredicate(predicates.HostNamePred,
func(_ ConfigProducerArgs) (plugins config.Plugins, pluginConfig []config.PluginConfig) {
plugins.Filter = appendToPluginSet(plugins.Filter, nodename.Name, nil)
Expand Down
15 changes: 15 additions & 0 deletions pkg/scheduler/framework/plugins/migration/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,18 @@ func PriorityMetadata(state *framework.CycleState) interface{} {
}
return meta
}

// PredicateMetadata returns predicate metadata stored in CycleState.
func PredicateMetadata(state *framework.CycleState) interface{} {
if state == nil {
return nil
}

var meta interface{}
if s, err := state.Read(PredicatesStateKey); err == nil {
meta = s.(*PredicatesStateData).Reference
} else {
klog.Errorf("reading key %q from CycleState, continuing without metadata: %v", PredicatesStateKey, err)
}
return meta
}
49 changes: 49 additions & 0 deletions pkg/scheduler/framework/plugins/noderesources/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
name = "go_default_library",
srcs = ["node_resources.go"],
importpath = "k8s.io/kubernetes/pkg/scheduler/framework/plugins/noderesources",
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_resources_test.go"],
embed = [":go_default_library"],
deps = [
"//pkg/apis/core/v1/helper:go_default_library",
"//pkg/features:go_default_library",
"//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/api/resource:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",
"//staging/src/k8s.io/component-base/featuregate/testing:go_default_library",
],
)
56 changes: 56 additions & 0 deletions pkg/scheduler/framework/plugins/noderesources/node_resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
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 noderesources

import (
"fmt"

"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"
)

// NodeResources is a plugin that checks if a node has sufficient resources.
type NodeResources struct{}

var _ = framework.FilterPlugin(&NodeResources{})

// Name is the name of the plugin used in the plugin registry and configurations.
const Name = "NodeResources"

// Name returns name of the plugin. It is used in logs, etc.
func (pl *NodeResources) Name() string {
return Name
}

// Filter invoked at the filter extension point.
func (pl *NodeResources) Filter(cycleState *framework.CycleState, pod *v1.Pod, nodeInfo *nodeinfo.NodeInfo) *framework.Status {
meta, ok := migration.PredicateMetadata(cycleState).(predicates.PredicateMetadata)
if !ok {
return migration.ErrorToFrameworkStatus(fmt.Errorf("%+v convert to predicates.PredicateMetadata error", cycleState))
}
_, reasons, err := predicates.PodFitsResources(pod, meta, nodeInfo)
return migration.PredicateResultToFrameworkStatus(reasons, err)
}

// New initializes a new plugin and returns it.
func New(_ *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
return &NodeResources{}, nil
}
Loading

0 comments on commit 4ca3d9c

Please sign in to comment.