labels.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. )
  19. // Labels allows you to present labels independently from their storage.
  20. type Labels interface {
  21. // Has returns whether the provided label exists.
  22. Has(label string) (exists bool)
  23. // Get returns the value for the provided label.
  24. Get(label string) (value string)
  25. }
  26. // Set is a map of label:value. It implements Labels.
  27. type Set map[string]string
  28. // String returns all labels listed as a human readable string.
  29. // Conveniently, exactly the format that ParseSelector takes.
  30. func (ls Set) String() string {
  31. selector := make([]string, 0, len(ls))
  32. for key, value := range ls {
  33. selector = append(selector, key+"="+value)
  34. }
  35. // Sort for determinism.
  36. sort.StringSlice(selector).Sort()
  37. return strings.Join(selector, ",")
  38. }
  39. // Has returns whether the provided label exists in the map.
  40. func (ls Set) Has(label string) bool {
  41. _, exists := ls[label]
  42. return exists
  43. }
  44. // Get returns the value in the map for the provided label.
  45. func (ls Set) Get(label string) string {
  46. return ls[label]
  47. }
  48. // AsSelector converts labels into a selectors.
  49. func (ls Set) AsSelector() Selector {
  50. return SelectorFromSet(ls)
  51. }
  52. // AsSelectorPreValidated converts labels into a selector, but
  53. // assumes that labels are already validated and thus don't
  54. // preform any validation.
  55. // According to our measurements this is significantly faster
  56. // in codepaths that matter at high scale.
  57. func (ls Set) AsSelectorPreValidated() Selector {
  58. return SelectorFromValidatedSet(ls)
  59. }
  60. // FormatLabels convert label map into plain string
  61. func FormatLabels(labelMap map[string]string) string {
  62. l := Set(labelMap).String()
  63. if l == "" {
  64. l = "<none>"
  65. }
  66. return l
  67. }
  68. // Conflicts takes 2 maps and returns true if there a key match between
  69. // the maps but the value doesn't match, and returns false in other cases
  70. func Conflicts(labels1, labels2 Set) bool {
  71. small := labels1
  72. big := labels2
  73. if len(labels2) < len(labels1) {
  74. small = labels2
  75. big = labels1
  76. }
  77. for k, v := range small {
  78. if val, match := big[k]; match {
  79. if val != v {
  80. return true
  81. }
  82. }
  83. }
  84. return false
  85. }
  86. // Merge combines given maps, and does not check for any conflicts
  87. // between the maps. In case of conflicts, second map (labels2) wins
  88. func Merge(labels1, labels2 Set) Set {
  89. mergedMap := Set{}
  90. for k, v := range labels1 {
  91. mergedMap[k] = v
  92. }
  93. for k, v := range labels2 {
  94. mergedMap[k] = v
  95. }
  96. return mergedMap
  97. }
  98. // Equals returns true if the given maps are equal
  99. func Equals(labels1, labels2 Set) bool {
  100. if len(labels1) != len(labels2) {
  101. return false
  102. }
  103. for k, v := range labels1 {
  104. value, ok := labels2[k]
  105. if !ok {
  106. return false
  107. }
  108. if value != v {
  109. return false
  110. }
  111. }
  112. return true
  113. }
  114. // AreLabelsInWhiteList verifies if the provided label list
  115. // is in the provided whitelist and returns true, otherwise false.
  116. func AreLabelsInWhiteList(labels, whitelist Set) bool {
  117. if len(whitelist) == 0 {
  118. return true
  119. }
  120. for k, v := range labels {
  121. value, ok := whitelist[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) (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); err != nil {
  146. return labelsMap, err
  147. }
  148. value := strings.TrimSpace(l[1])
  149. if err := validateLabelValue(value); err != nil {
  150. return labelsMap, err
  151. }
  152. labelsMap[key] = value
  153. }
  154. return labelsMap, nil
  155. }