Skip to content

Commit d0d693e

Browse files
authored
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 #50
1 parent b308dea commit d0d693e

File tree

7 files changed

+43
-31
lines changed

7 files changed

+43
-31
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ build: manifests generate fmt vet ## Build manager binary.
118118

119119
.PHONY: run
120120
run: manifests generate fmt vet ## Run a controller from your host.
121-
go run ./cmd/main.go
121+
go run ./cmd/main.go --zap-log-level=debug
122122

123123
.PHONY: docker-build
124124
docker-build: test ## Build docker image with the manager.

cmd/main.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2525
// to ensure that exec-entrypoint and run can make use of them.
26+
2627
_ "k8s.io/client-go/plugin/pkg/client/auth"
2728

2829
"k8s.io/apimachinery/pkg/runtime"
@@ -65,7 +66,7 @@ func main() {
6566
"Enabling this will ensure there is only one active controller manager.")
6667
flag.StringVar(&controllerDomain, "controller-domain", "k8s.checklyhq.com", "Domain to use for annotations and finalizers.")
6768
opts := zap.Options{
68-
Development: true,
69+
// Development: true,
6970
}
7071
opts.BindFlags(flag.CommandLine)
7172
flag.Parse()
@@ -147,16 +148,19 @@ func main() {
147148
}
148149
//+kubebuilder:scaffold:builder
149150

151+
setupLog.V(1).Info("starting health endpoint")
150152
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
151153
setupLog.Error(err, "unable to set up health check")
152154
os.Exit(1)
153155
}
156+
157+
setupLog.V(1).Info("starting ready endpoint")
154158
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
155159
setupLog.Error(err, "unable to set up ready check")
156160
os.Exit(1)
157161
}
158162

159-
setupLog.Info("starting manager")
163+
setupLog.V(1).Info("starting manager")
160164
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
161165
setupLog.Error(err, "problem running manager")
162166
os.Exit(1)

config/manager/manager.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ spec:
3333
args:
3434
- --leader-elect
3535
- --controller-domain=k8s.checklyhq.com
36+
- --zap-log-level=info
3637
image: controller:latest
3738
name: manager
3839
env:

docs/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ unset CHECKLY_OPERATOR_RELEASE
4747
Feel free to edit the `install.yaml` file to your liking, usually you'd want to change:
4848
* checkly-operator deployment replica count
4949
* checkly-operator deployment CPU and Memory resources
50+
* log levels via the `--zap-log-level` CLI options, valid options are `debug`, `info`, `error`
5051

