Skip to content

Commit 19d4af1

Browse files
dhaiducekopenshift-merge-bot[bot]
authored andcommitted
Fix requeue when Kind is missing
ref: https://issues.redhat.com/browse/ACM-9844 Signed-off-by: Dale Haiducek <[email protected]>
1 parent 36ca448 commit 19d4af1

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

controllers/templatesync/template_sync.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,11 @@ func (r *PolicyReconciler) Reconcile(ctx context.Context, request reconcile.Requ
280280
// Gather raw object definition from the policy template
281281
object, gvk, err := unstructured.UnstructuredJSONScheme.Decode(policyT.ObjectDefinition.Raw, nil, nil)
282282
if err != nil {
283-
resultError = err
283+
// If it's missing the Kind, don't requeue since that requires an update to the Policy
284+
if !runtime.IsMissingKind(err) {
285+
resultError = err
286+
}
287+
284288
errMsg := fmt.Sprintf("Failed to decode policy template with err: %s", err)
285289

286290
_ = r.emitTemplateError(ctx, instance, tIndex, fmt.Sprintf("template-%v", tIndex), false, errMsg)

test/e2e/case10_error_test.go

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ var _ = Describe("Test error handling", func() {
315315
By("Creating the ConfigurationPolicy on the managed cluster directly")
316316
_, err := kubectlManaged(
317317
"apply",
318-
"--filename=../resources/case10_template_sync_error_test/working-policy-configpol.yaml",
318+
"--filename="+yamlBasePath+"working-policy-configpol.yaml",
319319
"--namespace="+clusterNamespace,
320320
)
321321
Expect(err).ShouldNot(HaveOccurred())
@@ -353,6 +353,27 @@ var _ = Describe("Test error handling", func() {
353353
1,
354354
).Should(BeTrue())
355355
})
356+
It("should only generate one event for a missing kind", func() {
357+
policyName := "case10-missing-kind"
358+
hubApplyPolicy(policyName,
359+
yamlBasePath+"missing-kind.yaml")
360+
361+
By("Checking for the error event and ensuring it only occurs once")
362+
Eventually(
363+
checkForEvent(
364+
policyName, "Object 'Kind' is missing in",
365+
),
366+
defaultTimeoutSeconds,
367+
1,
368+
).Should(BeTrue())
369+
Consistently(
370+
getMatchingEvents(
371+
policyName, "Object 'Kind' is missing in",
372+
),
373+
defaultTimeoutSeconds,
374+
1,
375+
).Should(HaveLen(2))
376+
})
356377
})
357378

358379
// Checks for an event on the managed cluster
@@ -381,3 +402,32 @@ func checkForEvent(policyName, msgSubStr string) func() bool {
381402
return false
382403
}
383404
}
405+
406+
// Checks for an event on the managed cluster
407+
func getMatchingEvents(policyName, msgSubStr string) func() []string {
408+
return func() []string {
409+
eventInterface := clientManagedDynamic.Resource(gvrEvent).Namespace(clusterNamespace)
410+
411+
eventList, err := eventInterface.List(context.TODO(), metav1.ListOptions{
412+
FieldSelector: "involvedObject.name=" + policyName,
413+
})
414+
if err != nil {
415+
return []string{}
416+
}
417+
418+
matchingEvents := []string{}
419+
420+
for _, event := range eventList.Items {
421+
msg, found, err := unstructured.NestedString(event.Object, "message")
422+
if !found || err != nil {
423+
continue
424+
}
425+
426+
if strings.Contains(msg, msgSubStr) {
427+
matchingEvents = append(matchingEvents, msg)
428+
}
429+
}
430+
431+
return matchingEvents
432+
}
433+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
apiVersion: policy.open-cluster-management.io/v1
2+
kind: Policy
3+
metadata:
4+
name: case10-missing-kind
5+
labels:
6+
policy.open-cluster-management.io/cluster-name: managed
7+
policy.open-cluster-management.io/cluster-namespace: managed
8+
policy.open-cluster-management.io/root-policy: case10-missing-kind
9+
spec:
10+
remediationAction: inform
11+
disabled: false
12+
policy-templates:
13+
- objectDefinition:
14+
spec:
15+
object-templates:
16+
- metadata:
17+
labels: {}

0 commit comments

Comments
 (0)