Skip to content

Commit

Permalink
Merge pull request #199 from fluxcd/fix-same-ns-reconcile-regression
Browse files Browse the repository at this point in the history
Fix watched same-ns image repos trigger reconcile
  • Loading branch information
stefanprodan authored Nov 12, 2021
2 parents ab74049 + 68f7a60 commit 4463381
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
7 changes: 6 additions & 1 deletion controllers/imagepolicy_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,14 @@ func (r *ImagePolicyReconciler) SetupWithManager(mgr ctrl.Manager, opts ImagePol
// it's easy to list those out when an image repo changes.
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &imagev1.ImagePolicy{}, imageRepoKey, func(obj client.Object) []string {
pol := obj.(*imagev1.ImagePolicy)

namespace := pol.Spec.ImageRepositoryRef.Namespace
if namespace == "" {
namespace = obj.GetNamespace()
}
namespacedName := types.NamespacedName{
Name: pol.Spec.ImageRepositoryRef.Name,
Namespace: pol.Spec.ImageRepositoryRef.Namespace,
Namespace: namespace,
}
return []string{namespacedName.String()}
}); err != nil {
Expand Down
16 changes: 15 additions & 1 deletion controllers/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ var _ = Describe("ImagePolicy controller", func() {
When("is in same namespace", func() {
It("grants access", func() {
versions := []string{"1.0.0", "1.0.1"}
imgRepo := loadImages(registryServer, "test-acl-"+randStringRunes(5), versions)
imageName := "test-acl-" + randStringRunes(5)
imgRepo := loadImages(registryServer, imageName, versions)

repo := imagev1.ImageRepository{
Spec: imagev1.ImageRepositorySpec{
Expand Down Expand Up @@ -444,6 +445,19 @@ 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"})
Eventually(func() bool {
err := r.Get(ctx, imageObjectName, &repo)
return err == nil && repo.Status.LastScanResult.TagCount == len(versions)+1
}, timeout, interval).Should(BeTrue())

Eventually(func() bool {
err := r.Get(ctx, polName, &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())
})
})
Expand Down

0 comments on commit 4463381

Please sign in to comment.