Skip to content

Commit 65fbd61

Browse files
committed
Send leader election messages to the global logger
1 parent f9e12e1 commit 65fbd61

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

cmd/postgres-operator/main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"k8s.io/apimachinery/pkg/util/validation"
2222
"k8s.io/client-go/rest"
23+
"k8s.io/klog/v2"
2324
"sigs.k8s.io/controller-runtime/pkg/healthz"
2425
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
2526

@@ -88,14 +89,18 @@ func initLogging() {
8889

8990
global := logging.FromContext(context.Background())
9091
runtime.SetLogger(global)
92+
93+
// [k8s.io/client-go/tools/leaderelection] logs to the global [klog] instance.
94+
// - https://github.com/kubernetes-sigs/controller-runtime/issues/2656
95+
klog.SetLoggerWithOptions(global, klog.ContextualLogger(true))
9196
}
9297

9398
//+kubebuilder:rbac:groups="coordination.k8s.io",resources="leases",verbs={get,create,update,watch}
9499
//+kubebuilder:rbac:groups="authentication.k8s.io",resources="tokenreviews",verbs={create}
95100
//+kubebuilder:rbac:groups="authorization.k8s.io",resources="subjectaccessreviews",verbs={create}
96101

97102
func initManager(ctx context.Context) (runtime.Options, error) {
98-
log := logging.FromContext(ctx)
103+
log := logging.FromContext(ctx).WithName("manager")
99104

100105
options := runtime.Options{}
101106
options.Cache.SyncPeriod = initialize.Pointer(time.Hour)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ require (
2828
k8s.io/apimachinery v0.33.4
2929
k8s.io/client-go v0.33.4
3030
k8s.io/component-base v0.33.4
31+
k8s.io/klog/v2 v2.130.1
3132
k8s.io/kube-openapi v0.0.0-20250318190949-c8a335a9a2ff
3233
sigs.k8s.io/controller-runtime v0.21.0
3334
sigs.k8s.io/json v0.0.0-20241010143419-9aa6b5e7a4b3
@@ -127,7 +128,6 @@ require (
127128
gopkg.in/yaml.v3 v3.0.1 // indirect
128129
k8s.io/apiextensions-apiserver v0.33.0 // indirect
129130
k8s.io/apiserver v0.33.0 // indirect
130-
k8s.io/klog/v2 v2.130.1 // indirect
131131
k8s.io/utils v0.0.0-20241104100929-3ea5e8cea738 // indirect
132132
sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.31.2 // indirect
133133
sigs.k8s.io/controller-tools v0.17.3 // indirect

0 commit comments

Comments
 (0)