Skip to content

Commit

Permalink
Modify equivalentTemplates for OperatorPolicy complianceConfig defaults
Browse files Browse the repository at this point in the history
equivalentTemplates should correctly apply complianceConfig defaults

Ref:https://issues.redhat.com/browse/ACM-11023
Signed-off-by: Jeffrey Luo <jeluo@redhat.com>
  • Loading branch information
JeffeyL authored and openshift-merge-bot[bot] committed Jun 6, 2024
1 parent c34eec6 commit e6933aa
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
38 changes: 38 additions & 0 deletions controllers/templatesync/template_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,6 +990,44 @@ func equivalentTemplates(eObject *unstructured.Unstructured, tObject *unstructur
}
}

if tObject.GetKind() == "OperatorPolicy" {
// catalogSourceUnhealthy
catalogSourceUnhealthy, _, _ := unstructured.NestedString(tObject.Object, "spec",
"complianceConfig", "catalogSourceUnhealthy")

if catalogSourceUnhealthy == "" {
err := unstructured.SetNestedField(tObject.Object, "Compliant", "spec",
"complianceConfig", "catalogSourceUnhealthy")
if err != nil {
log.Error(err, "Failed to set the default value of catalogSourceUnhealthy")
}
}

// deploymentsUnavailable
deploymentsUnavailable, _, _ := unstructured.NestedString(tObject.Object, "spec",
"complianceConfig", "deploymentsUnavailable")

if deploymentsUnavailable == "" {
err := unstructured.SetNestedField(tObject.Object, "NonCompliant", "spec",
"complianceConfig", "deploymentsUnavailable")
if err != nil {
log.Error(err, "Failed to set the default value of deploymentsUnavailable")
}
}

// upgradesAvailable
upgradesAvailable, _, _ := unstructured.NestedString(tObject.Object, "spec",
"complianceConfig", "upgradesAvailable")

if upgradesAvailable == "" {
err := unstructured.SetNestedField(tObject.Object, "Compliant", "spec",
"complianceConfig", "upgradesAvailable")
if err != nil {
log.Error(err, "Failed to set the default value of upgradesAvailable")
}
}
}

if !equality.Semantic.DeepEqual(eObject.UnstructuredContent()["spec"], tObject.Object["spec"]) {
return false
}
Expand Down
59 changes: 59 additions & 0 deletions controllers/templatesync/template_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,62 @@ func TestEquivalentTemplatesRecreateOption(t *testing.T) {
t.Fatal("Expected the templates to be equivalent")
}
}

func TestEquivalentTemplatesOperatorPolicyComplianceConfig(t *testing.T) {
existing := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "policy.open-cluster-management.io/v1beta1",
"kind": "OperatorPolicy",
"metadata": map[string]interface{}{
"name": "test-policy",
},
"spec": map[string]interface{}{
"remediationAction": "inform",
"severity": "medium",
"complianceType": "musthave",
"subscription:": map[string]interface{}{
"channel": "stable-3.10",
"name": "project-quay",
"namespace": "operator-policy-testns",
"source": "operatorhubio-catalog",
"sourceNamespace": "olm",
"startingCSV": "quay-operator.v3.10.0",
},
"upgradeApproval": "Automatic",
"complianceConfig": map[string]interface{}{
"catalogSourceUnhealthy": "Compliant",
"deploymentsUnavailable": "NonCompliant",
"upgradesAvailable": "Compliant",
},
},
},
}

template := &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "policy.open-cluster-management.io/v1beta1",
"kind": "OperatorPolicy",
"metadata": map[string]interface{}{
"name": "test-policy",
},
"spec": map[string]interface{}{
"remediationAction": "inform",
"severity": "medium",
"complianceType": "musthave",
"subscription:": map[string]interface{}{
"channel": "stable-3.10",
"name": "project-quay",
"namespace": "operator-policy-testns",
"source": "operatorhubio-catalog",
"sourceNamespace": "olm",
"startingCSV": "quay-operator.v3.10.0",
},
"upgradeApproval": "Automatic",
},
},
}

if !equivalentTemplates(existing, template) {
t.Fatal("Expected the templates to be equivalent")
}
}

0 comments on commit e6933aa

Please sign in to comment.