Documentation
¶
Overview ¶
Package checkrules is a choudoufu fork port (#2409, of #2256): check blocks and resource preconditions and postconditions on OpenTofu's new runtime, until upstream's #4390 lands.
Upstream's new runtime compiles output preconditions and nothing else: resource preconditions and postconditions are TODO stubs and check blocks are not compiled at all, so the gap table refused all three. The port evaluates them where upstream's issue proposes, inside the evaluator (internal/lang/eval/internal/tofu2024/fork_checkrules.go):
- a resource instance's preconditions guard its configuration value, so a failing one stops the instance from being planned or applied;
- its postconditions are checked against the result the planning or apply engine returns for it, so an apply-time failure is reported after the object is saved, as the default runtime does;
- each check block's assertions are checked whenever the evaluator walks the module, and a failure is a warning.
What a failing condition reports depends on the phase, as in the default runtime: a resource condition failure is an error in a plan or an apply, a warning in a refresh-only plan, and nothing at all in a destroy plan's refresh or in validation; a check assertion failure is a warning except in those last two, and one that cannot be decided yet warns "known after apply" while planning. The planning engine, validation and the apply of a destroy plan name their phase with WithPhase; any other apply needs nothing. Output preconditions, which upstream compiles itself, are reported the same way while the port is on. A check block's scoped data source is read by the datasources port (#2414).
The port also records every check's result for the plan's Checks and the state's CheckResults (C3 of #2410, see Results), and the call sites of that are commented "choudoufu fork port (#2410)".
Every call site is commented "choudoufu fork port (#2409): remove when upstream #4390 lands". Removing the port is deleting this package, fork_checkrules.go, those call sites and the "checks-conditions" row's fork_port and switch in internal/live/newruntime/gaps.json.
Switched off (forkport.Enabled), nothing is compiled and the gap table's preflight refuses check blocks and resource conditions again.
Index ¶
- Constants
- func Enabled() bool
- func Record(ctx context.Context, obj addrs.Checkable, ruleType addrs.CheckRuleType, ...)
- func RecordObjects(ctx context.Context, configAddr addrs.ConfigCheckable, objs []addrs.Checkable, ...)
- func Report(ctx context.Context, checkBlock bool) (hcl.DiagnosticSeverity, bool)
- func WarnUnknown(ctx context.Context) bool
- func WithPhase(ctx context.Context, phase Phase) context.Context
- func WithResults(ctx context.Context, r *Results) context.Context
- type Phase
- type Results
Constants ¶
const Name = "checkrules"
Name is the port's name in the internal/engine/forkport registry.
Variables ¶
This section is empty.
Functions ¶
func Record ¶
func Record(ctx context.Context, obj addrs.Checkable, ruleType addrs.CheckRuleType, index int, status checks.Status, msg string)
Record records the result of the index'th rule of type ruleType of obj. msg is the failure message, for a failure. A later report of the same rule, from a value evaluated twice, replaces the earlier one.
func RecordObjects ¶
func RecordObjects(ctx context.Context, configAddr addrs.ConfigCheckable, objs []addrs.Checkable, placeholder bool)
RecordObjects records the instances a resource has, so that one whose rules never ran is reported with unknown results, and one with none is reported as having none. placeholder is true when the instances are not known yet.
func Report ¶
Report says how a failing condition is reported under ctx: with what severity, and whether at all. checkBlock is true for a check block's assertion and false for a resource precondition or postcondition.
func WarnUnknown ¶
WarnUnknown reports whether a check assertion that cannot be decided yet warns "known after apply" under ctx: while planning.
Types ¶
type Phase ¶
type Phase int
Phase is what the evaluator is evaluating for.
func PhaseForPlan ¶
PhaseForPlan is RefreshOnly for a refresh-only plan and Plan otherwise.
type Results ¶
type Results struct {
// contains filtered or unexported fields
}
Results collects the check results of one plan or apply on the new runtime, for the plan's Checks and the state's CheckResults (C3 of #2410). The evaluator reports into the Results its context carries (WithResults) as it goes; the default runtime's graph nodes report into a checks.State the same way.
The evaluator builds module instances lazily, so no single point knows every checkable object. A resource reports its instances when it decides them (RecordObjects); outputs, check blocks and variables are known by the results they report. Whatever the walk never reached is in a module with no instances, and Results.CheckResults reports it with none, as the default runtime does. A configuration object under an instance whose expansion is not known yet stays unknown.
A destroy plan and its apply check only input variables in the default runtime, so a destroy Results records only those and leaves every other configuration object unknown.
func NewResults ¶
NewResults returns an empty collector, for a destroy plan or its apply if destroy is true.
func (*Results) CheckResults ¶
func (r *Results) CheckResults(config *configs.Config) *states.CheckResults
CheckResults is what was recorded, as the default runtime's states.NewCheckResults gives it for config.