Skip to content

Commit 0ef8fc5

Browse files
committed
chore(logging): fix log level
* Fix log levels across multiple log lines * Add missing log lines * Adjust log levels * Update docs * This addresses issue #50
1 parent 5366d89 commit 0ef8fc5

File tree

6 files changed

+43
-31
lines changed

6 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"
@@ -63,7 +64,7 @@ func main() {
6364
"Enable leader election for controller manager. "+
6465
"Enabling this will ensure there is only one active controller manager.")
6566
opts := zap.Options{
66-
Development: true,
67+
// Development: true,
6768
}
6869
opts.BindFlags(flag.CommandLine)
6970
flag.Parse()
@@ -139,16 +140,19 @@ func main() {
139140
}
140141
//+kubebuilder:scaffold:builder
141142

143+
setupLog.V(1).Info("starting health endpoint")
142144
if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
143145
setupLog.Error(err, "unable to set up health check")
144146
os.Exit(1)
145147
}
148+
149+
setupLog.V(1).Info("starting ready endpoint")
146150
if err := mgr.AddReadyzCheck("readyz", healthz.Ping); err != nil {
147151
setupLog.Error(err, "unable to set up ready check")
148152
os.Exit(1)
149153
}
150154

151-
setupLog.Info("starting manager")
155+
setupLog.V(1).Info("starting manager")
152156
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
153157
setupLog.Error(err, "problem running manager")
154158
os.Exit(1)

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

2223
corev1 "k8s.io/api/core/v1"
2324
"k8s.io/apimachinery/pkg/api/errors"
@@ -53,7 +54,7 @@ type AlertChannelReconciler struct {
5354
func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
5455
logger := log.FromContext(ctx)
5556

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

5859
acFinalizer := "k8s.checklyhq.com/finalizer"
5960

@@ -67,7 +68,7 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
6768
if err != nil {
6869
if errors.IsNotFound(err) {
6970
// The resource has been deleted
70-
logger.Info("Deleted", "checkly AlertChannel ID", ac.Status.ID)
71+
logger.V(1).Info("Deleted", "checkly AlertChannel ID", ac.Status.ID)
7172
return ctrl.Result{}, nil
7273
}
7374
// Error reading the object
@@ -81,21 +82,22 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
8182

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

91-
logger.Info("Successfully deleted checkly AlertChannel", "ID", ac.Status.ID)
92+
logger.V(1).Info("Successfully deleted checkly AlertChannel", "ID", ac.Status.ID)
9293

9394
controllerutil.RemoveFinalizer(ac, acFinalizer)
9495
err = r.Update(ctx, ac)
9596
if err != nil {
97+
logger.Error(err, "Failed to delete finalizer.")
9698
return ctrl.Result{}, err
9799
}
98-
logger.Info("Successfully deleted finalizer from AlertChannel")
100+
logger.V(1).Info("Successfully deleted finalizer from AlertChannel")
99101
}
100102
return ctrl.Result{}, nil
101103
}
@@ -110,7 +112,7 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
110112
logger.Error(err, "Failed to update AlertChannel status")
111113
return ctrl.Result{}, err
112114
}
113-
logger.Info("Added finalizer", "checkly AlertChannel ID", ac.Status.ID)
115+
logger.V(1).Info("Added finalizer", "checkly AlertChannel ID", ac.Status.ID)
114116
return ctrl.Result{}, nil
115117
}
116118

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

133135
secretValue := string(secret.Data[ac.Spec.OpsGenie.APISecret.FieldPath])
134136
if secretValue == "" {
135-
logger.Info("Secret value is empty")
137+
secretErr := errs.New("secret value is empty")
138+
logger.Error(secretErr, "Please add Opsgenie secret")
136139
return ctrl.Result{}, err
137140
}
138141

