-
Notifications
You must be signed in to change notification settings - Fork 888
/
annotation.go
127 lines (110 loc) · 4.33 KB
/
annotation.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright 2021 The Karmada Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
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"
)
// MergeAnnotation adds annotation for the given object, keep the value unchanged if key exist.
func MergeAnnotation(obj *unstructured.Unstructured, annotationKey string, annotationValue string) {
objectAnnotation := obj.GetAnnotations()
if objectAnnotation == nil {
objectAnnotation = make(map[string]string, 1)
}
if _, exist := objectAnnotation[annotationKey]; !exist {
objectAnnotation[annotationKey] = annotationValue
obj.SetAnnotations(objectAnnotation)
}
}
// ReplaceAnnotation adds annotation for the given object, replace the value if key exist.
func ReplaceAnnotation(obj *unstructured.Unstructured, annotationKey string, annotationValue string) {
objectAnnotation := obj.GetAnnotations()
if objectAnnotation == nil {
objectAnnotation = make(map[string]string, 1)
}
objectAnnotation[annotationKey] = annotationValue
obj.SetAnnotations(objectAnnotation)
}
// RetainAnnotations merges the annotations 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-annotations" 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 RetainAnnotations(desired *unstructured.Unstructured, observed *unstructured.Unstructured) {
objectAnnotation := desired.GetAnnotations()
if objectAnnotation == nil {
objectAnnotation = make(map[string]string, 0)
}
deletedAnnotationKeys := getDeletedAnnotationKeys(desired, observed)
for key, value := range observed.GetAnnotations() {
if deletedAnnotationKeys.Has(key) {
continue
}
if _, exist := objectAnnotation[key]; exist {
continue
}
objectAnnotation[key] = value
}
if len(objectAnnotation) > 0 {
desired.SetAnnotations(objectAnnotation)
}
}
// GetAnnotationValue retrieves the value via 'annotationKey' (if it exists), otherwise an empty string is returned.
func GetAnnotationValue(annotations map[string]string, annotationKey string) string {
if annotations == nil {
return ""
}
return annotations[annotationKey]
}
func getDeletedAnnotationKeys(desired, observed *unstructured.Unstructured) sets.Set[string] {
recordKeys := sets.New[string](strings.Split(observed.GetAnnotations()[workv1alpha2.ManagedAnnotation], ",")...)
for key := range desired.GetAnnotations() {
recordKeys.Delete(key)
}
return recordKeys
}
// RecordManagedAnnotations sets or updates the annotation(resourcetemplate.karmada.io/managed-annotations)
// to record the annotation keys.
func RecordManagedAnnotations(object *unstructured.Unstructured) {
annotations := object.GetAnnotations()
if annotations == nil {
annotations = make(map[string]string, 1)
}
// record annotations.
managedKeys := []string{workv1alpha2.ManagedAnnotation, workv1alpha2.ManagedLabels}
for key := range annotations {
if key == workv1alpha2.ManagedAnnotation || key == workv1alpha2.ManagedLabels {
continue
}
managedKeys = append(managedKeys, key)
}
sort.Strings(managedKeys)
annotations[workv1alpha2.ManagedAnnotation] = strings.Join(managedKeys, ",")
object.SetAnnotations(annotations)
}
// DedupeAndMergeAnnotations merges the new annotations into exist annotations.
func DedupeAndMergeAnnotations(existAnnotation, newAnnotation map[string]string) map[string]string {
if existAnnotation == nil {
return newAnnotation
}
for k, v := range newAnnotation {
existAnnotation[k] = v
}
return existAnnotation
}