Watched cross-ns image repos trigger reconcile

Cross-namespace ImageRepository resources should trigger reconciles for
ImagePolicies that refer to them. Previously, this was only done for
resources in the same namespace.

Fixes #195

Signed-off-by: Aurel Canciu <aurelcanciu@gmail.com>
This commit is contained in:
Aurel Canciu 2021-11-11 14:57:22 +01:00
parent 2d05314858
commit 0dd4f706ff
No known key found for this signature in database
GPG Key ID: AB25339971E6F81E
2 changed files with 16 additions and 3 deletions

View File

@ -259,8 +259,7 @@ func (r *ImagePolicyReconciler) SetupWithManager(mgr ctrl.Manager, opts ImagePol
func (r *ImagePolicyReconciler) imagePoliciesForRepository(obj client.Object) []reconcile.Request { func (r *ImagePolicyReconciler) imagePoliciesForRepository(obj client.Object) []reconcile.Request {
ctx := context.Background() ctx := context.Background()
var policies imagev1.ImagePolicyList var policies imagev1.ImagePolicyList
if err := r.List(ctx, &policies, client.InNamespace(obj.GetNamespace()), if err := r.List(ctx, &policies, client.MatchingFields{imageRepoKey: obj.GetName()}); err != nil {
client.MatchingFields{imageRepoKey: obj.GetName()}); err != nil {
return nil return nil
} }
reqs := make([]reconcile.Request, len(policies.Items)) reqs := make([]reconcile.Request, len(policies.Items))

View File

@ -615,7 +615,8 @@ var _ = Describe("ImagePolicy controller", func() {
defer k8sClient.Delete(context.Background(), policyNamespace) defer k8sClient.Delete(context.Background(), policyNamespace)
versions := []string{"1.0.0", "1.0.1"} 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{ repo := imagev1.ImageRepository{
Spec: imagev1.ImageRepositorySpec{ Spec: imagev1.ImageRepositorySpec{
@ -687,6 +688,19 @@ var _ = Describe("ImagePolicy controller", func() {
}, timeout, interval).Should(BeTrue()) }, timeout, interval).Should(BeTrue())
Expect(pol.Status.LatestImage).To(Equal(imgRepo + ":1.0.1")) 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"})
Eventually(func() bool {
err := r.Get(ctx, repoObjectName, &repo)
return err == nil && repo.Status.LastScanResult.TagCount == len(versions)+1
}, 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()) Expect(r.Delete(ctx, &pol)).To(Succeed())
}) })
}) })