From 79a06e54a9e6a6988ab23f419a0079f9aa61261f Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Thu, 12 Sep 2024 21:36:25 +0100 Subject: [PATCH] fix: ensure unique controller names to fix name conflicts in multigroup setup --- .../project/internal/controller/cronjob_controller.go | 1 + .../project/internal/controller/memcached_controller.go | 1 + .../project/internal/controller/cronjob_controller.go | 1 + .../scaffolds/internal/templates/controllers/controller.go | 5 +++++ .../scaffolds/internal/templates/controllers/controller.go | 5 +++++ .../internal/controller/apps/deployment_controller.go | 1 + .../internal/controller/crew/captain_controller.go | 1 + .../internal/controller/example.com/busybox_controller.go | 1 + .../internal/controller/example.com/memcached_controller.go | 1 + .../internal/controller/fiz/bar_controller.go | 1 + .../controller/foo.policy/healthcheckpolicy_controller.go | 1 + .../internal/controller/foo/bar_controller.go | 1 + .../internal/controller/sea-creatures/kraken_controller.go | 1 + .../controller/sea-creatures/leviathan_controller.go | 1 + .../internal/controller/ship/cruiser_controller.go | 1 + .../internal/controller/ship/destroyer_controller.go | 1 + .../internal/controller/ship/frigate_controller.go | 1 + .../internal/controller/busybox_controller.go | 1 + .../internal/controller/memcached_controller.go | 1 + .../project-v4/internal/controller/admiral_controller.go | 1 + .../project-v4/internal/controller/captain_controller.go | 1 + .../project-v4/internal/controller/firstmate_controller.go | 1 + 22 files changed, 30 insertions(+) diff --git a/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go index d824c169039..eab47d7c1b8 100644 --- a/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go +++ b/docs/book/src/cronjob-tutorial/testdata/project/internal/controller/cronjob_controller.go @@ -574,5 +574,6 @@ func (r *CronJobReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&batchv1.CronJob{}). Owns(&kbatch.Job{}). + Named("cronjob"). Complete(r) } diff --git a/docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go b/docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go index e125b3e41c7..f5f398a72e8 100644 --- a/docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go +++ b/docs/book/src/getting-started/testdata/project/internal/controller/memcached_controller.go @@ -205,6 +205,7 @@ func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&cachev1alpha1.Memcached{}). Owns(&appsv1.Deployment{}). + Named("memcached"). Complete(r) } diff --git a/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go b/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go index d824c169039..eab47d7c1b8 100644 --- a/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go +++ b/docs/book/src/multiversion-tutorial/testdata/project/internal/controller/cronjob_controller.go @@ -574,5 +574,6 @@ func (r *CronJobReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&batchv1.CronJob{}). Owns(&kbatch.Job{}). + Named("cronjob"). Complete(r) } diff --git a/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go b/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go index 79e0cb12538..7baf50d40e7 100644 --- a/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go +++ b/pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/internal/templates/controllers/controller.go @@ -478,6 +478,11 @@ func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) erro // Uncomment the following line adding a pointer to an instance of the controlled resource as an argument // For(). {{- end }} + {{- if and (.MultiGroup) (not (isEmptyStr .Resource.Group)) }} + Named("{{ lower .Resource.Group }}-{{ lower .Resource.Kind }}"). + {{- else }} + Named("{{ lower .Resource.Kind }}"). + {{- end }} // Watch the Deployment managed by the {{ .Resource.Kind }}Reconciler. If any changes occur to the Deployment // owned and managed by this controller, it will trigger reconciliation, ensuring that the cluster // state aligns with the desired state. See that the ownerRef was set when the Deployment was created. diff --git a/pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller.go b/pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller.go index 27d4a7fc259..71f0736c7aa 100644 --- a/pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller.go +++ b/pkg/plugins/golang/v4/scaffolds/internal/templates/controllers/controller.go @@ -115,6 +115,11 @@ func (r *{{ .Resource.Kind }}Reconciler) SetupWithManager(mgr ctrl.Manager) erro // Uncomment the following line adding a pointer to an instance of the controlled resource as an argument // For(). {{- end }} + {{- if and (.MultiGroup) (not (isEmptyStr .Resource.Group)) }} + Named("{{ lower .Resource.Group }}-{{ lower .Resource.Kind }}"). + {{- else }} + Named("{{ lower .Resource.Kind }}"). + {{- end }} Complete(r) } ` diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/apps/deployment_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/apps/deployment_controller.go index 454ea50391f..c3c79078b17 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/apps/deployment_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/apps/deployment_controller.go @@ -57,5 +57,6 @@ func (r *DeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) func (r *DeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&appsv1.Deployment{}). + Named("apps-deployment"). Complete(r) } diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/crew/captain_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/crew/captain_controller.go index 924564ab3c2..a4c822d68fc 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/crew/captain_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/crew/captain_controller.go @@ -58,5 +58,6 @@ func (r *CaptainReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct func (r *CaptainReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&crewv1.Captain{}). + Named("crew-captain"). Complete(r) } diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/example.com/busybox_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/example.com/busybox_controller.go index 1df858c2c02..24286f8eca0 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/example.com/busybox_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/example.com/busybox_controller.go @@ -434,6 +434,7 @@ func (r *BusyboxReconciler) SetupWithManager(mgr ctrl.Manager) error { // Watch the Busybox CR(s) and trigger reconciliation whenever it // is created, updated, or deleted For(&examplecomv1alpha1.Busybox{}). + Named("example.com-busybox"). // Watch the Deployment managed by the BusyboxReconciler. If any changes occur to the Deployment // owned and managed by this controller, it will trigger reconciliation, ensuring that the cluster // state aligns with the desired state. See that the ownerRef was set when the Deployment was created. diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/example.com/memcached_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/example.com/memcached_controller.go index e76d2f5b3b9..6d6802f86b0 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/example.com/memcached_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/example.com/memcached_controller.go @@ -440,6 +440,7 @@ func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error { // Watch the Memcached CR(s) and trigger reconciliation whenever it // is created, updated, or deleted For(&examplecomv1alpha1.Memcached{}). + Named("example.com-memcached"). // Watch the Deployment managed by the MemcachedReconciler. If any changes occur to the Deployment // owned and managed by this controller, it will trigger reconciliation, ensuring that the cluster // state aligns with the desired state. See that the ownerRef was set when the Deployment was created. diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/fiz/bar_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/fiz/bar_controller.go index 7ed849a7d3b..d54be828639 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/fiz/bar_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/fiz/bar_controller.go @@ -58,5 +58,6 @@ func (r *BarReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R func (r *BarReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&fizv1.Bar{}). + Named("fiz-bar"). Complete(r) } diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/foo.policy/healthcheckpolicy_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/foo.policy/healthcheckpolicy_controller.go index 999be069c3d..96cc5218b91 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/foo.policy/healthcheckpolicy_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/foo.policy/healthcheckpolicy_controller.go @@ -58,5 +58,6 @@ func (r *HealthCheckPolicyReconciler) Reconcile(ctx context.Context, req ctrl.Re func (r *HealthCheckPolicyReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&foopolicyv1.HealthCheckPolicy{}). + Named("foo.policy-healthcheckpolicy"). Complete(r) } diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/foo/bar_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/foo/bar_controller.go index a6fc2cfa5e9..e9afa39c585 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/foo/bar_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/foo/bar_controller.go @@ -58,5 +58,6 @@ func (r *BarReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.R func (r *BarReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&foov1.Bar{}). + Named("foo-bar"). Complete(r) } diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/sea-creatures/kraken_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/sea-creatures/kraken_controller.go index 8d91ecdbc1e..69cef373560 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/sea-creatures/kraken_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/sea-creatures/kraken_controller.go @@ -58,5 +58,6 @@ func (r *KrakenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr func (r *KrakenReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&seacreaturesv1beta1.Kraken{}). + Named("sea-creatures-kraken"). Complete(r) } diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/sea-creatures/leviathan_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/sea-creatures/leviathan_controller.go index c31c5c533a6..a9d133eaf41 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/sea-creatures/leviathan_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/sea-creatures/leviathan_controller.go @@ -58,5 +58,6 @@ func (r *LeviathanReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( func (r *LeviathanReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&seacreaturesv1beta2.Leviathan{}). + Named("sea-creatures-leviathan"). Complete(r) } diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/cruiser_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/cruiser_controller.go index ed87ec7089b..3f001d16cc6 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/cruiser_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/cruiser_controller.go @@ -58,5 +58,6 @@ func (r *CruiserReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct func (r *CruiserReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&shipv2alpha1.Cruiser{}). + Named("ship-cruiser"). Complete(r) } diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/destroyer_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/destroyer_controller.go index d408be2d5b9..93067fafd25 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/destroyer_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/destroyer_controller.go @@ -58,5 +58,6 @@ func (r *DestroyerReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( func (r *DestroyerReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&shipv1.Destroyer{}). + Named("ship-destroyer"). Complete(r) } diff --git a/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/frigate_controller.go b/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/frigate_controller.go index ebfa13039b2..a8d6322ca82 100644 --- a/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/frigate_controller.go +++ b/testdata/project-v4-multigroup-with-plugins/internal/controller/ship/frigate_controller.go @@ -58,5 +58,6 @@ func (r *FrigateReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct func (r *FrigateReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&shipv1beta1.Frigate{}). + Named("ship-frigate"). Complete(r) } diff --git a/testdata/project-v4-with-plugins/internal/controller/busybox_controller.go b/testdata/project-v4-with-plugins/internal/controller/busybox_controller.go index 31a8638972f..792e34a2a7e 100644 --- a/testdata/project-v4-with-plugins/internal/controller/busybox_controller.go +++ b/testdata/project-v4-with-plugins/internal/controller/busybox_controller.go @@ -434,6 +434,7 @@ func (r *BusyboxReconciler) SetupWithManager(mgr ctrl.Manager) error { // Watch the Busybox CR(s) and trigger reconciliation whenever it // is created, updated, or deleted For(&examplecomv1alpha1.Busybox{}). + Named("busybox"). // Watch the Deployment managed by the BusyboxReconciler. If any changes occur to the Deployment // owned and managed by this controller, it will trigger reconciliation, ensuring that the cluster // state aligns with the desired state. See that the ownerRef was set when the Deployment was created. diff --git a/testdata/project-v4-with-plugins/internal/controller/memcached_controller.go b/testdata/project-v4-with-plugins/internal/controller/memcached_controller.go index 1a310bd2df4..8d667ce4741 100644 --- a/testdata/project-v4-with-plugins/internal/controller/memcached_controller.go +++ b/testdata/project-v4-with-plugins/internal/controller/memcached_controller.go @@ -440,6 +440,7 @@ func (r *MemcachedReconciler) SetupWithManager(mgr ctrl.Manager) error { // Watch the Memcached CR(s) and trigger reconciliation whenever it // is created, updated, or deleted For(&examplecomv1alpha1.Memcached{}). + Named("memcached"). // Watch the Deployment managed by the MemcachedReconciler. If any changes occur to the Deployment // owned and managed by this controller, it will trigger reconciliation, ensuring that the cluster // state aligns with the desired state. See that the ownerRef was set when the Deployment was created. diff --git a/testdata/project-v4/internal/controller/admiral_controller.go b/testdata/project-v4/internal/controller/admiral_controller.go index 81f7a6213a8..397aa618e76 100644 --- a/testdata/project-v4/internal/controller/admiral_controller.go +++ b/testdata/project-v4/internal/controller/admiral_controller.go @@ -58,5 +58,6 @@ func (r *AdmiralReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct func (r *AdmiralReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&crewv1.Admiral{}). + Named("admiral"). Complete(r) } diff --git a/testdata/project-v4/internal/controller/captain_controller.go b/testdata/project-v4/internal/controller/captain_controller.go index 0a7a8d9cf93..11c1c23a38c 100644 --- a/testdata/project-v4/internal/controller/captain_controller.go +++ b/testdata/project-v4/internal/controller/captain_controller.go @@ -58,5 +58,6 @@ func (r *CaptainReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct func (r *CaptainReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&crewv1.Captain{}). + Named("captain"). Complete(r) } diff --git a/testdata/project-v4/internal/controller/firstmate_controller.go b/testdata/project-v4/internal/controller/firstmate_controller.go index a3979a1ba12..7f4a58c99a9 100644 --- a/testdata/project-v4/internal/controller/firstmate_controller.go +++ b/testdata/project-v4/internal/controller/firstmate_controller.go @@ -58,5 +58,6 @@ func (r *FirstMateReconciler) Reconcile(ctx context.Context, req ctrl.Request) ( func (r *FirstMateReconciler) SetupWithManager(mgr ctrl.Manager) error { return ctrl.NewControllerManagedBy(mgr). For(&crewv1.FirstMate{}). + Named("firstmate"). Complete(r) }