Skip to content

Commit

Permalink
[v2] add ability to set controller log level
Browse files Browse the repository at this point in the history
Controller log level can be configured via a configmap.

Fixes #1698
  • Loading branch information
Karen-Schoener committed Oct 31, 2024
1 parent 83605ed commit b7b4050
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
41 changes: 41 additions & 0 deletions cmd/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"
"log"
"log/slog"
"os"
"strings"

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

Expand Down Expand Up @@ -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)
}
log.Printf("Updating log level to %s", slogLevel)
return nil
}
4 changes: 3 additions & 1 deletion cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit b7b4050

Please sign in to comment.