Add generated file
This PR adds generated files under pkg/client and vendor folder.
This commit is contained in:
58
vendor/k8s.io/kubernetes/plugin/pkg/admission/priority/BUILD
generated
vendored
Normal file
58
vendor/k8s.io/kubernetes/plugin/pkg/admission/priority/BUILD
generated
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
package(default_visibility = ["//visibility:public"])
|
||||
|
||||
load(
|
||||
"@io_bazel_rules_go//go:def.bzl",
|
||||
"go_library",
|
||||
"go_test",
|
||||
)
|
||||
|
||||
go_test(
|
||||
name = "go_default_test",
|
||||
srcs = ["admission_test.go"],
|
||||
embed = [":go_default_library"],
|
||||
deps = [
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/apis/scheduling:go_default_library",
|
||||
"//pkg/client/informers/informers_generated/internalversion:go_default_library",
|
||||
"//pkg/controller:go_default_library",
|
||||
"//pkg/features:go_default_library",
|
||||
"//vendor/github.com/golang/glog:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||
"//vendor/k8s.io/apiserver/pkg/authentication/user:go_default_library",
|
||||
"//vendor/k8s.io/apiserver/pkg/util/feature:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
go_library(
|
||||
name = "go_default_library",
|
||||
srcs = ["admission.go"],
|
||||
importpath = "k8s.io/kubernetes/plugin/pkg/admission/priority",
|
||||
deps = [
|
||||
"//pkg/apis/core:go_default_library",
|
||||
"//pkg/apis/scheduling:go_default_library",
|
||||
"//pkg/client/clientset_generated/internalclientset:go_default_library",
|
||||
"//pkg/client/informers/informers_generated/internalversion:go_default_library",
|
||||
"//pkg/client/listers/scheduling/internalversion:go_default_library",
|
||||
"//pkg/features:go_default_library",
|
||||
"//pkg/kubeapiserver/admission:go_default_library",
|
||||
"//pkg/kubelet/types:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||
"//vendor/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||
"//vendor/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||
"//vendor/k8s.io/apiserver/pkg/util/feature:go_default_library",
|
||||
],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "package-srcs",
|
||||
srcs = glob(["**"]),
|
||||
tags = ["automanaged"],
|
||||
visibility = ["//visibility:private"],
|
||||
)
|
||||
|
||||
filegroup(
|
||||
name = "all-srcs",
|
||||
srcs = [":package-srcs"],
|
||||
tags = ["automanaged"],
|
||||
)
|
232
vendor/k8s.io/kubernetes/plugin/pkg/admission/priority/admission.go
generated
vendored
Normal file
232
vendor/k8s.io/kubernetes/plugin/pkg/admission/priority/admission.go
generated
vendored
Normal file
@@ -0,0 +1,232 @@
|
||||
/*
|
||||
Copyright 2017 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 priority
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/apis/scheduling"
|
||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
||||
informers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
|
||||
schedulinglisters "k8s.io/kubernetes/pkg/client/listers/scheduling/internalversion"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
kubeapiserveradmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
|
||||
kubelettypes "k8s.io/kubernetes/pkg/kubelet/types"
|
||||
)
|
||||
|
||||
const (
|
||||
// PluginName indicates name of admission plugin.
|
||||
PluginName = "Priority"
|
||||
)
|
||||
|
||||
// Register registers a plugin
|
||||
func Register(plugins *admission.Plugins) {
|
||||
plugins.Register(PluginName, func(config io.Reader) (admission.Interface, error) {
|
||||
return newPlugin(), nil
|
||||
})
|
||||
}
|
||||
|
||||
// priorityPlugin is an implementation of admission.Interface.
|
||||
type priorityPlugin struct {
|
||||
*admission.Handler
|
||||
client internalclientset.Interface
|
||||
lister schedulinglisters.PriorityClassLister
|
||||
}
|
||||
|
||||
var _ admission.MutationInterface = &priorityPlugin{}
|
||||
var _ admission.ValidationInterface = &priorityPlugin{}
|
||||
var _ = kubeapiserveradmission.WantsInternalKubeInformerFactory(&priorityPlugin{})
|
||||
var _ = kubeapiserveradmission.WantsInternalKubeClientSet(&priorityPlugin{})
|
||||
|
||||
// NewPlugin creates a new priority admission plugin.
|
||||
func newPlugin() *priorityPlugin {
|
||||
return &priorityPlugin{
|
||||
Handler: admission.NewHandler(admission.Create, admission.Update, admission.Delete),
|
||||
}
|
||||
}
|
||||
|
||||
// ValidateInitialization implements the InitializationValidator interface.
|
||||
func (p *priorityPlugin) ValidateInitialization() error {
|
||||
if p.client == nil {
|
||||
return fmt.Errorf("%s requires a client", PluginName)
|
||||
}
|
||||
if p.lister == nil {
|
||||
return fmt.Errorf("%s requires a lister", PluginName)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetInternalKubeClientSet implements the WantsInternalKubeClientSet interface.
|
||||
func (p *priorityPlugin) SetInternalKubeClientSet(client internalclientset.Interface) {
|
||||
p.client = client
|
||||
}
|
||||
|
||||
// SetInternalKubeInformerFactory implements the WantsInternalKubeInformerFactory interface.
|
||||
func (p *priorityPlugin) SetInternalKubeInformerFactory(f informers.SharedInformerFactory) {
|
||||
priorityInformer := f.Scheduling().InternalVersion().PriorityClasses()
|
||||
p.lister = priorityInformer.Lister()
|
||||
p.SetReadyFunc(priorityInformer.Informer().HasSynced)
|
||||
}
|
||||
|
||||
var (
|
||||
podResource = api.Resource("pods")
|
||||
priorityClassResource = scheduling.Resource("priorityclasses")
|
||||
)
|
||||
|
||||
// Admit checks Pods and admits or rejects them. It also resolves the priority of pods based on their PriorityClass.
|
||||
// Note that pod validation mechanism prevents update of a pod priority.
|
||||
func (p *priorityPlugin) Admit(a admission.Attributes) error {
|
||||
operation := a.GetOperation()
|
||||
// Ignore all calls to subresources
|
||||
if len(a.GetSubresource()) != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch a.GetResource().GroupResource() {
|
||||
case podResource:
|
||||
if operation == admission.Create {
|
||||
return p.admitPod(a)
|
||||
}
|
||||
return nil
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// Validate checks PriorityClasses and admits or rejects them.
|
||||
func (p *priorityPlugin) Validate(a admission.Attributes) error {
|
||||
operation := a.GetOperation()
|
||||
// Ignore all calls to subresources
|
||||
if len(a.GetSubresource()) != 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
switch a.GetResource().GroupResource() {
|
||||
case priorityClassResource:
|
||||
if operation == admission.Create || operation == admission.Update {
|
||||
return p.validatePriorityClass(a)
|
||||
}
|
||||
return nil
|
||||
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// admitPod makes sure a new pod does not set spec.Priority field. It also makes sure that the PriorityClassName exists if it is provided and resolves the pod priority from the PriorityClassName.
|
||||
func (p *priorityPlugin) admitPod(a admission.Attributes) error {
|
||||
operation := a.GetOperation()
|
||||
pod, ok := a.GetObject().(*api.Pod)
|
||||
if !ok {
|
||||
return errors.NewBadRequest("resource was marked with kind Pod but was unable to be converted")
|
||||
}
|
||||
|
||||
// Make sure that the client has not set `priority` at the time of pod creation.
|
||||
if operation == admission.Create && pod.Spec.Priority != nil {
|
||||
return admission.NewForbidden(a, fmt.Errorf("the integer value of priority must not be provided in pod spec. Priority admission controller populates the value from the given PriorityClass name"))
|
||||
}
|
||||
if utilfeature.DefaultFeatureGate.Enabled(features.PodPriority) {
|
||||
var priority int32
|
||||
// TODO: @ravig - This is for backwards compatibility to ensure that critical pods with annotations just work fine.
|
||||
// Remove when no longer needed.
|
||||
if len(pod.Spec.PriorityClassName) == 0 &&
|
||||
utilfeature.DefaultFeatureGate.Enabled(features.ExperimentalCriticalPodAnnotation) &&
|
||||
kubelettypes.IsCritical(a.GetNamespace(), pod.Annotations) {
|
||||
pod.Spec.PriorityClassName = scheduling.SystemClusterCritical
|
||||
}
|
||||
if len(pod.Spec.PriorityClassName) == 0 {
|
||||
var err error
|
||||
priority, err = p.getDefaultPriority()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get default priority class: %v", err)
|
||||
}
|
||||
} else {
|
||||
// Try resolving the priority class name.
|
||||
pc, err := p.lister.Get(pod.Spec.PriorityClassName)
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
return admission.NewForbidden(a, fmt.Errorf("no PriorityClass with name %v was found", pod.Spec.PriorityClassName))
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed to get PriorityClass with name %s: %v", pod.Spec.PriorityClassName, err)
|
||||
}
|
||||
|
||||
priority = pc.Value
|
||||
}
|
||||
pod.Spec.Priority = &priority
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// validatePriorityClass ensures that the value field is not larger than the highest user definable priority. If the GlobalDefault is set, it ensures that there is no other PriorityClass whose GlobalDefault is set.
|
||||
func (p *priorityPlugin) validatePriorityClass(a admission.Attributes) error {
|
||||
operation := a.GetOperation()
|
||||
pc, ok := a.GetObject().(*scheduling.PriorityClass)
|
||||
if !ok {
|
||||
return errors.NewBadRequest("resource was marked with kind PriorityClass but was unable to be converted")
|
||||
}
|
||||
// If the new PriorityClass tries to be the default priority, make sure that no other priority class is marked as default.
|
||||
if pc.GlobalDefault {
|
||||
dpc, err := p.getDefaultPriorityClass()
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to get default priority class: %v", err)
|
||||
}
|
||||
if dpc != nil {
|
||||
// Throw an error if a second default priority class is being created, or an existing priority class is being marked as default, while another default already exists.
|
||||
if operation == admission.Create || (operation == admission.Update && dpc.GetName() != pc.GetName()) {
|
||||
return admission.NewForbidden(a, fmt.Errorf("PriorityClass %v is already marked as default. Only one default can exist", dpc.GetName()))
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *priorityPlugin) getDefaultPriorityClass() (*scheduling.PriorityClass, error) {
|
||||
list, err := p.lister.List(labels.Everything())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// In case more than one global default priority class is added as a result of a race condition,
|
||||
// we pick the one with the lowest priority value.
|
||||
var defaultPC *scheduling.PriorityClass
|
||||
for _, pci := range list {
|
||||
if pci.GlobalDefault {
|
||||
if defaultPC == nil || defaultPC.Value > pci.Value {
|
||||
defaultPC = pci
|
||||
}
|
||||
}
|
||||
}
|
||||
return defaultPC, nil
|
||||
}
|
||||
|
||||
func (p *priorityPlugin) getDefaultPriority() (int32, error) {
|
||||
dpc, err := p.getDefaultPriorityClass()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if dpc != nil {
|
||||
return dpc.Value, nil
|
||||
}
|
||||
return int32(scheduling.DefaultPriorityWhenNoDefaultClassExists), nil
|
||||
}
|
497
vendor/k8s.io/kubernetes/plugin/pkg/admission/priority/admission_test.go
generated
vendored
Normal file
497
vendor/k8s.io/kubernetes/plugin/pkg/admission/priority/admission_test.go
generated
vendored
Normal file
@@ -0,0 +1,497 @@
|
||||
/*
|
||||
Copyright 2017 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 priority
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/golang/glog"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apiserver/pkg/admission"
|
||||
"k8s.io/apiserver/pkg/authentication/user"
|
||||
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||||
api "k8s.io/kubernetes/pkg/apis/core"
|
||||
"k8s.io/kubernetes/pkg/apis/scheduling"
|
||||
informers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
|
||||
"k8s.io/kubernetes/pkg/controller"
|
||||
"k8s.io/kubernetes/pkg/features"
|
||||
)
|
||||
|
||||
func addPriorityClasses(ctrl *priorityPlugin, priorityClasses []*scheduling.PriorityClass) {
|
||||
informerFactory := informers.NewSharedInformerFactory(nil, controller.NoResyncPeriodFunc())
|
||||
ctrl.SetInternalKubeInformerFactory(informerFactory)
|
||||
// First add the existing classes to the cache.
|
||||
for _, c := range priorityClasses {
|
||||
informerFactory.Scheduling().InternalVersion().PriorityClasses().Informer().GetStore().Add(c)
|
||||
}
|
||||
}
|
||||
|
||||
var defaultClass1 = &scheduling.PriorityClass{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "PriorityClass",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default1",
|
||||
},
|
||||
Value: 1000,
|
||||
GlobalDefault: true,
|
||||
}
|
||||
|
||||
var defaultClass2 = &scheduling.PriorityClass{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "PriorityClass",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "default2",
|
||||
},
|
||||
Value: 2000,
|
||||
GlobalDefault: true,
|
||||
}
|
||||
|
||||
var nondefaultClass1 = &scheduling.PriorityClass{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "PriorityClass",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "nondefault1",
|
||||
},
|
||||
Value: 2000,
|
||||
Description: "Just a test priority class",
|
||||
}
|
||||
|
||||
var systemClusterCritical = &scheduling.PriorityClass{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "PriorityClass",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: scheduling.SystemClusterCritical,
|
||||
},
|
||||
Value: scheduling.SystemCriticalPriority,
|
||||
GlobalDefault: true,
|
||||
}
|
||||
|
||||
func TestPriorityClassAdmission(t *testing.T) {
|
||||
var systemClass = &scheduling.PriorityClass{
|
||||
TypeMeta: metav1.TypeMeta{
|
||||
Kind: "PriorityClass",
|
||||
},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: scheduling.SystemPriorityClassPrefix + "test",
|
||||
},
|
||||
Value: scheduling.HighestUserDefinablePriority + 1,
|
||||
Description: "Name has system critical prefix",
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
existingClasses []*scheduling.PriorityClass
|
||||
newClass *scheduling.PriorityClass
|
||||
userInfo user.Info
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
"one default class",
|
||||
[]*scheduling.PriorityClass{},
|
||||
defaultClass1,
|
||||
nil,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"more than one default classes",
|
||||
[]*scheduling.PriorityClass{defaultClass1},
|
||||
defaultClass2,
|
||||
nil,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"system name and value are allowed by admission controller",
|
||||
[]*scheduling.PriorityClass{},
|
||||
systemClass,
|
||||
&user.DefaultInfo{
|
||||
Name: user.APIServerUser,
|
||||
},
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
glog.V(4).Infof("starting test %q", test.name)
|
||||
|
||||
ctrl := newPlugin()
|
||||
// Add existing priority classes.
|
||||
addPriorityClasses(ctrl, test.existingClasses)
|
||||
// Now add the new class.
|
||||
attrs := admission.NewAttributesRecord(
|
||||
test.newClass,
|
||||
nil,
|
||||
scheduling.Kind("PriorityClass").WithVersion("version"),
|
||||
"",
|
||||
"",
|
||||
scheduling.Resource("priorityclasses").WithVersion("version"),
|
||||
"",
|
||||
admission.Create,
|
||||
test.userInfo,
|
||||
)
|
||||
err := ctrl.Validate(attrs)
|
||||
glog.Infof("Got %v", err)
|
||||
if err != nil && !test.expectError {
|
||||
t.Errorf("Test %q: unexpected error received: %v", test.name, err)
|
||||
}
|
||||
if err == nil && test.expectError {
|
||||
t.Errorf("Test %q: expected error and no error recevied", test.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestDefaultPriority tests that default priority is resolved correctly.
|
||||
func TestDefaultPriority(t *testing.T) {
|
||||
pcResource := scheduling.Resource("priorityclasses").WithVersion("version")
|
||||
pcKind := scheduling.Kind("PriorityClass").WithVersion("version")
|
||||
updatedDefaultClass1 := *defaultClass1
|
||||
updatedDefaultClass1.GlobalDefault = false
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
classesBefore []*scheduling.PriorityClass
|
||||
classesAfter []*scheduling.PriorityClass
|
||||
attributes admission.Attributes
|
||||
expectedDefaultBefore int32
|
||||
expectedDefaultAfter int32
|
||||
}{
|
||||
{
|
||||
name: "simple resolution with a default class",
|
||||
classesBefore: []*scheduling.PriorityClass{defaultClass1},
|
||||
classesAfter: []*scheduling.PriorityClass{defaultClass1},
|
||||
attributes: nil,
|
||||
expectedDefaultBefore: defaultClass1.Value,
|
||||
expectedDefaultAfter: defaultClass1.Value,
|
||||
},
|
||||
{
|
||||
name: "add a default class",
|
||||
classesBefore: []*scheduling.PriorityClass{nondefaultClass1},
|
||||
classesAfter: []*scheduling.PriorityClass{nondefaultClass1, defaultClass1},
|
||||
attributes: admission.NewAttributesRecord(defaultClass1, nil, pcKind, "", defaultClass1.Name, pcResource, "", admission.Create, nil),
|
||||
expectedDefaultBefore: scheduling.DefaultPriorityWhenNoDefaultClassExists,
|
||||
expectedDefaultAfter: defaultClass1.Value,
|
||||
},
|
||||
{
|
||||
name: "multiple default classes resolves to the minimum value among them",
|
||||
classesBefore: []*scheduling.PriorityClass{defaultClass1, defaultClass2},
|
||||
classesAfter: []*scheduling.PriorityClass{defaultClass2},
|
||||
attributes: admission.NewAttributesRecord(nil, nil, pcKind, "", defaultClass1.Name, pcResource, "", admission.Delete, nil),
|
||||
expectedDefaultBefore: defaultClass1.Value,
|
||||
expectedDefaultAfter: defaultClass2.Value,
|
||||
},
|
||||
{
|
||||
name: "delete default priority class",
|
||||
classesBefore: []*scheduling.PriorityClass{defaultClass1},
|
||||
classesAfter: []*scheduling.PriorityClass{},
|
||||
attributes: admission.NewAttributesRecord(nil, nil, pcKind, "", defaultClass1.Name, pcResource, "", admission.Delete, nil),
|
||||
expectedDefaultBefore: defaultClass1.Value,
|
||||
expectedDefaultAfter: scheduling.DefaultPriorityWhenNoDefaultClassExists,
|
||||
},
|
||||
{
|
||||
name: "update default class and remove its global default",
|
||||
classesBefore: []*scheduling.PriorityClass{defaultClass1},
|
||||
classesAfter: []*scheduling.PriorityClass{&updatedDefaultClass1},
|
||||
attributes: admission.NewAttributesRecord(&updatedDefaultClass1, defaultClass1, pcKind, "", defaultClass1.Name, pcResource, "", admission.Update, nil),
|
||||
expectedDefaultBefore: defaultClass1.Value,
|
||||
expectedDefaultAfter: scheduling.DefaultPriorityWhenNoDefaultClassExists,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
glog.V(4).Infof("starting test %q", test.name)
|
||||
ctrl := newPlugin()
|
||||
addPriorityClasses(ctrl, test.classesBefore)
|
||||
defaultPriority, err := ctrl.getDefaultPriority()
|
||||
if err != nil {
|
||||
t.Errorf("Test %q: unexpected error while getting default priority: %v", test.name, err)
|
||||
}
|
||||
if err == nil && defaultPriority != test.expectedDefaultBefore {
|
||||
t.Errorf("Test %q: expected default priority %d, but got %d", test.name, test.expectedDefaultBefore, defaultPriority)
|
||||
}
|
||||
if test.attributes != nil {
|
||||
err := ctrl.Validate(test.attributes)
|
||||
if err != nil {
|
||||
t.Errorf("Test %q: unexpected error received: %v", test.name, err)
|
||||
}
|
||||
}
|
||||
addPriorityClasses(ctrl, test.classesAfter)
|
||||
defaultPriority, err = ctrl.getDefaultPriority()
|
||||
if err != nil {
|
||||
t.Errorf("Test %q: unexpected error while getting default priority: %v", test.name, err)
|
||||
}
|
||||
if err == nil && defaultPriority != test.expectedDefaultAfter {
|
||||
t.Errorf("Test %q: expected default priority %d, but got %d", test.name, test.expectedDefaultAfter, defaultPriority)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var intPriority = int32(1000)
|
||||
|
||||
func TestPodAdmission(t *testing.T) {
|
||||
containerName := "container"
|
||||
|
||||
pods := []*api.Pod{
|
||||
// pod[0]: Pod with a proper priority class.
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod-w-priorityclass",
|
||||
Namespace: "namespace",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: containerName,
|
||||
},
|
||||
},
|
||||
PriorityClassName: "default1",
|
||||
},
|
||||
},
|
||||
// pod[1]: Pod with no priority class
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod-wo-priorityclass",
|
||||
Namespace: "namespace",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: containerName,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
// pod[2]: Pod with non-existing priority class
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod-w-non-existing-priorityclass",
|
||||
Namespace: "namespace",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: containerName,
|
||||
},
|
||||
},
|
||||
PriorityClassName: "non-existing",
|
||||
},
|
||||
},
|
||||
// pod[3]: Pod with integer value of priority
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod-w-integer-priority",
|
||||
Namespace: "namespace",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: containerName,
|
||||
},
|
||||
},
|
||||
PriorityClassName: "default1",
|
||||
Priority: &intPriority,
|
||||
},
|
||||
},
|
||||
// pod[4]: Pod with a system priority class name
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod-w-system-priority",
|
||||
Namespace: "namespace",
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: containerName,
|
||||
},
|
||||
},
|
||||
PriorityClassName: scheduling.SystemClusterCritical,
|
||||
},
|
||||
},
|
||||
// pod[5]: mirror Pod with a system priority class name
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "mirror-pod-w-system-priority",
|
||||
Namespace: "namespace",
|
||||
Annotations: map[string]string{api.MirrorPodAnnotationKey: ""},
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: containerName,
|
||||
},
|
||||
},
|
||||
PriorityClassName: "system-cluster-critical",
|
||||
},
|
||||
},
|
||||
// pod[6]: mirror Pod with integer value of priority
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "mirror-pod-w-integer-priority",
|
||||
Namespace: "namespace",
|
||||
Annotations: map[string]string{api.MirrorPodAnnotationKey: ""},
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: containerName,
|
||||
},
|
||||
},
|
||||
PriorityClassName: "default1",
|
||||
Priority: &intPriority,
|
||||
},
|
||||
},
|
||||
// pod[7]: Pod with a critical priority annotation. This needs to be automatically assigned
|
||||
// system-cluster-critical
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "pod-w-system-priority",
|
||||
Namespace: "kube-system",
|
||||
Annotations: map[string]string{"scheduler.alpha.kubernetes.io/critical-pod": ""},
|
||||
},
|
||||
Spec: api.PodSpec{
|
||||
Containers: []api.Container{
|
||||
{
|
||||
Name: containerName,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
// Enable PodPriority feature gate.
|
||||
utilfeature.DefaultFeatureGate.Set(fmt.Sprintf("%s=true", features.PodPriority))
|
||||
// Enable ExperimentalCriticalPodAnnotation feature gate.
|
||||
utilfeature.DefaultFeatureGate.Set(fmt.Sprintf("%s=true", features.ExperimentalCriticalPodAnnotation))
|
||||
tests := []struct {
|
||||
name string
|
||||
existingClasses []*scheduling.PriorityClass
|
||||
// Admission controller changes pod spec. So, we take an api.Pod instead of
|
||||
// *api.Pod to avoid interfering with other tests.
|
||||
pod api.Pod
|
||||
expectedPriority int32
|
||||
expectError bool
|
||||
}{
|
||||
{
|
||||
"Pod with priority class",
|
||||
[]*scheduling.PriorityClass{defaultClass1, nondefaultClass1},
|
||||
*pods[0],
|
||||
1000,
|
||||
false,
|
||||
},
|
||||
|
||||
{
|
||||
"Pod without priority class",
|
||||
[]*scheduling.PriorityClass{defaultClass1},
|
||||
*pods[1],
|
||||
1000,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"pod without priority class and no existing priority class",
|
||||
[]*scheduling.PriorityClass{},
|
||||
*pods[1],
|
||||
scheduling.DefaultPriorityWhenNoDefaultClassExists,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"pod without priority class and no default class",
|
||||
[]*scheduling.PriorityClass{nondefaultClass1},
|
||||
*pods[1],
|
||||
scheduling.DefaultPriorityWhenNoDefaultClassExists,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"pod with a system priority class",
|
||||
[]*scheduling.PriorityClass{systemClusterCritical},
|
||||
*pods[4],
|
||||
scheduling.SystemCriticalPriority,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"Pod with non-existing priority class",
|
||||
[]*scheduling.PriorityClass{defaultClass1, nondefaultClass1},
|
||||
*pods[2],
|
||||
0,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"pod with integer priority",
|
||||
[]*scheduling.PriorityClass{},
|
||||
*pods[3],
|
||||
0,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"mirror pod with system priority class",
|
||||
[]*scheduling.PriorityClass{systemClusterCritical},
|
||||
*pods[5],
|
||||
scheduling.SystemCriticalPriority,
|
||||
false,
|
||||
},
|
||||
{
|
||||
"mirror pod with integer priority",
|
||||
[]*scheduling.PriorityClass{},
|
||||
*pods[6],
|
||||
0,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"pod with critical pod annotation",
|
||||
[]*scheduling.PriorityClass{systemClusterCritical},
|
||||
*pods[7],
|
||||
scheduling.SystemCriticalPriority,
|
||||
false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
glog.V(4).Infof("starting test %q", test.name)
|
||||
|
||||
ctrl := newPlugin()
|
||||
// Add existing priority classes.
|
||||
addPriorityClasses(ctrl, test.existingClasses)
|
||||
|
||||
// Create pod.
|
||||
attrs := admission.NewAttributesRecord(
|
||||
&test.pod,
|
||||
nil,
|
||||
api.Kind("Pod").WithVersion("version"),
|
||||
test.pod.ObjectMeta.Namespace,
|
||||
"",
|
||||
api.Resource("pods").WithVersion("version"),
|
||||
"",
|
||||
admission.Create,
|
||||
nil,
|
||||
)
|
||||
err := ctrl.Admit(attrs)
|
||||
glog.Infof("Got %v", err)
|
||||
if !test.expectError {
|
||||
if err != nil {
|
||||
t.Errorf("Test %q: unexpected error received: %v", test.name, err)
|
||||
}
|
||||
if *test.pod.Spec.Priority != test.expectedPriority {
|
||||
t.Errorf("Test %q: expected priority is %d, but got %d.", test.name, test.expectedPriority, *test.pod.Spec.Priority)
|
||||
}
|
||||
}
|
||||
if err == nil && test.expectError {
|
||||
t.Errorf("Test %q: expected error and no error recevied", test.name)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user