Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add JSON log format and deprecate klog flags #6072

Merged
merged 1 commit into from
Feb 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion bootstrap/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
sbueringer marked this conversation as resolved.
Show resolved Hide resolved
_ "k8s.io/component-base/logs/json/register"
"k8s.io/klog/v2"
"k8s.io/klog/v2/klogr"
ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -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.")

Expand Down Expand Up @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion controlplane/kubeadm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.")

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand All @@ -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.")

Expand Down Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
23 changes: 20 additions & 3 deletions test/infrastructure/docker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -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,
Expand All @@ -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)
}

Expand All @@ -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)
Expand Down