kubernetes/pkg/apis/node/validation/validation.go

103 lines
3.8 KiB
Go
Raw Permalink Normal View History

2019-02-22 13:45:00 -05:00
/*
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"
2019-02-22 13:45:00 -05:00
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/kubernetes/pkg/apis/core"
corevalidation "k8s.io/kubernetes/pkg/apis/core/validation"
2019-02-22 13:45:00 -05:00
"k8s.io/kubernetes/pkg/apis/node"
"regexp"
2019-02-22 13:45:00 -05:00
)
// Define normalization rules to handle field name differences across API versions
// v1alpha1 uses "spec.runtimeHandler" while v1/v1beta1 use "handler"
var NodeNormalizationRules = []field.NormalizationRule{
{
Regexp: regexp.MustCompile(`^spec\.runtimeHandler(.*)$`),
Replacement: "handler$1",
},
}
2019-02-22 13:45:00 -05:00
// ValidateRuntimeClass validates the RuntimeClass
func ValidateRuntimeClass(rc *node.RuntimeClass) field.ErrorList {
allErrs := apivalidation.ValidateObjectMeta(&rc.ObjectMeta, false, apivalidation.NameIsDNSSubdomain, field.NewPath("metadata"))
2025-12-06 14:02:19 -05:00
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")
}
2019-02-22 13:45:00 -05:00
}
2019-08-02 20:09:52 -04:00
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"))...)
}
2019-02-22 13:45:00 -05:00
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")
2019-02-22 13:45:00 -05:00
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 {
KEP-5471: Extend tolerations operators (#134665) * Add numeric operations to tolerations Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * code review feedback Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * add default feature gate Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * Add integration tests Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * Add toleration value validation Signed-off-by: Heba Elayoty <heelayot@microsoft.com> * Add validate options for new operators Signed-off-by: helayoty <heelayot@microsoft.com> * Remove log Signed-off-by: helayoty <heelayot@microsoft.com> * Update feature gate check Signed-off-by: helayoty <heelayot@microsoft.com> * emove IsValidNumericString func Signed-off-by: helayoty <heelayot@microsoft.com> * Implement IsDecimalInteger Signed-off-by: helayoty <heelayot@microsoft.com> * code review feedback Signed-off-by: helayoty <heelayot@microsoft.com> * Add logs to v1/toleration Signed-off-by: Heba Elayoty <heelayot@microsoft.com> Signed-off-by: helayoty <heelayot@microsoft.com> * Update integration tests and address code review feedback Signed-off-by: helayoty <heelayot@microsoft.com> * Add feature gate to the scheduler framework Signed-off-by: helayoty <heelayot@microsoft.com> * Remove extra test Signed-off-by: helayoty <heelayot@microsoft.com> * Fix integration test Signed-off-by: helayoty <heelayot@microsoft.com> * pass feature gate via TolerationsTolerateTaint Signed-off-by: helayoty <heelayot@microsoft.com> --------- Signed-off-by: Heba Elayoty <heelayot@microsoft.com> Signed-off-by: helayoty <heelayot@microsoft.com>
2025-11-10 15:42:54 -05:00
allErrs := corevalidation.ValidateTolerations(tolerations, fldPath, corevalidation.PodValidationOptions{})
// 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
}