-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmetadata.go
199 lines (161 loc) · 5.8 KB
/
metadata.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Copyright 2025 The KCP 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 sync
import (
"fmt"
"maps"
"strings"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
func stripMetadata(obj *unstructured.Unstructured) error {
obj.SetCreationTimestamp(metav1.Time{})
obj.SetFinalizers(nil)
obj.SetGeneration(0)
obj.SetOwnerReferences(nil)
obj.SetResourceVersion("")
obj.SetManagedFields(nil)
obj.SetUID("")
obj.SetSelfLink("")
if err := stripAnnotations(obj); err != nil {
return fmt.Errorf("failed to strip annotations: %w", err)
}
if err := stripLabels(obj); err != nil {
return fmt.Errorf("failed to strip labels: %w", err)
}
return nil
}
func setNestedMapOmitempty(obj *unstructured.Unstructured, value map[string]string, path ...string) error {
if len(value) == 0 {
unstructured.RemoveNestedField(obj.Object, path...)
return nil
}
return unstructured.SetNestedStringMap(obj.Object, value, path...)
}
func stripAnnotations(obj *unstructured.Unstructured) error {
annotations := obj.GetAnnotations()
if annotations == nil {
return nil
}
if err := setNestedMapOmitempty(obj, filterUnsyncableAnnotations(annotations), "metadata", "annotations"); err != nil {
return err
}
return nil
}
func stripLabels(obj *unstructured.Unstructured) error {
labels := obj.GetLabels()
if labels == nil {
return nil
}
if err := setNestedMapOmitempty(obj, filterUnsyncableLabels(labels), "metadata", "labels"); err != nil {
return err
}
return nil
}
// unsyncableLabels are labels we never want to copy from the remote to local objects.
var unsyncableLabels = sets.New(
remoteObjectClusterLabel,
remoteObjectNamespaceHashLabel,
remoteObjectNameHashLabel,
)
// filterUnsyncableLabels removes all unwanted remote labels and returns a new label set.
func filterUnsyncableLabels(original labels.Set) labels.Set {
filtered := filterLabels(original, unsyncableLabels)
out := labels.Set{}
for k, v := range filtered {
if !strings.HasPrefix(k, "claimed.internal.apis.kcp.io/") {
out[k] = v
}
}
return out
}
// unsyncableAnnotations are annotations we never want to copy from the remote to local objects.
var unsyncableAnnotations = sets.New(
"kcp.io/cluster",
"kubectl.kubernetes.io/last-applied-configuration",
remoteObjectNamespaceAnnotation,
remoteObjectNameAnnotation,
remoteObjectWorkspacePathAnnotation,
)
// filterUnsyncableAnnotations removes all unwanted remote annotations and returns a new label set.
func filterUnsyncableAnnotations(original labels.Set) labels.Set {
filtered := filterLabels(original, unsyncableAnnotations)
maps.DeleteFunc(filtered, func(annotation string, _ string) bool {
return strings.HasPrefix(annotation, relatedObjectAnnotationPrefix)
})
return filtered
}
func filterLabels(original labels.Set, forbidList sets.Set[string]) labels.Set {
filtered := labels.Set{}
for k, v := range original {
if !forbidList.Has(k) {
filtered[k] = v
}
}
return filtered
}
func RemoteNameForLocalObject(localObj ctrlruntimeclient.Object) *reconcile.Request {
labels := localObj.GetLabels()
annotations := localObj.GetAnnotations()
clusterName := labels[remoteObjectClusterLabel]
namespace := annotations[remoteObjectNamespaceAnnotation]
name := annotations[remoteObjectNameAnnotation]
// reject/ignore invalid/badly labelled object
if clusterName == "" || name == "" {
return nil
}
return &reconcile.Request{
ClusterName: clusterName,
NamespacedName: types.NamespacedName{
Namespace: namespace,
Name: name,
},
}
}
// threeWayDiffMetadata is used when updating an object. Since the lastKnownState for any object
// does not contain syncer-related metadata, this function determines whether labels/annotations are
// missing by comparing the desired* sets with the current state on the destObj.
// If a label/annotation is found to be missing or wrong, this function will set it on the sourceObj.
// This is confusing at first, but the source object here is just a DeepCopy from the actual source
// object and the caller is not meant to persist changes on the source object. The reason the changes
// are performed on the source object is so that when creating the patch later on (which is done by
// comparing the source object with the lastKnownState), the patch will contain the necessary changes.
func threeWayDiffMetadata(sourceObj, destObj *unstructured.Unstructured, desiredLabels, desiredAnnotations labels.Set) {
destLabels := destObj.GetLabels()
sourceLabels := sourceObj.GetLabels()
for label, value := range desiredLabels {
if destValue, ok := destLabels[label]; !ok || destValue != value {
if sourceLabels == nil {
sourceLabels = map[string]string{}
}
sourceLabels[label] = value
}
}
sourceObj.SetLabels(sourceLabels)
destAnnotations := destObj.GetAnnotations()
sourceAnnotations := sourceObj.GetAnnotations()
for label, value := range desiredAnnotations {
if destValue, ok := destAnnotations[label]; !ok || destValue != value {
if sourceAnnotations == nil {
sourceAnnotations = map[string]string{}
}
sourceAnnotations[label] = value
}
}
sourceObj.SetAnnotations(sourceAnnotations)
}