checkrules

package
v0.34.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MPL-2.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const Name = "checkrules"

Name is the port's name in the internal/engine/forkport registry.

Variables

This section is empty.

Functions

func Enabled

func Enabled() bool

Enabled reports whether the port is on.

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

func Report(ctx context.Context, checkBlock bool) (hcl.DiagnosticSeverity, bool)

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

func WarnUnknown(ctx context.Context) bool

WarnUnknown reports whether a check assertion that cannot be decided yet warns "known after apply" under ctx: while planning.

func WithPhase

func WithPhase(ctx context.Context, phase Phase) context.Context

WithPhase returns ctx for evaluating in phase.

func WithResults

func WithResults(ctx context.Context, r *Results) context.Context

WithResults returns ctx for a walk that reports into r. With the port off it returns ctx unchanged, and nothing is recorded.

Types

type Phase

type Phase int

Phase is what the evaluator is evaluating for.

const (
	// Apply is the default, for a context [WithPhase] never saw.
	Apply Phase = iota
	Plan
	RefreshOnly
	DestroyRefresh
	Validate
	// DestroyApply is the apply of a destroy plan (C3, #2410), which checks
	// nothing in the default runtime.
	DestroyApply
)

func PhaseForPlan

func PhaseForPlan(refreshOnly bool) Phase

PhaseForPlan is RefreshOnly for a refresh-only plan and Plan otherwise.

func PhaseOf

func PhaseOf(ctx context.Context) Phase

PhaseOf is the phase ctx was given with WithPhase, or Apply.

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

func NewResults(destroy bool) *Results

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL