From f58d1e101ff9773c4ec345a10a0d33be5534cda5 Mon Sep 17 00:00:00 2001 From: Aditi Gupta Date: Tue, 16 Sep 2025 23:48:02 -0700 Subject: [PATCH] refactor(controller): Use WithContext variants in cloud node controllers This change refactors the cloud-specific versions of the node lifecycle and node IPAM controllers to use a context.Context for cancellation and contextual logging, replacing the legacy stopCh pattern. This is a follow-up to PR #133985, where these controllers were separated out due to their use in the legacy Cloud Controller Manager (CCM). It is a known issue that the CCM's startup logic does not pass the controller name via the context. This change proceeds with the refactoring to unify the cancellation logic across controllers, while acknowledging that contextual logs will be less detailed when these controllers are run in the CCM. Signed-off-by: Aditi Gupta --- cmd/kube-controller-manager/names/controller_names.go | 1 - pkg/controller/nodeipam/ipam/range_allocator.go | 2 +- pkg/controller/nodeipam/node_ipam_controller.go | 2 +- pkg/controller/nodelifecycle/node_lifecycle_controller.go | 2 +- 4 files changed, 3 insertions(+), 4 deletions(-) diff --git a/cmd/kube-controller-manager/names/controller_names.go b/cmd/kube-controller-manager/names/controller_names.go index a00358c3826..baaa97c076b 100644 --- a/cmd/kube-controller-manager/names/controller_names.go +++ b/cmd/kube-controller-manager/names/controller_names.go @@ -36,7 +36,6 @@ package names // 2.1. [TODO] logging should use a canonical controller name when referencing a controller (Eg. Starting X, Shutting down X) // 2.2. [TODO] emitted events should have an EventSource.Component set to the controller name (usually when initializing an EventRecorder) // 2.3. [TODO] registering ControllerManagerMetrics with ControllerStarted and ControllerStopped -// 2.4. [TODO] calling WaitForNamedCacheSync // 3. defining controller options for "--help" command or generated documentation // 3.1. controller name should be used to create a pflag.FlagSet when registering controller options (the name is rendered in a controller flag group header) in options.KubeControllerManagerOptions // 3.2. when defined flag's help mentions a controller name diff --git a/pkg/controller/nodeipam/ipam/range_allocator.go b/pkg/controller/nodeipam/ipam/range_allocator.go index 50df3de49ce..cbcc91678fa 100644 --- a/pkg/controller/nodeipam/ipam/range_allocator.go +++ b/pkg/controller/nodeipam/ipam/range_allocator.go @@ -183,7 +183,7 @@ func (r *rangeAllocator) Run(ctx context.Context) { logger.Info("Starting range CIDR allocator") defer logger.Info("Shutting down range CIDR allocator") - if !cache.WaitForNamedCacheSync("cidrallocator", ctx.Done(), r.nodesSynced) { + if !cache.WaitForNamedCacheSyncWithContext(ctx, r.nodesSynced) { return } diff --git a/pkg/controller/nodeipam/node_ipam_controller.go b/pkg/controller/nodeipam/node_ipam_controller.go index 27b079839d0..ce101c840b9 100644 --- a/pkg/controller/nodeipam/node_ipam_controller.go +++ b/pkg/controller/nodeipam/node_ipam_controller.go @@ -141,7 +141,7 @@ func (nc *Controller) Run(ctx context.Context) { klog.FromContext(ctx).Info("Starting ipam controller") defer klog.FromContext(ctx).Info("Shutting down ipam controller") - if !cache.WaitForNamedCacheSync("node", ctx.Done(), nc.nodeInformerSynced) { + if !cache.WaitForNamedCacheSyncWithContext(ctx, nc.nodeInformerSynced) { return } diff --git a/pkg/controller/nodelifecycle/node_lifecycle_controller.go b/pkg/controller/nodelifecycle/node_lifecycle_controller.go index 3aff066e832..c1a02346aec 100644 --- a/pkg/controller/nodelifecycle/node_lifecycle_controller.go +++ b/pkg/controller/nodelifecycle/node_lifecycle_controller.go @@ -464,7 +464,7 @@ func (nc *Controller) Run(ctx context.Context) { logger.Info("Starting node controller") defer logger.Info("Shutting down node controller") - if !cache.WaitForNamedCacheSync("taint", ctx.Done(), nc.leaseInformerSynced, nc.nodeInformerSynced, nc.podInformerSynced, nc.daemonSetInformerSynced) { + if !cache.WaitForNamedCacheSyncWithContext(ctx, nc.leaseInformerSynced, nc.nodeInformerSynced, nc.podInformerSynced, nc.daemonSetInformerSynced) { return }