Skip to content

Commit c34eec6

Browse files
mprahlopenshift-merge-bot[bot]
authored andcommitted
Handle the default value for recreateOption
Relates: https://issues.redhat.com/browse/ACM-11846 Signed-off-by: mprahl <[email protected]>
1 parent f2e60b7 commit c34eec6

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

controllers/templatesync/template_sync.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -961,7 +961,31 @@ func equivalentTemplates(eObject *unstructured.Unstructured, tObject *unstructur
961961
if pruneObjectBehavior == "" {
962962
err := unstructured.SetNestedField(tObject.Object, "None", "spec", "pruneObjectBehavior")
963963
if err != nil {
964-
log.Error(err, "Failed to set the default value of pruneObjectBehavior for")
964+
log.Error(err, "Failed to set the default value of pruneObjectBehavior")
965+
}
966+
}
967+
968+
var updatedObjectTemplates bool
969+
970+
objectTemplates, _, _ := unstructured.NestedSlice(tObject.Object, "spec", "object-templates")
971+
972+
for i := range objectTemplates {
973+
objectTemplate, ok := objectTemplates[i].(map[string]interface{})
974+
if !ok {
975+
continue
976+
}
977+
978+
if _, ok := objectTemplate["recreateOption"]; !ok {
979+
objectTemplate["recreateOption"] = "None"
980+
objectTemplates[i] = objectTemplate
981+
updatedObjectTemplates = true
982+
}
983+
}
984+
985+
if updatedObjectTemplates {
986+
err := unstructured.SetNestedField(tObject.Object, objectTemplates, "spec", "object-templates")
987+
if err != nil {
988+
log.Error(err, "Failed to set the default value of recreateOption")
965989
}
966990
}
967991
}

controllers/templatesync/template_sync_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,63 @@ func TestGetDupName(t *testing.T) {
213213
t.Fatal("Duplicate name not detected")
214214
}
215215
}
216+
217+
func TestEquivalentTemplatesRecreateOption(t *testing.T) {
218+
existing := &unstructured.Unstructured{
219+
Object: map[string]interface{}{
220+
"apiVersion": "policy.open-cluster-management.io/v1",
221+
"kind": "ConfigurationPolicy",
222+
"metadata": map[string]interface{}{
223+
"name": "my-policy",
224+
"namespace": "local-cluster",
225+
},
226+
"spec": map[string]interface{}{
227+
"pruneObjectBehavior": "None",
228+
"object-templates": []interface{}{
229+
map[string]interface{}{
230+
"complianceType": "musthave",
231+
"recreateOption": "None",
232+
"objectDefinition": map[string]interface{}{
233+
"apiVersion": "v1",
234+
"kind": "Pod",
235+
"metadata": map[string]interface{}{
236+
"name": "nginx-pod-e2e",
237+
"namespace": "default",
238+
},
239+
},
240+
},
241+
},
242+
},
243+
},
244+
}
245+
246+
template := &unstructured.Unstructured{
247+
Object: map[string]interface{}{
248+
"apiVersion": "policy.open-cluster-management.io/v1",
249+
"kind": "ConfigurationPolicy",
250+
"metadata": map[string]interface{}{
251+
"name": "my-policy",
252+
"namespace": "local-cluster",
253+
},
254+
"spec": map[string]interface{}{
255+
"object-templates": []interface{}{
256+
map[string]interface{}{
257+
"complianceType": "musthave",
258+
"objectDefinition": map[string]interface{}{
259+
"apiVersion": "v1",
260+
"kind": "Pod",
261+
"metadata": map[string]interface{}{
262+
"name": "nginx-pod-e2e",
263+
"namespace": "default",
264+
},
265+
},
266+
},
267+
},
268+
},
269+
},
270+
}
271+
272+
if !equivalentTemplates(existing, template) {
273+
t.Fatal("Expected the templates to be equivalent")
274+
}
275+
}

0 commit comments

Comments
 (0)