Skip to content

Commit 58f834f

Browse files
authored
Merge pull request #5846 from sbueringer/pr-topology-managed-fields-more-tests
🌱 ClusterClass: Add reconcile repeatedly test
2 parents 6a3a91b + 62fa051 commit 58f834f

File tree

5 files changed

+645
-21
lines changed

5 files changed

+645
-21
lines changed

internal/controllers/topology/cluster/mergepatch/managed_paths.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,27 @@ func toManagedFieldsMap(m map[string]interface{}, ignorePaths []contract.Path) m
114114
continue
115115
}
116116

117-
// If the field has nested values, process them.
118-
nestedV := make(map[string]interface{})
117+
// If the field has nested values (it is an object/map), process them.
119118
if nestedM, ok := v.(map[string]interface{}); ok {
120119
nestedIgnorePaths := make([]contract.Path, 0)
121120
for _, i := range ignorePaths {
122121
if i[0] == k && len(i) > 1 {
123122
nestedIgnorePaths = append(nestedIgnorePaths, i[1:])
124123
}
125124
}
126-
nestedV = toManagedFieldsMap(nestedM, nestedIgnorePaths)
125+
nestedV := toManagedFieldsMap(nestedM, nestedIgnorePaths)
126+
127+
// Note: we are considering the object managed only if it is setting a value for one of the nested fields.
128+
// This prevents the topology controller to become authoritative on all the empty maps generated due to
129+
// how serialization works.
130+
if len(nestedV) > 0 {
131+
r[k] = nestedV
132+
}
133+
continue
127134
}
128-
r[k] = nestedV
135+
136+
// Otherwise, it is a "simple" field so mark it as managed
137+
r[k] = make(map[string]interface{})
129138
}
130139
return r
131140
}

internal/controllers/topology/cluster/mergepatch/managed_paths_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,43 @@ func Test_ManagedFieldAnnotation(t *testing.T) {
191191
{"spec", "kubeadmConfigSpec", "joinConfiguration"},
192192
},
193193
},
194+
{
195+
name: "Managed fields annotation ignore empty maps",
196+
obj: &unstructured.Unstructured{
197+
Object: map[string]interface{}{
198+
"spec": map[string]interface{}{
199+
"kubeadmConfigSpec": map[string]interface{}{
200+
"clusterConfiguration": map[string]interface{}{
201+
"version": "v2.0.1",
202+
},
203+
"initConfiguration": map[string]interface{}{},
204+
},
205+
},
206+
},
207+
},
208+
wantPaths: []contract.Path{
209+
{"spec", "kubeadmConfigSpec", "clusterConfiguration", "version"},
210+
},
211+
},
212+
{
213+
name: "Managed fields annotation ignore empty maps - excluding ignore paths",
214+
obj: &unstructured.Unstructured{
215+
Object: map[string]interface{}{
216+
"spec": map[string]interface{}{
217+
"kubeadmConfigSpec": map[string]interface{}{
218+
"clusterConfiguration": map[string]interface{}{
219+
"version": "v2.0.1",
220+
},
221+
"initConfiguration": map[string]interface{}{},
222+
},
223+
},
224+
},
225+
},
226+
ignorePaths: []contract.Path{
227+
{"spec", "kubeadmConfigSpec", "clusterConfiguration", "version"},
228+
},
229+
wantPaths: []contract.Path{},
230+
},
194231
}
195232
for _, tt := range tests {
196233
t.Run(tt.name, func(t *testing.T) {

internal/controllers/topology/cluster/mergepatch/mergepatch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func NewHelper(original, modified client.Object, c client.Client, opts ...Helper
6262
// changes to those paths are going to be considered authoritative.
6363
managedPaths, err := getManagedPaths(original)
6464
if err != nil {
65-
return nil, errors.Wrap(err, "failed to marshal original object to json")
65+
return nil, errors.Wrap(err, "failed to get managed paths")
6666
}
6767
helperOptions.managedPaths = managedPaths
6868

internal/controllers/topology/cluster/mergepatch/mergepatch_test.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -801,15 +801,7 @@ func TestNewHelper(t *testing.T) {
801801
wantPatch: []byte(fmt.Sprintf(
802802
"{\"metadata\":{\"annotations\":{%q:%q}},\"spec\":{\"kubeadmConfigSpec\":{\"clusterConfiguration\":{\"controllerManager\":{\"extraArgs\":{\"enable-hostpath-provisioner\":null}}}}}}",
803803
clusterv1.ClusterTopologyManagedFieldsAnnotation,
804-
mustManagedFieldAnnotation(map[string]interface{}{
805-
"kubeadmConfigSpec": map[string]interface{}{
806-
"clusterConfiguration": map[string]interface{}{
807-
"controllerManager": map[string]interface{}{
808-
"extraArgs": map[string]interface{}{},
809-
},
810-
},
811-
},
812-
}),
804+
mustManagedFieldAnnotation(map[string]interface{}{}),
813805
)),
814806
},
815807
{
@@ -923,13 +915,7 @@ func TestNewHelper(t *testing.T) {
923915
wantPatch: []byte(fmt.Sprintf(
924916
"{\"metadata\":{\"annotations\":{%q:%q}},\"spec\":{\"kubeadmConfigSpec\":{\"clusterConfiguration\":{\"controllerManager\":{\"extraArgs\":{\"enable-hostpath-provisioner\":null}}}}}}",
925917
clusterv1.ClusterTopologyManagedFieldsAnnotation,
926-
mustManagedFieldAnnotation(map[string]interface{}{
927-
"kubeadmConfigSpec": map[string]interface{}{
928-
"clusterConfiguration": map[string]interface{}{
929-
"controllerManager": map[string]interface{}{},
930-
},
931-
},
932-
}),
918+
mustManagedFieldAnnotation(map[string]interface{}{}),
933919
)),
934920
},
935921

0 commit comments

Comments
 (0)