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