Skip to content

Commit

Permalink
reduce log messages (#158)
Browse files Browse the repository at this point in the history
Signed-off-by: Manabu McCloskey <[email protected]>
  • Loading branch information
nabuskey authored Mar 12, 2024
1 parent 95ca015 commit db35227
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 38 deletions.
8 changes: 4 additions & 4 deletions pkg/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion pkg/cmd/helpers/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
12 changes: 6 additions & 6 deletions pkg/controllers/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
}
Expand All @@ -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
Expand All @@ -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)))
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/custompackage/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion pkg/controllers/custompackage/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ func TestReconcileCustomPkg(t *testing.T) {
}
}
time.Sleep(1 * time.Second)

// verify repo.
c := mgr.GetClient()
repo := v1alpha1.GitRepository{
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/gitrepository/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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},
Expand Down
16 changes: 8 additions & 8 deletions pkg/controllers/localbuild/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -94,24 +94,24 @@ 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{
Name: globals.GetProjectNamespace(resource.Name),
},
}

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
}
Expand Down Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions pkg/controllers/localbuild/installer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
}
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/controllers/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand All @@ -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
}

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

0 comments on commit db35227

Please sign in to comment.