From 5bc9a34e13d846432c73c0675fedd045ede08356 Mon Sep 17 00:00:00 2001 From: Hans Kristian Flaatten Date: Mon, 22 Jan 2024 19:13:36 +0100 Subject: [PATCH] Add logger name to each log instance (#289) --- controllers/apitoken_controller.go | 6 +++--- controllers/remoteunleash_controller.go | 14 +++++++------- controllers/unleash_controller.go | 21 ++++++++++----------- pkg/federation/publisher.go | 2 +- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/controllers/apitoken_controller.go b/controllers/apitoken_controller.go index 3b98180d..005a3e56 100644 --- a/controllers/apitoken_controller.go +++ b/controllers/apitoken_controller.go @@ -75,7 +75,7 @@ type ApiTokenReconciler struct { // For more details, check Reconcile and its Result here: // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.13.0/pkg/reconcile func (r *ApiTokenReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("apitoken") log.Info("Starting reconciliation of ApiToken") token := &unleashv1.ApiToken{} @@ -321,7 +321,7 @@ func (r *ApiTokenReconciler) getUnleashInstance(ctx context.Context, token *unle // updateStatusSuccess will set the status condition as created and reset the failed status condition func (r *ApiTokenReconciler) updateStatusSuccess(ctx context.Context, apiToken *unleashv1.ApiToken) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("apitoken") log.Info("Successfully created ApiToken") apiTokenStatus.WithLabelValues(apiToken.Namespace, apiToken.Name, unleashv1.ApiTokenStatusConditionTypeCreated).Set(1.0) @@ -354,7 +354,7 @@ func (r *ApiTokenReconciler) updateStatusSuccess(ctx context.Context, apiToken * // updateStatusFailed will set the status condition as failed, but not reset the created status condition func (r *ApiTokenReconciler) updateStatusFailed(ctx context.Context, apiToken *unleashv1.ApiToken, err error, reason, message string) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("apitoken") if err != nil { log.Error(err, fmt.Sprintf("%s for ApiToken", message)) diff --git a/controllers/remoteunleash_controller.go b/controllers/remoteunleash_controller.go index 4a1990f1..854c02e0 100644 --- a/controllers/remoteunleash_controller.go +++ b/controllers/remoteunleash_controller.go @@ -72,7 +72,7 @@ type RemoteUnleashFederation struct { //+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch;create;update;patch;delete func (r *RemoteUnleashReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("remoteunleash") log.Info("Starting reconciliation of RemoteUnleash") remoteUnleash := &unleashv1.RemoteUnleash{} @@ -242,7 +242,7 @@ func (r *RemoteUnleashReconciler) Reconcile(ctx context.Context, req ctrl.Reques } func (r *RemoteUnleashReconciler) updateStatusConnectionSuccess(ctx context.Context, stats *unleashclient.InstanceAdminStatsResult, remoteUnleash *unleashv1.RemoteUnleash) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("remoteunleash") log.Info("Successfully connected to Unleash") return r.updateStatus(ctx, remoteUnleash, stats, metav1.Condition{ @@ -254,7 +254,7 @@ func (r *RemoteUnleashReconciler) updateStatusConnectionSuccess(ctx context.Cont } func (r *RemoteUnleashReconciler) updateStatusConnectionFailed(ctx context.Context, remoteUnleash *unleashv1.RemoteUnleash, stats *unleashclient.InstanceAdminStatsResult, err error, message string) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("remoteunleash") log.Error(err, fmt.Sprintf("%s for Unleash", message)) return r.updateStatus(ctx, remoteUnleash, stats, metav1.Condition{ @@ -266,7 +266,7 @@ func (r *RemoteUnleashReconciler) updateStatusConnectionFailed(ctx context.Conte } func (r *RemoteUnleashReconciler) updateStatusReconcileSuccess(ctx context.Context, remoteUnleash *unleashv1.RemoteUnleash, stats *unleashclient.InstanceAdminStatsResult) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("remoteunleash") log.Info("Successfully reconciled RemoteUnleash") return r.updateStatus(ctx, remoteUnleash, stats, metav1.Condition{ @@ -278,7 +278,7 @@ func (r *RemoteUnleashReconciler) updateStatusReconcileSuccess(ctx context.Conte } func (r *RemoteUnleashReconciler) updateStatusReconcileFailed(ctx context.Context, remoteUnleash *unleashv1.RemoteUnleash, stats *unleashclient.InstanceAdminStatsResult, err error, message string) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("remoteunleash") log.Error(err, fmt.Sprintf("%s for RemoteUnleash", message)) return r.updateStatus(ctx, remoteUnleash, stats, metav1.Condition{ @@ -290,7 +290,7 @@ func (r *RemoteUnleashReconciler) updateStatusReconcileFailed(ctx context.Contex } func (r *RemoteUnleashReconciler) updateStatus(ctx context.Context, remoteUnleash *unleashv1.RemoteUnleash, stats *unleashclient.InstanceAdminStatsResult, status metav1.Condition) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("remoteunleash") if err := r.Get(ctx, remoteUnleash.NamespacedName(), remoteUnleash); err != nil { log.Error(err, "Failed to get RemoteUnleash") @@ -329,7 +329,7 @@ func (r *RemoteUnleashReconciler) doFinalizerOperationsForToken(remoteUnleash *u } func (r *RemoteUnleashReconciler) FederationSubscribe(ctx context.Context) error { - log := log.FromContext(ctx).WithName("FederationSubscribe") + log := log.FromContext(ctx).WithName("subscribe") if !r.Federation.Enabled { log.Info("Federation is disabled, not consuming pubsub messages") diff --git a/controllers/unleash_controller.go b/controllers/unleash_controller.go index 0a75c4b4..4e0356d6 100644 --- a/controllers/unleash_controller.go +++ b/controllers/unleash_controller.go @@ -94,8 +94,7 @@ type UnleashFederation struct { //+kubebuilder:rbac:groups=monitoring.coreos.com,resources=servicemonitors,verbs=get;list;watch;create;update;patch;delete func (r *UnleashReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - log := log.FromContext(ctx) - + log := log.FromContext(ctx).WithName("unleash") log.Info("Starting reconciliation of Unleash") unleash := &unleashv1.Unleash{} @@ -366,7 +365,7 @@ func (r *UnleashReconciler) doFinalizerOperationsForUnleash(cr *unleashv1.Unleas // reconcileServiceMonitor will ensure that the required ServiceMonitor is created func (r *UnleashReconciler) reconcileServiceMonitor(ctx context.Context, unleash *unleashv1.Unleash) (ctrl.Result, error) { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("unleash") newServiceMonitor, err := resources.ServiceMonitorForUnleash(unleash, r.Scheme) if err != nil { @@ -421,7 +420,7 @@ func (r *UnleashReconciler) reconcileServiceMonitor(ctx context.Context, unleash // reconcileNetworkPolicy will ensure that the required network policy is created func (r *UnleashReconciler) reconcileNetworkPolicy(ctx context.Context, unleash *unleashv1.Unleash) (ctrl.Result, error) { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("unleash") newNetPol, err := resources.NetworkPolicyForUnleash(unleash, r.Scheme, r.OperatorNamespace) if err != nil { @@ -479,7 +478,7 @@ func (r *UnleashReconciler) reconcileNetworkPolicy(ctx context.Context, unleash // reconcileIngress will ensure that the required ingress is created func (r *UnleashReconciler) reconcileIngress(ctx context.Context, unleash *unleashv1.Unleash, ingress *unleashv1.UnleashIngressConfig, nameSuffix string) (ctrl.Result, error) { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("unleash") newIngress, err := resources.IngressForUnleash(unleash, ingress, nameSuffix, r.Scheme) if err != nil { @@ -552,7 +551,7 @@ func (r *UnleashReconciler) reconcileIngresses(ctx context.Context, unleash *unl // reconcileSecrets will ensure that the required secrets are created func (r *UnleashReconciler) reconcileSecrets(ctx context.Context, unleash *unleashv1.Unleash) (ctrl.Result, error) { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("unleash") // Check if operator secret already exists, if not create a new one operatorSecret := &corev1.Secret{} @@ -603,7 +602,7 @@ func (r *UnleashReconciler) reconcileSecrets(ctx context.Context, unleash *unlea // reconcileDeployment will ensure that the required deployment is created func (r *UnleashReconciler) reconcileDeployment(ctx context.Context, unleash *unleashv1.Unleash) (ctrl.Result, error) { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("unleash") dep, err := resources.DeploymentForUnleash(unleash, r.Scheme) if err != nil { @@ -641,7 +640,7 @@ func (r *UnleashReconciler) reconcileDeployment(ctx context.Context, unleash *un // reconcileService will ensure that the Service for the Unleash instance is created func (r *UnleashReconciler) reconcileService(ctx context.Context, unleash *unleashv1.Unleash) (ctrl.Result, error) { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("unleash") newSvc, err := resources.ServiceForUnleash(unleash, r.Scheme) if err != nil { @@ -710,7 +709,7 @@ func (r *UnleashReconciler) updateStatusReconcileSuccess(ctx context.Context, un } func (r *UnleashReconciler) updateStatusReconcileFailed(ctx context.Context, unleash *unleashv1.Unleash, err error, message string) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("unleash") if verr, ok := err.(*resources.ValidationError); ok { message = fmt.Sprintf("%s: %s", message, verr.Error()) @@ -735,7 +734,7 @@ func (r *UnleashReconciler) updateStatusConnectionSuccess(ctx context.Context, u } func (r *UnleashReconciler) updateStatusConnectionFailed(ctx context.Context, unleash *unleashv1.Unleash, err error, message string) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("unleash") log.Error(err, fmt.Sprintf("%s for Unleash", message)) return r.updateStatus(ctx, unleash, nil, metav1.Condition{ @@ -747,7 +746,7 @@ func (r *UnleashReconciler) updateStatusConnectionFailed(ctx context.Context, un } func (r *UnleashReconciler) updateStatus(ctx context.Context, unleash *unleashv1.Unleash, stats *unleashclient.InstanceAdminStatsResult, status metav1.Condition) error { - log := log.FromContext(ctx) + log := log.FromContext(ctx).WithName("unleash") switch status.Type { case unleashv1.UnleashStatusConditionTypeReconciled: diff --git a/pkg/federation/publisher.go b/pkg/federation/publisher.go index 149d5b44..e18bd3b2 100644 --- a/pkg/federation/publisher.go +++ b/pkg/federation/publisher.go @@ -32,7 +32,7 @@ func (p *publisher) Close() error { // Publish publishes the given Unleash instance to the federation topic using the provided API token. // Returns an error if the message could not be published. func (p *publisher) Publish(ctx context.Context, unleash *unleashv1.Unleash, apiToken string) error { - log := log.FromContext(ctx).WithName("publisher") + log := log.FromContext(ctx).WithName("publish") instance := UnleashFederationInstance(unleash, apiToken) payload, err := proto.Marshal(instance)