From ec9f0d55360f74337f9ef40879434a063821ff5b Mon Sep 17 00:00:00 2001 From: zhangzhifei16 Date: Tue, 22 Jul 2025 11:10:31 +0800 Subject: [PATCH] chore(kubelet): migrate nodestatus to contextual logging --- pkg/kubelet/nodestatus/setters.go | 17 ++++++++++------- pkg/kubelet/nodestatus/setters_test.go | 23 ++++++++++++----------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/pkg/kubelet/nodestatus/setters.go b/pkg/kubelet/nodestatus/setters.go index 24728267a30..4a22a830feb 100644 --- a/pkg/kubelet/nodestatus/setters.go +++ b/pkg/kubelet/nodestatus/setters.go @@ -81,17 +81,18 @@ func NodeAddress(nodeIPs []net.IP, // typically Kubelet.nodeIPs secondaryNodeIPSpecified := secondaryNodeIP != nil && !secondaryNodeIP.IsUnspecified() return func(ctx context.Context, node *v1.Node) error { + logger := klog.FromContext(ctx) if nodeIPSpecified { if err := validateNodeIPFunc(nodeIP); err != nil { return fmt.Errorf("failed to validate nodeIP: %v", err) } - klog.V(4).InfoS("Using node IP", "IP", nodeIP.String()) + logger.V(4).Info("Using node IP", "IP", nodeIP.String()) } if secondaryNodeIPSpecified { if err := validateNodeIPFunc(secondaryNodeIP); err != nil { return fmt.Errorf("failed to validate secondaryNodeIP: %v", err) } - klog.V(4).InfoS("Using secondary node IP", "IP", secondaryNodeIP.String()) + logger.V(4).Info("Using secondary node IP", "IP", secondaryNodeIP.String()) } if externalCloudProvider && nodeIPSpecified { @@ -202,6 +203,7 @@ func MachineInfo(nodeName string, localStorageCapacityIsolation bool, ) Setter { return func(ctx context.Context, node *v1.Node) error { + logger := klog.FromContext(ctx) // Note: avoid blindly overwriting the capacity in case opaque // resources are being advertised. if node.Status.Capacity == nil { @@ -221,7 +223,7 @@ func MachineInfo(nodeName string, node.Status.Capacity[v1.ResourceCPU] = *resource.NewMilliQuantity(0, resource.DecimalSI) node.Status.Capacity[v1.ResourceMemory] = resource.MustParse("0Gi") node.Status.Capacity[v1.ResourcePods] = *resource.NewQuantity(int64(maxPods), resource.DecimalSI) - klog.ErrorS(err, "Error getting machine info") + logger.Error(err, "Error getting machine info") } else { node.Status.NodeInfo.MachineID = info.MachineID node.Status.NodeInfo.SystemUUID = info.SystemUUID @@ -263,13 +265,13 @@ func MachineInfo(nodeName string, devicePluginCapacity, devicePluginAllocatable, removedDevicePlugins = devicePluginResourceCapacityFunc() for k, v := range devicePluginCapacity { if old, ok := node.Status.Capacity[k]; !ok || old.Value() != v.Value() { - klog.V(2).InfoS("Updated capacity for device plugin", "plugin", k, "capacity", v.Value()) + logger.V(2).Info("Updated capacity for device plugin", "plugin", k, "capacity", v.Value()) } node.Status.Capacity[k] = v } for _, removedResource := range removedDevicePlugins { - klog.V(2).InfoS("Set capacity for removed resource to 0 on device removal", "device", removedResource) + logger.V(2).Info("Set capacity for removed resource to 0 on device removal", "device", removedResource) // Set the capacity of the removed resource to 0 instead of // removing the resource from the node status. This is to indicate // that the resource is managed by device plugin and had been @@ -315,7 +317,7 @@ func MachineInfo(nodeName string, for k, v := range devicePluginAllocatable { if old, ok := node.Status.Allocatable[k]; !ok || old.Value() != v.Value() { - klog.V(2).InfoS("Updated allocatable", "device", k, "allocatable", v.Value()) + logger.V(2).Info("Updated allocatable", "device", k, "allocatable", v.Value()) } node.Status.Allocatable[k] = v } @@ -540,7 +542,8 @@ func ReadyCondition( recordEventFunc(v1.EventTypeNormal, events.NodeReady) } else { recordEventFunc(v1.EventTypeNormal, events.NodeNotReady) - klog.InfoS("Node became not ready", "node", klog.KObj(node), "condition", newNodeReadyCondition) + logger := klog.FromContext(ctx) + logger.Info("Node became not ready", "node", klog.KObj(node), "condition", newNodeReadyCondition) } } return nil diff --git a/pkg/kubelet/nodestatus/setters_test.go b/pkg/kubelet/nodestatus/setters_test.go index 6e5971ed9af..c7f441a9dc3 100644 --- a/pkg/kubelet/nodestatus/setters_test.go +++ b/pkg/kubelet/nodestatus/setters_test.go @@ -47,6 +47,7 @@ import ( kubecontainertest "k8s.io/kubernetes/pkg/kubelet/container/testing" "k8s.io/kubernetes/pkg/kubelet/events" "k8s.io/kubernetes/pkg/kubelet/util/sliceutils" + "k8s.io/kubernetes/test/utils/ktesting" netutils "k8s.io/utils/net" "k8s.io/utils/ptr" ) @@ -279,7 +280,7 @@ func TestNodeAddress(t *testing.T) { } for _, testCase := range cases { t.Run(testCase.name, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) // testCase setup existingNode := &v1.Node{ ObjectMeta: metav1.ObjectMeta{ @@ -385,7 +386,7 @@ func TestNodeAddress_NoCloudProvider(t *testing.T) { } for _, testCase := range cases { t.Run(testCase.name, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) // testCase setup existingNode := &v1.Node{ ObjectMeta: metav1.ObjectMeta{Name: testKubeletHostname, Annotations: make(map[string]string)}, @@ -914,7 +915,7 @@ func TestMachineInfo(t *testing.T) { } t.Run(tc.desc, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) machineInfoFunc := func() (*cadvisorapiv1.MachineInfo, error) { return tc.machineInfo, tc.machineInfoError } @@ -1081,7 +1082,7 @@ func TestVersionInfo(t *testing.T) { t.Run(tc.desc, func(t *testing.T) { featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.DisableNodeKubeProxyVersion, !tc.kubeProxyVersion) - ctx := context.Background() + ctx := ktesting.Init(t) versionInfoFunc := func() (*cadvisorapiv1.VersionInfo, error) { return tc.versionInfo, tc.versionInfoError } @@ -1158,7 +1159,7 @@ func TestImages(t *testing.T) { for _, tc := range cases { t.Run(tc.desc, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) imageListFunc := func() ([]kubecontainer.Image, error) { // today, imageListFunc is expected to return a sorted list, // but we may choose to sort in the setter at some future point @@ -1324,7 +1325,7 @@ func TestReadyCondition(t *testing.T) { } for _, tc := range cases { t.Run(tc.desc, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) runtimeErrorsFunc := func() error { return tc.runtimeErrors } @@ -1458,7 +1459,7 @@ func TestMemoryPressureCondition(t *testing.T) { } for _, tc := range cases { t.Run(tc.desc, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) events := []testEvent{} recordEventFunc := func(eventType, event string) { events = append(events, testEvent{ @@ -1580,7 +1581,7 @@ func TestPIDPressureCondition(t *testing.T) { } for _, tc := range cases { t.Run(tc.desc, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) events := []testEvent{} recordEventFunc := func(eventType, event string) { events = append(events, testEvent{ @@ -1702,7 +1703,7 @@ func TestDiskPressureCondition(t *testing.T) { } for _, tc := range cases { t.Run(tc.desc, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) events := []testEvent{} recordEventFunc := func(eventType, event string) { events = append(events, testEvent{ @@ -1763,7 +1764,7 @@ func TestVolumesInUse(t *testing.T) { for _, tc := range cases { t.Run(tc.desc, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) syncedFunc := func() bool { return tc.synced } @@ -1801,7 +1802,7 @@ func TestDaemonEndpoints(t *testing.T) { }, } { t.Run(test.name, func(t *testing.T) { - ctx := context.Background() + ctx := ktesting.Init(t) existingNode := &v1.Node{ ObjectMeta: metav1.ObjectMeta{ Name: testKubeletHostname,