generic.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. Copyright 2014 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package validation
  14. import (
  15. "strings"
  16. "k8s.io/apimachinery/pkg/util/validation"
  17. "k8s.io/apimachinery/pkg/util/validation/field"
  18. )
  19. // IsNegativeErrorMsg is a error message for value must be greater than or equal to 0.
  20. const IsNegativeErrorMsg string = `must be greater than or equal to 0`
  21. // ValidateNameFunc validates that the provided name is valid for a given resource type.
  22. // Not all resources have the same validation rules for names. Prefix is true
  23. // if the name will have a value appended to it. If the name is not valid,
  24. // this returns a list of descriptions of individual characteristics of the
  25. // value that were not valid. Otherwise this returns an empty list or nil.
  26. type ValidateNameFunc func(name string, prefix bool) []string
  27. // NameIsDNSSubdomain is a ValidateNameFunc for names that must be a DNS subdomain.
  28. func NameIsDNSSubdomain(name string, prefix bool) []string {
  29. if prefix {
  30. name = maskTrailingDash(name)
  31. }
  32. return validation.IsDNS1123Subdomain(name)
  33. }
  34. // NameIsDNSLabel is a ValidateNameFunc for names that must be a DNS 1123 label.
  35. func NameIsDNSLabel(name string, prefix bool) []string {
  36. if prefix {
  37. name = maskTrailingDash(name)
  38. }
  39. return validation.IsDNS1123Label(name)
  40. }
  41. // NameIsDNS1035Label is a ValidateNameFunc for names that must be a DNS 952 label.
  42. func NameIsDNS1035Label(name string, prefix bool) []string {
  43. if prefix {
  44. name = maskTrailingDash(name)
  45. }
  46. return validation.IsDNS1035Label(name)
  47. }
  48. // ValidateNamespaceName can be used to check whether the given namespace name is valid.
  49. // Prefix indicates this name will be used as part of generation, in which case
  50. // trailing dashes are allowed.
  51. var ValidateNamespaceName = NameIsDNSLabel
  52. // ValidateServiceAccountName can be used to check whether the given service account name is valid.
  53. // Prefix indicates this name will be used as part of generation, in which case
  54. // trailing dashes are allowed.
  55. var ValidateServiceAccountName = NameIsDNSSubdomain
  56. // maskTrailingDash replaces the final character of a string with a subdomain safe
  57. // value if it is a dash and if the length of this string is greater than 1. Note that
  58. // this is used when a value could be appended to the string, see ValidateNameFunc
  59. // for more info.
  60. func maskTrailingDash(name string) string {
  61. if len(name) > 1 && strings.HasSuffix(name, "-") {
  62. return name[:len(name)-2] + "a"
  63. }
  64. return name
  65. }
  66. // ValidateNonnegativeField validates that given value is not negative.
  67. func ValidateNonnegativeField(value int64, fldPath *field.Path) field.ErrorList {
  68. allErrs := field.ErrorList{}
  69. if value < 0 {
  70. allErrs = append(allErrs, field.Invalid(fldPath, value, IsNegativeErrorMsg))
  71. }
  72. return allErrs
  73. }