github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/api/validation/generic.go (about) 1 /* 2 Copyright 2014 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 "strings" 21 22 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/validation" 23 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/util/validation/field" 24 ) 25 26 // IsNegativeErrorMsg is a error message for value must be greater than or equal to 0. 27 const IsNegativeErrorMsg string = `must be greater than or equal to 0` 28 29 // ValidateNameFunc validates that the provided name is valid for a given resource type. 30 // Not all resources have the same validation rules for names. Prefix is true 31 // if the name will have a value appended to it. If the name is not valid, 32 // this returns a list of descriptions of individual characteristics of the 33 // value that were not valid. Otherwise this returns an empty list or nil. 34 type ValidateNameFunc func(name string, prefix bool) []string 35 36 // NameIsDNSSubdomain is a ValidateNameFunc for names that must be a DNS subdomain. 37 func NameIsDNSSubdomain(name string, prefix bool) []string { 38 if prefix { 39 name = maskTrailingDash(name) 40 } 41 return validation.IsDNS1123Subdomain(name) 42 } 43 44 // NameIsDNSLabel is a ValidateNameFunc for names that must be a DNS 1123 label. 45 func NameIsDNSLabel(name string, prefix bool) []string { 46 if prefix { 47 name = maskTrailingDash(name) 48 } 49 return validation.IsDNS1123Label(name) 50 } 51 52 // NameIsDNS1035Label is a ValidateNameFunc for names that must be a DNS 952 label. 53 func NameIsDNS1035Label(name string, prefix bool) []string { 54 if prefix { 55 name = maskTrailingDash(name) 56 } 57 return validation.IsDNS1035Label(name) 58 } 59 60 // ValidateNamespaceName can be used to check whether the given namespace name is valid. 61 // Prefix indicates this name will be used as part of generation, in which case 62 // trailing dashes are allowed. 63 var ValidateNamespaceName = NameIsDNSLabel 64 65 // ValidateServiceAccountName can be used to check whether the given service account name is valid. 66 // Prefix indicates this name will be used as part of generation, in which case 67 // trailing dashes are allowed. 68 var ValidateServiceAccountName = NameIsDNSSubdomain 69 70 // maskTrailingDash replaces the final character of a string with a subdomain safe 71 // value if it is a dash and if the length of this string is greater than 1. Note that 72 // this is used when a value could be appended to the string, see ValidateNameFunc 73 // for more info. 74 func maskTrailingDash(name string) string { 75 if len(name) > 1 && strings.HasSuffix(name, "-") { 76 return name[:len(name)-2] + "a" 77 } 78 return name 79 } 80 81 // ValidateNonnegativeField validates that given value is not negative. 82 func ValidateNonnegativeField(value int64, fldPath *field.Path) field.ErrorList { 83 allErrs := field.ErrorList{} 84 if value < 0 { 85 allErrs = append(allErrs, field.Invalid(fldPath, value, IsNegativeErrorMsg)) 86 } 87 return allErrs 88 }