tofu: Context.Plan now takes a context.Context

This continues our ongoing effort to get a coherent chain of
context.Context all the way from "package main" to all of our calls to
external components.

Context.Plan does not yet do anything with its new context, but this gets
the context plumbed in enough that we should be able to pass values like
telemetry spans all the way from the top-level in future.

OpenTofu has some historical situational private uses of context.Context
to handle the graceful shutdown behaviors. Those use context.Context as
a private implementation detail rather than public API, and so this commit
leaves them as-is and adds a new "primary context" alongside. Hopefully
in future refactoring we can simplify this to use the primary context also
as the primary cancellation signal, but that's too risky a change to bundle
in with this otherwise-mostly-harmless context plumbing.

All of the _test.go file updates here are purely mechanical additions of
the extra argument. No test is materially modified by this change, which
is intentional to get some assurance that isn't a breaking change.

Signed-off-by: Martin Atkins <mart@degeneration.co.uk>
This commit is contained in:
Martin Atkins
2024-11-12 15:19:45 -08:00
parent 35bedc479f
commit 3d5039f1f3
16 changed files with 648 additions and 614 deletions

View File

@@ -32,6 +32,15 @@ func (b *Local) opPlan(
var diags tfdiags.Diagnostics
// For the moment we have a bit of a tangled mess of context.Context here, for
// historical reasons. Hopefully we'll clean this up one day, but here's the
// guide for now:
// - ctx is used only for its values, and should be connected to the top-level ctx
// from "package main" so that we can obtain telemetry objects, etc from it.
// - stopCtx is cancelled to trigger a graceful shutdown.
// - cancelCtx is cancelled for a graceless shutdown.
ctx := context.WithoutCancel(stopCtx)
if op.PlanFile != nil {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
@@ -108,7 +117,7 @@ func (b *Local) opPlan(
defer panicHandler()
defer close(doneCh)
log.Printf("[INFO] backend/local: plan calling Plan")
plan, planDiags = lr.Core.Plan(lr.Config, lr.InputState, lr.PlanOpts)
plan, planDiags = lr.Core.Plan(ctx, lr.Config, lr.InputState, lr.PlanOpts)
}()
if b.opWait(doneCh, stopCtx, cancelCtx, lr.Core, opState, op.View) {