Skip to content

Commit e6933aa

Browse files
JeffeyLopenshift-merge-bot[bot]
authored andcommitted
Modify equivalentTemplates for OperatorPolicy complianceConfig defaults
equivalentTemplates should correctly apply complianceConfig defaults Ref:https://issues.redhat.com/browse/ACM-11023 Signed-off-by: Jeffrey Luo <[email protected]>
1 parent c34eec6 commit e6933aa

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

controllers/templatesync/template_sync.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,44 @@ func equivalentTemplates(eObject *unstructured.Unstructured, tObject *unstructur
990990
}
991991
}
992992

993+
if tObject.GetKind() == "OperatorPolicy" {
994+
// catalogSourceUnhealthy
995+
catalogSourceUnhealthy, _, _ := unstructured.NestedString(tObject.Object, "spec",
996+
"complianceConfig", "catalogSourceUnhealthy")
997+
998+
if catalogSourceUnhealthy == "" {
999+
err := unstructured.SetNestedField(tObject.Object, "Compliant", "spec",
1000+
"complianceConfig", "catalogSourceUnhealthy")
1001+
if err != nil {
1002+
log.Error(err, "Failed to set the default value of catalogSourceUnhealthy")
1003+
}
1004+
}
1005+
1006+
// deploymentsUnavailable
1007+
deploymentsUnavailable, _, _ := unstructured.NestedString(tObject.Object, "spec",
1008+
"complianceConfig", "deploymentsUnavailable")
1009+
1010+
if deploymentsUnavailable == "" {
1011+
err := unstructured.SetNestedField(tObject.Object, "NonCompliant", "spec",
1012+
"complianceConfig", "deploymentsUnavailable")
1013+
if err != nil {
1014+
log.Error(err, "Failed to set the default value of deploymentsUnavailable")
1015+
}
1016+
}
1017+
1018+
// upgradesAvailable
1019+
upgradesAvailable, _, _ := unstructured.NestedString(tObject.Object, "spec",
1020+
"complianceConfig", "upgradesAvailable")
1021+
1022+
if upgradesAvailable == "" {
1023+
err := unstructured.SetNestedField(tObject.Object, "Compliant", "spec",
1024+
"complianceConfig", "upgradesAvailable")
1025+
if err != nil {
1026+
log.Error(err, "Failed to set the default value of upgradesAvailable")
1027+
}
1028+
}
1029+
}
1030+
9931031
if !equality.Semantic.DeepEqual(eObject.UnstructuredContent()["spec"], tObject.Object["spec"]) {
9941032
return false
9951033
}

controllers/templatesync/template_sync_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,3 +273,62 @@ func TestEquivalentTemplatesRecreateOption(t *testing.T) {
273273
t.Fatal("Expected the templates to be equivalent")
274274
}
275275
}
276+
277+
func TestEquivalentTemplatesOperatorPolicyComplianceConfig(t *testing.T) {
278+
existing := &unstructured.Unstructured{
279+
Object: map[string]interface{}{
280+
"apiVersion": "policy.open-cluster-management.io/v1beta1",
281+
"kind": "OperatorPolicy",
282+
"metadata": map[string]interface{}{
283+
"name": "test-policy",
284+
},
285+
"spec": map[string]interface{}{
286+
"remediationAction": "inform",
287+
"severity": "medium",
288+
"complianceType": "musthave",
289+
"subscription:": map[string]interface{}{
290+
"channel": "stable-3.10",
291+
"name": "project-quay",
292+
"namespace": "operator-policy-testns",
293+
"source": "operatorhubio-catalog",
294+
"sourceNamespace": "olm",
295+
"startingCSV": "quay-operator.v3.10.0",
296+
},
297+
"upgradeApproval": "Automatic",
298+
"complianceConfig": map[string]interface{}{
299+
"catalogSourceUnhealthy": "Compliant",
300+
"deploymentsUnavailable": "NonCompliant",
301+
"upgradesAvailable": "Compliant",
302+
},
303+
},
304+
},
305+
}
306+
307+
template := &unstructured.Unstructured{
308+
Object: map[string]interface{}{
309+
"apiVersion": "policy.open-cluster-management.io/v1beta1",
310+
"kind": "OperatorPolicy",
311+
"metadata": map[string]interface{}{
312+
"name": "test-policy",
313+
},
314+
"spec": map[string]interface{}{
315+
"remediationAction": "inform",
316+
"severity": "medium",
317+
"complianceType": "musthave",
318+
"subscription:": map[string]interface{}{
319+
"channel": "stable-3.10",
320+
"name": "project-quay",
321+
"namespace": "operator-policy-testns",
322+
"source": "operatorhubio-catalog",
323+
"sourceNamespace": "olm",
324+
"startingCSV": "quay-operator.v3.10.0",
325+
},
326+
"upgradeApproval": "Automatic",
327+
},
328+
},
329+
}
330+
331+
if !equivalentTemplates(existing, template) {
332+
t.Fatal("Expected the templates to be equivalent")
333+
}
334+
}

0 commit comments

Comments
 (0)