Skip to content

Commit 2877f53

Browse files
Merge pull request #642 from Kuadrant/log_level_env_var
fix: wrapped zap log level and mode with custom flags.
2 parents bc2c180 + 3963f9e commit 2877f53

File tree

8 files changed

+54
-13
lines changed

8 files changed

+54
-13
lines changed

CLAUDE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ Follow these guidelines when working with logs:
245245
- `logger.Error()` - Errors not returned in reconciliation result (one error message only)
246246
- `logger.V(1).Info()` - Debug logs (every change/event/update)
247247

248-
Use `--zap-devel` flag to enable debug level logs.
248+
Use `--log-mode=development` flag to enable debug level logs.
249249

250250
Common log metadata:
251251
- `DNSRecord` - Name/namespace of DNSRecord being reconciled

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ build: manifests generate fmt vet ## Build manager binary.
290290
RUN_METRICS_ADDR=":8080"
291291
RUN_HEALTH_ADDR=":8081"
292292
RUN_DELEGATION_ROLE="primary"
293-
DEFAULT_RUN_FLAGS ?= --zap-devel --provider inmemory,aws,google,azure,coredns,endpoint --delegation-role=${RUN_DELEGATION_ROLE} --metrics-bind-address=${RUN_METRICS_ADDR} --health-probe-bind-address=${RUN_HEALTH_ADDR}
293+
DEFAULT_RUN_FLAGS ?= --log-mode=development --provider inmemory,aws,google,azure,coredns,endpoint --delegation-role=${RUN_DELEGATION_ROLE} --metrics-bind-address=${RUN_METRICS_ADDR} --health-probe-bind-address=${RUN_HEALTH_ADDR}
294294
RUN_FLAGS ?= $(DEFAULT_RUN_FLAGS)
295295

296296
.PHONY: run
@@ -310,7 +310,7 @@ run-secondary: ## Run a controller from your host with the secondary delegation
310310
run-with-probes: GIT_SHA=$(shell git rev-parse HEAD || echo "unknown")
311311
run-with-probes: DIRTY=$(shell hack/check-git-dirty.sh || echo "unknown")
312312
run-with-probes: manifests generate fmt vet ## Run a controller from your host.
313-
go run -ldflags "-X main.version=v${VERSION} -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" --race ./cmd/main.go --zap-devel --provider inmemory,aws,google,azure
313+
go run -ldflags "-X main.version=v${VERSION} -X main.gitSHA=${GIT_SHA} -X main.dirty=${DIRTY}" --race ./cmd/main.go --log-mode=development --provider inmemory,aws,google,azure
314314

315315
# If you wish built the manager image targeting other platforms you can use the --platform flag.
316316
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,15 @@ Logs are following the general guidelines:
141141
- `logger.Error()` describe only those errors that are not returned in the result of the reconciliation. If error is occurred there should be only one error message.
142142
- `logger.V(1).Info()` debug level logs to give information about every change or event caused by the resource as well as every update of the resource.
143143

