diff --git a/pkg/controller/controllername/controllername.go b/pkg/controller/controllername/controllername.go index 7822085d..495d7123 100644 --- a/pkg/controller/controllername/controllername.go +++ b/pkg/controller/controllername/controllername.go @@ -5,6 +5,8 @@ import ( "unicode" "github.com/go-logr/logr" + "sigs.k8s.io/controller-runtime/pkg/builder" + "sigs.k8s.io/controller-runtime/pkg/reconcile" ) const ( @@ -22,6 +24,8 @@ type ControllerNamer interface { LoggerName() string // AddToLogger adds controller name fields to the logger then returns the logger with the added fields AddToLogger(l logr.Logger) logr.Logger + // AddToController adds the controller name to the controller builder then returns the builder with the added name. This is useful for naming managed controllers from controller-runtime + AddToController(blder *builder.Builder, l logr.Logger) *builder.Builder } // controllerName ex. {"My","Controller", "Name"} -> MyControllerName @@ -69,3 +73,18 @@ func (c controllerName) AddToLogger(l logr.Logger) logr.Logger { WithValues("controller", c.String()). WithValues("controllerMetricsName", c.MetricsName()) // include metrics name, so we can automate creating queries that check Logs based on alerts } + +func (c controllerName) AddToController(blder *builder.Builder, l logr.Logger) *builder.Builder { + return blder. + Named(c.MetricsName()). + WithLogConstructor(func(req *reconcile.Request) logr.Logger { + logger := c.AddToLogger(l) + if req != nil { + logger.WithValues( + "namespace", req.Namespace, + "name", req.Name, + ) + } + return logger + }) +} diff --git a/pkg/controller/keyvault/event_mirror.go b/pkg/controller/keyvault/event_mirror.go index 103d4dcc..40547447 100644 --- a/pkg/controller/keyvault/event_mirror.go +++ b/pkg/controller/keyvault/event_mirror.go @@ -42,11 +42,12 @@ func NewEventMirror(manager ctrl.Manager, conf *config.Config) error { client: manager.GetClient(), events: manager.GetEventRecorderFor("aks-app-routing-operator"), } - return ctrl. - NewControllerManagedBy(manager). - For(&corev1.Event{}). - WithEventFilter(e.newPredicates()). - Complete(e) + return eventMirrorControllerName.AddToController( + ctrl. + NewControllerManagedBy(manager). + For(&corev1.Event{}). + WithEventFilter(e.newPredicates()), manager.GetLogger(), + ).Complete(e) } func (e *EventMirror) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { diff --git a/pkg/controller/keyvault/ingress_secret_provider_class.go b/pkg/controller/keyvault/ingress_secret_provider_class.go index 31137bd0..f22a3bca 100644 --- a/pkg/controller/keyvault/ingress_secret_provider_class.go +++ b/pkg/controller/keyvault/ingress_secret_provider_class.go @@ -44,15 +44,16 @@ func NewIngressSecretProviderClassReconciler(manager ctrl.Manager, conf *config. if conf.DisableKeyvault { return nil } - return ctrl. - NewControllerManagedBy(manager). - For(&netv1.Ingress{}). - Complete(&IngressSecretProviderClassReconciler{ - client: manager.GetClient(), - events: manager.GetEventRecorderFor("aks-app-routing-operator"), - config: conf, - ingressManager: ingressManager, - }) + return ingressSecretProviderControllerName.AddToController( + ctrl. + NewControllerManagedBy(manager). + For(&netv1.Ingress{}), manager.GetLogger(), + ).Complete(&IngressSecretProviderClassReconciler{ + client: manager.GetClient(), + events: manager.GetEventRecorderFor("aks-app-routing-operator"), + config: conf, + ingressManager: ingressManager, + }) } func (i *IngressSecretProviderClassReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { diff --git a/pkg/controller/keyvault/placeholder_pod.go b/pkg/controller/keyvault/placeholder_pod.go index 515ce932..9e99d5be 100644 --- a/pkg/controller/keyvault/placeholder_pod.go +++ b/pkg/controller/keyvault/placeholder_pod.go @@ -45,10 +45,11 @@ func NewPlaceholderPodController(manager ctrl.Manager, conf *config.Config, ingr if conf.DisableKeyvault { return nil } - return ctrl. - NewControllerManagedBy(manager). - For(&secv1.SecretProviderClass{}). - Complete(&PlaceholderPodController{client: manager.GetClient(), config: conf, ingressManager: ingressManager}) + return placeholderPodControllerName.AddToController( + ctrl. + NewControllerManagedBy(manager). + For(&secv1.SecretProviderClass{}), manager.GetLogger(), + ).Complete(&PlaceholderPodController{client: manager.GetClient(), config: conf, ingressManager: ingressManager}) } func (p *PlaceholderPodController) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { diff --git a/pkg/controller/osm/ingress_backend_reconciler.go b/pkg/controller/osm/ingress_backend_reconciler.go index a03d5215..79badd68 100644 --- a/pkg/controller/osm/ingress_backend_reconciler.go +++ b/pkg/controller/osm/ingress_backend_reconciler.go @@ -69,10 +69,12 @@ func NewIngressBackendReconciler(manager ctrl.Manager, conf *config.Config, ingr if conf.DisableOSM { return nil } - return ctrl. - NewControllerManagedBy(manager). - For(&netv1.Ingress{}). - Complete(&IngressBackendReconciler{client: manager.GetClient(), config: conf, ingressControllerNamer: ingressControllerNamer}) + return ingressBackendControllerName.AddToController( + ctrl. + NewControllerManagedBy(manager). + For(&netv1.Ingress{}), + manager.GetLogger(), + ).Complete(&IngressBackendReconciler{client: manager.GetClient(), config: conf, ingressControllerNamer: ingressControllerNamer}) } func (i *IngressBackendReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { diff --git a/pkg/controller/osm/ingress_cert_config_reconciler.go b/pkg/controller/osm/ingress_cert_config_reconciler.go index 7f5cc3b1..0d620960 100644 --- a/pkg/controller/osm/ingress_cert_config_reconciler.go +++ b/pkg/controller/osm/ingress_cert_config_reconciler.go @@ -39,10 +39,11 @@ func NewIngressCertConfigReconciler(manager ctrl.Manager, conf *config.Config) e if conf.DisableOSM { return nil } - return ctrl. - NewControllerManagedBy(manager). - For(&cfgv1alpha2.MeshConfig{}). - Complete(&IngressCertConfigReconciler{client: manager.GetClient()}) + return ingressCertConfigControllerName.AddToController( + ctrl. + NewControllerManagedBy(manager). + For(&cfgv1alpha2.MeshConfig{}), manager.GetLogger(), + ).Complete(&IngressCertConfigReconciler{client: manager.GetClient()}) } func (i *IngressCertConfigReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { diff --git a/pkg/controller/service/ingress_reconciler.go b/pkg/controller/service/ingress_reconciler.go index 1ec2b639..d18bc97d 100644 --- a/pkg/controller/service/ingress_reconciler.go +++ b/pkg/controller/service/ingress_reconciler.go @@ -8,7 +8,9 @@ import ( "fmt" "github.com/Azure/aks-app-routing-operator/pkg/controller/controllername" + "github.com/Azure/aks-app-routing-operator/pkg/controller/metrics" "github.com/Azure/aks-app-routing-operator/pkg/manifests" + "github.com/Azure/aks-app-routing-operator/pkg/util" "github.com/go-logr/logr" corev1 "k8s.io/api/core/v1" netv1 "k8s.io/api/networking/v1" @@ -16,9 +18,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - - "github.com/Azure/aks-app-routing-operator/pkg/controller/metrics" - "github.com/Azure/aks-app-routing-operator/pkg/util" ) var ( @@ -45,10 +44,12 @@ type NginxIngressReconciler struct { func NewNginxIngressReconciler(manager ctrl.Manager, ingConfig *manifests.NginxIngressConfig) error { metrics.InitControllerMetrics(ingressControllerName) - return ctrl. - NewControllerManagedBy(manager). - For(&corev1.Service{}). - Complete(&NginxIngressReconciler{client: manager.GetClient(), ingConfig: ingConfig}) + return ingressControllerName.AddToController( + ctrl. + NewControllerManagedBy(manager). + For(&corev1.Service{}), + manager.GetLogger(), + ).Complete(&NginxIngressReconciler{client: manager.GetClient(), ingConfig: ingConfig}) } func (i *NginxIngressReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {