Add predicates for GitRepo and ImagePolicy watches #639
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While working on the refactoring the controller, I noticed that it was getting triggered more than expected. This change is similar to a corresponding change in image-reflector-controller ImagePolicyReconciler, refer fluxcd/image-reflector-controller#334, to reduce unnecessary reconciliations. Adding this to the main branch separately from the refactor to reduce introducing new behavior in the refactor branch.
ImageUpdateAutomationReconciler watches GitRepository and ImagePolicy kinds for every event. This leads to unnecessary extra reconciliations at times. For example when the controller starts with existing resources, the same ImageUpdateAutomation object gets reconciled at least twice, once due to the watch on ImageUpdateAutomation at startup and again due to the watches on GitRepository and ImagePolicy for create event, as they get registered in the cache.
Add predicates to filter the ImagePolicy watch to only allow events for latest image update, and GitRepository watch to only allow events for change in the source configuration.
For example, previously when the controller started with existing resources, following multiple reconciliations could be seen:
With the new predicates:
2024-02-27T03:25:27.862+0530 INFO no changes made in working directory; no commit {"controller": "imageupdateautomation", "controllerGroup": "image.toolkit.fluxcd.io", "controllerKind": "ImageUpdateAutomation", "ImageUpdateAutomation": {"name":"test-update-auto","namespace":"default"}, "namespace": "default", "name": "test-update-auto", "reconcileID": "86861ba9-5466-42a8-8ada-c0fb1b307f5e"}
Also adds a test to make sure that ImageUpdateAutomation continues to get triggered on updates to ImagePolicy and GitRepository.