diff --git a/logging/config.go b/logging/config.go index cdcc81b62..628fbdb7d 100644 --- a/logging/config.go +++ b/logging/config.go @@ -96,14 +96,15 @@ type Config struct { LoggingLevel map[string]zapcore.Level } -// NewConfigFromMap creates a LoggingConfig from the supplied map -func NewConfigFromMap(data map[string]string) (*Config, error) { +// NewConfigFromMap creates a LoggingConfig from the supplied map, +// expecting the given list of components. +func NewConfigFromMap(data map[string]string, components ...string) (*Config, error) { lc := &Config{} if zlc, ok := data["zap-logger-config"]; ok { lc.LoggingConfig = zlc } lc.LoggingLevel = make(map[string]zapcore.Level) - for _, component := range []string{"controller", "queueproxy", "webhook", "activator", "autoscaler"} { + for _, component := range components { if ll, ok := data["loglevel."+component]; ok { if len(ll) > 0 { level, err := levelFromString(ll) @@ -117,9 +118,10 @@ func NewConfigFromMap(data map[string]string) (*Config, error) { return lc, nil } -// NewConfigFromConfigMap creates a LoggingConfig from the supplied ConfigMap -func NewConfigFromConfigMap(configMap *corev1.ConfigMap) (*Config, error) { - return NewConfigFromMap(configMap.Data) +// NewConfigFromConfigMap creates a LoggingConfig from the supplied ConfigMap, +// expecting the given list of components. +func NewConfigFromConfigMap(configMap *corev1.ConfigMap, components ...string) (*Config, error) { + return NewConfigFromMap(configMap.Data, components...) } func levelFromString(level string) (*zapcore.Level, error) { @@ -132,9 +134,10 @@ func levelFromString(level string) (*zapcore.Level, error) { // UpdateLevelFromConfigMap returns a helper func that can be used to update the logging level // when a config map is updated -func UpdateLevelFromConfigMap(logger *zap.SugaredLogger, atomicLevel zap.AtomicLevel, levelKey string) func(configMap *corev1.ConfigMap) { +func UpdateLevelFromConfigMap(logger *zap.SugaredLogger, atomicLevel zap.AtomicLevel, + levelKey string, components ...string) func(configMap *corev1.ConfigMap) { return func(configMap *corev1.ConfigMap) { - loggingConfig, err := NewConfigFromConfigMap(configMap) + loggingConfig, err := NewConfigFromConfigMap(configMap, components...) if err != nil { logger.Error("Failed to parse the logging configmap. Previous config map will be used.", zap.Error(err)) return diff --git a/logging/config_test.go b/logging/config_test.go index c0443995f..96f40c053 100644 --- a/logging/config_test.go +++ b/logging/config_test.go @@ -191,7 +191,7 @@ func TestInvalidLevel(t *testing.T) { "zap-logger-config": wantCfg, "loglevel.queueproxy": "invalid", }, - }) + }, "queueproxy") if err == nil { t.Errorf("Expected errors. got nothing") } @@ -209,7 +209,7 @@ func getTestConfig() (*Config, string, string) { "zap-logger-config": wantCfg, "loglevel.queueproxy": wantLevel, }, - }) + }, "queueproxy") return c, wantCfg, wantLevel } @@ -242,7 +242,7 @@ func TestUpdateLevelFromConfigMap(t *testing.T) { {"debug", zapcore.DebugLevel}, } - u := UpdateLevelFromConfigMap(logger, atomicLevel, "controller") + u := UpdateLevelFromConfigMap(logger, atomicLevel, "controller", "controller") for _, tt := range tests { cm.Data["loglevel.controller"] = tt.setLevel u(cm)