Skip to content

fix(live): propagate caller ctx into ResourceGroup inventory ops#4501

Open
Jaisheesh-2006 wants to merge 3 commits intokptdev:mainfrom
Jaisheesh-2006:fix/live-ctx-propagation
Open

fix(live): propagate caller ctx into ResourceGroup inventory ops#4501
Jaisheesh-2006 wants to merge 3 commits intokptdev:mainfrom
Jaisheesh-2006:fix/live-ctx-propagation

Conversation

@Jaisheesh-2006
Copy link
Copy Markdown
Contributor

Description

pkg/live/inventoryrg.go was using context.TODO() for every live Kubernetes API call it makes — 7 call sites covering Apply, ApplyWithPrune, and the ResourceGroup CRD match check. This PR threads the caller's real context through each of those calls.

Because the upstream inventory.Storage / inventory.ClientFactory interfaces do not accept a ctx parameter, the fix:

  • Carries ctx on InventoryResourceGroup and ClusterClientFactory structs.
  • Injects it at wrap time via two new factories: WrapInventoryObjWithContext(ctx) and NewClusterClientFactoryWithContext(ctx).
  • Falls back to context.Background() when the legacy constructors are used, so the change is fully backward-compatible.
  • Adds a ctx parameter directly to ResourceGroupCRDMatched (not interface-constrained).

Six callers updated to pass their existing command ctx: commands/live/apply, commands/live/destroy, commands/live/migrate, commands/live/livecmd, commands/alpha/live/plan, and pkg/live/planner.

Three unit tests added in pkg/live/inventoryrg_test.go:

  • TestWrapInventoryObjWithContext_StoresContext — verifies the factory persists the passed ctx onto the struct.
  • TestWrapInventoryObj_LeavesContextNil — locks in legacy behavior for callers that haven't migrated.
  • TestContextOrBackground — asserts the fallback path and, crucially, that cancellation on the caller's ctx propagates through (cancel() fires Done()).

Motivation

Before this change, pressing Ctrl-C during kpt live apply / destroy / plan against a slow or unreachable cluster could not cancel the in-flight API call — the CLI would block until the network-level timeout fired. Any context.WithTimeout the caller wired at the command level was silently discarded at this boundary.

context.TODO() is a placeholder from the Go stdlib. This PR fulfills that TODO.

Discovered via a grep-level audit of main for context.TODO() / context.Background() usage in production code paths.

AI use

I have used AI in the creation of this PR.

I have used the following AI tools:

  • Claude Code (Claude Opus 4.7) to audit main for context-propagation gaps, to plan the interface-safe fix (struct-stored ctx + context-aware factory), to apply the refactor across the 10 affected files, and to author the unit tests.

I reviewed every change and can explain the design (why struct-stored ctx instead of adding it to Apply's signature) and the test assertions independently.

pkg/live/inventoryrg.go passed context.TODO() to all 7 of its live
cluster API calls, so Ctrl-C and command-level timeouts could not
cancel an in-flight apply/destroy/plan.

The upstream inventory.Storage interface doesn't accept a ctx
parameter, so carry ctx on the InventoryResourceGroup struct and
inject it at wrap time via a new WrapInventoryObjWithContext factory.
Legacy callers without a ctx fall back to context.Background().
ResourceGroupCRDMatched takes ctx directly. All 6 callers updated.

Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Copilot AI review requested due to automatic review settings April 24, 2026 15:45
@netlify
Copy link
Copy Markdown

netlify Bot commented Apr 24, 2026

Deploy Preview for kptdocs ready!

Name Link
🔨 Latest commit 4887c66
🔍 Latest deploy log https://app.netlify.com/projects/kptdocs/deploys/69ebc166c254f40008e198a7
😎 Deploy Preview https://deploy-preview-4501--kptdocs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@dosubot dosubot Bot added size:L This PR changes 100-499 lines, ignoring generated files. area/live go Pull requests that update Go code labels Apr 24, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Note

Copilot was unable to run its full agentic suite in this review.

This PR ensures caller cancellation/timeouts propagate into live Kubernetes inventory operations by threading a real context.Context through ResourceGroup-based inventory calls instead of using context.TODO().

Changes:

  • Store a caller ctx on inventory wrappers/factories and use it for live K8s API calls in Apply / ApplyWithPrune.
  • Update multiple command paths to pass their existing command context into inventory client creation and CRD-match checks.
  • Add focused unit tests validating context storage and fallback behavior.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
pkg/live/planner/cluster.go Plumbs caller ctx into inventory wrapper when building planner’s inventory client.
pkg/live/inventoryrg.go Stores ctx on InventoryResourceGroup and uses it for dynamic client calls; updates CRD match helper to accept ctx.
pkg/live/inventory-client-factory.go Adds context-aware factory constructor and uses ctx-aware wrapper when provided.
commands/live/apply/cmdapply.go Passes runner ctx into CRD check and inventory client creation for apply.
commands/live/destroy/cmddestroy.go Passes runner ctx into inventory client creation for destroy.
commands/live/livecmd.go Constructs inventory factory with command ctx.
commands/alpha/live/plan/command.go Updates planner construction to pass runner ctx.
commands/live/migrate/migratecmd.go Updates inventory client factory funcs to accept ctx and passes runner ctx through.
commands/live/migrate/migratecmd_test.go Updates test stubs for new ctx-accepting inventory client funcs.
pkg/live/inventoryrg_test.go Adds unit tests for context propagation and fallback behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/live/planner/cluster.go Outdated
Comment thread pkg/live/planner/cluster.go
Comment thread pkg/live/inventoryrg.go Outdated
Comment thread pkg/live/inventoryrg.go
Comment thread pkg/live/inventoryrg.go Outdated
Comment thread pkg/live/inventoryrg.go
Apply/ApplyWithPrune/ResourceGroupCRDMatched passed context.TODO(),
so Ctrl-C and command-level timeouts could not cancel in-flight
apply/destroy/plan calls. Carry ctx on the InventoryResourceGroup
struct and inject it via new WrapInventoryObjWithContext,
NewClusterClientFactoryWithContext, ResourceGroupCRDMatchedWithContext,
and NewClusterPlannerWithContext. Legacy entry points kept as thin
back-compat wrappers; nil ctx normalized to Background().

Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Copilot AI review requested due to automatic review settings April 24, 2026 18:59
@Jaisheesh-2006 Jaisheesh-2006 force-pushed the fix/live-ctx-propagation branch from 3a21b69 to f39ab39 Compare April 24, 2026 18:59
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread pkg/live/inventoryrg.go Outdated
Comment thread pkg/live/inventoryrg.go Outdated
Comment thread pkg/live/inventory-client-factory.go
Signed-off-by: Jaisheesh-2006 <jaicodes2006@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/live go Pull requests that update Go code size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants