Skip to content

Commit 294917f

Browse files
authored
Merge pull request #6072 from sbueringer/pr-json-log
✨ Add JSON log format and deprecate klog flags
2 parents 422f420 + 9895ed0 commit 294917f

File tree

7 files changed

+84
-8
lines changed

7 files changed

+84
-8
lines changed

bootstrap/kubeadm/main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3434
"k8s.io/client-go/tools/leaderelection/resourcelock"
3535
cliflag "k8s.io/component-base/cli/flag"
36+
"k8s.io/component-base/logs"
37+
_ "k8s.io/component-base/logs/json/register"
3638
"k8s.io/klog/v2"
3739
"k8s.io/klog/v2/klogr"
3840
ctrl "sigs.k8s.io/controller-runtime"
@@ -82,10 +84,14 @@ var (
8284
webhookCertDir string
8385
healthAddr string
8486
tokenTTL time.Duration
87+
logOptions = logs.NewOptions()
8588
)
8689

8790
// InitFlags initializes this manager's flags.
8891
func InitFlags(fs *pflag.FlagSet) {
92+
logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags())
93+
logOptions.AddFlags(fs)
94+
8995
fs.StringVar(&metricsBindAddr, "metrics-bind-addr", "localhost:8080",
9096
"The address the metric endpoint binds to.")
9197

