From 0cb31bc4073ec76883282c25e20f02c0c5fe62da Mon Sep 17 00:00:00 2001 From: Ed Bartosh Date: Sat, 1 Feb 2025 09:45:15 +0200 Subject: [PATCH] migrate pkg/kubelet/nodeshutdown to contextual logging --- hack/golangci-hints.yaml | 1 + hack/golangci.yaml | 1 + hack/logcheck.conf | 1 + pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux.go | 4 ++-- pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux_test.go | 3 ++- pkg/kubelet/nodeshutdown/systemd/inhibit_linux.go | 6 +++--- pkg/kubelet/nodeshutdown/systemd/inhibit_linux_test.go | 4 +++- 7 files changed, 13 insertions(+), 7 deletions(-) diff --git a/hack/golangci-hints.yaml b/hack/golangci-hints.yaml index 3256239cf9e..3c212d199ba 100644 --- a/hack/golangci-hints.yaml +++ b/hack/golangci-hints.yaml @@ -213,6 +213,7 @@ linters: contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.* contextual k8s.io/kubernetes/pkg/kubelet/apis/.* contextual k8s.io/kubernetes/pkg/kubelet/kubeletconfig/.* + contextual k8s.io/kubernetes/pkg/kubelet/nodeshutdown/.* # As long as contextual logging is alpha or beta, all WithName, WithValues, # NewContext calls have to go through klog. Once it is GA, we can lift diff --git a/hack/golangci.yaml b/hack/golangci.yaml index e05e56b25c5..e3c95e68627 100644 --- a/hack/golangci.yaml +++ b/hack/golangci.yaml @@ -227,6 +227,7 @@ linters: contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.* contextual k8s.io/kubernetes/pkg/kubelet/apis/.* contextual k8s.io/kubernetes/pkg/kubelet/kubeletconfig/.* + contextual k8s.io/kubernetes/pkg/kubelet/nodeshutdown/.* # As long as contextual logging is alpha or beta, all WithName, WithValues, # NewContext calls have to go through klog. Once it is GA, we can lift diff --git a/hack/logcheck.conf b/hack/logcheck.conf index d8a3880099e..a5e949cda68 100644 --- a/hack/logcheck.conf +++ b/hack/logcheck.conf @@ -59,6 +59,7 @@ contextual k8s.io/kubernetes/pkg/kubelet/status/.* contextual k8s.io/kubernetes/pkg/kubelet/sysctl/.* contextual k8s.io/kubernetes/pkg/kubelet/apis/.* contextual k8s.io/kubernetes/pkg/kubelet/kubeletconfig/.* +contextual k8s.io/kubernetes/pkg/kubelet/nodeshutdown/.* # As long as contextual logging is alpha or beta, all WithName, WithValues, # NewContext calls have to go through klog. Once it is GA, we can lift diff --git a/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux.go b/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux.go index b6c02fbb94b..5b1f2f5aa05 100644 --- a/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux.go +++ b/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux.go @@ -51,7 +51,7 @@ type dbusInhibiter interface { InhibitShutdown() (systemd.InhibitLock, error) ReleaseInhibitLock(lock systemd.InhibitLock) error ReloadLogindConf() error - MonitorShutdown() (<-chan bool, error) + MonitorShutdown(klog.Logger) (<-chan bool, error) OverrideInhibitDelay(inhibitDelayMax time.Duration) error } @@ -207,7 +207,7 @@ func (m *managerImpl) start() (chan struct{}, error) { return nil, err } - events, err := m.dbusCon.MonitorShutdown() + events, err := m.dbusCon.MonitorShutdown(m.logger) if err != nil { releaseErr := m.dbusCon.ReleaseInhibitLock(m.inhibitLock) if releaseErr != nil { diff --git a/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux_test.go b/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux_test.go index 692541cee5a..85ef3aeb9f4 100644 --- a/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux_test.go +++ b/pkg/kubelet/nodeshutdown/nodeshutdown_manager_linux_test.go @@ -37,6 +37,7 @@ import ( utilfeature "k8s.io/apiserver/pkg/util/feature" "k8s.io/client-go/tools/record" featuregatetesting "k8s.io/component-base/featuregate/testing" + "k8s.io/klog/v2" "k8s.io/klog/v2/ktesting" _ "k8s.io/klog/v2/ktesting/init" // activate ktesting command line flags "k8s.io/kubernetes/pkg/apis/scheduling" @@ -81,7 +82,7 @@ func (f *fakeDbus) ReloadLogindConf() error { return nil } -func (f *fakeDbus) MonitorShutdown() (<-chan bool, error) { +func (f *fakeDbus) MonitorShutdown(_ klog.Logger) (<-chan bool, error) { return f.shutdownChan, nil } diff --git a/pkg/kubelet/nodeshutdown/systemd/inhibit_linux.go b/pkg/kubelet/nodeshutdown/systemd/inhibit_linux.go index 270177bc2b7..18b6b9be869 100644 --- a/pkg/kubelet/nodeshutdown/systemd/inhibit_linux.go +++ b/pkg/kubelet/nodeshutdown/systemd/inhibit_linux.go @@ -135,7 +135,7 @@ func (bus *DBusCon) ReloadLogindConf() error { // MonitorShutdown detects the node shutdown by watching for "PrepareForShutdown" logind events. // see https://www.freedesktop.org/wiki/Software/systemd/inhibit/ for more details. -func (bus *DBusCon) MonitorShutdown() (<-chan bool, error) { +func (bus *DBusCon) MonitorShutdown(logger klog.Logger) (<-chan bool, error) { err := bus.SystemBus.AddMatchSignal(dbus.WithMatchInterface(logindInterface), dbus.WithMatchMember("PrepareForShutdown"), dbus.WithMatchObjectPath("/org/freedesktop/login1")) if err != nil { @@ -155,12 +155,12 @@ func (bus *DBusCon) MonitorShutdown() (<-chan bool, error) { return } if event == nil || len(event.Body) == 0 { - klog.ErrorS(nil, "Failed obtaining shutdown event, PrepareForShutdown event was empty") + logger.Error(nil, "Failed obtaining shutdown event, PrepareForShutdown event was empty") continue } shutdownActive, ok := event.Body[0].(bool) if !ok { - klog.ErrorS(nil, "Failed obtaining shutdown event, PrepareForShutdown event was not bool type as expected") + logger.Error(nil, "Failed obtaining shutdown event, PrepareForShutdown event was not bool type as expected") continue } shutdownChan <- shutdownActive diff --git a/pkg/kubelet/nodeshutdown/systemd/inhibit_linux_test.go b/pkg/kubelet/nodeshutdown/systemd/inhibit_linux_test.go index f854b035181..bd00aa70bb8 100644 --- a/pkg/kubelet/nodeshutdown/systemd/inhibit_linux_test.go +++ b/pkg/kubelet/nodeshutdown/systemd/inhibit_linux_test.go @@ -27,6 +27,7 @@ import ( "github.com/godbus/dbus/v5" "github.com/stretchr/testify/assert" + "k8s.io/klog/v2/ktesting" ) type fakeDBusObject struct { @@ -145,6 +146,7 @@ func TestReloadLogindConf(t *testing.T) { } func TestMonitorShutdown(t *testing.T) { + logger, _ := ktesting.NewTestContext(t) var tests = []struct { desc string shutdownActive bool @@ -167,7 +169,7 @@ func TestMonitorShutdown(t *testing.T) { SystemBus: fakeSystemBus, } - outChan, err := bus.MonitorShutdown() + outChan, err := bus.MonitorShutdown(logger) assert.NoError(t, err) done := make(chan bool)