Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(logging): fix log level #53

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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()
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ spec:
args:
- --leader-elect
- --controller-domain=k8s.checklyhq.com
- --zap-log-level=info
image: controller:latest
name: manager
env:
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"
"fmt"

corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -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)

Expand All @@ -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
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}

Expand All @@ -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
}
Expand Down
20 changes: 11 additions & 9 deletions internal/controller/checkly/apicheck_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
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 @@ -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)

Expand All @@ -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")
Expand All @@ -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
Expand All @@ -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
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}

Expand Down
Loading