From 595548021dc108f524048798c90c5bcc0cfc4215 Mon Sep 17 00:00:00 2001 From: Akanksha Trehun Date: Mon, 31 Aug 2026 14:15:39 +0530 Subject: [PATCH] Thread real context through phase DryRun instead of TODO/Background Disconnect.DryRun called p.Run(context.TODO()) and used context.Background() for its host cleanup, detached from the context that phase.Manager already has in scope while running phases. InstallBinaries and StageBinaries had the same context.Background() pattern in their DryRun paths. Add context.Context to the withDryRun interface and pass the manager's own ctx through to all DryRun implementations, so dry-run phases respect the same cancellation/deadline as a real run. Signed-off-by: Akanksha Trehun --- phase/configure_k0s.go | 2 +- phase/disconnect.go | 6 +++--- phase/get_kubeconfig.go | 2 +- phase/install_binaries.go | 4 ++-- phase/manager.go | 4 ++-- phase/reset_controllers.go | 2 +- phase/reset_leader.go | 2 +- phase/reset_workers.go | 2 +- phase/stage_binaries.go | 4 ++-- 9 files changed, 14 insertions(+), 14 deletions(-) diff --git a/phase/configure_k0s.go b/phase/configure_k0s.go index 759510a1..578ca75e 100644 --- a/phase/configure_k0s.go +++ b/phase/configure_k0s.go @@ -150,7 +150,7 @@ func (p *ConfigureK0s) Prepare(config *v1beta1.Cluster) error { } // DryRun prints the actions that would be taken -func (p *ConfigureK0s) DryRun() error { +func (p *ConfigureK0s) DryRun(_ context.Context) error { for _, h := range p.hosts { p.DryMsgf(h, "write k0s configuration to %s", h.Configurer.K0sConfigPath()) switch p.configSource { diff --git a/phase/disconnect.go b/phase/disconnect.go index cc11b144..7301efff 100644 --- a/phase/disconnect.go +++ b/phase/disconnect.go @@ -17,8 +17,8 @@ func (p *Disconnect) Title() string { } // DryRun cleans up the temporary k0s binary from the hosts -func (p *Disconnect) DryRun() error { - _ = p.Config.Spec.Hosts.ParallelEach(context.Background(), func(_ context.Context, h *cluster.Host) error { +func (p *Disconnect) DryRun(ctx context.Context) error { + _ = p.Config.Spec.Hosts.ParallelEach(ctx, func(_ context.Context, h *cluster.Host) error { if h.Metadata.K0sBinaryTempFile != "" && h.FS().FileExist(h.Metadata.K0sBinaryTempFile) { _ = h.Sudo().FS().Remove(h.Metadata.K0sBinaryTempFile) } @@ -26,7 +26,7 @@ func (p *Disconnect) DryRun() error { return nil }) - return p.Run(context.TODO()) + return p.Run(ctx) } // Run the phase diff --git a/phase/get_kubeconfig.go b/phase/get_kubeconfig.go index 730d85c7..44821368 100644 --- a/phase/get_kubeconfig.go +++ b/phase/get_kubeconfig.go @@ -35,7 +35,7 @@ var readKubeconfig = func(h *cluster.Host) (string, error) { return output, nil } -func (p *GetKubeconfig) DryRun() error { +func (p *GetKubeconfig) DryRun(_ context.Context) error { p.DryMsg(p.Config.Spec.Hosts.Controllers()[0], "get admin kubeconfig") return nil } diff --git a/phase/install_binaries.go b/phase/install_binaries.go index f2d870b7..2160a67b 100644 --- a/phase/install_binaries.go +++ b/phase/install_binaries.go @@ -58,9 +58,9 @@ func (p *InstallBinaries) ShouldRun() bool { } // DryRun reports what would happen if Run is called. -func (p *InstallBinaries) DryRun() error { +func (p *InstallBinaries) DryRun(ctx context.Context) error { return p.parallelDo( - context.Background(), + ctx, p.Config.Spec.Hosts.Filter(func(h *cluster.Host) bool { return h.Metadata.K0sBinaryTempFile != "" }), func(_ context.Context, h *cluster.Host) error { p.DryMsgf(h, "install k0s %s binary from %s to %s", p.Config.Spec.K0s.Version, h.Metadata.K0sBinaryTempFile, h.K0sInstallLocation()) diff --git a/phase/manager.go b/phase/manager.go index e48fe102..81f93d10 100644 --- a/phase/manager.go +++ b/phase/manager.go @@ -95,7 +95,7 @@ type withmanager interface { } type withDryRun interface { - DryRun() error + DryRun(context.Context) error } // In-phase hooks for phases to run logic immediately before/after Run(). @@ -260,7 +260,7 @@ func (m *Manager) Run(ctx context.Context) error { if dp, ok := p.(withDryRun); ok && m.DryRun { ran = append(ran, p) - if err := dp.DryRun(); err != nil { + if err := dp.DryRun(ctx); err != nil { result = err return result } diff --git a/phase/reset_controllers.go b/phase/reset_controllers.go index 26742618..df1df1ce 100644 --- a/phase/reset_controllers.go +++ b/phase/reset_controllers.go @@ -60,7 +60,7 @@ func (p *ResetControllers) ShouldRun() bool { } // DryRun reports nodes that would get reset -func (p *ResetControllers) DryRun() error { +func (p *ResetControllers) DryRun(_ context.Context) error { for _, h := range p.hosts { p.DryMsg(h, "reset node") } diff --git a/phase/reset_leader.go b/phase/reset_leader.go index 4da61baf..ecea5fba 100644 --- a/phase/reset_leader.go +++ b/phase/reset_leader.go @@ -40,7 +40,7 @@ func (p *ResetLeader) Prepare(config *v1beta1.Cluster) error { } // DryRun reports that the host will be reset -func (p *ResetLeader) DryRun() error { +func (p *ResetLeader) DryRun(_ context.Context) error { p.DryMsg(p.leader, "reset node") return nil } diff --git a/phase/reset_workers.go b/phase/reset_workers.go index 5700ed98..eba48dc6 100644 --- a/phase/reset_workers.go +++ b/phase/reset_workers.go @@ -59,7 +59,7 @@ func (p *ResetWorkers) ShouldRun() bool { } // DryRun reports the nodes will be reset -func (p *ResetWorkers) DryRun() error { +func (p *ResetWorkers) DryRun(_ context.Context) error { for _, h := range p.hosts { p.DryMsg(h, "node would be reset") } diff --git a/phase/stage_binaries.go b/phase/stage_binaries.go index f81e072a..f73dd87c 100644 --- a/phase/stage_binaries.go +++ b/phase/stage_binaries.go @@ -60,8 +60,8 @@ func (p *StageBinaries) ShouldRun() bool { // not a permanent cluster change — on success, the temp file is removed by // Disconnect (including its DryRun behavior), while CleanUp is only invoked on // failure paths. CleanUp is also called here on error as an extra safety net. -func (p *StageBinaries) DryRun() error { - if err := p.Run(context.Background()); err != nil { +func (p *StageBinaries) DryRun(ctx context.Context) error { + if err := p.Run(ctx); err != nil { p.CleanUp() return err }