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

[v2] add ability to set controller log level #1760

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
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
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 = "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
}
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