Skip to content

Commit

Permalink
feat: zap log level as environment variable (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikouaj authored Mar 15, 2024
1 parent 0493cc5 commit d019b7a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 7 deletions.
9 changes: 6 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ import (

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

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
ctrlZap "sigs.k8s.io/controller-runtime/pkg/log/zap"
"sigs.k8s.io/controller-runtime/pkg/webhook"

autoscalingv1alpha1 "github.com/google/kube-startup-cpu-boost/api/v1alpha1"
Expand Down Expand Up @@ -59,10 +61,11 @@ func main() {
setupLog.Error(err, "unable to load configuration")
os.Exit(1)
}
opts := zap.Options{
ctrlZapOpts := ctrlZap.Options{
Development: true,
Level: zap.NewAtomicLevelAt(zapcore.Level(cfg.ZapLogLevel)),
}
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opts)))
ctrl.SetLogger(ctrlZap.New(ctrlZap.UseFlagOptions(&ctrlZapOpts)))
tlsOpts := []func(*tls.Config){}

webhookServer := webhook.NewServer(webhook.Options{
Expand Down
4 changes: 2 additions & 2 deletions config/default/manager_config_patch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ spec:
spec:
containers:
- name: manager
args:
- --zap-log-level=5
env:
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
- name: ZAP_LOG_LEVEL
value: "-5"
- name: "LEADER_ELECTION"
value: "true"
- name: METRICS_PROBE_BIND_ADDR
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/emicklei/go-restful/v3 v3.11.3 // indirect
github.com/evanphx/json-patch/v5 v5.9.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-logr/zapr v1.3.0
github.com/go-openapi/jsonpointer v0.21.0 // indirect
github.com/go-openapi/jsonreference v0.21.0 // indirect
github.com/go-openapi/swag v0.23.0 // indirect
Expand Down Expand Up @@ -51,7 +51,7 @@ require (
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/mock v0.4.0
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
go.uber.org/zap v1.27.0
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const (
MetricsProbeBindAddrDefault = ":8080"
HealthProbeBindAddrDefault = ":8081"
SecureMetricsDefault = false
ZapLogLevelDefault = 0 // zapcore.InfoLevel
)

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

// LoadDefaults loads the default configuration values
Expand All @@ -55,4 +58,5 @@ func (c *Config) LoadDefaults() {
c.MetricsProbeBindAddr = MetricsProbeBindAddrDefault
c.HealthProbeBindAddr = HealthProbeBindAddrDefault
c.SecureMetrics = SecureMetricsDefault
c.ZapLogLevel = ZapLogLevelDefault
}
3 changes: 3 additions & 0 deletions internal/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@ var _ = Describe("Config", func() {
It("has valid secure metrics", func() {
Expect(cfg.SecureMetrics).To(Equal(config.SecureMetricsDefault))
})
It("has valid ZAP log level", func() {
Expect(cfg.ZapLogLevel).To(Equal(config.ZapLogLevelDefault))
})
})
})
15 changes: 15 additions & 0 deletions internal/config/env_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
MetricsProbeBindAddrEnvVar = "METRICS_PROBE_BIND_ADDR"
HealthProbeBindAddrEnvVar = "HEALTH_PROBE_BIND_ADDR"
SecureMetricsEnvVar = "SECURE_METRICS"
ZapLogLevelEnvVar = "ZAP_LOG_LEVEL"
)

type LookupEnvFunc func(key string) (string, bool)
Expand Down Expand Up @@ -76,6 +77,20 @@ func (p *EnvConfigProvider) LoadConfig() (*Config, error) {
errs = append(errs, fmt.Errorf("%s value is not a bool: %s", SecureMetricsEnvVar, err))
}
}
if v, ok := p.lookupFunc(MgrCheckIntervalSecEnvVar); ok {
intVal, err := strconv.Atoi(v)
config.MgrCheckIntervalSec = intVal
if err != nil {
errs = append(errs, fmt.Errorf("%s value is not an int: %s", MgrCheckIntervalSecEnvVar, err))
}
}
if v, ok := p.lookupFunc(ZapLogLevelEnvVar); ok {
intVal, err := strconv.Atoi(v)
config.ZapLogLevel = intVal
if err != nil {
errs = append(errs, fmt.Errorf("%s value is not an int: %s", ZapLogLevelEnvVar, err))
}
}
var err error
if len(errs) > 0 {
err = errors.Join(errs...)
Expand Down
10 changes: 10 additions & 0 deletions internal/config/env_provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,15 @@ var _ = Describe("EnvProvider", func() {
Expect(cfg.SecureMetrics).To(BeTrue())
})
})
When("zap log level variable is set", func() {
var logLevel int
BeforeEach(func() {
logLevel = -5
lookupFuncMap[config.ZapLogLevelEnvVar] = fmt.Sprintf("%d", logLevel)
})
It("has valid check manager interval", func() {
Expect(cfg.ZapLogLevel).To(Equal(logLevel))
})
})
})
})

0 comments on commit d019b7a

Please sign in to comment.