Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.
This repository was archived by the owner on Aug 12, 2025. It is now read-only.

Use component-base/logs instead of old logging style #546

@cprivitere

Description

@cprivitere

Logging:

To align with the upstream Kubernetes community CAPI now configures logging via component-base/logs. This provides advantages like support for the JSON logging format (via --logging-format=json) and automatic deprecation of klog flags aligned to the upstream Kubernetes deprecation period.

View main.go diff

import (
  ...
+ "k8s.io/component-base/logs"
+ _ "k8s.io/component-base/logs/json/register"
)

var (
	...
+	logOptions = logs.NewOptions()
)

func init() {
-	klog.InitFlags(nil)

func InitFlags(fs *pflag.FlagSet) {
+	logs.AddFlags(fs, logs.SkipLoggingConfigurationFlags())
+	logOptions.AddFlags(fs)

func main() {
	...
	pflag.Parse()

+	if err := logOptions.ValidateAndApply(); err != nil {
+		setupLog.Error(err, "unable to start manager")
+		os.Exit(1)
+	}
+
+	// klog.Background will automatically use the right logger.
+	ctrl.SetLogger(klog.Background())
-	ctrl.SetLogger(klogr.New())

This change has been introduced in CAPI in the following PRs: #6072, #6190, #6602.
Note: This change is not mandatory for providers, but highly recommended.

/kind feature

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/featureCategorizes issue or PR as related to a new feature.lifecycle/frozenIndicates that an issue or PR should not be auto-closed due to staleness.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions