diff --git a/controllers/imagepolicy_controller.go b/controllers/imagepolicy_controller.go index 8995cfb8..77249bd7 100644 --- a/controllers/imagepolicy_controller.go +++ b/controllers/imagepolicy_controller.go @@ -259,8 +259,7 @@ func (r *ImagePolicyReconciler) SetupWithManager(mgr ctrl.Manager, opts ImagePol func (r *ImagePolicyReconciler) imagePoliciesForRepository(obj client.Object) []reconcile.Request { ctx := context.Background() var policies imagev1.ImagePolicyList - if err := r.List(ctx, &policies, client.InNamespace(obj.GetNamespace()), - client.MatchingFields{imageRepoKey: obj.GetName()}); err != nil { + if err := r.List(ctx, &policies, client.MatchingFields{imageRepoKey: obj.GetName()}); err != nil { return nil } reqs := make([]reconcile.Request, len(policies.Items)) diff --git a/controllers/policy_test.go b/controllers/policy_test.go index 7369ce06..cb4fb996 100644 --- a/controllers/policy_test.go +++ b/controllers/policy_test.go @@ -615,7 +615,8 @@ var _ = Describe("ImagePolicy controller", func() { defer k8sClient.Delete(context.Background(), policyNamespace) versions := []string{"1.0.0", "1.0.1"} - imgRepo := loadImages(registryServer, "acl-image-"+randStringRunes(5), versions) + imageName := "acl-image-" + randStringRunes(5) + imgRepo := loadImages(registryServer, imageName, versions) repo := imagev1.ImageRepository{ Spec: imagev1.ImageRepositorySpec{ @@ -687,6 +688,20 @@ var _ = Describe("ImagePolicy controller", func() { }, timeout, interval).Should(BeTrue()) Expect(pol.Status.LatestImage).To(Equal(imgRepo + ":1.0.1")) + // Updating the image should reconcile the cross-namespace policy + imgRepo = loadImages(registryServer, imageName, []string{"1.0.2"}) + lastScanTime := repo.Status.LastScanResult.ScanTime + Eventually(func() bool { + err := r.Get(ctx, repoObjectName, &repo) + return err == nil && repo.Status.LastScanResult.ScanTime.After(lastScanTime.Time) + }, timeout, interval).Should(BeTrue()) + + Eventually(func() bool { + err := r.Get(ctx, polObjectName, &pol) + return err == nil && pol.Status.LatestImage != "" + }, timeout, interval).Should(BeTrue()) + Expect(pol.Status.LatestImage).To(Equal(imgRepo + ":1.0.2")) + Expect(r.Delete(ctx, &pol)).To(Succeed()) }) })