@@ -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+ }
0 commit comments