k8s.io/kubernetes@v1.29.3/pkg/apis/node/validation/validation.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package validation 18 19 import ( 20 apivalidation "k8s.io/apimachinery/pkg/api/validation" 21 unversionedvalidation "k8s.io/apimachinery/pkg/apis/meta/v1/validation" 22 "k8s.io/apimachinery/pkg/util/validation/field" 23 "k8s.io/kubernetes/pkg/apis/core" 24 corevalidation "k8s.io/kubernetes/pkg/apis/core/validation" 25 "k8s.io/kubernetes/pkg/apis/node" 26 ) 27 28 // ValidateRuntimeClass validates the RuntimeClass 29 func ValidateRuntimeClass(rc *node.RuntimeClass) field.ErrorList { 30 allErrs := apivalidation.ValidateObjectMeta(&rc.ObjectMeta, false, apivalidation.NameIsDNSSubdomain, field.NewPath("metadata")) 31 32 for _, msg := range apivalidation.NameIsDNSLabel(rc.Handler, false) { 33 allErrs = append(allErrs, field.Invalid(field.NewPath("handler"), rc.Handler, msg)) 34 } 35 36 if rc.Overhead != nil { 37 allErrs = append(allErrs, validateOverhead(rc.Overhead, field.NewPath("overhead"))...) 38 } 39 if rc.Scheduling != nil { 40 allErrs = append(allErrs, validateScheduling(rc.Scheduling, field.NewPath("scheduling"))...) 41 } 42 43 return allErrs 44 } 45 46 // ValidateRuntimeClassUpdate validates an update to the object 47 func ValidateRuntimeClassUpdate(new, old *node.RuntimeClass) field.ErrorList { 48 allErrs := apivalidation.ValidateObjectMetaUpdate(&new.ObjectMeta, &old.ObjectMeta, field.NewPath("metadata")) 49 50 allErrs = append(allErrs, apivalidation.ValidateImmutableField(new.Handler, old.Handler, field.NewPath("handler"))...) 51 52 return allErrs 53 } 54 55 func validateOverhead(overhead *node.Overhead, fldPath *field.Path) field.ErrorList { 56 // reuse the ResourceRequirements validation logic 57 return corevalidation.ValidateResourceRequirements(&core.ResourceRequirements{Limits: overhead.PodFixed}, nil, fldPath, 58 corevalidation.PodValidationOptions{}) 59 } 60 61 func validateScheduling(s *node.Scheduling, fldPath *field.Path) field.ErrorList { 62 var allErrs field.ErrorList 63 if s.NodeSelector != nil { 64 allErrs = append(allErrs, unversionedvalidation.ValidateLabels(s.NodeSelector, fldPath.Child("nodeSelector"))...) 65 } 66 allErrs = append(allErrs, validateTolerations(s.Tolerations, fldPath.Child("tolerations"))...) 67 return allErrs 68 } 69 70 func validateTolerations(tolerations []core.Toleration, fldPath *field.Path) field.ErrorList { 71 allErrs := corevalidation.ValidateTolerations(tolerations, fldPath.Child("tolerations")) 72 // Ensure uniquenes of tolerations. 73 tolerationSet := map[core.Toleration]bool{} 74 for i, t := range tolerations { 75 // listKey includes the toleration fields identified as listKeys in the API. 76 listKey := core.Toleration{ 77 Key: t.Key, 78 Operator: t.Operator, 79 Value: t.Value, 80 Effect: t.Effect, 81 } 82 if tolerationSet[listKey] { 83 allErrs = append(allErrs, field.Duplicate(fldPath.Index(i), t)) 84 } else { 85 tolerationSet[listKey] = true 86 } 87 } 88 return allErrs 89 }