Skip to content

Commit 747b933

Browse files
zyjjayopenshift-merge-robot
authored andcommitted
Remove duplicated compliance in some policy compliance messages
The compliance state is contenated twice to a message before it is emitted as an event. To fix this, the compliance state is removed from the message and now only passed as an argument. ref: https://issues.redhat.com/browse/ACM-6346 Signed-off-by: Jason Zhang <[email protected]>
1 parent 8f38a69 commit 747b933

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

controllers/templatesync/template_sync.go

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,7 +1260,7 @@ func overrideRemediationAction(instance *policiesv1.Policy, tObjectUnstructured
12601260
func (r *PolicyReconciler) emitTemplateSuccess(
12611261
ctx context.Context, pol *policiesv1.Policy, tIndex int, tName string, clusterScoped bool, msg string,
12621262
) error {
1263-
err := r.emitTemplateEvent(ctx, pol, tIndex, tName, clusterScoped, "Normal", "Compliant; ", msg)
1263+
err := r.emitTemplateEvent(ctx, pol, tIndex, tName, clusterScoped, "Normal", policiesv1.Compliant, msg)
12641264
if err != nil {
12651265
tlog := log.WithValues("Policy.Namespace", pol.Namespace, "Policy.Name", pol.Name, "template", tName)
12661266
tlog.Error(err, "Failed to emit template success event")
@@ -1276,7 +1276,7 @@ func (r *PolicyReconciler) emitTemplateError(
12761276
ctx context.Context, pol *policiesv1.Policy, tIndex int, tName string, clusterScoped bool, errMsg string,
12771277
) error {
12781278
err := r.emitTemplateEvent(ctx, pol, tIndex, tName, clusterScoped,
1279-
"Warning", "NonCompliant; template-error; ", errMsg)
1279+
"Warning", policiesv1.NonCompliant, "template-error; "+errMsg)
12801280
if err != nil {
12811281
tlog := log.WithValues("Policy.Namespace", pol.Namespace, "Policy.Name", pol.Name, "template", tName)
12821282
tlog.Error(err, "Failed to emit template error event")
@@ -1291,16 +1291,16 @@ func (r *PolicyReconciler) emitTemplateError(
12911291
func (r *PolicyReconciler) emitTemplatePending(
12921292
ctx context.Context, pol *policiesv1.Policy, tIndex int, tName string, clusterScoped bool, msg string,
12931293
) error {
1294-
msgMeta := "Pending; "
1294+
compliance := policiesv1.Pending
12951295
eventType := "Warning"
12961296

12971297
if pol.Spec.PolicyTemplates[tIndex].IgnorePending {
1298-
msgMeta = "Compliant; "
1298+
compliance = policiesv1.Compliant
12991299
msg += " but ignorePending is true"
13001300
eventType = "Normal"
13011301
}
13021302

1303-
err := r.emitTemplateEvent(ctx, pol, tIndex, tName, clusterScoped, eventType, msgMeta, msg)
1303+
err := r.emitTemplateEvent(ctx, pol, tIndex, tName, clusterScoped, eventType, compliance, msg)
13041304
if err != nil {
13051305
tlog := log.WithValues("Policy.Namespace", pol.Namespace, "Policy.Name", pol.Name, "template", tName)
13061306
tlog.Error(err, "Failed to emit template pending event")
@@ -1310,14 +1310,13 @@ func (r *PolicyReconciler) emitTemplatePending(
13101310
}
13111311

13121312
// emitTemplateEvent performs actions that ensure correct reporting of template sync events. If the
1313-
// policy's status already reflects the current status, then no actions are taken. The msgMeta and
1314-
// msg are concatenated without spaces, so any spacing should be included inside the msgMeta string.
1313+
// policy's status already reflects the current status, then no actions are taken.
13151314
func (r *PolicyReconciler) emitTemplateEvent(
13161315
ctx context.Context, pol *policiesv1.Policy, tIndex int, tName string, clusterScoped bool,
1317-
eventType string, msgMeta string, msg string,
1316+
eventType string, compliance policiesv1.ComplianceState, msg string,
13181317
) error {
13191318
// check if the error is already present in the policy status - if so, return early
1320-
if strings.Contains(getLatestStatusMessage(pol, tIndex), msgMeta+msg) {
1319+
if strings.Contains(getLatestStatusMessage(pol, tIndex), string(compliance)+"; "+msg) {
13211320
return nil
13221321
}
13231322

@@ -1346,16 +1345,7 @@ func (r *PolicyReconciler) emitTemplateEvent(
13461345
UID: pol.UID,
13471346
}
13481347

1349-
var compState policiesv1.ComplianceState
1350-
if strings.HasPrefix(msgMeta, "Compliant") {
1351-
compState = policiesv1.Compliant
1352-
} else if strings.HasPrefix(msgMeta, "NonCompliant") {
1353-
compState = policiesv1.NonCompliant
1354-
} else if strings.HasPrefix(msgMeta, "Pending") {
1355-
compState = policiesv1.Pending
1356-
}
1357-
1358-
return sender.SendEvent(ctx, nil, ownerref, policyComplianceReason, msgMeta+msg, compState)
1348+
return sender.SendEvent(ctx, nil, ownerref, policyComplianceReason, msg, compliance)
13591349
}
13601350

13611351
// handleSyncSuccess performs common actions that should be run whenever a template is in sync,

test/e2e/case10_error_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ var _ = Describe("Test error handling", func() {
2222
dupNamePolicyYaml = yamlBasePath + "dup-name-policy.yaml"
2323
dupNamePolicyName = "dup-policy"
2424
dupConfigPolicyName = "policy-config-dup"
25+
nonCompliantPrefix = "NonCompliant; "
2526
)
2627

2728
AfterEach(func() {
@@ -240,6 +241,13 @@ var _ = Describe("Test error handling", func() {
240241
defaultTimeoutSeconds,
241242
1,
242243
).Should(BeTrue())
244+
245+
By("Checking for the compliance message formatting")
246+
Eventually(
247+
checkForEvent("case10-bad-hubtemplate", nonCompliantPrefix+nonCompliantPrefix),
248+
defaultTimeoutSeconds,
249+
1,
250+
).Should(BeFalse())
243251
})
244252
It("should throw a noncompliance event if the template object is invalid", func() {
245253
hubApplyPolicy("case10-invalid-severity",
@@ -251,6 +259,13 @@ var _ = Describe("Test error handling", func() {
251259
defaultTimeoutSeconds,
252260
1,
253261
).Should(BeTrue())
262+
263+
By("Checking for the compliance message formatting")
264+
Eventually(
265+
checkForEvent("case10-invalid-severity", nonCompliantPrefix+nonCompliantPrefix),
266+
defaultTimeoutSeconds,
267+
1,
268+
).Should(BeFalse())
254269
})
255270
It("should not throw a noncompliance event if the policy-templates array is empty", func() {
256271
hubApplyPolicy("case10-empty-templates",

0 commit comments

Comments
 (0)