kubernetes/pkg/apis/node/validation/validation.go
Yongrui Lin 8976f7a9e1 Declaratively validate Toleration.Key as k8s-label-key
Tag corev1.Toleration.Key (alpha) and mark the shared handwritten error covered
via an opt-in KeyFormatCovered option, so the annotation-encoded path stays
authoritative. Add a RuntimeClass spec.scheduling normalization rule for the
v1alpha1 cross-version path.
2026-06-11 06:33:42 +00:00

108 lines
4.1 KiB
Go

/*
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 validation
import (
apivalidation "k8s.io/apimachinery/pkg/api/validation"
unversionedvalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/apis/core"
corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
"k8s.io/kubernetes/pkg/apis/node"
"regexp"
)
// Define normalization rules to handle field name differences across API versions.
// v1alpha1 nests fields under "spec" (e.g. "spec.runtimeHandler", "spec.scheduling")
// while v1/v1beta1 expose them at the top level ("handler", "scheduling"), matching
// the internal type used by handwritten validation.
var NodeNormalizationRules = []field.NormalizationRule{
{
Regexp: regexp.MustCompile(`^spec\.runtimeHandler(.*)$`),
Replacement: "handler$1",
},
{
Regexp: regexp.MustCompile(`^spec\.scheduling(.*)$`),
Replacement: "scheduling$1",
},
}
// ValidateRuntimeClass validates the RuntimeClass
func ValidateRuntimeClass(rc *node.RuntimeClass) field.ErrorList {
allErrs := apivalidation.ValidateObjectMeta(&rc.ObjectMeta, false, apivalidation.NameIsDNSSubdomain, field.NewPath("metadata"))
if rc.Handler == "" {
allErrs = append(allErrs, field.Required(field.NewPath("handler"), "").MarkCoveredByDeclarative())
} else {
for _, msg := range apivalidation.NameIsDNSLabel(rc.Handler, false) {
allErrs = append(allErrs, field.Invalid(field.NewPath("handler"), rc.Handler, msg).MarkCoveredByDeclarative().WithOrigin("format=k8s-short-name"))
}
}
if rc.Overhead != nil {
allErrs = append(allErrs, validateOverhead(rc.Overhead, field.NewPath("overhead"))...)
}
if rc.Scheduling != nil {
allErrs = append(allErrs, validateScheduling(rc.Scheduling, field.NewPath("scheduling"))...)
}
return allErrs
}
// ValidateRuntimeClassUpdate validates an update to the object
func ValidateRuntimeClassUpdate(new, old *node.RuntimeClass) field.ErrorList {
allErrs := apivalidation.ValidateObjectMetaUpdate(&new.ObjectMeta, &old.ObjectMeta, field.NewPath("metadata"))
allErrs = append(allErrs, apivalidation.ValidateImmutableField(new.Handler, old.Handler, field.NewPath("handler")).MarkCoveredByDeclarative().WithOrigin("immutable")...)
return allErrs
}
func validateOverhead(overhead *node.Overhead, fldPath *field.Path) field.ErrorList {
// reuse the ResourceRequirements validation logic
return corevalidation.ValidateContainerResourceRequirements(&core.ResourceRequirements{Limits: overhead.PodFixed}, nil, fldPath,
corevalidation.PodValidationOptions{})
}
func validateScheduling(s *node.Scheduling, fldPath *field.Path) field.ErrorList {
var allErrs field.ErrorList
if s.NodeSelector != nil {
allErrs = append(allErrs, unversionedvalidation.ValidateLabels(s.NodeSelector, fldPath.Child("nodeSelector"))...)
}
allErrs = append(allErrs, validateTolerations(s.Tolerations, fldPath.Child("tolerations"))...)
return allErrs
}
func validateTolerations(tolerations []core.Toleration, fldPath *field.Path) field.ErrorList {
allErrs := corevalidation.ValidateTolerations(tolerations, fldPath, corevalidation.PodValidationOptions{}, corevalidation.KeyFormatCovered)
// Ensure uniquenes of tolerations.
tolerationSet := map[core.Toleration]bool{}
for i, t := range tolerations {
// listKey includes the toleration fields identified as listKeys in the API.
listKey := core.Toleration{
Key: t.Key,
Operator: t.Operator,
Value: t.Value,
Effect: t.Effect,
}
if tolerationSet[listKey] {
allErrs = append(allErrs, field.Duplicate(fldPath.Index(i), t))
} else {
tolerationSet[listKey] = true
}
}
return allErrs
}