Update scheduler framework plugins to align with the latest changes to the framework design

This commit is contained in:
Bobby (Babak) Salamat
2019-03-20 17:07:25 -07:00
parent b3ad4cd6b9
commit 404dc1ed79
26 changed files with 631 additions and 701 deletions

View File

@@ -0,0 +1,31 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")
go_library(
name = "go_default_library",
srcs = [
"multipoint.go",
"prebind.go",
"stateful.go",
],
importpath = "k8s.io/kubernetes/pkg/scheduler/plugins/examples",
visibility = ["//visibility:public"],
deps = [
"//pkg/scheduler/plugins/v1alpha1:go_default_library",
"//staging/src/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/klog: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"],
)

View File

@@ -0,0 +1,68 @@
/*
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 multipoint
import (
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)
// CommunicatingPlugin is an example of a plugin that implements two
// extension points. It communicates through pluginContext with another function.
type CommunicatingPlugin struct{}
var _ = framework.ReservePlugin(CommunicatingPlugin{})
// Name is the name of the plug used in Registry and configurations.
const Name = "multipoint-communicating-plugin"
// Name returns name of the plugin. It is used in logs, etc.
func (mc CommunicatingPlugin) Name() string {
return Name
}
// Reserve is the functions invoked by the framework at "reserve" extension point.
func (mc CommunicatingPlugin) Reserve(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
if pod == nil {
return framework.NewStatus(framework.Error, "pod cannot be nil")
}
if pod.Name == "my-test-pod" {
pc.Lock()
pc.Write(framework.ContextKey(pod.Name), "never bind")
pc.Unlock()
}
return nil
}
// Prebind is the functions invoked by the framework at "prebind" extension point.
func (mc CommunicatingPlugin) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
if pod == nil {
return framework.NewStatus(framework.Error, "pod cannot be nil")
}
pc.RLock()
defer pc.RUnlock()
if v, e := pc.Read(framework.ContextKey(pod.Name)); e == nil && v == "never bind" {
return framework.NewStatus(framework.Unschedulable, "pod is not permitted")
}
return nil
}
// New initializes a new plugin and returns it.
func New(_ *runtime.Unknown, _ framework.Framework) (framework.Plugin, error) {
return &CommunicatingPlugin{}, nil
}

View File

@@ -0,0 +1,55 @@
/*
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 prebind
import (
"fmt"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)
// StatelessPrebindExample is an example of a simple plugin that has no state
// and implements only one hook for prebind.
type StatelessPrebindExample struct{}
var _ = framework.PrebindPlugin(StatelessPrebindExample{})
// Name is the name of the plugin used in Registry and configurations.
const Name = "stateless-prebind-plugin-example"
// Name returns name of the plugin. It is used in logs, etc.
func (sr StatelessPrebindExample) Name() string {
return Name
}
// Prebind is the functions invoked by the framework at "prebind" extension point.
func (sr StatelessPrebindExample) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
if pod == nil {
return framework.NewStatus(framework.Error, fmt.Sprintf("pod cannot be nil"))
}
if pod.Namespace != "foo" {
return framework.NewStatus(framework.Unschedulable, "only pods from 'foo' namespace are allowed")
}
return nil
}
// New initializes a new plugin and returns it.
func New(_ *runtime.Unknown, _ framework.Framework) (framework.Plugin, error) {
return &StatelessPrebindExample{}, nil
}

View File

@@ -0,0 +1,72 @@
/*
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 stateful
import (
"fmt"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/klog"
framework "k8s.io/kubernetes/pkg/scheduler/framework/v1alpha1"
)
// MultipointExample is an example plugin that is executed at multiple extension points.
// This plugin is stateful. It receives arguments at initialization (NewMultipointPlugin)
// and changes its state when it is executed.
type MultipointExample struct {
mpState map[int]string
numRuns int
}
var _ = framework.ReservePlugin(&MultipointExample{})
var _ = framework.PrebindPlugin(&MultipointExample{})
// Name is the name of the plug used in Registry and configurations.
const Name = "multipoint-plugin-example"
// Name returns name of the plugin. It is used in logs, etc.
func (mp *MultipointExample) Name() string {
return Name
}
// Reserve is the functions invoked by the framework at "reserve" extension point.
func (mp *MultipointExample) Reserve(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
mp.numRuns++
return nil
}
// Prebind is the functions invoked by the framework at "prebind" extension point.
func (mp *MultipointExample) Prebind(pc *framework.PluginContext, pod *v1.Pod, nodeName string) *framework.Status {
mp.numRuns++
if pod == nil {
return framework.NewStatus(framework.Error, "pod must not be nil")
}
return nil
}
// New initializes a new plugin and returns it.
func New(config *runtime.Unknown, _ framework.FrameworkHandle) (framework.Plugin, error) {
if config == nil {
klog.Error("MultipointExample configuration cannot be empty")
return nil, fmt.Errorf("MultipointExample configuration cannot be empty")
}
mp := MultipointExample{
mpState: make(map[int]string),
}
return &mp, nil
}