mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-20 17:38:50 +00:00
Allow system critical priority classes in API validation
This commit is contained in:
65
pkg/apis/scheduling/helpers.go
Normal file
65
pkg/apis/scheduling/helpers.go
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
Copyright 2018 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 scheduling
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
// SystemPriorityClasses define system priority classes that are auto-created at cluster bootstrapping.
|
||||
// Our API validation logic ensures that any priority class that has a system prefix or its value
|
||||
// is higher than HighestUserDefinablePriority is equal to one of these SystemPriorityClasses.
|
||||
var systemPriorityClasses = []*PriorityClass{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: SystemNodeCritical,
|
||||
},
|
||||
Value: SystemCriticalPriority + 1000,
|
||||
Description: "Used for system critical pods that must not be moved from their current node.",
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: SystemClusterCritical,
|
||||
},
|
||||
Value: SystemCriticalPriority,
|
||||
Description: "Used for system critical pods that must run in the cluster, but can be moved to another node if necessary.",
|
||||
},
|
||||
}
|
||||
|
||||
// SystemPriorityClasses returns the list of system priority classes.
|
||||
// NOTE: be careful not to modify any of elements of the returned array directly.
|
||||
func SystemPriorityClasses() []*PriorityClass {
|
||||
return systemPriorityClasses
|
||||
}
|
||||
|
||||
// IsKnownSystemPriorityClass checks that "pc" is equal to one of the system PriorityClasses.
|
||||
// It ignores "description", labels, annotations, etc. of the PriorityClass.
|
||||
func IsKnownSystemPriorityClass(pc *PriorityClass) (bool, error) {
|
||||
for _, spc := range systemPriorityClasses {
|
||||
if spc.Name == pc.Name {
|
||||
if spc.Value != pc.Value {
|
||||
return false, fmt.Errorf("value of %v PriorityClass must be %v", spc.Name, spc.Value)
|
||||
}
|
||||
if spc.GlobalDefault != pc.GlobalDefault {
|
||||
return false, fmt.Errorf("globalDefault of %v PriorityClass must be %v", spc.Name, spc.GlobalDefault)
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
return false, fmt.Errorf("%v is not a known system priority class", pc.Name)
|
||||
}
|
54
pkg/apis/scheduling/helpers_test.go
Normal file
54
pkg/apis/scheduling/helpers_test.go
Normal file
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
Copyright 2018 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 scheduling
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
)
|
||||
|
||||
func TestIsKnownSystemPriorityClass(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
pc *PriorityClass
|
||||
expected bool
|
||||
}{
|
||||
{
|
||||
name: "system priority class",
|
||||
pc: SystemPriorityClasses()[0],
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "non-system priority class",
|
||||
pc: &PriorityClass{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: SystemNodeCritical,
|
||||
},
|
||||
Value: SystemCriticalPriority, // This is the value of system cluster critical
|
||||
Description: "Used for system critical pods that must not be moved from their current node.",
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
if is, err := IsKnownSystemPriorityClass(test.pc); test.expected != is {
|
||||
t.Errorf("Test [%v]: Expected %v, but got %v. Error: %v", test.name, test.expected, is, err)
|
||||
}
|
||||
}
|
||||
}
|
@@ -17,22 +17,29 @@ limitations under the License.
|
||||
package validation
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/validation/field"
|
||||
apivalidation "k8s.io/kubernetes/pkg/apis/core/validation"
|
||||
"k8s.io/kubernetes/pkg/apis/scheduling"
|
||||
)
|
||||
|
||||
// ValidatePriorityClassName checks whether the given priority class name is valid.
|
||||
func ValidatePriorityClassName(name string, prefix bool) []string {
|
||||
return apivalidation.NameIsDNSSubdomain(name, prefix)
|
||||
}
|
||||
|
||||
// ValidatePriorityClass tests whether required fields in the PriorityClass are
|
||||
// set correctly.
|
||||
func ValidatePriorityClass(pc *scheduling.PriorityClass) field.ErrorList {
|
||||
allErrs := field.ErrorList{}
|
||||
allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&pc.ObjectMeta, false, ValidatePriorityClassName, field.NewPath("metadata"))...)
|
||||
// The "Value" field can be any valid integer. So, no need to validate.
|
||||
allErrs = append(allErrs, apivalidation.ValidateObjectMeta(&pc.ObjectMeta, false, apivalidation.NameIsDNSSubdomain, field.NewPath("metadata"))...)
|
||||
// If the priorityClass starts with a system prefix, it must be one of the
|
||||
// predefined system priority classes.
|
||||
if strings.HasPrefix(pc.Name, scheduling.SystemPriorityClassPrefix) {
|
||||
if is, err := scheduling.IsKnownSystemPriorityClass(pc); !is {
|
||||
allErrs = append(allErrs, field.Forbidden(field.NewPath("metadata", "name"), "priority class names with '"+scheduling.SystemPriorityClassPrefix+"' prefix are reserved for system use only. error: "+err.Error()))
|
||||
}
|
||||
} else if pc.Value > scheduling.HighestUserDefinablePriority {
|
||||
// Non-system critical priority classes are not allowed to have a value larger than HighestUserDefinablePriority.
|
||||
allErrs = append(allErrs, field.Forbidden(field.NewPath("value"), fmt.Sprintf("maximum allowed value of a user defined priority is %v", scheduling.HighestUserDefinablePriority)))
|
||||
}
|
||||
return allErrs
|
||||
}
|
||||
|
||||
|
@@ -25,6 +25,7 @@ import (
|
||||
)
|
||||
|
||||
func TestValidatePriorityClass(t *testing.T) {
|
||||
spcs := scheduling.SystemPriorityClasses()
|
||||
successCases := map[string]scheduling.PriorityClass{
|
||||
"no description": {
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "tier1", Namespace: ""},
|
||||
@@ -36,6 +37,12 @@ func TestValidatePriorityClass(t *testing.T) {
|
||||
GlobalDefault: false,
|
||||
Description: "Used for the highest priority pods.",
|
||||
},
|
||||
"system node critical": {
|
||||
ObjectMeta: metav1.ObjectMeta{Name: spcs[0].Name, Namespace: ""},
|
||||
Value: spcs[0].Value,
|
||||
GlobalDefault: spcs[0].GlobalDefault,
|
||||
Description: "system priority class 0",
|
||||
},
|
||||
}
|
||||
|
||||
for k, v := range successCases {
|
||||
@@ -53,6 +60,16 @@ func TestValidatePriorityClass(t *testing.T) {
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "tier&1", Namespace: ""},
|
||||
Value: 100,
|
||||
},
|
||||
"incorrect system class name": {
|
||||
ObjectMeta: metav1.ObjectMeta{Name: spcs[0].Name, Namespace: ""},
|
||||
Value: 0,
|
||||
GlobalDefault: spcs[0].GlobalDefault,
|
||||
},
|
||||
"incorrect system class value": {
|
||||
ObjectMeta: metav1.ObjectMeta{Name: "system-something", Namespace: ""},
|
||||
Value: spcs[0].Value,
|
||||
GlobalDefault: spcs[0].GlobalDefault,
|
||||
},
|
||||
}
|
||||
|
||||
for k, v := range errorCases {
|
||||
|
Reference in New Issue
Block a user