diff --git a/pkg/build/build.go b/pkg/build/build.go index 80a248bc..5ddad064 100644 --- a/pkg/build/build.go +++ b/pkg/build/build.go @@ -113,13 +113,13 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error { return err } - setupLog.Info("Getting Kube config") + setupLog.V(1).Info("Getting Kube config") kubeConfig, err := b.GetKubeConfig() if err != nil { return err } - setupLog.Info("Getting Kube client") + setupLog.V(1).Info("Getting Kube client") kubeClient, err := b.GetKubeClient(kubeConfig) if err != nil { return err @@ -130,7 +130,7 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error { return err } - setupLog.Info("Creating controller manager") + setupLog.V(1).Info("Creating controller manager") // Create controller manager mgr, err := ctrl.NewManager(kubeConfig, ctrl.Options{ Scheme: b.scheme, @@ -143,7 +143,7 @@ func (b *Build) Run(ctx context.Context, recreateCluster bool) error { return err } - setupLog.Info("Running controllers") + setupLog.V(1).Info("Running controllers") if err := b.RunControllers(ctx, mgr, managerExit); err != nil { setupLog.Error(err, "Error running controllers") return err diff --git a/pkg/cmd/helpers/logger.go b/pkg/cmd/helpers/logger.go index 7fcb1dc6..cdac64c0 100644 --- a/pkg/cmd/helpers/logger.go +++ b/pkg/cmd/helpers/logger.go @@ -21,9 +21,13 @@ func SetLogger() error { if err != nil { return err } + slogger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: l})) + kslogger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: getKlogLevel(l)})) logger := logr.FromSlogHandler(slogger.Handler()) - klog.SetLogger(logger) + klogger := logr.FromSlogHandler(kslogger.Handler()) + + klog.SetLogger(klogger) ctrl.SetLogger(logger) return nil } @@ -42,3 +46,11 @@ func getSlogLevel(s string) (slog.Level, error) { return slog.LevelDebug, fmt.Errorf("%s is not a valid log level", s) } } + +// For end users, klog messages are mostly useless. We set it to error level unless debug logging is enabled. +func getKlogLevel(l slog.Level) slog.Level { + if l < slog.LevelInfo { + return l + } + return slog.LevelError +} diff --git a/pkg/controllers/crd.go b/pkg/controllers/crd.go index c9ae9792..ca7e2771 100644 --- a/pkg/controllers/crd.go +++ b/pkg/controllers/crd.go @@ -29,7 +29,7 @@ func getK8sResources(scheme *runtime.Scheme, templateData any) ([]client.Object, } func EnsureCRD(ctx context.Context, scheme *runtime.Scheme, kubeClient client.Client, obj client.Object) error { - log := log.FromContext(ctx) + logger := log.FromContext(ctx) // Check if the CRD already exists crd, ok := obj.(*apiextensionsv1.CustomResourceDefinition) @@ -45,16 +45,16 @@ func EnsureCRD(ctx context.Context, scheme *runtime.Scheme, kubeClient client.Cl switch { case apierrors.IsNotFound(err): if err := kubeClient.Create(ctx, obj); err != nil { - log.Error(err, "Unable to create CRD", "resource", obj) + logger.Error(err, "Unable to create CRD", "resource", obj) return err } case err != nil: - log.Error(err, "Unable to get CRD during initial check", "resource", obj) + logger.Error(err, "Unable to get CRD during initial check", "resource", obj) return err default: crd.SetResourceVersion(curCRD.GetResourceVersion()) if err = kubeClient.Update(ctx, crd); err != nil { - log.Error(err, "Updating CRD", "resource", obj) + logger.Error(err, "Updating CRD", "resource", obj) return err } } @@ -66,7 +66,7 @@ func EnsureCRD(ctx context.Context, scheme *runtime.Scheme, kubeClient client.Cl types.NamespacedName{Name: obj.GetName(), Namespace: "default"}, &curCRD, ); err != nil { - log.Error(err, "Failed to get CRD", "crd name", obj.GetName()) + logger.Error(err, "Failed to get CRD", "crd name", obj.GetName()) return err } crdEstablished := false @@ -80,7 +80,7 @@ func EnsureCRD(ctx context.Context, scheme *runtime.Scheme, kubeClient client.Cl if crdEstablished { break } else { - log.Info("crd not yet established, waiting.", "crd name", obj.GetName()) + logger.V(1).Info("crd not yet established, waiting.", "crd name", obj.GetName()) } time.Sleep(time.Duration(time.Duration.Milliseconds(500))) } diff --git a/pkg/controllers/custompackage/controller.go b/pkg/controllers/custompackage/controller.go index ebf0a44f..6368b155 100644 --- a/pkg/controllers/custompackage/controller.go +++ b/pkg/controllers/custompackage/controller.go @@ -43,7 +43,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu return ctrl.Result{}, client.IgnoreNotFound(err) } - logger.Info("reconciling custom package", "name", req.Name, "namespace", req.Namespace) + logger.V(1).Info("reconciling custom package", "name", req.Name, "namespace", req.Namespace) defer r.postProcessReconcile(ctx, req, &pkg) result, err := r.reconcileCustomPackage(ctx, &pkg) if err != nil { diff --git a/pkg/controllers/custompackage/controller_test.go b/pkg/controllers/custompackage/controller_test.go index 0627bb95..d7096e39 100644 --- a/pkg/controllers/custompackage/controller_test.go +++ b/pkg/controllers/custompackage/controller_test.go @@ -151,7 +151,6 @@ func TestReconcileCustomPkg(t *testing.T) { } } time.Sleep(1 * time.Second) - // verify repo. c := mgr.GetClient() repo := v1alpha1.GitRepository{ diff --git a/pkg/controllers/gitrepository/controller.go b/pkg/controllers/gitrepository/controller.go index df7aeb44..c8e06f20 100644 --- a/pkg/controllers/gitrepository/controller.go +++ b/pkg/controllers/gitrepository/controller.go @@ -103,7 +103,7 @@ func (r *RepositoryReconciler) Reconcile(ctx context.Context, req ctrl.Request) return ctrl.Result{Requeue: false}, nil } - logger.Info("reconciling GitRepository", "name", req.Name, "namespace", req.Namespace) + logger.V(1).Info("reconciling GitRepository", "name", req.Name, "namespace", req.Namespace) result, err := r.reconcileGitRepo(ctx, &gitRepo) if err != nil { r.Recorder.Event(&gitRepo, "Warning", "reconcile error", err.Error()) @@ -130,7 +130,7 @@ func (r *RepositoryReconciler) postProcessReconcile(ctx context.Context, req ctr func (r *RepositoryReconciler) reconcileGitRepo(ctx context.Context, repo *v1alpha1.GitRepository) (ctrl.Result, error) { logger := log.FromContext(ctx) - logger.Info("reconciling", "name", repo.Name, "dir", repo.Spec.Source) + logger.V(1).Info("reconciling", "name", repo.Name, "dir", repo.Spec.Source) repo.Status.Synced = false tr := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, diff --git a/pkg/controllers/localbuild/controller.go b/pkg/controllers/localbuild/controller.go index 2e84cdef..97aed648 100644 --- a/pkg/controllers/localbuild/controller.go +++ b/pkg/controllers/localbuild/controller.go @@ -43,12 +43,12 @@ type LocalbuildReconciler struct { type subReconciler func(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) func (r *LocalbuildReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) { - log := log.FromContext(ctx) - log.Info("Reconciling", "resource", req.NamespacedName) + logger := log.FromContext(ctx) + logger.V(1).Info("Reconciling", "resource", req.NamespacedName) var localBuild v1alpha1.Localbuild if err := r.Get(ctx, req.NamespacedName, &localBuild); err != nil { - log.Error(err, "unable to fetch Resource") + logger.Error(err, "unable to fetch Resource") // we'll ignore not-found errors, since they can't be fixed by an immediate // requeue (we'll need to wait for a new notification), and we can get them // on deleted requests. @@ -94,7 +94,7 @@ func (r *LocalbuildReconciler) postProcessReconcile(ctx context.Context, req ctr } func (r *LocalbuildReconciler) ReconcileProjectNamespace(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild) (ctrl.Result, error) { - log := log.FromContext(ctx) + logger := log.FromContext(ctx) nsResource := &corev1.Namespace{ ObjectMeta: metav1.ObjectMeta{ @@ -102,16 +102,16 @@ func (r *LocalbuildReconciler) ReconcileProjectNamespace(ctx context.Context, re }, } - log.Info("Create or update namespace", "resource", nsResource) + logger.V(1).Info("Create or update namespace", "resource", nsResource) _, err := controllerutil.CreateOrUpdate(ctx, r.Client, nsResource, func() error { if err := controllerutil.SetControllerReference(resource, nsResource, r.Scheme); err != nil { - log.Error(err, "Setting controller ref on namespace resource") + logger.Error(err, "Setting controller ref on namespace resource") return err } return nil }) if err != nil { - log.Error(err, "Create or update namespace resource") + logger.Error(err, "Create or update namespace resource") } return ctrl.Result{}, err } @@ -157,7 +157,7 @@ func (r *LocalbuildReconciler) ReconcileArgoAppsWithGitea(ctx context.Context, r func (r *LocalbuildReconciler) reconcileEmbeddedApp(ctx context.Context, appName string, resource *v1alpha1.Localbuild) (ctrl.Result, error) { logger := log.FromContext(ctx) - logger.Info("Ensuring embedded ArgoCD Application", "name", appName) + logger.V(1).Info("Ensuring embedded ArgoCD Application", "name", appName) repo, err := r.reconcileGitRepo(ctx, resource, "embedded", appName, appName, "") if err != nil { diff --git a/pkg/controllers/localbuild/installer.go b/pkg/controllers/localbuild/installer.go index 94017d6c..761609d2 100644 --- a/pkg/controllers/localbuild/installer.go +++ b/pkg/controllers/localbuild/installer.go @@ -61,7 +61,7 @@ func (e *EmbeddedInstallation) newNamespace(namespace string) *corev1.Namespace } func (e *EmbeddedInstallation) Install(ctx context.Context, req ctrl.Request, resource *v1alpha1.Localbuild, cli client.Client, sc *runtime.Scheme, cfg util.CorePackageTemplateConfig) (ctrl.Result, error) { - log := log.FromContext(ctx) + logger := log.FromContext(ctx) nsClient := client.NewNamespacedClient(cli, e.namespace) installObjs, err := e.installResources(sc, cfg) @@ -78,7 +78,7 @@ func (e *EmbeddedInstallation) Install(ctx context.Context, req ctrl.Request, re } } - log.Info(fmt.Sprintf("Installing/Reconciling %s resources", e.name)) + logger.V(1).Info(fmt.Sprintf("Installing/Reconciling %s resources", e.name)) for _, obj := range installObjs { if gvk, ok := e.monitoredResources[obj.GetName()]; ok { if obj.GetObjectKind().GroupVersionKind() == gvk { @@ -88,7 +88,7 @@ func (e *EmbeddedInstallation) Install(ctx context.Context, req ctrl.Request, re if gotObj, ok := gvkObj.(client.Object); ok { if err := cli.Get(ctx, types.NamespacedName{Namespace: e.namespace, Name: obj.GetName()}, gotObj); err != nil { if err = controllerutil.SetControllerReference(resource, obj, sc); err != nil { - log.Error(err, "Setting controller reference for deployment", obj.GetName(), obj) + logger.Error(err, "Setting controller reference for deployment", obj.GetName(), obj) return ctrl.Result{}, err } } @@ -140,18 +140,18 @@ func (e *EmbeddedInstallation) Install(ctx context.Context, req ctrl.Request, re switch t := gotObj.(type) { case *appsv1.Deployment: if t.Status.AvailableReplicas >= 1 { - log.Info(t.GetName(), "deployment", t.Status.AvailableReplicas) + logger.V(1).Info(t.GetName(), "deployment", t.Status.AvailableReplicas) return } case *appsv1.StatefulSet: if t.Status.AvailableReplicas >= 1 { - log.Info(t.GetName(), "statefulset", t.Status.AvailableReplicas) + logger.V(1).Info(t.GetName(), "statefulset", t.Status.AvailableReplicas) return } } } - log.Info(fmt.Sprintf("Waiting for %s %s to become ready", gvk.Kind, obj.GetName())) + logger.Info(fmt.Sprintf("Waiting for %s %s to become ready", gvk.Kind, obj.GetName())) time.Sleep(30 * time.Second) } }(obj, gvk) @@ -166,13 +166,13 @@ func (e *EmbeddedInstallation) Install(ctx context.Context, req ctrl.Request, re select { case <-timeout: err := errors.New("Timeout") - log.Error(err, fmt.Sprintf("Didn't reconcile %s on time", e.name)) + logger.Error(err, fmt.Sprintf("Didn't reconcile %s on time", e.name)) return ctrl.Result{}, err case err, errOccurred := <-errCh: if !errOccurred { - log.Info(fmt.Sprintf("%s is ready!", e.name)) + logger.V(1).Info(fmt.Sprintf("%s is ready!", e.name)) } else { - log.Error(err, fmt.Sprintf("failed to reconcile the %s resources", e.name)) + logger.Error(err, fmt.Sprintf("failed to reconcile the %s resources", e.name)) return ctrl.Result{}, err } } diff --git a/pkg/controllers/run.go b/pkg/controllers/run.go index 6fa88cc2..3f60cfd0 100644 --- a/pkg/controllers/run.go +++ b/pkg/controllers/run.go @@ -13,7 +13,7 @@ import ( ) func RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error, ctxCancel context.CancelFunc, exitOnSync bool, cfg util.CorePackageTemplateConfig) error { - log := log.FromContext(ctx) + logger := log.FromContext(ctx) // Run Localbuild controller if err := (&localbuild.LocalbuildReconciler{ @@ -23,7 +23,7 @@ func RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error, CancelFunc: ctxCancel, Config: cfg, }).SetupWithManager(mgr); err != nil { - log.Error(err, "unable to create localbuild controller") + logger.Error(err, "unable to create localbuild controller") return err } @@ -35,7 +35,7 @@ func RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error, Config: cfg, }).SetupWithManager(mgr, nil) if err != nil { - log.Error(err, "unable to create repo controller") + logger.Error(err, "unable to create repo controller") } err = (&custompackage.Reconciler{ @@ -44,14 +44,14 @@ func RunControllers(ctx context.Context, mgr manager.Manager, exitCh chan error, Recorder: mgr.GetEventRecorderFor("custompackage-controller"), }).SetupWithManager(mgr) if err != nil { - log.Error(err, "unable to create custom package controller") + logger.Error(err, "unable to create custom package controller") } // Start our manager in another goroutine - log.Info("starting manager") + logger.V(1).Info("starting manager") go func() { if err := mgr.Start(ctx); err != nil { - log.Error(err, "problem running manager") + logger.Error(err, "problem running manager") exitCh <- err } exitCh <- nil