diff --git a/bootstrap/kubeadm/main.go b/bootstrap/kubeadm/main.go index f6f81139ed9b..46320bfacb18 100644 --- a/bootstrap/kubeadm/main.go +++ b/bootstrap/kubeadm/main.go @@ -33,6 +33,8 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/leaderelection/resourcelock" cliflag "k8s.io/component-base/cli/flag" + "k8s.io/component-base/logs" + _ "k8s.io/component-base/logs/json/register" "k8s.io/klog/v2" "k8s.io/klog/v2/klogr" ctrl "sigs.k8s.io/controller-runtime" @@ -82,10 +84,14 @@ var ( webhookCertDir string healthAddr string tokenTTL time.Duration + logOptions = logs.NewOptions() ) // InitFlags initializes this manager's flags. func InitFlags(fs *pflag.FlagSet) { + logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags()) + logOptions.AddFlags(fs) + fs.StringVar(&metricsBindAddr, "metrics-bind-addr", "localhost:8080", "The address the metric endpoint binds to.") @@ -139,7 +145,19 @@ func main() { pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.Parse() - ctrl.SetLogger(klogr.New()) + if err := logOptions.ValidateAndApply(); err != nil { + setupLog.Error(err, "unable to start manager") + os.Exit(1) + } + + // The JSON log format requires the Klog format in klog, otherwise log lines + // are serialized twice, e.g.: + // { ... "msg":"controller/cluster \"msg\"=\"Starting workers\"\n"} + if logOptions.Config.Format == logs.JSONLogFormat { + ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog))) + } else { + ctrl.SetLogger(klogr.New()) + } if profilerAddress != "" { klog.Infof("Profiler listening for requests at %s", profilerAddress) diff --git a/controlplane/kubeadm/main.go b/controlplane/kubeadm/main.go index a3f8858009af..696f0a5c0866 100644 --- a/controlplane/kubeadm/main.go +++ b/controlplane/kubeadm/main.go @@ -35,6 +35,8 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/leaderelection/resourcelock" cliflag "k8s.io/component-base/cli/flag" + "k8s.io/component-base/logs" + _ "k8s.io/component-base/logs/json/register" "k8s.io/klog/v2" "k8s.io/klog/v2/klogr" ctrl "sigs.k8s.io/controller-runtime" @@ -86,10 +88,14 @@ var ( webhookCertDir string healthAddr string etcdDialTimeout time.Duration + logOptions = logs.NewOptions() ) // InitFlags initializes the flags. func InitFlags(fs *pflag.FlagSet) { + logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags()) + logOptions.AddFlags(fs) + fs.StringVar(&metricsBindAddr, "metrics-bind-addr", "localhost:8080", "The address the metric endpoint binds to.") @@ -142,7 +148,19 @@ func main() { pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.Parse() - ctrl.SetLogger(klogr.New()) + if err := logOptions.ValidateAndApply(); err != nil { + setupLog.Error(err, "unable to start manager") + os.Exit(1) + } + + // The JSON log format requires the Klog format in klog, otherwise log lines + // are serialized twice, e.g.: + // { ... "msg":"controller/cluster \"msg\"=\"Starting workers\"\n"} + if logOptions.Config.Format == logs.JSONLogFormat { + ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog))) + } else { + ctrl.SetLogger(klogr.New()) + } if profilerAddress != "" { klog.Infof("Profiler listening for requests at %s", profilerAddress) diff --git a/go.mod b/go.mod index cd863d0aa90f..f4ac8fb2a9eb 100644 --- a/go.mod +++ b/go.mod @@ -47,6 +47,8 @@ require ( require golang.org/x/net v0.0.0-20210825183410-e898025ed96a +require github.com/go-logr/zapr v1.2.0 // indirect + require ( cloud.google.com/go v0.93.3 // indirect github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect diff --git a/main.go b/main.go index 5c9afa911f2d..3f2ed449d641 100644 --- a/main.go +++ b/main.go @@ -33,6 +33,8 @@ import ( clientgoscheme "k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/tools/leaderelection/resourcelock" cliflag "k8s.io/component-base/cli/flag" + "k8s.io/component-base/logs" + _ "k8s.io/component-base/logs/json/register" "k8s.io/klog/v2" "k8s.io/klog/v2/klogr" ctrl "sigs.k8s.io/controller-runtime" @@ -84,11 +86,10 @@ var ( webhookPort int webhookCertDir string healthAddr string + logOptions = logs.NewOptions() ) func init() { - klog.InitFlags(nil) - _ = clientgoscheme.AddToScheme(scheme) _ = apiextensionsv1.AddToScheme(scheme) @@ -109,6 +110,9 @@ func init() { // InitFlags initializes the flags. func InitFlags(fs *pflag.FlagSet) { + logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags()) + logOptions.AddFlags(fs) + fs.StringVar(&metricsBindAddr, "metrics-bind-addr", "localhost:8080", "The address the metric endpoint binds to.") @@ -183,7 +187,19 @@ func main() { pflag.CommandLine.AddGoFlagSet(flag.CommandLine) pflag.Parse() - ctrl.SetLogger(klogr.New()) + if err := logOptions.ValidateAndApply(); err != nil { + setupLog.Error(err, "unable to start manager") + os.Exit(1) + } + + // The JSON log format requires the Klog format in klog, otherwise log lines + // are serialized twice, e.g.: + // { ... "msg":"controller/cluster \"msg\"=\"Starting workers\"\n"} + if logOptions.Config.Format == logs.JSONLogFormat { + ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog))) + } else { + ctrl.SetLogger(klogr.New()) + } if profilerAddress != "" { klog.Infof("Profiler listening for requests at %s", profilerAddress) diff --git a/test/go.mod b/test/go.mod index 4abd6db3d369..335f912dd9e7 100644 --- a/test/go.mod +++ b/test/go.mod @@ -52,6 +52,7 @@ require ( github.com/evanphx/json-patch v4.12.0+incompatible // indirect github.com/evanphx/json-patch/v5 v5.6.0 // indirect github.com/fsnotify/fsnotify v1.5.1 // indirect + github.com/go-logr/zapr v1.2.0 // indirect github.com/go-openapi/jsonpointer v0.19.5 // indirect github.com/go-openapi/jsonreference v0.19.5 // indirect github.com/go-openapi/swag v0.19.14 // indirect @@ -96,6 +97,9 @@ require ( github.com/stoewer/go-strcase v1.2.0 // indirect github.com/subosito/gotenv v1.2.0 // indirect github.com/valyala/fastjson v1.6.3 // 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/net v0.0.0-20210825183410-e898025ed96a // indirect golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect diff --git a/test/go.sum b/test/go.sum index 006d63eacb11..b89563b61082 100644 --- a/test/go.sum +++ b/test/go.sum @@ -121,6 +121,7 @@ github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a/go.mod h1:l github.com/aws/aws-sdk-go v1.8.39/go.mod h1:ZRmQr0FajVIyZ4ZzBYKG5P3ZqPz9IHG41ZoMu1ADI3k= github.com/aws/aws-sdk-go v1.15.11/go.mod h1:mFuSZ37Z9YOHbQEwBWztmVzqXrEkub65tZoCYDt7FT0= github.com/benbjohnson/clock v1.0.3/go.mod h1:bGMdMPoPVvcYyt1gHDf4J2KE153Yf9BuiUKYMaxlTDM= +github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= github.com/beorn7/perks v0.0.0-20160804104726-4c0e84591b9a/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= diff --git a/test/infrastructure/docker/main.go b/test/infrastructure/docker/main.go index 1d95ea7b0442..16d970623f5d 100644 --- a/test/infrastructure/docker/main.go +++ b/test/infrastructure/docker/main.go @@ -31,6 +31,8 @@ import ( _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" "k8s.io/client-go/tools/leaderelection/resourcelock" cliflag "k8s.io/component-base/cli/flag" + "k8s.io/component-base/logs" + _ "k8s.io/component-base/logs/json/register" "k8s.io/klog/v2" "k8s.io/klog/v2/klogr" ctrl "sigs.k8s.io/controller-runtime" @@ -64,11 +66,10 @@ var ( healthAddr string webhookPort int webhookCertDir string + logOptions = logs.NewOptions() ) func init() { - klog.InitFlags(nil) - _ = scheme.AddToScheme(myscheme) _ = infrav1alpha3.AddToScheme(myscheme) _ = infrav1alpha4.AddToScheme(myscheme) @@ -82,6 +83,9 @@ func init() { } func initFlags(fs *pflag.FlagSet) { + logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags()) + logOptions.AddFlags(fs) + fs.StringVar(&metricsBindAddr, "metrics-bind-addr", "localhost:8080", "The address the metric endpoint binds to.") fs.IntVar(&concurrency, "concurrency", 10, @@ -105,6 +109,7 @@ func initFlags(fs *pflag.FlagSet) { func main() { rand.Seed(time.Now().UnixNano()) if _, err := os.ReadDir("/tmp/"); err != nil { + setupLog.Error(err, "unable to start manager") os.Exit(1) } @@ -113,7 +118,19 @@ func main() { pflag.CommandLine.SetNormalizeFunc(cliflag.WordSepNormalizeFunc) pflag.Parse() - ctrl.SetLogger(klogr.New()) + if err := logOptions.ValidateAndApply(); err != nil { + setupLog.Error(err, "unable to start manager") + os.Exit(1) + } + + // The JSON log format requires the Klog format in klog, otherwise log lines + // are serialized twice, e.g.: + // { ... "msg":"controller/cluster \"msg\"=\"Starting workers\"\n"} + if logOptions.Config.Format == logs.JSONLogFormat { + ctrl.SetLogger(klogr.NewWithOptions(klogr.WithFormat(klogr.FormatKlog))) + } else { + ctrl.SetLogger(klogr.New()) + } if profilerAddress != "" { klog.Infof("Profiler listening for requests at %s", profilerAddress)