labels.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. Copyright 2016 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. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  16. )
  17. // Clones the given map and returns a new map with the given key and value added.
  18. // Returns the given map, if labelKey is empty.
  19. func CloneAndAddLabel(labels map[string]string, labelKey, labelValue string) map[string]string {
  20. if labelKey == "" {
  21. // Don't need to add a label.
  22. return labels
  23. }
  24. // Clone.
  25. newLabels := map[string]string{}
  26. for key, value := range labels {
  27. newLabels[key] = value
  28. }
  29. newLabels[labelKey] = labelValue
  30. return newLabels
  31. }
  32. // CloneAndRemoveLabel clones the given map and returns a new map with the given key removed.
  33. // Returns the given map, if labelKey is empty.
  34. func CloneAndRemoveLabel(labels map[string]string, labelKey string) map[string]string {
  35. if labelKey == "" {
  36. // Don't need to add a label.
  37. return labels
  38. }
  39. // Clone.
  40. newLabels := map[string]string{}
  41. for key, value := range labels {
  42. newLabels[key] = value
  43. }
  44. delete(newLabels, labelKey)
  45. return newLabels
  46. }
  47. // AddLabel returns a map with the given key and value added to the given map.
  48. func AddLabel(labels map[string]string, labelKey, labelValue string) map[string]string {
  49. if labelKey == "" {
  50. // Don't need to add a label.
  51. return labels
  52. }
  53. if labels == nil {
  54. labels = make(map[string]string)
  55. }
  56. labels[labelKey] = labelValue
  57. return labels
  58. }
  59. // Clones the given selector and returns a new selector with the given key and value added.
  60. // Returns the given selector, if labelKey is empty.
  61. func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey, labelValue string) *metav1.LabelSelector {
  62. if labelKey == "" {
  63. // Don't need to add a label.
  64. return selector
  65. }
  66. // Clone.
  67. newSelector := new(metav1.LabelSelector)
  68. // TODO(madhusudancs): Check if you can use deepCopy_extensions_LabelSelector here.
  69. newSelector.MatchLabels = make(map[string]string)
  70. if selector.MatchLabels != nil {
  71. for key, val := range selector.MatchLabels {
  72. newSelector.MatchLabels[key] = val
  73. }
  74. }
  75. newSelector.MatchLabels[labelKey] = labelValue
  76. if selector.MatchExpressions != nil {
  77. newMExps := make([]metav1.LabelSelectorRequirement, len(selector.MatchExpressions))
  78. for i, me := range selector.MatchExpressions {
  79. newMExps[i].Key = me.Key
  80. newMExps[i].Operator = me.Operator
  81. if me.Values != nil {
  82. newMExps[i].Values = make([]string, len(me.Values))
  83. copy(newMExps[i].Values, me.Values)
  84. } else {
  85. newMExps[i].Values = nil
  86. }
  87. }
  88. newSelector.MatchExpressions = newMExps
  89. } else {
  90. newSelector.MatchExpressions = nil
  91. }
  92. return newSelector
  93. }
  94. // AddLabelToSelector returns a selector with the given key and value added to the given selector's MatchLabels.
  95. func AddLabelToSelector(selector *metav1.LabelSelector, labelKey, labelValue string) *metav1.LabelSelector {
  96. if labelKey == "" {
  97. // Don't need to add a label.
  98. return selector
  99. }
  100. if selector.MatchLabels == nil {
  101. selector.MatchLabels = make(map[string]string)
  102. }
  103. selector.MatchLabels[labelKey] = labelValue
  104. return selector
  105. }
  106. // SelectorHasLabel checks if the given selector contains the given label key in its MatchLabels
  107. func SelectorHasLabel(selector *metav1.LabelSelector, labelKey string) bool {
  108. return len(selector.MatchLabels[labelKey]) > 0
  109. }