From 28c0a720dc5b4d62efc0eb354584e1f71a5e828d Mon Sep 17 00:00:00 2001 From: Wei-Xiang Sun Date: Fri, 8 Dec 2023 09:52:53 +0800 Subject: [PATCH] [Fix] ResourceDistribution should watch unstructured object (#1464) * fix resourcedistribution event watch Signed-off-by: mingzhou.swx * fix image list pull job e2e Signed-off-by: mingzhou.swx --------- Signed-off-by: mingzhou.swx Co-authored-by: mingzhou.swx --- .../resourcedistribution_controller.go | 26 +++++++------------ test/e2e/apps/imagelistpulljobs.go | 15 ++++++++--- 2 files changed, 22 insertions(+), 19 deletions(-) diff --git a/pkg/controller/resourcedistribution/resourcedistribution_controller.go b/pkg/controller/resourcedistribution/resourcedistribution_controller.go index 847ea12914..79d34d3623 100644 --- a/pkg/controller/resourcedistribution/resourcedistribution_controller.go +++ b/pkg/controller/resourcedistribution/resourcedistribution_controller.go @@ -110,19 +110,16 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error { } // Watch for changes to Secrets - err = c.Watch(&source.Kind{Type: &corev1.Secret{}}, &handler.EnqueueRequestForOwner{ + secret := unstructured.Unstructured{} + secret.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("Secret")) + err = c.Watch(&source.Kind{Type: &secret}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &appsv1alpha1.ResourceDistribution{}, }, predicate.Funcs{ CreateFunc: func(createEvent event.CreateEvent) bool { return false }, - UpdateFunc: func(updateEvent event.UpdateEvent) bool { - oldObject, oldOK := updateEvent.ObjectOld.(*corev1.Secret) - newObject, newOK := updateEvent.ObjectNew.(*corev1.Secret) - if !oldOK || !newOK { - return false - } - return !reflect.DeepEqual(oldObject.Data, newObject.Data) || !reflect.DeepEqual(oldObject.StringData, newObject.StringData) + GenericFunc: func(genericEvent event.GenericEvent) bool { + return false }, }) if err != nil { @@ -130,19 +127,16 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error { } // Watch for changes to ConfigMap - err = c.Watch(&source.Kind{Type: &corev1.ConfigMap{}}, &handler.EnqueueRequestForOwner{ + configMap := unstructured.Unstructured{} + configMap.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap")) + err = c.Watch(&source.Kind{Type: &configMap}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &appsv1alpha1.ResourceDistribution{}, }, predicate.Funcs{ CreateFunc: func(createEvent event.CreateEvent) bool { return false }, - UpdateFunc: func(updateEvent event.UpdateEvent) bool { - oldObject, oldOK := updateEvent.ObjectOld.(*corev1.ConfigMap) - newObject, newOK := updateEvent.ObjectNew.(*corev1.ConfigMap) - if !oldOK || !newOK { - return false - } - return !reflect.DeepEqual(oldObject.Data, newObject.Data) || !reflect.DeepEqual(oldObject.BinaryData, newObject.BinaryData) + GenericFunc: func(genericEvent event.GenericEvent) bool { + return false }, }) if err != nil { diff --git a/test/e2e/apps/imagelistpulljobs.go b/test/e2e/apps/imagelistpulljobs.go index a29dc735d5..a79807f6e7 100644 --- a/test/e2e/apps/imagelistpulljobs.go +++ b/test/e2e/apps/imagelistpulljobs.go @@ -194,11 +194,20 @@ var _ = SIGDescribe("PullImages", func() { gomega.Expect(err).NotTo(gomega.HaveOccurred()) ginkgo.By("Check imagepulljob should be cleaned") + time.Sleep(3 * time.Second) gomega.Eventually(func() bool { - imagePullJobs, err := testerForImagePullJob.ListJobs(job.Namespace) + imagePullJobLister, err := testerForImagePullJob.ListJobs(job.Namespace) gomega.Expect(err).NotTo(gomega.HaveOccurred()) - return len(imagePullJobs.Items) > 0 - }, 3*time.Second, time.Second).Should(gomega.Equal(false)) + var imagePullJobs []*appsv1alpha1.ImagePullJob + for i := range imagePullJobLister.Items { + pullJob := &imagePullJobLister.Items[i] + if metav1.IsControlledBy(pullJob, job) { + imagePullJobs = append(imagePullJobs, pullJob) + } + fmt.Printf("waiting imagePullJob GC: %v", imagePullJobs) + } + return len(imagePullJobs) == 0 + }, time.Minute, time.Second).Should(gomega.BeTrue()) }) framework.ConformanceIt("create an always job to pull an image on all nodes", func() {