@@ -152,13 +155,13 @@ func (r *AlertChannelReconciler) Reconcile(ctx context.Context, req ctrl.Request
152155
// Determine if it's a new object or if it's an update to an existing object
153156
if ac.Status.ID != 0 {
154157
// Existing object, we need to update it
155-
logger.Info("Existing object, with ID", "checkly AlertChannel ID", ac.Status.ID)
158+
logger.V(1).Info("Existing object, with ID", "checkly AlertChannel ID", ac.Status.ID)
156159
err := external.UpdateAlertChannel(ac, opsGenieConfig, r.ApiClient)
157160
if err != nil {
158161
logger.Error(err, "Failed to update checkly AlertChannel")
159162
return ctrl.Result{}, err
160163
}
161-
logger.Info("Updated checkly AlertChannel", "ID", ac.Status.ID)
164+
logger.V(1).Info("Updated checkly AlertChannel", "ID", ac.Status.ID)
162165
return ctrl.Result{}, nil
163166
}
164167

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

183186
return ctrl.Result{}, nil
184187
}

internal/controller/checkly/apicheck_controller.go

+12-9
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ type ApiCheckReconciler struct {
5656
func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
5757
logger := log.FromContext(ctx)
5858

59+
logger.V(1).Info("Reconciler started")
60+
5961
apiCheckFinalizer := "k8s.checklyhq.com/finalizer"
6062

6163
apiCheck := &checklyv1alpha1.ApiCheck{}
@@ -67,7 +69,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
6769
if err != nil {
6870
if errors.IsNotFound(err) {
6971
// The resource has been deleted
70-
logger.Info("Deleted", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint, "name", apiCheck.Name)
72+
logger.V(1).Info("Deleted", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint, "name", apiCheck.Name)
7173
return ctrl.Result{}, nil
7274
}
7375
// Error reading the object
@@ -77,7 +79,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
7779

7880
if apiCheck.GetDeletionTimestamp() != nil {
7981
if controllerutil.ContainsFinalizer(apiCheck, apiCheckFinalizer) {
80-
logger.Info("Finalizer is present, trying to delete Checkly check", "checkly ID", apiCheck.Status.ID)
82+
logger.V(1).Info("Finalizer is present, trying to delete Checkly check", "checkly ID", apiCheck.Status.ID)
8183
err := external.Delete(apiCheck.Status.ID, r.ApiClient)
8284
if err != nil {
8385
logger.Error(err, "Failed to delete checkly API check")
@@ -89,15 +91,16 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
8991
controllerutil.RemoveFinalizer(apiCheck, apiCheckFinalizer)
9092
err = r.Update(ctx, apiCheck)
9193
if err != nil {
94+
logger.Error(err, "Failed to delete finalizer")
9295
return ctrl.Result{}, err
9396
}
94-
logger.Info("Successfully deleted finalizer")
97+
logger.V(1).Info("Successfully deleted finalizer")
9598
}
9699
return ctrl.Result{}, nil
97100
}
98101

99102
// Object found, let's do something with it. It's either updated, or it's new.
100-
logger.Info("Object found", "endpoint", apiCheck.Spec.Endpoint)
103+
logger.V(1).Info("Object found", "endpoint", apiCheck.Spec.Endpoint)
101104

102105
// /////////////////////////////
103106
// Finalizer logic
@@ -109,7 +112,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
109112
logger.Error(err, "Failed to update ApiCheck status")
110113
return ctrl.Result{}, err
111114
}
112-
logger.Info("Added finalizer", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
115+
logger.V(1).Info("Added finalizer", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
113116
return ctrl.Result{}, nil
114117
}
115118

@@ -121,7 +124,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
121124
if err != nil {
122125
if errors.IsNotFound(err) {
123126
// The resource has been deleted
124-
logger.Info("Group not found, probably deleted or does not exist", "name", apiCheck.Spec.Group)
127+
logger.Error(err, "Group not found, probably deleted or does not exist", "name", apiCheck.Spec.Group)
125128
return ctrl.Result{}, err
126129
}
127130
// Error reading the object
@@ -130,7 +133,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
130133
}
131134

132135
if group.Status.ID == 0 {
133-
logger.Info("Group ID has not been populated, we're too quick, requeining for retry", "group name", apiCheck.Spec.Group)
136+
logger.V(1).Info("Group ID has not been populated, we're too quick, requeining for retry", "group name", apiCheck.Spec.Group)
134137
return ctrl.Result{Requeue: true}, nil
135138
}
136139

@@ -155,7 +158,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
155158
// Determine if it's a new object or if it's an update to an existing object
156159
if apiCheck.Status.ID != "" {
157160
// Existing object, we need to update it
158-
logger.Info("Existing object, with ID", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
161+
logger.V(1).Info("Existing object, with ID", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
159162
err := external.Update(internalCheck, r.ApiClient)
160163
// err :=
161164
if err != nil {
@@ -185,7 +188,7 @@ func (r *ApiCheckReconciler) Reconcile(ctx context.Context, req ctrl.Request) (c
185188
logger.Error(err, "Failed to update ApiCheck status")
186189
return ctrl.Result{}, err
187190
}
188-
logger.Info("New checkly check created with", "checkly ID", apiCheck.Status.ID, "endpoint", apiCheck.Spec.Endpoint)
191+
logger.V(1).Info("New checkly check created with", "checkly ID", apiCheck.Status.ID, "spec", apiCheck.Spec)
189192

190193
return ctrl.Result{}, nil
191194
}

internal/controller/checkly/group_controller.go

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

54-
logger.Info("Reconciler started")
54+
logger.V(1).Info("Reconciler started")
5555

5656
groupFinalizer := "k8s.checklyhq.com/finalizer"
5757

@@ -76,7 +76,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
7676
// If DeletionTimestamp is present, the object is marked for deletion, we need to remove the finalizer
7777
if group.GetDeletionTimestamp() != nil {
7878
if controllerutil.ContainsFinalizer(group, groupFinalizer) {
79-
logger.Info("Finalizer is present, trying to delete Checkly group", "checkly group ID", group.Status.ID)
79+
logger.V(1).Info("Finalizer is present, trying to delete Checkly group", "checkly group ID", group.Status.ID)
8080
err := external.GroupDelete(group.Status.ID, r.ApiClient)
8181
if err != nil {
8282
logger.Error(err, "Failed to delete checkly group")
@@ -88,15 +88,16 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
8888
controllerutil.RemoveFinalizer(group, groupFinalizer)
8989
err = r.Update(ctx, group)
9090
if err != nil {
91+
logger.Error(err, "Failed to delete finalizer")
9192
return ctrl.Result{}, err
9293
}
93-
logger.Info("Successfully deleted finalizer")
94+
logger.V(1).Info("Successfully deleted finalizer")
9495
}
9596
return ctrl.Result{}, nil
9697
}
9798

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

101102
// /////////////////////////////
102103
// Finalizer logic
@@ -108,7 +109,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
108109
logger.Error(err, "Failed to add Group finalizer")
109110
return ctrl.Result{}, err
110111
}
111-
logger.Info("Added finalizer", "checkly group ID", group.Status.ID)
112+
logger.V(1).Info("Added finalizer", "checkly group ID", group.Status.ID)
112113
return ctrl.Result{}, nil
113114
}
114115

@@ -122,7 +123,7 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
122123
ac := &checklyv1alpha1.AlertChannel{}
123124
err := r.Get(ctx, types.NamespacedName{Name: alertChannel}, ac)
124125
if err != nil {
125-
logger.Info("Could not find alertChannel resource", "name", alertChannel)
126+
logger.Error(err, "Could not find alertChannel resource", "name", alertChannel)
126127
return ctrl.Result{}, err
127128
}
128129
if ac.Status.ID == 0 {
@@ -153,13 +154,13 @@ func (r *GroupReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
153154
// Determine if it's a new object or if it's an update to an existing object
154155
if group.Status.ID != 0 {
155156
// Existing object, we need to update it
156-
logger.Info("Existing object, with ID", "checkly group ID", group.Status.ID)
157+
logger.V(1).Info("Existing object, with ID", "checkly group ID", group.Status.ID)
157158
err := external.GroupUpdate(internalCheck, r.ApiClient)
158159
if err != nil {
159160
logger.Error(err, "Failed to update the checkly group")
160161
return ctrl.Result{}, err
161162
}
162-
logger.Info("Updated checkly check", "checkly group ID", group.Status.ID)
163+
logger.V(1).Info("Updated checkly check", "checkly group ID", group.Status.ID)
163164
return ctrl.Result{}, nil
164165
}
165166

0 commit comments

Comments
 (0)