@@ -139,7 +145,19 @@ func main() {
139145
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
140146
pflag.Parse()
141147

142-
ctrl.SetLogger(klogr.New())
148+
if err := logOptions.ValidateAndApply(); err != nil {
149+
setupLog.Error(err, "unable to start manager")
150+
os.Exit(1)
151+
}
152+
153+
// The JSON log format requires the Klog format in klog, otherwise log lines
154+
// are serialized twice, e.g.:
155+
// { ... "msg":"controller/cluster \"msg\"=\"Starting workers\"\n"}
156+
if logOptions.Config.Format == logs.JSONLogFormat {
157+
ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
158+
} else {
159+
ctrl.SetLogger(klogr.New())
160+
}
143161

144162
if profilerAddress != "" {
145163
klog.Infof("Profiler listening for requests at %s", profilerAddress)

controlplane/kubeadm/main.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3636
"k8s.io/client-go/tools/leaderelection/resourcelock"
3737
cliflag "k8s.io/component-base/cli/flag"
38+
"k8s.io/component-base/logs"
39+
_ "k8s.io/component-base/logs/json/register"
3840
"k8s.io/klog/v2"
3941
"k8s.io/klog/v2/klogr"
4042
ctrl "sigs.k8s.io/controller-runtime"
@@ -86,10 +88,14 @@ var (
8688
webhookCertDir string
8789
healthAddr string
8890
etcdDialTimeout time.Duration
91+
logOptions = logs.NewOptions()
8992
)
9093

9194
// InitFlags initializes the flags.
9295
func InitFlags(fs *pflag.FlagSet) {
96+
logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags())
97+
logOptions.AddFlags(fs)
98+
9399
fs.StringVar(&metricsBindAddr, "metrics-bind-addr", "localhost:8080",
94100
"The address the metric endpoint binds to.")
95101

@@ -142,7 +148,19 @@ func main() {
142148
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
143149
pflag.Parse()
144150

145-
ctrl.SetLogger(klogr.New())
151+
if err := logOptions.ValidateAndApply(); err != nil {
152+
setupLog.Error(err, "unable to start manager")
153+
os.Exit(1)
154+
}
155+
156+
// The JSON log format requires the Klog format in klog, otherwise log lines
157+
// are serialized twice, e.g.:
158+
// { ... "msg":"controller/cluster \"msg\"=\"Starting workers\"\n"}
159+
if logOptions.Config.Format == logs.JSONLogFormat {
160+
ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
161+
} else {
162+
ctrl.SetLogger(klogr.New())
163+
}
146164

147165
if profilerAddress != "" {
148166
klog.Infof("Profiler listening for requests at %s", profilerAddress)

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ require (
4747

4848
require golang.org/x/net v0.0.0-20210825183410-e898025ed96a
4949

50+
require github.com/go-logr/zapr v1.2.0 // indirect
51+
5052
require (
5153
cloud.google.com/go v0.93.3 // indirect
5254
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect

main.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ import (
3333
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
3434
"k8s.io/client-go/tools/leaderelection/resourcelock"
3535
cliflag "k8s.io/component-base/cli/flag"
36+
"k8s.io/component-base/logs"
37+
_ "k8s.io/component-base/logs/json/register"
3638
"k8s.io/klog/v2"
3739
"k8s.io/klog/v2/klogr"
3840
ctrl "sigs.k8s.io/controller-runtime"
@@ -84,11 +86,10 @@ var (
8486
webhookPort int
8587
webhookCertDir string
8688
healthAddr string
89+
logOptions = logs.NewOptions()
8790
)
8891

8992
func init() {
90-
klog.InitFlags(nil)
91-
9293
_ = clientgoscheme.AddToScheme(scheme)
9394
_ = apiextensionsv1.AddToScheme(scheme)
9495

@@ -109,6 +110,9 @@ func init() {
109110

110111
// InitFlags initializes the flags.
111112
func InitFlags(fs *pflag.FlagSet) {
113+
logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags())
114+
logOptions.AddFlags(fs)
115+
112116
fs.StringVar(&metricsBindAddr, "metrics-bind-addr", "localhost:8080",
113117
"The address the metric endpoint binds to.")
114118

@@ -183,7 +187,19 @@ func main() {
183187
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
184188
pflag.Parse()
185189

186-
ctrl.SetLogger(klogr.New())
190+
if err := logOptions.ValidateAndApply(); err != nil {
191+
setupLog.Error(err, "unable to start manager")
192+
os.Exit(1)
193+
}
194+
195+
// The JSON log format requires the Klog format in klog, otherwise log lines
196+
// are serialized twice, e.g.:
197+
// { ... "msg":"controller/cluster \"msg\"=\"Starting workers\"\n"}
198+
if logOptions.Config.Format == logs.JSONLogFormat {
199+
ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
200+
} else {
201+
ctrl.SetLogger(klogr.New())
202+
}
187203

188204
if profilerAddress != "" {
189205
klog.Infof("Profiler listening for requests at %s", profilerAddress)

test/go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ require (
5252
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
5353
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
5454
github.com/fsnotify/fsnotify v1.5.1 // indirect
55+
github.com/go-logr/zapr v1.2.0 // indirect
5556
github.com/go-openapi/jsonpointer v0.19.5 // indirect
5657
github.com/go-openapi/jsonreference v0.19.5 // indirect
5758
github.com/go-openapi/swag v0.19.14 // indirect
@@ -96,6 +97,9 @@ require (
9697
github.com/stoewer/go-strcase v1.2.0 // indirect
9798
github.com/subosito/gotenv v1.2.0 // indirect
9899
github.com/valyala/fastjson v1.6.3 // indirect
100+
go.uber.org/atomic v1.7.0 // indirect
101+
go.uber.org/multierr v1.6.0 // indirect
102+
go.uber.org/zap v1.19.1 // indirect
99103
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
100104
golang.org/x/net v0.0.0-20210825183410-e898025ed96a // indirect
101105
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect

test/go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:l
121121
github.com/aws/aws-sdk-go v1.8.39/go.mod h1:ZRmQr0FajVIyZ4ZzBYKG5P3ZqPz9IHG41ZoMu1ADI3k=
122122
github.com/aws/aws-sdk-go v1.15.11/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0=
123123
github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM=
124+
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
124125
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
125126
github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
126127
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=

test/infrastructure/docker/main.go

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import (
3131
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
3232
"k8s.io/client-go/tools/leaderelection/resourcelock"
3333
cliflag "k8s.io/component-base/cli/flag"
34+
"k8s.io/component-base/logs"
35+
_ "k8s.io/component-base/logs/json/register"
3436
"k8s.io/klog/v2"
3537
"k8s.io/klog/v2/klogr"
3638
ctrl "sigs.k8s.io/controller-runtime"
@@ -64,11 +66,10 @@ var (
6466
healthAddr string
6567
webhookPort int
6668
webhookCertDir string
69+
logOptions = logs.NewOptions()
6770
)
6871

6972
func init() {
70-
klog.InitFlags(nil)
71-
7273
_ = scheme.AddToScheme(myscheme)
7374
_ = infrav1alpha3.AddToScheme(myscheme)
7475
_ = infrav1alpha4.AddToScheme(myscheme)
@@ -82,6 +83,9 @@ func init() {
8283
}
8384

8485
func initFlags(fs *pflag.FlagSet) {
86+
logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags())
87+
logOptions.AddFlags(fs)
88+
8589
fs.StringVar(&metricsBindAddr, "metrics-bind-addr", "localhost:8080",
8690
"The address the metric endpoint binds to.")
8791
fs.IntVar(&concurrency, "concurrency", 10,
@@ -105,6 +109,7 @@ func initFlags(fs *pflag.FlagSet) {
105109
func main() {
106110
rand.Seed(time.Now().UnixNano())
107111
if _, err := os.ReadDir("/tmp/"); err != nil {
112+
setupLog.Error(err, "unable to start manager")
108113
os.Exit(1)
109114
}
110115

@@ -113,7 +118,19 @@ func main() {
113118
pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc)
114119
pflag.Parse()
115120

116-
ctrl.SetLogger(klogr.New())
121+
if err := logOptions.ValidateAndApply(); err != nil {
122+
setupLog.Error(err, "unable to start manager")
123+
os.Exit(1)
124+
}
125+
126+
// The JSON log format requires the Klog format in klog, otherwise log lines
127+
// are serialized twice, e.g.:
128+
// { ... "msg":"controller/cluster \"msg\"=\"Starting workers\"\n"}
129+
if logOptions.Config.Format == logs.JSONLogFormat {
130+
ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog)))
131+
} else {
132+
ctrl.SetLogger(klogr.New())
133+
}
117134

118135
if profilerAddress != "" {
119136
klog.Infof("Profiler listening for requests at %s", profilerAddress)

0 commit comments

Comments
 (0)