labels.go 4.9 KB

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