5152
You can apply the `install.yaml`, this will create the namespace, we need this to create the secrets in the next step:
5253
```bash

internal/controller/checkly/alertchannel_controller.go

+14-11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package checkly
1818

1919
import (
2020
"context"
21+
errs "errors"
2122
"fmt"
2223

2324
corev1 "k8s.io/api/core/v1"
@@ -55,7 +56,7 @@ type AlertChannelReconciler struct {
5556
func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
5657
logger := log.FromContext(ctx)
5758

58-
logger.Info("Reconciler started")
59+
logger.V(1).Info("Reconciler started")
5960

6061
acFinalizer := fmt.Sprintf("%s/finalizer", r.ControllerDomain)
6162

@@ -69,7 +70,7 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
6970
if err != nil {
7071
if errors.IsNotFound(err) {
7172
// The resource has been deleted
72-
logger.Info("Deleted", "checkly AlertChannel ID", ac.Status.ID)
73+
logger.V(1).Info("Deleted", "checkly AlertChannel ID", ac.Status.ID)
7374
return ctrl.Result{}, nil
7475
}
7576
// Error reading the object
@@ -83,21 +84,22 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
8384

8485
if ac.GetDeletionTimestamp() != nil {
8586
if controllerutil.ContainsFinalizer(ac, acFinalizer) {
86-
logger.Info("Finalizer is present, trying to delete Checkly AlertChannel", "ID", ac.Status.ID)
87+
logger.V(1).Info("Finalizer is present, trying to delete Checkly AlertChannel", "ID", ac.Status.ID)
8788
err := external.DeleteAlertChannel(ac, r.ApiClient)
8889
if err != nil {
8990
logger.Error(err, "Failed to delete checkly AlertChannel")
9091
return ctrl.Result{}, err
9192
}
9293

93-
logger.Info("Successfully deleted checkly AlertChannel", "ID", ac.Status.ID)
94+
logger.V(1).Info("Successfully deleted checkly AlertChannel", "ID", ac.Status.ID)
9495

9596
controllerutil.RemoveFinalizer(ac, acFinalizer)
9697
err = r.Update(ctx, ac)
9798
if err != nil {
99+
logger.Error(err, "Failed to delete finalizer.")
98100
return ctrl.Result{}, err
99101
}
100-
logger.Info("Successfully deleted finalizer from AlertChannel")
102+
logger.V(1).Info("Successfully deleted finalizer from AlertChannel")
101103
}
102104
return ctrl.Result{}, nil
103105
}
@@ -112,7 +114,7 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
112114
logger.Error(err, "Failed to update AlertChannel status")
113115
return ctrl.Result{}, err
114116
}
115-
logger.Info("Added finalizer", "checkly AlertChannel ID", ac.Status.ID)
117+
logger.V(1).Info("Added finalizer", "checkly AlertChannel ID", ac.Status.ID)
116118
return ctrl.Result{}, nil
117119
}
118120

@@ -128,13 +130,14 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
128130
Namespace: ac.Spec.OpsGenie.APISecret.Namespace},
129131
secret)
130132
if err != nil {
131-
logger.Info("Unable to read secret for API Key", "err", err)
133+
logger.Error(err, "Unable to read secret for API Key")
132134
return ctrl.Result{}, err
133135
}
134136

135137
secretValue := string(secret.Data[ac.Spec.OpsGenie.APISecret.FieldPath])
136138
if secretValue == "" {
137-
logger.Info("Secret value is empty")
139+
secretErr := errs.New("secret value is empty")
140+
logger.Error(secretErr, "Please add Opsgenie secret")
138141
return ctrl.Result{}, err
139142
}
140143

@@ -154,13 +157,13 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
154157
// Determine if it's a new object or if it's an update to an existing object
155158
if ac.Status.ID != 0 {
156159
// Existing object, we need to update it
157-
logger.Info("Existing object, with ID", "checkly AlertChannel ID", ac.Status.ID)
160+
logger.V(1).Info("Existing object, with ID", "checkly AlertChannel ID", ac.Status.ID)
158161
err := external.UpdateAlertChannel(ac, opsGenieConfig, r.ApiClient)
159162
if err != nil {
160163
logger.Error(err, "Failed to update checkly AlertChannel")
161164
return ctrl.Result{}, err
162165
}
163-
logger.Info("Updated checkly AlertChannel", "ID", ac.Status.ID)
166+
logger.V(1).Info("Updated checkly AlertChannel", "ID", ac.Status.ID)
164167
return ctrl.Result{}, nil
165168
}
166169

@@ -180,7 +183,7 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
180183
logger.Error(err, "Failed to update AlertChannel status", "ID", ac.Status.ID)
181184
return ctrl.Result{}, err
182185
}
183-
logger.Info("New checkly AlertChannel created", "ID", ac.Status.ID)
186+
logger.V(1).Info("New checkly AlertChannel created", "ID", ac.Status.ID)
184187

185188
return ctrl.Result{}, nil
186189
}

internal/controller/checkly/apicheck_controller.go

+11-9
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
5959
logger := log.FromContext(ctx)
6060

6161
apiCheckFinalizer := fmt.Sprintf("%s/finalizer", r.ControllerDomain)
62+
logger.V(1).Info("Reconciler started")
6263

6364
apiCheck := &checklyv1alpha1.ApiCheck{}
6465

@@ -69,7 +70,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
6970
if err != nil {
7071
if errors.IsNotFound(err) {
7172
// The resource has been deleted
72-
logger.Info("Deleted", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint, "name", apiCheck.Name)
73+
logger.V(1).Info("Deleted", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint, "name", apiCheck.Name)
7374
return ctrl.Result{}, nil
7475
}
7576
// Error reading the object
@@ -79,7 +80,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
7980

8081
if apiCheck.GetDeletionTimestamp() != nil {
8182
if controllerutil.ContainsFinalizer(apiCheck, apiCheckFinalizer) {
82-
logger.Info("Finalizer is present, trying to delete Checkly check", "checkly ID", apiCheck.Status.ID)
83+
logger.V(1).Info("Finalizer is present, trying to delete Checkly check", "checkly ID", apiCheck.Status.ID)
8384
err := external.Delete(apiCheck.Status.ID, r.ApiClient)
8485
if err != nil {
8586
logger.Error(err, "Failed to delete checkly API check")
@@ -91,15 +92,16 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
9192
controllerutil.RemoveFinalizer(apiCheck, apiCheckFinalizer)
9293
err = r.Update(ctx, apiCheck)
9394
if err != nil {
95+
logger.Error(err, "Failed to delete finalizer")
9496
return ctrl.Result{}, err
9597
}
96-
logger.Info("Successfully deleted finalizer")
98+
logger.V(1).Info("Successfully deleted finalizer")
9799
}
98100
return ctrl.Result{}, nil
99101
}
100102

101103
// Object found, let's do something with it. It's either updated, or it's new.
102-
logger.Info("Object found", "endpoint", apiCheck.Spec.Endpoint)
104+
logger.V(1).Info("Object found", "endpoint", apiCheck.Spec.Endpoint)
103105

104106
// /////////////////////////////
105107
// Finalizer logic
@@ -111,7 +113,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
111113
logger.Error(err, "Failed to update ApiCheck status")
112114
return ctrl.Result{}, err
113115
}
114-
logger.Info("Added finalizer", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
116+
logger.V(1).Info("Added finalizer", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
115117
return ctrl.Result{}, nil
116118
}
117119

@@ -123,7 +125,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
123125
if err != nil {
124126
if errors.IsNotFound(err) {
125127
// The resource has been deleted
126-
logger.Info("Group not found, probably deleted or does not exist", "name", apiCheck.Spec.Group)
128+
logger.Error(err, "Group not found, probably deleted or does not exist", "name", apiCheck.Spec.Group)
127129
return ctrl.Result{}, err
128130
}
129131
// Error reading the object
@@ -132,7 +134,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
132134
}
133135

134136
if group.Status.ID == 0 {
135-
logger.Info("Group ID has not been populated, we're too quick, requeining for retry", "group name", apiCheck.Spec.Group)
137+
logger.V(1).Info("Group ID has not been populated, we're too quick, requeining for retry", "group name", apiCheck.Spec.Group)
136138
return ctrl.Result{Requeue: true}, nil
137139
}
138140

@@ -157,7 +159,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
157159
// Determine if it's a new object or if it's an update to an existing object
158160
if apiCheck.Status.ID != "" {
159161
// Existing object, we need to update it
160-
logger.Info("Existing object, with ID", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
162+
logger.V(1).Info("Existing object, with ID", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
161163
err := external.Update(internalCheck, r.ApiClient)
162164
// err :=
163165
if err != nil {
@@ -187,7 +189,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
187189
logger.Error(err, "Failed to update ApiCheck status")
188190
return ctrl.Result{}, err
189191
}
190-
logger.Info("New checkly check created with", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
192+
logger.V(1).Info("New checkly check created with", "checkly ID", apiCheck.Status.ID, "spec", apiCheck.Spec)
191193

192194
return ctrl.Result{}, nil
193195
}

internal/controller/checkly/group_controller.go

+9-8
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type GroupReconciler struct {
5353
func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
5454
logger := log.FromContext(ctx)
5555

56-
logger.Info("Reconciler started")
56+
logger.V(1).Info("Reconciler started")
5757

5858
groupFinalizer := fmt.Sprintf("%s/finalizer", r.ControllerDomain)
5959

@@ -78,7 +78,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
7878
// If DeletionTimestamp is present, the object is marked for deletion, we need to remove the finalizer
7979
if group.GetDeletionTimestamp() != nil {
8080
if controllerutil.ContainsFinalizer(group, groupFinalizer) {
81-
logger.Info("Finalizer is present, trying to delete Checkly group", "checkly group ID", group.Status.ID)
81+
logger.V(1).Info("Finalizer is present, trying to delete Checkly group", "checkly group ID", group.Status.ID)
8282
err := external.GroupDelete(group.Status.ID, r.ApiClient)
8383
if err != nil {
8484
logger.Error(err, "Failed to delete checkly group")
@@ -90,15 +90,16 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
9090
controllerutil.RemoveFinalizer(group, groupFinalizer)
9191
err = r.Update(ctx, group)
9292
if err != nil {
93+
logger.Error(err, "Failed to delete finalizer")
9394
return ctrl.Result{}, err
9495
}
95-
logger.Info("Successfully deleted finalizer")
96+
logger.V(1).Info("Successfully deleted finalizer")
9697
}
9798
return ctrl.Result{}, nil
9899
}
99100

100101
// Object found, let's do something with it. It's either updated, or it's new.
101-
logger.Info("Checkly group found")
102+
logger.V(1).Info("Checkly group found")
102103

103104
// /////////////////////////////
104105
// Finalizer logic
@@ -110,7 +111,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
110111
logger.Error(err, "Failed to add Group finalizer")
111112
return ctrl.Result{}, err
112113
}
113-
logger.Info("Added finalizer", "checkly group ID", group.Status.ID)
114+
logger.V(1).Info("Added finalizer", "checkly group ID", group.Status.ID)
114115
return ctrl.Result{}, nil
115116
}
116117

@@ -124,7 +125,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
124125
ac := &checklyv1alpha1.AlertChannel{}
125126
err := r.Get(ctx, types.NamespacedName{Name: alertChannel}, ac)
126127
if err != nil {
127-
logger.Info("Could not find alertChannel resource", "name", alertChannel)
128+
logger.Error(err, "Could not find alertChannel resource", "name", alertChannel)
128129
return ctrl.Result{}, err
129130
}
130131
if ac.Status.ID == 0 {
@@ -155,13 +156,13 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
155156
// Determine if it's a new object or if it's an update to an existing object
156157
if group.Status.ID != 0 {
157158
// Existing object, we need to update it
158-
logger.Info("Existing object, with ID", "checkly group ID", group.Status.ID)
159+
logger.V(1).Info("Existing object, with ID", "checkly group ID", group.Status.ID)
159160
err := external.GroupUpdate(internalCheck, r.ApiClient)
160161
if err != nil {
161162
logger.Error(err, "Failed to update the checkly group")
162163
return ctrl.Result{}, err
163164
}
164-
logger.Info("Updated checkly check", "checkly group ID", group.Status.ID)
165+
logger.V(1).Info("Updated checkly check", "checkly group ID", group.Status.ID)
165166
return ctrl.Result{}, nil
166167
}
167168

0 commit comments

Comments
 (0)