Skip to content

Commit

Permalink
Use flag to define log timestamp format
Browse files Browse the repository at this point in the history
Since v0.11 controller-runtime supports the zap-time-encoding flag to
specify the time encoding of log timestamps. This flag is used to give
the user the possibilty to specify the required format.
By default nmstate-kubernetes uses the ISO8601 format.

Signed-off-by: Christoph Stäbler <cstabler@redhat.com>
  • Loading branch information
creydr committed Jan 18, 2022
1 parent b17e7b6 commit 991749f
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 23 deletions.
12 changes: 1 addition & 11 deletions cmd/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"os"
"time"

"go.uber.org/zap/zapcore"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -91,7 +90,7 @@ func main() {
flag.CommandLine.Set("zap-devel", "true")
}

setupLogger(opt)
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opt)))

// Lock only for handler, we can run old and new version of
// webhook without problems, policy status will be updated
Expand Down Expand Up @@ -249,15 +248,6 @@ func main() {
}
}

func setupLogger(opts zap.Options) {
opts.EncoderConfigOptions = append(opts.EncoderConfigOptions, func(ec *zapcore.EncoderConfig) {
ec.EncodeTime = zapcore.ISO8601TimeEncoder
})

logger := zap.New(zap.UseFlagOptions(&opts))
ctrl.SetLogger(logger)
}

// Start profiler on given port if ENABLE_PROFILER is True
func setProfiler() {
cfg := ProfilerConfig{}
Expand Down
12 changes: 1 addition & 11 deletions cmd/operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
_ "net/http/pprof"
"os"

"go.uber.org/zap/zapcore"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -76,7 +75,7 @@ func main() {
flag.CommandLine.Set("zap-devel", "true")
}

setupLogger(opt)
ctrl.SetLogger(zap.New(zap.UseFlagOptions(&opt)))

ctrlOptions := ctrl.Options{
Scheme: scheme,
Expand Down Expand Up @@ -107,15 +106,6 @@ func main() {
}
}

func setupLogger(opts zap.Options) {
opts.EncoderConfigOptions = append(opts.EncoderConfigOptions, func(ec *zapcore.EncoderConfig) {
ec.EncodeTime = zapcore.ISO8601TimeEncoder
})

logger := zap.New(zap.UseFlagOptions(&opts))
ctrl.SetLogger(logger)
}

// Start profiler on given port if ENABLE_PROFILER is True
func setProfiler() {
cfg := ProfilerConfig{}
Expand Down
6 changes: 6 additions & 0 deletions deploy/handler/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ spec:
priorityClassName: system-cluster-critical
containers:
- name: nmstate-webhook
args:
- --zap-time-encoding=iso8601
# Replace this with the built image name
image: {{ .HandlerImage }}
imagePullPolicy: {{ .HandlerPullPolicy }}
Expand Down Expand Up @@ -113,6 +115,8 @@ spec:
priorityClassName: system-cluster-critical
containers:
- name: nmstate-cert-manager
args:
- --zap-time-encoding=iso8601
# Replace this with the built image name
image: {{ .HandlerImage }}
imagePullPolicy: {{ .HandlerPullPolicy }}
Expand Down Expand Up @@ -205,6 +209,8 @@ spec:
priorityClassName: system-node-critical
containers:
- name: nmstate-handler
args:
- --zap-time-encoding=iso8601
# Replace this with the built image name
image: {{ .HandlerImage }}
imagePullPolicy: {{ .HandlerPullPolicy }}
Expand Down
2 changes: 2 additions & 0 deletions deploy/operator/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ spec:
effect: NoSchedule
containers:
- name: nmstate-operator
args:
- --zap-time-encoding=iso8601
# Replace this with the built image name
image: {{ .OperatorImage }}
imagePullPolicy: {{ .OperatorPullPolicy }}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ require (
github.com/spf13/pflag v1.0.5
github.com/tidwall/gjson v1.9.3
github.com/tidwall/sjson v1.1.7
go.uber.org/zap v1.19.1
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.23.1
k8s.io/apimachinery v0.23.1
Expand Down Expand Up @@ -185,6 +184,7 @@ require (
go.starlark.net v0.0.0-20200306205701-8dd3e2ee1dd5 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.19.1 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/mod v0.5.1 // indirect
golang.org/x/net v0.0.0-20211209124913-491a49abca63 // indirect
Expand Down

0 comments on commit 991749f

Please sign in to comment.