-
Notifications
You must be signed in to change notification settings - Fork 888
/
label.go
49 lines (41 loc) · 1.13 KB
/
label.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package util
import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
// GetLabelValue retrieves the value via 'labelKey' if exist, otherwise returns an empty string.
func GetLabelValue(labels map[string]string, labelKey string) string {
if labels == nil {
return ""
}
return labels[labelKey]
}
// MergeLabel adds label for the given object.
func MergeLabel(obj *unstructured.Unstructured, labelKey string, labelValue string) {
labels := obj.GetLabels()
if labels == nil {
labels = make(map[string]string, 1)
}
labels[labelKey] = labelValue
obj.SetLabels(labels)
}
// RemoveLabels removes the labels from the given object.
func RemoveLabels(obj *unstructured.Unstructured, labelKeys ...string) {
if len(labelKeys) == 0 {
return
}
objLabels := obj.GetLabels()
for _, labelKey := range labelKeys {
delete(objLabels, labelKey)
}
obj.SetLabels(objLabels)
}
// DedupeAndMergeLabels merges the new labels into exist labels.
func DedupeAndMergeLabels(existLabel, newLabel map[string]string) map[string]string {
if existLabel == nil {
return newLabel
}
for k, v := range newLabel {
existLabel[k] = v
}
return existLabel
}