From d0d693e913d2e4dfc5ba3cbfd3a8629f1b5c5251 Mon Sep 17 00:00:00 2001 From: Akos Veres Date: Wed, 4 Dec 2024 22:28:20 +0000 Subject: [PATCH] chore(logging): fix log level (#53) * Fix log levels across multiple log lines * Add missing log lines * Adjust log levels * Update docs * This addresses issue https://github.com/checkly/checkly-operator/issues/50 --- Makefile | 2 +- cmd/main.go | 8 ++++-- config/manager/manager.yaml | 1 + docs/README.md | 1 + .../checkly/alertchannel_controller.go | 25 +++++++++++-------- .../controller/checkly/apicheck_controller.go | 20 ++++++++------- .../controller/checkly/group_controller.go | 17 +++++++------ 7 files changed, 43 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index 89fe51e..31122d8 100644 --- a/Makefile +++ b/Makefile @@ -118,7 +118,7 @@ build: manifests generate fmt vet ## Build manager binary. .PHONY: run run: manifests generate fmt vet ## Run a controller from your host. - go run ./cmd/main.go + go run ./cmd/main.go --zap-log-level=debug .PHONY: docker-build docker-build: test ## Build docker image with the manager. diff --git a/cmd/main.go b/cmd/main.go index 2523e9a..7a3393b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -23,6 +23,7 @@ import ( // Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.) // to ensure that exec-entrypoint and run can make use of them. + _ "k8s.io/client-go/plugin/pkg/client/auth" "k8s.io/apimachinery/pkg/runtime" @@ -65,7 +66,7 @@ func main() { "Enabling this will ensure there is only one active controller manager.") flag.StringVar(&controllerDomain, "controller-domain", "k8s.checklyhq.com", "Domain to use for annotations and finalizers.") opts := zap.Options{ - Development: true, + // Development: true, } opts.BindFlags(flag.CommandLine) flag.Parse() @@ -147,16 +148,19 @@ func main() { } //+kubebuilder:scaffold:builder + setupLog.V(1).Info("starting health endpoint") if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil { setupLog.Error(err, "unable to set up health check") os.Exit(1) } + + setupLog.V(1).Info("starting ready endpoint") if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil { setupLog.Error(err, "unable to set up ready check") os.Exit(1) } - setupLog.Info("starting manager") + setupLog.V(1).Info("starting manager") if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil { setupLog.Error(err, "problem running manager") os.Exit(1) diff --git a/config/manager/manager.yaml b/config/manager/manager.yaml index 123c1ac..5686606 100644 --- a/config/manager/manager.yaml +++ b/config/manager/manager.yaml @@ -33,6 +33,7 @@ spec: args: - --leader-elect - --controller-domain=k8s.checklyhq.com + - --zap-log-level=info image: controller:latest name: manager env: diff --git a/docs/README.md b/docs/README.md index adb6fe2..e693197 100644 --- a/docs/README.md +++ b/docs/README.md @@ -47,6 +47,7 @@ unset CHECKLY_OPERATOR_RELEASE Feel free to edit the `install.yaml` file to your liking, usually you'd want to change: * checkly-operator deployment replica count * checkly-operator deployment CPU and Memory resources +* log levels via the `--zap-log-level` CLI options, valid options are `debug`, `info`, `error` You can apply the `install.yaml`, this will create the namespace, we need this to create the secrets in the next step: ```bash diff --git a/internal/controller/checkly/alertchannel_controller.go b/internal/controller/checkly/alertchannel_controller.go index 22105e7..2521159 100644 --- a/internal/controller/checkly/alertchannel_controller.go +++ b/internal/controller/checkly/alertchannel_controller.go @@ -18,6 +18,7 @@ package checkly import ( "context" + errs "errors" "fmt" corev1 "k8s.io/api/core/v1" @@ -55,7 +56,7 @@ type AlertChannelReconciler struct { func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { logger := log.FromContext(ctx) - logger.Info("Reconciler started") + logger.V(1).Info("Reconciler started") acFinalizer := fmt.Sprintf("%s/finalizer", r.ControllerDomain) @@ -69,7 +70,7 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request if err != nil { if errors.IsNotFound(err) { // The resource has been deleted - logger.Info("Deleted", "checkly AlertChannel ID", ac.Status.ID) + logger.V(1).Info("Deleted", "checkly AlertChannel ID", ac.Status.ID) return ctrl.Result{}, nil } // Error reading the object @@ -83,21 +84,22 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request if ac.GetDeletionTimestamp() != nil { if controllerutil.ContainsFinalizer(ac, acFinalizer) { - logger.Info("Finalizer is present, trying to delete Checkly AlertChannel", "ID", ac.Status.ID) + logger.V(1).Info("Finalizer is present, trying to delete Checkly AlertChannel", "ID", ac.Status.ID) err := external.DeleteAlertChannel(ac, r.ApiClient) if err != nil { logger.Error(err, "Failed to delete checkly AlertChannel") return ctrl.Result{}, err } - logger.Info("Successfully deleted checkly AlertChannel", "ID", ac.Status.ID) + logger.V(1).Info("Successfully deleted checkly AlertChannel", "ID", ac.Status.ID) controllerutil.RemoveFinalizer(ac, acFinalizer) err = r.Update(ctx, ac) if err != nil { + logger.Error(err, "Failed to delete finalizer.") return ctrl.Result{}, err } - logger.Info("Successfully deleted finalizer from AlertChannel") + logger.V(1).Info("Successfully deleted finalizer from AlertChannel") } return ctrl.Result{}, nil } @@ -112,7 +114,7 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request logger.Error(err, "Failed to update AlertChannel status") return ctrl.Result{}, err } - logger.Info("Added finalizer", "checkly AlertChannel ID", ac.Status.ID) + logger.V(1).Info("Added finalizer", "checkly AlertChannel ID", ac.Status.ID) return ctrl.Result{}, nil } @@ -128,13 +130,14 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request Namespace: ac.Spec.OpsGenie.APISecret.Namespace}, secret) if err != nil { - logger.Info("Unable to read secret for API Key", "err", err) + logger.Error(err, "Unable to read secret for API Key") return ctrl.Result{}, err } secretValue := string(secret.Data[ac.Spec.OpsGenie.APISecret.FieldPath]) if secretValue == "" { - logger.Info("Secret value is empty") + secretErr := errs.New("secret value is empty") + logger.Error(secretErr, "Please add Opsgenie secret") return ctrl.Result{}, err } @@ -154,13 +157,13 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request // Determine if it's a new object or if it's an update to an existing object if ac.Status.ID != 0 { // Existing object, we need to update it - logger.Info("Existing object, with ID", "checkly AlertChannel ID", ac.Status.ID) + logger.V(1).Info("Existing object, with ID", "checkly AlertChannel ID", ac.Status.ID) err := external.UpdateAlertChannel(ac, opsGenieConfig, r.ApiClient) if err != nil { logger.Error(err, "Failed to update checkly AlertChannel") return ctrl.Result{}, err } - logger.Info("Updated checkly AlertChannel", "ID", ac.Status.ID) + logger.V(1).Info("Updated checkly AlertChannel", "ID", ac.Status.ID) return ctrl.Result{}, nil } @@ -180,7 +183,7 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request logger.Error(err, "Failed to update AlertChannel status", "ID", ac.Status.ID) return ctrl.Result{}, err } - logger.Info("New checkly AlertChannel created", "ID", ac.Status.ID) + logger.V(1).Info("New checkly AlertChannel created", "ID", ac.Status.ID) return ctrl.Result{}, nil } diff --git a/internal/controller/checkly/apicheck_controller.go b/internal/controller/checkly/apicheck_controller.go index db34136..47099fe 100644 --- a/internal/controller/checkly/apicheck_controller.go +++ b/internal/controller/checkly/apicheck_controller.go @@ -59,6 +59,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c logger := log.FromContext(ctx) apiCheckFinalizer := fmt.Sprintf("%s/finalizer", r.ControllerDomain) + logger.V(1).Info("Reconciler started") apiCheck := &checklyv1alpha1.ApiCheck{} @@ -69,7 +70,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c if err != nil { if errors.IsNotFound(err) { // The resource has been deleted - logger.Info("Deleted", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint, "name", apiCheck.Name) + logger.V(1).Info("Deleted", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint, "name", apiCheck.Name) return ctrl.Result{}, nil } // Error reading the object @@ -79,7 +80,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c if apiCheck.GetDeletionTimestamp() != nil { if controllerutil.ContainsFinalizer(apiCheck, apiCheckFinalizer) { - logger.Info("Finalizer is present, trying to delete Checkly check", "checkly ID", apiCheck.Status.ID) + logger.V(1).Info("Finalizer is present, trying to delete Checkly check", "checkly ID", apiCheck.Status.ID) err := external.Delete(apiCheck.Status.ID, r.ApiClient) if err != nil { logger.Error(err, "Failed to delete checkly API check") @@ -91,15 +92,16 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c controllerutil.RemoveFinalizer(apiCheck, apiCheckFinalizer) err = r.Update(ctx, apiCheck) if err != nil { + logger.Error(err, "Failed to delete finalizer") return ctrl.Result{}, err } - logger.Info("Successfully deleted finalizer") + logger.V(1).Info("Successfully deleted finalizer") } return ctrl.Result{}, nil } // Object found, let's do something with it. It's either updated, or it's new. - logger.Info("Object found", "endpoint", apiCheck.Spec.Endpoint) + logger.V(1).Info("Object found", "endpoint", apiCheck.Spec.Endpoint) // ///////////////////////////// // Finalizer logic @@ -111,7 +113,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c logger.Error(err, "Failed to update ApiCheck status") return ctrl.Result{}, err } - logger.Info("Added finalizer", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint) + logger.V(1).Info("Added finalizer", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint) return ctrl.Result{}, nil } @@ -123,7 +125,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c if err != nil { if errors.IsNotFound(err) { // The resource has been deleted - logger.Info("Group not found, probably deleted or does not exist", "name", apiCheck.Spec.Group) + logger.Error(err, "Group not found, probably deleted or does not exist", "name", apiCheck.Spec.Group) return ctrl.Result{}, err } // Error reading the object @@ -132,7 +134,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c } if group.Status.ID == 0 { - logger.Info("Group ID has not been populated, we're too quick, requeining for retry", "group name", apiCheck.Spec.Group) + logger.V(1).Info("Group ID has not been populated, we're too quick, requeining for retry", "group name", apiCheck.Spec.Group) return ctrl.Result{Requeue: true}, nil } @@ -157,7 +159,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c // Determine if it's a new object or if it's an update to an existing object if apiCheck.Status.ID != "" { // Existing object, we need to update it - logger.Info("Existing object, with ID", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint) + logger.V(1).Info("Existing object, with ID", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint) err := external.Update(internalCheck, r.ApiClient) // err := if err != nil { @@ -187,7 +189,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c logger.Error(err, "Failed to update ApiCheck status") return ctrl.Result{}, err } - logger.Info("New checkly check created with", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint) + logger.V(1).Info("New checkly check created with", "checkly ID", apiCheck.Status.ID, "spec", apiCheck.Spec) return ctrl.Result{}, nil } diff --git a/internal/controller/checkly/group_controller.go b/internal/controller/checkly/group_controller.go index 30c5089..c51f27a 100644 --- a/internal/controller/checkly/group_controller.go +++ b/internal/controller/checkly/group_controller.go @@ -53,7 +53,7 @@ type GroupReconciler struct { func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { logger := log.FromContext(ctx) - logger.Info("Reconciler started") + logger.V(1).Info("Reconciler started") groupFinalizer := fmt.Sprintf("%s/finalizer", r.ControllerDomain) @@ -78,7 +78,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl // If DeletionTimestamp is present, the object is marked for deletion, we need to remove the finalizer if group.GetDeletionTimestamp() != nil { if controllerutil.ContainsFinalizer(group, groupFinalizer) { - logger.Info("Finalizer is present, trying to delete Checkly group", "checkly group ID", group.Status.ID) + logger.V(1).Info("Finalizer is present, trying to delete Checkly group", "checkly group ID", group.Status.ID) err := external.GroupDelete(group.Status.ID, r.ApiClient) if err != nil { logger.Error(err, "Failed to delete checkly group") @@ -90,15 +90,16 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl controllerutil.RemoveFinalizer(group, groupFinalizer) err = r.Update(ctx, group) if err != nil { + logger.Error(err, "Failed to delete finalizer") return ctrl.Result{}, err } - logger.Info("Successfully deleted finalizer") + logger.V(1).Info("Successfully deleted finalizer") } return ctrl.Result{}, nil } // Object found, let's do something with it. It's either updated, or it's new. - logger.Info("Checkly group found") + logger.V(1).Info("Checkly group found") // ///////////////////////////// // Finalizer logic @@ -110,7 +111,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl logger.Error(err, "Failed to add Group finalizer") return ctrl.Result{}, err } - logger.Info("Added finalizer", "checkly group ID", group.Status.ID) + logger.V(1).Info("Added finalizer", "checkly group ID", group.Status.ID) return ctrl.Result{}, nil } @@ -124,7 +125,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl ac := &checklyv1alpha1.AlertChannel{} err := r.Get(ctx, types.NamespacedName{Name: alertChannel}, ac) if err != nil { - logger.Info("Could not find alertChannel resource", "name", alertChannel) + logger.Error(err, "Could not find alertChannel resource", "name", alertChannel) return ctrl.Result{}, err } if ac.Status.ID == 0 { @@ -155,13 +156,13 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl // Determine if it's a new object or if it's an update to an existing object if group.Status.ID != 0 { // Existing object, we need to update it - logger.Info("Existing object, with ID", "checkly group ID", group.Status.ID) + logger.V(1).Info("Existing object, with ID", "checkly group ID", group.Status.ID) err := external.GroupUpdate(internalCheck, r.ApiClient) if err != nil { logger.Error(err, "Failed to update the checkly group") return ctrl.Result{}, err } - logger.Info("Updated checkly check", "checkly group ID", group.Status.ID) + logger.V(1).Info("Updated checkly check", "checkly group ID", group.Status.ID) return ctrl.Result{}, nil }