Skip to content

Commit

Permalink
chore(logging): fix log level
Browse files Browse the repository at this point in the history
* Fix log levels across multiple log lines
* Add missing log lines
* Adjust log levels
* Update docs
* This addresses issue
  #50
  • Loading branch information
akosveres committed Nov 11, 2024
1 parent 5366d89 commit 0ef8fc5
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 31 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -63,7 +64,7 @@ func main() {
"Enable leader election for controller manager. "+
"Enabling this will ensure there is only one active controller manager.")
opts := zap.Options{
Development: true,
// Development: true,
}
opts.BindFlags(flag.CommandLine)
flag.Parse()
Expand Down Expand Up @@ -139,16 +140,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)
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 14 additions & 11 deletions internal/controller/checkly/alertchannel_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package checkly

import (
"context"
errs "errors"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -53,7 +54,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 := "k8s.checklyhq.com/finalizer"

Expand All @@ -67,7 +68,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
Expand All @@ -81,21 +82,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
}
Expand All @@ -110,7 +112,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
}

Expand All @@ -126,13 +128,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
}

Expand All @@ -152,13 +155,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
}

Expand All @@ -178,7 +181,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
}
Expand Down
21 changes: 12 additions & 9 deletions internal/controller/checkly/apicheck_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ type ApiCheckReconciler struct {
func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
logger := log.FromContext(ctx)

logger.V(1).Info("Reconciler started")

apiCheckFinalizer := "k8s.checklyhq.com/finalizer"

apiCheck := &checklyv1alpha1.ApiCheck{}
Expand All @@ -67,7 +69,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
Expand All @@ -77,7 +79,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")
Expand All @@ -89,15 +91,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
Expand All @@ -109,7 +112,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
}

Expand All @@ -121,7 +124,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
Expand All @@ -130,7 +133,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
}

Expand All @@ -155,7 +158,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 {
Expand Down Expand Up @@ -185,7 +188,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
}
Expand Down
17 changes: 9 additions & 8 deletions internal/controller/checkly/group_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,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 := "k8s.checklyhq.com/finalizer"

Expand All @@ -76,7 +76,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")
Expand All @@ -88,15 +88,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
Expand All @@ -108,7 +109,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
}

Expand All @@ -122,7 +123,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 {
Expand Down Expand Up @@ -153,13 +154,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
}

Expand Down

0 comments on commit 0ef8fc5

Please sign in to comment.