-
Notifications
You must be signed in to change notification settings - Fork 842
/
label.go
109 lines (96 loc) · 3.08 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package util
import (
"sort"
"strings"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/util/sets"
workv1alpha2 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha2"
)
// 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]
}
// RetainLabels merges the labels that added by controllers running
// in member cluster to avoid overwriting.
// Following keys will be ignored if :
// - the keys were previous propagated to member clusters(that are tracked
// by "resourcetemplate.karmada.io/managed-lables" annotation in observed)
// but have been removed from Karmada control plane(don't exist in desired anymore).
// - the keys that exist in both desired and observed even those been accidentally modified
// in member clusters.
func RetainLabels(desired *unstructured.Unstructured, observed *unstructured.Unstructured) {
labels := desired.GetLabels()
if labels == nil {
labels = make(map[string]string, 0)
}
deletedLabelKeys := getDeletedLabelKeys(desired, observed)
for key, value := range observed.GetLabels() {
if deletedLabelKeys.Has(key) {
continue
}
if _, exist := labels[key]; exist {
continue
}
labels[key] = value
}
if len(labels) > 0 {
desired.SetLabels(labels)
}
}
// 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
}
func getDeletedLabelKeys(desired, observed *unstructured.Unstructured) sets.Set[string] {
recordKeys := sets.New[string](strings.Split(observed.GetAnnotations()[workv1alpha2.ManagedLabels], ",")...)
for key := range desired.GetLabels() {
recordKeys.Delete(key)
}
return recordKeys
}
// RecordManagedLabels sets or updates the annotation(resourcetemplate.karmada.io/managed-labels)
// to record the label keys.
func RecordManagedLabels(object *unstructured.Unstructured) {
annotations := object.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string, 1)
}
var managedKeys []string
// record labels.
labels := object.GetLabels()
for key := range labels {
managedKeys = append(managedKeys, key)
}
sort.Strings(managedKeys)
annotations[workv1alpha2.ManagedLabels] = strings.Join(managedKeys, ",")
object.SetAnnotations(annotations)
}