k8s.io/kubernetes@v1.29.3/pkg/apis/coordination/validation/validation.go (about) 1 /* 2 Copyright 2018 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 "k8s.io/apimachinery/pkg/api/validation" 21 "k8s.io/apimachinery/pkg/util/validation/field" 22 "k8s.io/kubernetes/pkg/apis/coordination" 23 ) 24 25 // ValidateLease validates a Lease. 26 func ValidateLease(lease *coordination.Lease) field.ErrorList { 27 allErrs := validation.ValidateObjectMeta(&lease.ObjectMeta, true, validation.NameIsDNSSubdomain, field.NewPath("metadata")) 28 allErrs = append(allErrs, ValidateLeaseSpec(&lease.Spec, field.NewPath("spec"))...) 29 return allErrs 30 } 31 32 // ValidateLeaseUpdate validates an update of Lease object. 33 func ValidateLeaseUpdate(lease, oldLease *coordination.Lease) field.ErrorList { 34 allErrs := validation.ValidateObjectMetaUpdate(&lease.ObjectMeta, &oldLease.ObjectMeta, field.NewPath("metadata")) 35 allErrs = append(allErrs, ValidateLeaseSpec(&lease.Spec, field.NewPath("spec"))...) 36 return allErrs 37 } 38 39 // ValidateLeaseSpec validates spec of Lease. 40 func ValidateLeaseSpec(spec *coordination.LeaseSpec, fldPath *field.Path) field.ErrorList { 41 allErrs := field.ErrorList{} 42 43 if spec.LeaseDurationSeconds != nil && *spec.LeaseDurationSeconds <= 0 { 44 fld := fldPath.Child("leaseDurationSeconds") 45 allErrs = append(allErrs, field.Invalid(fld, spec.LeaseDurationSeconds, "must be greater than 0")) 46 } 47 if spec.LeaseTransitions != nil && *spec.LeaseTransitions < 0 { 48 fld := fldPath.Child("leaseTransitions") 49 allErrs = append(allErrs, field.Invalid(fld, spec.LeaseTransitions, "must be greater than or equal to 0")) 50 } 51 return allErrs 52 }