Skip to content

Commit ba93b8a

Browse files
committed
feat: add helm log level / zap-devel config and configurable slog LOG_LEVEL
1 parent 0dc5d75 commit ba93b8a

5 files changed

Lines changed: 77 additions & 7 deletions

File tree

cmd/main.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"os"
1313
"path/filepath"
1414
"slices"
15+
"strings"
1516
"time"
1617

1718
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
@@ -151,10 +152,27 @@ func main() {
151152

152153
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
153154

154-
// Configure slog (used across internal packages) with structured JSON output.
155+
// Configure slog (used across internal packages) with JSON output and
156+
// level control via the LOG_LEVEL environment variable.
157+
// Supported values: debug, info (default), warn, error.
158+
slogLevel := new(slog.LevelVar)
159+
slogLevel.Set(slog.LevelInfo)
160+
if lvl := os.Getenv("LOG_LEVEL"); lvl != "" {
161+
switch strings.ToLower(lvl) {
162+
case "debug":
163+
slogLevel.Set(slog.LevelDebug)
164+
case "info":
165+
slogLevel.Set(slog.LevelInfo)
166+
case "warn", "warning":
167+
slogLevel.Set(slog.LevelWarn)
168+
case "error":
169+
slogLevel.Set(slog.LevelError)
170+
}
171+
}
155172
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
156-
Level: slog.LevelInfo,
173+
Level: slogLevel,
157174
})))
175+
slog.Info("slog configured", "level", slogLevel.Level().String())
158176

159177
// Log the main configuration
160178
setupLog.Info("loaded main configuration",

cortex.secrets.example.yaml

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# Copyright SAP SE
22
# SPDX-License-Identifier: Apache-2.0
33

4-
# Override config values that contain sensitive information or
5-
# are specific to your environment. These values can be used in the Tiltfile.
4+
# Override config values for local development. This includes secrets,
5+
# environment-specific settings, and logging configuration.
6+
# These values can be used in the Tiltfile.
67

78
# SSO certificate to use.
89
sharedSSOCert: &sharedSSOCert
@@ -20,6 +21,23 @@ sharedSSOCert: &sharedSSOCert
2021
# If true, the certificate is not verified.
2122
selfSigned: "false"
2223

24+
# Logging configuration for local development.
25+
# Set logLevel to "debug" for verbose output from both zap and slog loggers.
26+
# Set zapDevel to true for human-readable console logs instead of JSON.
27+
# These apply per sub-chart, e.g. for cortex-nova:
28+
#
29+
# cortex-scheduling-controllers:
30+
# controllerManager:
31+
# container:
32+
# logLevel: "debug"
33+
# zapDevel: true
34+
#
35+
# cortex-knowledge-controllers:
36+
# controllerManager:
37+
# container:
38+
# logLevel: "debug"
39+
# zapDevel: true
40+
2341
# Enable kvm pipelines and scheduling support.
2442
kvm:
2543
enabled: true

helm/library/cortex/templates/_helpers.tpl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@ app.kubernetes.io/instance: {{ .Release.Name }}
4141
{{ $hasMutating }}}}{{- end }}
4242

4343

44+
{{/*
45+
chart.argsContainPrefix checks if any string in args starts with prefix.
46+
Usage: include "chart.argsContainPrefix" (dict "prefix" "--zap-log-level" "args" .Values.controllerManager.container.args)
47+
Returns "true" or "false".
48+
*/}}
49+
{{- define "chart.argsContainPrefix" -}}
50+
{{- $prefix := .prefix -}}
51+
{{- $result := dict "found" "false" -}}
52+
{{- range .args -}}
53+
{{- if hasPrefix $prefix . -}}
54+
{{- $_ := set $result "found" "true" -}}
55+
{{- end -}}
56+
{{- end -}}
57+
{{- get $result "found" -}}
58+
{{- end -}}
59+
4460
{{- define "chart.hasValidatingWebhooks" -}}
4561
{{- $hasValidating := false }}
4662
{{- range . }}

helm/library/cortex/templates/manager/manager.yaml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ spec:
3535
{{- range .Values.controllerManager.container.args }}
3636
- {{ . }}
3737
{{- end }}
38+
{{- if and .Values.controllerManager.container.logLevel (ne (include "chart.argsContainPrefix" (dict "prefix" "--zap-log-level" "args" .Values.controllerManager.container.args)) "true") }}
39+
- "--zap-log-level={{ .Values.controllerManager.container.logLevel }}"
40+
{{- end }}
41+
{{- if and .Values.controllerManager.container.zapDevel (ne (include "chart.argsContainPrefix" (dict "prefix" "--zap-devel" "args" .Values.controllerManager.container.args)) "true") }}
42+
- "--zap-devel"
43+
{{- end }}
3844
{{- if and .Values.webhook.enable .Values.certmanager.enable }}
3945
- "--webhook-cert-path=/tmp/k8s-webhook-server/serving-certs"
4046
{{- end }}
@@ -56,13 +62,17 @@ spec:
5662
{{- if .Values.controllerManager.container.image.pullPolicy }}
5763
imagePullPolicy: {{ .Values.controllerManager.container.image.pullPolicy }}
5864
{{- end }}
59-
{{- if .Values.controllerManager.container.env }}
6065
env:
66+
{{- if and .Values.controllerManager.container.logLevel (not (and .Values.controllerManager.container.env (hasKey .Values.controllerManager.container.env "LOG_LEVEL"))) }}
67+
- name: LOG_LEVEL
68+
value: {{ .Values.controllerManager.container.logLevel | quote }}
69+
{{- end }}
70+
{{- if .Values.controllerManager.container.env }}
6171
{{- range $key, $value := .Values.controllerManager.container.env }}
6272
- name: {{ $key }}
6373
value: {{ $value }}
6474
{{- end }}
65-
{{- end }}
75+
{{- end }}
6676
livenessProbe:
6777
{{- toYaml .Values.controllerManager.container.livenessProbe | nindent 12 }}
6878
readinessProbe:
@@ -140,4 +150,4 @@ data:
140150
{{- $mergedSecrets = mergeOverwrite .Values.secrets $mergedSecrets }}
141151
{{- end }}
142152
{{ toJson $mergedSecrets | b64enc }}
143-
{{- end }}
153+
{{- end }}

helm/library/cortex/values.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,14 @@ controllerManager:
1313
- "--metrics-bind-address=:2112"
1414
- "--health-probe-bind-address=:8081"
1515
- "--metrics-secure=false"
16+
# Log level for both zap (controller-runtime) and slog (internal packages).
17+
# Supported: debug, info (default), warn, error.
18+
logLevel: "info"
19+
# Enable zap development mode (human-readable console logs, development stack traces).
20+
# This only changes output format and stack trace behavior, not the log level.
21+
# The effective log level is controlled by logLevel above (default: "info").
22+
# Set to true for local development (e.g. Tilt), keep false for production.
23+
zapDevel: false
1624
resources:
1725
limits:
1826
cpu: 500m

0 commit comments

Comments
 (0)