labels.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. // Note: this method copies the Set; if the Set is immutable, consider wrapping it with ValidatedSetSelector
  66. // instead, which does not copy.
  67. func (ls Set) AsSelectorPreValidated() Selector {
  68. return SelectorFromValidatedSet(ls)
  69. }
  70. // FormatLabels converts label map into plain string
  71. func FormatLabels(labelMap map[string]string) string {
  72. l := Set(labelMap).String()
  73. if l == "" {
  74. l = "<none>"
  75. }
  76. return l
  77. }
  78. // Conflicts takes 2 maps and returns true if there a key match between
  79. // the maps but the value doesn't match, and returns false in other cases
  80. func Conflicts(labels1, labels2 Set) bool {
  81. small := labels1
  82. big := labels2
  83. if len(labels2) < len(labels1) {
  84. small = labels2
  85. big = labels1
  86. }
  87. for k, v := range small {
  88. if val, match := big[k]; match {
  89. if val != v {
  90. return true
  91. }
  92. }
  93. }
  94. return false
  95. }
  96. // Merge combines given maps, and does not check for any conflicts
  97. // between the maps. In case of conflicts, second map (labels2) wins
  98. func Merge(labels1, labels2 Set) Set {
  99. mergedMap := Set{}
  100. for k, v := range labels1 {
  101. mergedMap[k] = v
  102. }
  103. for k, v := range labels2 {
  104. mergedMap[k] = v
  105. }
  106. return mergedMap
  107. }
  108. // Equals returns true if the given maps are equal
  109. func Equals(labels1, labels2 Set) bool {
  110. if len(labels1) != len(labels2) {
  111. return false
  112. }
  113. for k, v := range labels1 {
  114. value, ok := labels2[k]
  115. if !ok {
  116. return false
  117. }
  118. if value != v {
  119. return false
  120. }
  121. }
  122. return true
  123. }
  124. // ConvertSelectorToLabelsMap converts selector string to labels map
  125. // and validates keys and values
  126. func ConvertSelectorToLabelsMap(selector string, opts ...field.PathOption) (Set, error) {
  127. labelsMap := Set{}
  128. if len(selector) == 0 {
  129. return labelsMap, nil
  130. }
  131. labels := strings.Split(selector, ",")
  132. for _, label := range labels {
  133. l := strings.Split(label, "=")
  134. if len(l) != 2 {
  135. return labelsMap, fmt.Errorf("invalid selector: %s", l)
  136. }
  137. key := strings.TrimSpace(l[0])
  138. if err := validateLabelKey(key, field.ToPath(opts...)); err != nil {
  139. return labelsMap, err
  140. }
  141. value := strings.TrimSpace(l[1])
  142. if err := validateLabelValue(key, value, field.ToPath(opts...)); err != nil {
  143. return labelsMap, err
  144. }
  145. labelsMap[key] = value
  146. }
  147. return labelsMap, nil
  148. }