Skip to content

Commit d019b7a

Browse files
authored
feat: zap log level as environment variable (#35)
1 parent 0493cc5 commit d019b7a

File tree

7 files changed

+42
-7
lines changed

7 files changed

+42
-7
lines changed

cmd/main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ import (
2020

2121
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
2222
// to ensure that exec-entrypoint and run can make use of them.
23+
"go.uber.org/zap"
24+
"go.uber.org/zap/zapcore"
2325
_ "k8s.io/client-go/plugin/pkg/client/auth"
2426

2527
"k8s.io/apimachinery/pkg/runtime"
2628
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
2729
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
2830
ctrl "sigs.k8s.io/controller-runtime"
2931
"sigs.k8s.io/controller-runtime/pkg/healthz"
30-
"sigs.k8s.io/controller-runtime/pkg/log/zap"
32+
ctrlZap "sigs.k8s.io/controller-runtime/pkg/log/zap"
3133
"sigs.k8s.io/controller-runtime/pkg/webhook"
3234

3335
autoscalingv1alpha1 "github.com/google/kube-startup-cpu-boost/api/v1alpha1"
@@ -59,10 +61,11 @@ func main() {
5961
setupLog.Error(err, "unable to load configuration")
6062
os.Exit(1)
6163
}
62-
opts := zap.Options{
64+
ctrlZapOpts := ctrlZap.Options{
6365
Development: true,
66+
Level: zap.NewAtomicLevelAt(zapcore.Level(cfg.ZapLogLevel)),
6467
}
65-
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
68+
ctrl.SetLogger(ctrlZap.New(ctrlZap.UseFlagOptions(&ctrlZapOpts)))
6669
tlsOpts := []func(*tls.Config){}
6770

6871
webhookServer := webhook.NewServer(webhook.Options{

config/default/manager_config_patch.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ spec:
88
spec:
99
containers:
1010
- name: manager
11-
args:
12-
- --zap-log-level=5
1311
env:
1412
- name: POD_NAMESPACE
1513
valueFrom:
1614
fieldRef:
1715
fieldPath: metadata.namespace
16+
- name: ZAP_LOG_LEVEL
17+
value: "-5"
1818
- name: "LEADER_ELECTION"
1919
value: "true"
2020
- name: METRICS_PROBE_BIND_ADDR

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ require (
2222
github.com/emicklei/go-restful/v3 v3.11.3 // indirect
2323
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
2424
github.com/fsnotify/fsnotify v1.7.0 // indirect
25-
github.com/go-logr/zapr v1.3.0 // indirect
25+
github.com/go-logr/zapr v1.3.0
2626
github.com/go-openapi/jsonpointer v0.21.0 // indirect
2727
github.com/go-openapi/jsonreference v0.21.0 // indirect
2828
github.com/go-openapi/swag v0.23.0 // indirect
@@ -51,7 +51,7 @@ require (
5151
go.uber.org/atomic v1.11.0 // indirect
5252
go.uber.org/mock v0.4.0
5353
go.uber.org/multierr v1.11.0 // indirect
54-
go.uber.org/zap v1.27.0 // indirect
54+
go.uber.org/zap v1.27.0
5555
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
5656
golang.org/x/net v0.22.0 // indirect
5757
golang.org/x/oauth2 v0.18.0 // indirect

internal/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const (
2222
MetricsProbeBindAddrDefault = ":8080"
2323
HealthProbeBindAddrDefault = ":8081"
2424
SecureMetricsDefault = false
25+
ZapLogLevelDefault = 0 // zapcore.InfoLevel
2526
)
2627

2728
// ConfigProvider provides the Kube Startup CPU Boost configuration
@@ -45,6 +46,8 @@ type Config struct {
4546
HealthProbeBindAddr string
4647
// SecureMetrics determines if the metrics endpoint is served securely
4748
SecureMetrics bool
49+
// ZapLogLevel determines the log level for the ZAP logger
50+
ZapLogLevel int
4851
}
4952

5053
// LoadDefaults loads the default configuration values
@@ -55,4 +58,5 @@ func (c *Config) LoadDefaults() {
5558
c.MetricsProbeBindAddr = MetricsProbeBindAddrDefault
5659
c.HealthProbeBindAddr = HealthProbeBindAddrDefault
5760
c.SecureMetrics = SecureMetricsDefault
61+
c.ZapLogLevel = ZapLogLevelDefault
5862
}

internal/config/config_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,8 @@ var _ = Describe("Config", func() {
4747
It("has valid secure metrics", func() {
4848
Expect(cfg.SecureMetrics).To(Equal(config.SecureMetricsDefault))
4949
})
50+
It("has valid ZAP log level", func() {
51+
Expect(cfg.ZapLogLevel).To(Equal(config.ZapLogLevelDefault))
52+
})
5053
})
5154
})

internal/config/env_provider.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const (
2727
MetricsProbeBindAddrEnvVar = "METRICS_PROBE_BIND_ADDR"
2828
HealthProbeBindAddrEnvVar = "HEALTH_PROBE_BIND_ADDR"
2929
SecureMetricsEnvVar = "SECURE_METRICS"
30+
ZapLogLevelEnvVar = "ZAP_LOG_LEVEL"
3031
)
3132

3233
type LookupEnvFunc func(key string) (string, bool)
@@ -76,6 +77,20 @@ func (p *EnvConfigProvider) LoadConfig() (*Config, error) {
7677
errs = append(errs, fmt.Errorf("%s value is not a bool: %s", SecureMetricsEnvVar, err))
7778
}
7879
}
80+
if v, ok := p.lookupFunc(MgrCheckIntervalSecEnvVar); ok {
81+
intVal, err := strconv.Atoi(v)
82+
config.MgrCheckIntervalSec = intVal
83+
if err != nil {
84+
errs = append(errs, fmt.Errorf("%s value is not an int: %s", MgrCheckIntervalSecEnvVar, err))
85+
}
86+
}
87+
if v, ok := p.lookupFunc(ZapLogLevelEnvVar); ok {
88+
intVal, err := strconv.Atoi(v)
89+
config.ZapLogLevel = intVal
90+
if err != nil {
91+
errs = append(errs, fmt.Errorf("%s value is not an int: %s", ZapLogLevelEnvVar, err))
92+
}
93+
}
7994
var err error
8095
if len(errs) > 0 {
8196
err = errors.Join(errs...)

internal/config/env_provider_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,15 @@ var _ = Describe("EnvProvider", func() {
105105
Expect(cfg.SecureMetrics).To(BeTrue())
106106
})
107107
})
108+
When("zap log level variable is set", func() {
109+
var logLevel int
110+
BeforeEach(func() {
111+
logLevel = -5
112+
lookupFuncMap[config.ZapLogLevelEnvVar] = fmt.Sprintf("%d", logLevel)
113+
})
114+
It("has valid check manager interval", func() {
115+
Expect(cfg.ZapLogLevel).To(Equal(logLevel))
116+
})
117+
})
108118
})
109119
})

0 commit comments

Comments
 (0)