labels.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 labels
  14. import (
  15. "fmt"
  16. "sort"
  17. "strings"
  18. "k8s.io/apimachinery/pkg/util/validation/field"
  19. )
  20. // Labels allows you to present labels independently from their storage.
  21. type Labels interface {
  22. // Has returns whether the provided label exists.
  23. Has(label string) (exists bool)
  24. // Get returns the value for the provided label.
  25. Get(label string) (value string)
  26. }
  27. // Set is a map of label:value. It implements Labels.
  28. type Set map[string]string
  29. // String returns all labels listed as a human readable string.
  30. // Conveniently, exactly the format that ParseSelector takes.
  31. func (ls Set) String() string {
  32. selector := make([]string, 0, len(ls))
  33. for key, value := range ls {
  34. selector = append(selector, key+"="+value)
  35. }
  36. // Sort for determinism.
  37. sort.StringSlice(selector).Sort()
  38. return strings.Join(selector, ",")
  39. }
  40. // Has returns whether the provided label exists in the map.
  41. func (ls Set) Has(label string) bool {
  42. _, exists := ls[label]
  43. return exists
  44. }
  45. // Get returns the value in the map for the provided label.
  46. func (ls Set) Get(label string) string {
  47. return ls[label]
  48. }
  49. // AsSelector converts labels into a selectors. It does not
  50. // perform any validation, which means the server will reject
  51. // the request if the Set contains invalid values.
  52. func (ls Set) AsSelector() Selector {
  53. return SelectorFromSet(ls)
  54. }
  55. // AsValidatedSelector converts labels into a selectors.
  56. // The Set is validated client-side, which allows to catch errors early.
  57. func (ls Set) AsValidatedSelector() (Selector, error) {
  58. return ValidatedSelectorFromSet(ls)
  59. }
  60. // AsSelectorPreValidated converts labels into a selector, but
  61. // assumes that labels are already validated and thus doesn't
  62. // perform any validation.
  63. // According to our measurements this is significantly faster
  64. // in codepaths that matter at high scale.
  65. func (ls Set) AsSelectorPreValidated() Selector {
  66. return SelectorFromValidatedSet(ls)
  67. }
  68. // FormatLabels converts label map into plain string
  69. func FormatLabels(labelMap map[string]string) string {
  70. l := Set(labelMap).String()
  71. if l == "" {
  72. l = "<none>"
  73. }
  74. return l
  75. }
  76. // Conflicts takes 2 maps and returns true if there a key match between
  77. // the maps but the value doesn't match, and returns false in other cases
  78. func Conflicts(labels1, labels2 Set) bool {
  79. small := labels1
  80. big := labels2
  81. if len(labels2) < len(labels1) {
  82. small = labels2
  83. big = labels1
  84. }
  85. for k, v := range small {
  86. if val, match := big[k]; match {
  87. if val != v {
  88. return true
  89. }
  90. }
  91. }
  92. return false
  93. }
  94. // Merge combines given maps, and does not check for any conflicts
  95. // between the maps. In case of conflicts, second map (labels2) wins
  96. func Merge(labels1, labels2 Set) Set {
  97. mergedMap := Set{}
  98. for k, v := range labels1 {
  99. mergedMap[k] = v
  100. }
  101. for k, v := range labels2 {
  102. mergedMap[k] = v
  103. }
  104. return mergedMap
  105. }
  106. // Equals returns true if the given maps are equal
  107. func Equals(labels1, labels2 Set) bool {
  108. if len(labels1) != len(labels2) {
  109. return false
  110. }
  111. for k, v := range labels1 {
  112. value, ok := labels2[k]
  113. if !ok {
  114. return false
  115. }
  116. if value != v {
  117. return false
  118. }
  119. }
  120. return true
  121. }
  122. // ConvertSelectorToLabelsMap converts selector string to labels map
  123. // and validates keys and values
  124. func ConvertSelectorToLabelsMap(selector string, opts ...field.PathOption) (Set, error) {
  125. labelsMap := Set{}
  126. if len(selector) == 0 {
  127. return labelsMap, nil
  128. }
  129. labels := strings.Split(selector, ",")
  130. for _, label := range labels {
  131. l := strings.Split(label, "=")
  132. if len(l) != 2 {
  133. return labelsMap, fmt.Errorf("invalid selector: %s", l)
  134. }
  135. key := strings.TrimSpace(l[0])
  136. if err := validateLabelKey(key, field.ToPath(opts...)); err != nil {
  137. return labelsMap, err
  138. }
  139. value := strings.TrimSpace(l[1])
  140. if err := validateLabelValue(key, value, field.ToPath(opts...)); err != nil {
  141. return labelsMap, err
  142. }
  143. labelsMap[key] = value
  144. }
  145. return labelsMap, nil
  146. }