fluxcd / image-automation-controller

GitOps Toolkit controller that patches container image tags in Git
https://fluxcd.io
Apache License 2.0
155 stars 67 forks source link

Add predicates for GitRepo and ImagePolicy watches #639

Closed darkowlzz closed 4 months ago

darkowlzz commented 4 months ago

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 https://github.com/fluxcd/image-reflector-controller/pull/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:

2024-02-27T02:00:08.759+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": "84600cf1-8461-4636-96c7-b0f5fe464506"}
2024-02-27T02:00:08.832+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": "ff70cb0f-ef70-41d2-b036-3008fc5a26c4"}

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.