kube.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. Copyright 2025 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 content
  14. import (
  15. "regexp"
  16. "strings"
  17. )
  18. const labelKeyCharFmt string = "[A-Za-z0-9]"
  19. const labelKeyExtCharFmt string = "[-A-Za-z0-9_.]"
  20. const labelKeyFmt string = "(" + labelKeyCharFmt + labelKeyExtCharFmt + "*)?" + labelKeyCharFmt
  21. const labelKeyErrMsg string = "must consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character"
  22. const labelKeyMaxLength int = 63
  23. var labelKeyRegexp = regexp.MustCompile("^" + labelKeyFmt + "$")
  24. // IsQualifiedName tests whether the value passed is what Kubernetes calls a
  25. // "qualified name", which is the same as a label key.
  26. //
  27. // Deprecated: use IsLabelKey instead.
  28. var IsQualifiedName = IsLabelKey
  29. // IsLabelKey tests whether the value passed is a valid label key. This format
  30. // is used to validate many fields in the Kubernetes API.
  31. // Label keys consist of an optional prefix and a name, separated by a '/'.
  32. // If the value is not valid, a list of error strings is returned. Otherwise, an
  33. // empty list (or nil) is returned.
  34. func IsLabelKey(value string) []string {
  35. var errs []string
  36. parts := strings.Split(value, "/")
  37. var name string
  38. switch len(parts) {
  39. case 1:
  40. name = parts[0]
  41. case 2:
  42. var prefix string
  43. prefix, name = parts[0], parts[1]
  44. if len(prefix) == 0 {
  45. errs = append(errs, "prefix part "+EmptyError())
  46. } else if msgs := IsDNS1123Subdomain(prefix); len(msgs) != 0 {
  47. errs = append(errs, prefixEach(msgs, "prefix part ")...)
  48. }
  49. default:
  50. return append(errs, "a valid label key "+RegexError(labelKeyErrMsg, labelKeyFmt, "MyName", "my.name", "123-abc")+
  51. " with an optional DNS subdomain prefix and '/' (e.g. 'example.com/MyName')")
  52. }
  53. if len(name) == 0 {
  54. errs = append(errs, "name part "+EmptyError())
  55. } else if len(name) > labelKeyMaxLength {
  56. errs = append(errs, "name part "+MaxLenError(labelKeyMaxLength))
  57. }
  58. if !labelKeyRegexp.MatchString(name) {
  59. errs = append(errs, "name part "+RegexError(labelKeyErrMsg, labelKeyFmt, "MyName", "my.name", "123-abc"))
  60. }
  61. return errs
  62. }
  63. const labelValueFmt string = "(" + labelKeyFmt + ")?"
  64. const labelValueErrMsg string = "a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character"
  65. // LabelValueMaxLength is a label's max length
  66. const LabelValueMaxLength int = 63
  67. var labelValueRegexp = regexp.MustCompile("^" + labelValueFmt + "$")
  68. // IsLabelValue tests whether the value passed is a valid label value. If
  69. // the value is not valid, a list of error strings is returned. Otherwise an
  70. // empty list (or nil) is returned.
  71. func IsLabelValue(value string) []string {
  72. var errs []string
  73. if len(value) > LabelValueMaxLength {
  74. errs = append(errs, MaxLenError(LabelValueMaxLength))
  75. }
  76. if !labelValueRegexp.MatchString(value) {
  77. errs = append(errs, RegexError(labelValueErrMsg, labelValueFmt, "MyValue", "my_value", "12345"))
  78. }
  79. return errs
  80. }
  81. func prefixEach(msgs []string, prefix string) []string {
  82. for i := range msgs {
  83. msgs[i] = prefix + msgs[i]
  84. }
  85. return msgs
  86. }