From 38a9c9b48004c5a89ec71e7d1c70df519a05a097 Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Fri, 19 Jun 2020 16:02:45 +0100 Subject: [PATCH 1/2] feat: add plugin to fix controller typo alias and improve its logs (only V3) --- pkg/plugin/v3/scaffolds/internal/templates/main.go | 7 ++----- testdata/project-v3-multigroup/main.go | 14 +++++++------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/pkg/plugin/v3/scaffolds/internal/templates/main.go b/pkg/plugin/v3/scaffolds/internal/templates/main.go index eda5a454572..157ef3846c9 100644 --- a/pkg/plugin/v3/scaffolds/internal/templates/main.go +++ b/pkg/plugin/v3/scaffolds/internal/templates/main.go @@ -121,12 +121,9 @@ const ( os.Exit(1) } ` - // TODO(v3): loggers for the same Kind controllers from different groups use the same logger. - // `.WithName("controllers").WithName(GROUP).WithName(KIND)` should be used instead. However, - // this is a backwards incompatible change, and thus should be done for next project version. multiGroupReconcilerSetupCodeFragment = `if err = (&%scontrollers.%sReconciler{ Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("%s"), + Log: ctrl.Log.WithName("controllers").WithName("%s").WithName("%s"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "%s") @@ -173,7 +170,7 @@ func (f *MainUpdater) GetCodeFragments() file.CodeFragmentsMap { f.Resource.Kind, f.Resource.Kind, f.Resource.Kind)) } else { setup = append(setup, fmt.Sprintf(multiGroupReconcilerSetupCodeFragment, - f.Resource.GroupPackageName, f.Resource.Kind, f.Resource.Kind, f.Resource.Kind)) + f.Resource.GroupPackageName, f.Resource.Kind, f.Resource.Group, f.Resource.Kind, f.Resource.Kind)) } } if f.WireWebhook { diff --git a/testdata/project-v3-multigroup/main.go b/testdata/project-v3-multigroup/main.go index eab2ba800e8..6212af4eded 100644 --- a/testdata/project-v3-multigroup/main.go +++ b/testdata/project-v3-multigroup/main.go @@ -84,7 +84,7 @@ func main() { if err = (&crewcontrollers.CaptainReconciler{ Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("Captain"), + Log: ctrl.Log.WithName("controllers").WithName("crew").WithName("Captain"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Captain") @@ -96,7 +96,7 @@ func main() { } if err = (&shipcontrollers.FrigateReconciler{ Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("Frigate"), + Log: ctrl.Log.WithName("controllers").WithName("ship").WithName("Frigate"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Frigate") @@ -108,7 +108,7 @@ func main() { } if err = (&shipcontrollers.DestroyerReconciler{ Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("Destroyer"), + Log: ctrl.Log.WithName("controllers").WithName("ship").WithName("Destroyer"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Destroyer") @@ -116,7 +116,7 @@ func main() { } if err = (&shipcontrollers.CruiserReconciler{ Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("Cruiser"), + Log: ctrl.Log.WithName("controllers").WithName("ship").WithName("Cruiser"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Cruiser") @@ -124,7 +124,7 @@ func main() { } if err = (&seacreaturescontrollers.KrakenReconciler{ Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("Kraken"), + Log: ctrl.Log.WithName("controllers").WithName("sea-creatures").WithName("Kraken"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Kraken") @@ -132,7 +132,7 @@ func main() { } if err = (&seacreaturescontrollers.LeviathanReconciler{ Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("Leviathan"), + Log: ctrl.Log.WithName("controllers").WithName("sea-creatures").WithName("Leviathan"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "Leviathan") @@ -140,7 +140,7 @@ func main() { } if err = (&foopolicycontrollers.HealthCheckPolicyReconciler{ Client: mgr.GetClient(), - Log: ctrl.Log.WithName("controllers").WithName("HealthCheckPolicy"), + Log: ctrl.Log.WithName("controllers").WithName("foo.policy").WithName("HealthCheckPolicy"), Scheme: mgr.GetScheme(), }).SetupWithManager(mgr); err != nil { setupLog.Error(err, "unable to create controller", "controller", "HealthCheckPolicy") From fc3fd83d927bc4db3c39fecc1428a704323d0fa4 Mon Sep 17 00:00:00 2001 From: Camila Macedo Date: Fri, 19 Jun 2020 19:17:47 +0100 Subject: [PATCH 2/2] feat: add group in the controller logs for multi-group