144-
The `--zap-devel` argument will enable debug level logs for the output. Otherwise, all `V()` logs are ignored.
144+
There are two flags to control logging output
145+
- `--log-mode=[development|<any-other-value>]` will enable debug level logs for the output.
146+
The debug mode is the most verbose.
147+
- `--log-level` controls the level of displayed logs. Defaults to the most verbose in the `development` mode.
148+
In any other modes it can take numerical values form `-1` (Debug level) to `4` (Nothing).
149+
It is possible to specify other values, but hey will have no effect (e.g. `4` will do the same as `128`)
150+
151+
You can find more [here](https://pkg.go.dev/github.com/go-logr/zapr#section-readme).
152+
145153

146154
### Common metadata
147155
Not exhaustive list of metadata for DNSRecord controller:

cmd/main.go

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ import (
2727
"strings"
2828
"time"
2929

30+
"go.uber.org/zap/zapcore"
31+
3032
"k8s.io/apimachinery/pkg/util/runtime"
3133
"k8s.io/client-go/dynamic"
3234
"k8s.io/client-go/kubernetes/scheme"
3335
_ "k8s.io/client-go/plugin/pkg/client/auth"
3436
ctrl "sigs.k8s.io/controller-runtime"
3537
"sigs.k8s.io/controller-runtime/pkg/cache"
3638
"sigs.k8s.io/controller-runtime/pkg/healthz"
37-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
39+
zap "sigs.k8s.io/controller-runtime/pkg/log/zap"
3840
"sigs.k8s.io/controller-runtime/pkg/metrics"
3941
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
4042
"sigs.k8s.io/controller-runtime/pkg/webhook"
@@ -78,6 +80,8 @@ var (
7880
watchNamespaces string
7981
delegationRole string
8082
group types.Group
83+
logMode string
84+
logLevel string
8185

8286
// represents booth flag and envar key
8387
metricsAddrKey = variableKey("metrics-bind-address")
@@ -94,6 +98,8 @@ var (
9498
watchNamespacesKey = variableKey("watch-namespaces")
9599
delegationRoleKey = variableKey("delegation-role")
96100
groupKey = variableKey("group")
101+
logModeKey = variableKey("log-mode")
102+
logLevelKey = variableKey("log-level")
97103
)
98104

99105
const (
@@ -103,6 +109,9 @@ const (
103109

104110
DefaultClusterSecretNamespace = "dns-operator-system"
105111
DefaultClusterSecretLabel = "kuadrant.io/multicluster-kubeconfig"
112+
113+
DefaultLogLevel = zapcore.InfoLevel
114+
DefaultLogMode = "debug"
106115
)
107116

108117
func init() {
@@ -137,16 +146,16 @@ func main() {
137146
flag.StringVar(&clusterSecretNamespace, clusterSecretNamespaceKey.Flag(), DefaultClusterSecretNamespace, "The Namespace to look for cluster secrets.")
138147
flag.StringVar(&clusterSecretLabel, clusterSecretLabelKey.Flag(), DefaultClusterSecretLabel, "The label that identifies a Secret resource as a cluster secret.")
139148
flag.StringVar(&watchNamespaces, watchNamespacesKey.Flag(), "", "Comma separated list of default namespaces.")
149+
flag.StringVar(&logLevel, logLevelKey.Flag(), DefaultLogLevel.String(), "Log level")
150+
flag.StringVar(&logMode, logModeKey.Flag(), DefaultLogMode, "Log mode")
140151

141152
flag.Var(newDelegationRoleValue(controller.DelegationRolePrimary, &delegationRole), delegationRoleKey.Flag(), "The delegation role for this controller. Must be one of 'primary'(default), or 'secondary'")
142153

143-
flag.Var(&group, groupKey.Flag(), "Set Group for dns-operater")
154+
flag.Var(&group, groupKey.Flag(), "Set Group for dns-operator")
144155

145-
opts := zap.Options{}
146-
opts.BindFlags(flag.CommandLine)
147156
flag.Parse()
148157

149-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
158+
ctrl.SetLogger(zap.New(withLogLevel(logLevel), withLogMode(logMode)))
150159

151160
printControllerMetaInfo()
152161
overrideControllerFlags()
@@ -392,6 +401,12 @@ func overrideControllerFlags() {
392401
os.Exit(1)
393402
}
394403
setupLog.Info(fmt.Sprintf("overriding %s flag with \"%s\" value", groupKey.Flag(), v))
404+
case logModeKey.Envar():
405+
logMode = v
406+
setupLog.Info(fmt.Sprintf("overriding %s flag with \"%s\" value", logModeKey.Flag(), v))
407+
case logLevelKey.Envar():
408+
logLevel = v
409+
setupLog.Info(fmt.Sprintf("overriding %s flag with \"%s\" value", logLevelKey.Flag(), v))
395410
}
396411
}
397412
}
@@ -448,3 +463,21 @@ func newDelegationRoleValue(val string, p *string) *delegationRoleValue {
448463
*p = val
449464
return (*delegationRoleValue)(p)
450465
}
466+
467+
func withLogLevel(logLevel string) func(*zap.Options) {
468+
lvl, err := zapcore.ParseLevel(logLevel)
469+
if err != nil {
470+
// If unable to parse the log level, set default
471+
lvl = DefaultLogLevel
472+
}
473+
474+
return func(options *zap.Options) {
475+
options.Level = lvl
476+
}
477+
}
478+
479+
func withLogMode(logMode string) func(*zap.Options) {
480+
return func(options *zap.Options) {
481+
options.Development = logMode == "development"
482+
}
483+
}

config/deploy/local/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ patches:
1313
value: --provider=aws,google,inmemory,azure
1414
- op: add
1515
path: /spec/template/spec/containers/0/args/-
16-
value: --zap-log-level=debug
16+
value: --log-level=debug
1717
target:
1818
kind: Deployment
1919
- path: manager_config_patch.yaml

config/deploy/local/manager_config_patch.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ spec:
1313
- --metrics-bind-address=:8080
1414
- --leader-elect
1515
- --provider=aws,google,inmemory,azure,coredns,endpoint
16-
- --zap-log-level=debug
16+
- --log-level=debug

test/scale/kubeburner-object-templates/dns-operator/dns-operator-deployment.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ spec:
2121
- --leader-elect
2222
- --metrics-bind-address=:8080
2323
- --provider=aws,google,inmemory,azure
24-
- --zap-log-level=debug
24+
- --log-level=debug
2525
command:
2626
- /manager
2727
env:

test/scale/kubeburner-object-templates/dns-operator/kustomization.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ patches:
2323
value: --provider=aws,google,inmemory,azure
2424
- op: add
2525
path: /spec/template/spec/containers/0/args/-
26-
value: --zap-log-level=debug
26+
value: --log-level=debug
2727
target:
2828
kind: Deployment

0 commit comments

Comments
 (0)