Skip to content

Commit

Permalink
Allow log level to be configured through config
Browse files Browse the repository at this point in the history
  • Loading branch information
idlewis committed Nov 13, 2024
1 parent b0a45a1 commit b5cbdbc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"os"
"time"

uberzap "go.uber.org/zap"
"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -33,6 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/webhook"

appstacksv1 "github.com/application-stacks/runtime-component-operator/api/v1"
"github.com/application-stacks/runtime-component-operator/common"
"github.com/application-stacks/runtime-component-operator/internal/controller"
"github.com/application-stacks/runtime-component-operator/utils"
certmanagerv1 "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
Expand Down Expand Up @@ -80,7 +83,14 @@ func main() {

utils.CreateConfigMap(controller.OperatorName)

ctrl.SetLogger(zap.New(zap.UseDevMode(true)))
levelFunc := uberzap.LevelEnablerFunc(func(lvl zapcore.Level) bool {
return lvl >= common.Config.GetZapLogLevel()
})
opts := zap.Options{
Level: levelFunc,
Development: true,
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))

// see https://github.com/operator-framework/operator-sdk/issues/1813
leaseDuration := 30 * time.Second
Expand Down
40 changes: 40 additions & 0 deletions common/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package common

import (
"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
)

Expand All @@ -18,9 +19,24 @@ const (
// OpConfigCMCADuration default duration for cert-manager issued service certificate
OpConfigCMCertDuration = "certManagerCertDuration"

// OpConfigLogLevel the level of logs to be written
OpConfigLogLevel = "logLevel"

// The allowed values for OpConfigLogLevel
logLevelWarning = "warning"
logLevelInfo = "info"
logLevelDebug = "debug"
logLevelDebug2 = "debug2"

// Constants to use when fetching a debug level logger
LogLevelDebug = 1
LogLevelDebug2 = 2

// zap logging level constants
zLevelWarn zapcore.Level = 1
zLevelInfo zapcore.Level = 0
zLevelDebug zapcore.Level = -1
zLevelDebug2 zapcore.Level = -2
)

// Config stores operator configuration
Expand All @@ -37,11 +53,35 @@ func (oc OpConfig) LoadFromConfigMap(cm *corev1.ConfigMap) {
}
}

// Returns the zap log level corresponding to the value of the
// 'logLevel' key in the config map. Returns 'info' if they key
// is missing or contains an invalid value.
func (oc OpConfig) GetZapLogLevel() zapcore.Level {
level, ok := oc[OpConfigLogLevel]
if !ok {
return zLevelInfo
}
switch level {
case logLevelWarning:
return zLevelWarn
case logLevelInfo:
return zLevelInfo
case logLevelDebug:
return zLevelDebug
case logLevelDebug2:
return zLevelDebug2
default:
// config value is invalid.
return zLevelInfo
}
}

// DefaultOpConfig returns default configuration
func DefaultOpConfig() OpConfig {
cfg := OpConfig{}
cfg[OpConfigDefaultHostname] = ""
cfg[OpConfigCMCADuration] = "8766h"
cfg[OpConfigCMCertDuration] = "2160h"
cfg[OpConfigLogLevel] = logLevelInfo
return cfg
}

0 comments on commit b5cbdbc

Please sign in to comment.