k8s.io/kubernetes@v1.29.3/pkg/util/labels/labels.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package labels 18 19 import ( 20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 ) 22 23 // Clones the given map and returns a new map with the given key and value added. 24 // Returns the given map, if labelKey is empty. 25 func CloneAndAddLabel(labels map[string]string, labelKey, labelValue string) map[string]string { 26 if labelKey == "" { 27 // Don't need to add a label. 28 return labels 29 } 30 // Clone. 31 newLabels := map[string]string{} 32 for key, value := range labels { 33 newLabels[key] = value 34 } 35 newLabels[labelKey] = labelValue 36 return newLabels 37 } 38 39 // CloneAndRemoveLabel clones the given map and returns a new map with the given key removed. 40 // Returns the given map, if labelKey is empty. 41 func CloneAndRemoveLabel(labels map[string]string, labelKey string) map[string]string { 42 if labelKey == "" { 43 // Don't need to add a label. 44 return labels 45 } 46 // Clone. 47 newLabels := map[string]string{} 48 for key, value := range labels { 49 newLabels[key] = value 50 } 51 delete(newLabels, labelKey) 52 return newLabels 53 } 54 55 // AddLabel returns a map with the given key and value added to the given map. 56 func AddLabel(labels map[string]string, labelKey, labelValue string) map[string]string { 57 if labelKey == "" { 58 // Don't need to add a label. 59 return labels 60 } 61 if labels == nil { 62 labels = make(map[string]string) 63 } 64 labels[labelKey] = labelValue 65 return labels 66 } 67 68 // Clones the given selector and returns a new selector with the given key and value added. 69 // Returns the given selector, if labelKey is empty. 70 func CloneSelectorAndAddLabel(selector *metav1.LabelSelector, labelKey, labelValue string) *metav1.LabelSelector { 71 if labelKey == "" { 72 // Don't need to add a label. 73 return selector 74 } 75 76 // Clone. 77 newSelector := new(metav1.LabelSelector) 78 79 // TODO(madhusudancs): Check if you can use deepCopy_extensions_LabelSelector here. 80 newSelector.MatchLabels = make(map[string]string) 81 if selector.MatchLabels != nil { 82 for key, val := range selector.MatchLabels { 83 newSelector.MatchLabels[key] = val 84 } 85 } 86 newSelector.MatchLabels[labelKey] = labelValue 87 88 if selector.MatchExpressions != nil { 89 newMExps := make([]metav1.LabelSelectorRequirement, len(selector.MatchExpressions)) 90 for i, me := range selector.MatchExpressions { 91 newMExps[i].Key = me.Key 92 newMExps[i].Operator = me.Operator 93 if me.Values != nil { 94 newMExps[i].Values = make([]string, len(me.Values)) 95 copy(newMExps[i].Values, me.Values) 96 } else { 97 newMExps[i].Values = nil 98 } 99 } 100 newSelector.MatchExpressions = newMExps 101 } else { 102 newSelector.MatchExpressions = nil 103 } 104 105 return newSelector 106 } 107 108 // AddLabelToSelector returns a selector with the given key and value added to the given selector's MatchLabels. 109 func AddLabelToSelector(selector *metav1.LabelSelector, labelKey, labelValue string) *metav1.LabelSelector { 110 if labelKey == "" { 111 // Don't need to add a label. 112 return selector 113 } 114 if selector.MatchLabels == nil { 115 selector.MatchLabels = make(map[string]string) 116 } 117 selector.MatchLabels[labelKey] = labelValue 118 return selector 119 } 120 121 // SelectorHasLabel checks if the given selector contains the given label key in its MatchLabels 122 func SelectorHasLabel(selector *metav1.LabelSelector, labelKey string) bool { 123 return len(selector.MatchLabels[labelKey]) > 0 124 }