From f7768f2573f19d058ac871228aa9433ecc91aef4 Mon Sep 17 00:00:00 2001 From: Karen Schoener Date: Wed, 30 Oct 2024 10:52:56 -0700 Subject: [PATCH] [v2] add ability to set controller log level Controller log level can be configured via a configmap. Fixes #1698 --- cmd/controller/controller.go | 41 ++++++++++++++++++++++++++++++++++++ cmd/controller/main.go | 4 +++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/cmd/controller/controller.go b/cmd/controller/controller.go index f9d24d9a0..fb146aecb 100644 --- a/cmd/controller/controller.go +++ b/cmd/controller/controller.go @@ -5,6 +5,7 @@ import ( "fmt" "io" "log" + "log/slog" "os" "strings" @@ -92,6 +93,8 @@ func NewController(cli internalclient.Clients, grantConfig *grants.GrantConfig, controller.startGrantServer = grants.Initialise(controller.controller, currentNamespace, watchNamespace, grantConfig, controller.generateLinkConfig) + controller.controller.WatchConfigMaps(skupperLogConfig(), currentNamespace, controller.logConfigUpdate) + return controller, nil } @@ -348,3 +351,41 @@ func extractSiteRecords(status network.NetworkStatusInfo) []skupperv2alpha1.Site } return records } + +func skupperLogConfig() internalinterfaces.TweakListOptionsFunc { + return func(options *metav1.ListOptions) { + options.FieldSelector = "metadata.name=skupper-log-config" + } +} + +func convertLogLevel(logLevel string) slog.Level { + switch strings.ToLower(logLevel) { + case "debug": + return slog.LevelDebug + case "info": + return slog.LevelInfo + case "warn": + return slog.LevelWarn + case "error": + return slog.LevelError + } + return slog.LevelInfo +} + +func (c *Controller) logConfigUpdate(key string, cm *corev1.ConfigMap) error { + const controllerLogLevelKey = "SKUPPER_CONTROLLER_LOG_LEVEL" + var slogLevel slog.Level + if cm == nil { + // if configmap is deleted, then set log level to info + slogLevel = slog.LevelInfo + } else { + logLevel := cm.Data[controllerLogLevelKey] + slogLevel = convertLogLevel(logLevel) + } + + if slogLevel != controllerLogLevel.Level() { + controllerLogLevel.Set(slogLevel) + } + slog.Info("Updating log level", slog.String("logLevel", slogLevel.String())) + return nil +} diff --git a/cmd/controller/main.go b/cmd/controller/main.go index 0abb595b4..80c3d3d97 100644 --- a/cmd/controller/main.go +++ b/cmd/controller/main.go @@ -18,8 +18,10 @@ import ( "github.com/skupperproject/skupper/pkg/version" ) +var controllerLogLevel = new(slog.LevelVar) // defaults to Info + func init() { - logger := slog.New(slog.NewTextHandler(os.Stdout, nil)) + logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: controllerLogLevel})) slog.SetDefault(logger) }