Skip to content

Commit c009fa4

Browse files
Set template-error on correct details item
When adding a template-error to the status for a template that can't be decoded, the status-sync was incorrectly appending to the details array twice. So for something that would be called "template-0", the 0th item in the details array would be almost empty, and the 1st item would have all of the status events. This was causing some events to be emitted repeatedly, since the template-sync logic to prevent this was looking at the 0th item, which would never get the event. This commit also cleans up some awkward pointer gymnastics. Signed-off-by: Justin Kulikauskas <[email protected]>
1 parent 311debf commit c009fa4

File tree

2 files changed

+7
-16
lines changed

2 files changed

+7
-16
lines changed

controllers/statussync/policy_status_sync.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
205205
return reconcile.Result{}, err
206206
}
207207
// filter events to current policy instance and build map
208-
eventForPolicyMap := make(map[string]*[]historyEvent)
208+
eventForPolicyMap := make(map[string][]historyEvent)
209209
// panic if regexp invalid
210210
rgx := regexp.MustCompile(`(?i)^policy:\s*(?:([a-z0-9.-]+)\s*\/)?(.+)`)
211211

@@ -217,7 +217,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
217217
// Only handle events that match the UID of the current Policy
218218
if event.InvolvedObject.UID == instance.UID && reason != "" {
219219
templateName := rgx.FindStringSubmatch(event.Reason)[2]
220-
eventHistory := historyEvent{
220+
histEvent := historyEvent{
221221
ComplianceHistory: policiesv1.ComplianceHistory{
222222
LastTimestamp: event.LastTimestamp,
223223
Message: strings.TrimSpace(strings.TrimPrefix(
@@ -228,12 +228,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
228228
event: &event,
229229
}
230230

231-
if eventForPolicyMap[templateName] == nil {
232-
eventForPolicyMap[templateName] = &[]historyEvent{}
233-
}
234-
235-
templateEvents := append(*eventForPolicyMap[templateName], eventHistory)
236-
eventForPolicyMap[templateName] = &templateEvents
231+
eventForPolicyMap[templateName] = append(eventForPolicyMap[templateName], histEvent)
237232
}
238233
}
239234

@@ -253,7 +248,6 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
253248
reqLogger.Error(err, "Failed to decode policy template, skipping it")
254249

255250
existingDpt.ComplianceState = policiesv1.NonCompliant
256-
newStatus.Details = append(newStatus.Details, existingDpt)
257251
tName = fmt.Sprintf("template-%v", i) // template-sync emits this name on error
258252
} else {
259253
tName = object.(metav1.Object).GetName()
@@ -284,10 +278,7 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
284278
}
285279
}
286280

287-
history := []historyEvent{}
288-
if eventForPolicyMap[tName] != nil {
289-
history = *eventForPolicyMap[tName]
290-
}
281+
history := eventForPolicyMap[tName]
291282

292283
// Queue up all new status events to record on the compliance API
293284
if r.EventsQueue != nil {

test/e2e/case22_user_validation_error_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,15 @@ var _ = Describe("Test proper metrics handling on syntax error", Ordered, func()
5959
defaultTimeoutSeconds)
6060
err := runtime.DefaultUnstructuredConverter.FromUnstructured(managedPlc.Object, &plc)
6161
g.Expect(err).ToNot(HaveOccurred())
62-
if len(plc.Status.Details) < 2 {
62+
if len(plc.Status.Details) < 1 {
6363
return ""
6464
}
6565

66-
if len(plc.Status.Details[1].History) < 1 {
66+
if len(plc.Status.Details[0].History) < 1 {
6767
return ""
6868
}
6969

70-
return plc.Status.Details[1].History[0].Message
70+
return plc.Status.Details[0].History[0].Message
7171
}, defaultTimeoutSeconds, 1).Should(ContainSubstring("NonCompliant; template-error;"))
7272
})
7373

0 commit comments

Comments
 (0)