Skip to content

terraform: observe — TerraformWatchOp, describeResources() over terraform show -json, and the GitHub cron watch #2087

Closed

Description

@lex00

Sub-issue 6 of #2081.

With #2086's activities in place, observe is two things: an Op that reports drift on a schedule, and a describeResources() that answers the lifecycle question for declared roots.

src/composites/terraform-watch-op.ts returns { op, schedule? }, the shape lexicons/temporal/src/composites/workflow-audit-op.ts and reconcile-op.ts both use (schedule present only when a cron was given). Phases Init, then Plan with -detailed-exitcode, with outcomeAttribute: { name: "Drift", from: "changed" } on the plan step. Finding modes report | issue | pull-request, matching OpFindingMode in packages/core/src/lexicon.ts. For issue and PR modes reuse reconcilePr from lexicons/temporal/src/op/activities/reconcile.ts, which is the activity that shells to gh issue create / gh pr create and returns issueUrl / prUrl. Note that WorkflowAuditOp's own activity, workflowSupplyChainAudit in lexicons/temporal/src/op/activities/workflow-audit.ts, only computes findings and a summary and opens nothing, so WorkflowAuditOp is the composite precedent, not the opening mechanism. If reconcilePr cannot take a pre-built summary, make that additive change here rather than copying the gh calls again.

Sensitive outputs are a hard rule. terraform show -json on a plan file carries resource attribute values, including provider secrets not marked sensitive. Only the -no-color human plan captured by terraformPlan may be posted into an issue or PR body; the JSON stays in-process for counting changes and destroys.

src/describe-resources.ts runs terraform show -json per declared root and maps values.root_module.resources[] to entities by address, keyed exactly the way buildRoots() keys them in #2083, the same discipline lexicons/k8s/src/kustomize/root.ts follows (root label prefix plus a deterministic per-document id). Tri-state per packages/core/src/observation.ts: a root whose init or show fails reports every declared entity as not-observed with reason read-failed and the root named in detail, never absent, so a failed read never renders as a list of creates. Wire it like lexicons/k3s/src/describe-resources.ts and its lazy import in lexicons/k3s/src/plugin.ts.

Ownership is where terraform differs from every other lexicon. The state file is the ownership answer here. Everything in state is owned, anything else is unknown. Declare ownershipChannel ({ keys, reads: ["describeResources"] }) so that answer is declared rather than silently degraded. The lexicon doc page must carry a sentence pointing at the third axis of docs/src/content/docs/concepts/lifecycle-models.mdx ("Who answers 'is this mine?'"), saying plainly that terraform sits in the trusted-state-file row while chant elsewhere sits in the live-marker row.

Run describeObservationConformance from @intentius/chant-test-utils (packages/test-utils/src/observation-conformance.ts), passing plugin.ownershipChannel so the ownership assertion is behavioral.

The GitHub cron watch is an example, not new generator code. Add an example directory with a TerraformWatchOp carrying a schedule and findingMode: "issue", plus a test calling generateOpsPipeline (packages/core/src/op/generate-pipeline.ts) against the github lexicon and asserting the emitted YAML. beforeScript lines are emitted as run: steps (lexicons/github/src/components/generate-op-pipeline.ts, around line 127), so the install is a shell line, the HashiCorp apt repo or an unzip of the release. Using the hashicorp/setup-terraform action would need a uses:-shaped option that does not exist; file that separately if wanted.

Out of scope: pull_request plan and push-to-main apply triggers (#2084), HCL regeneration from live (#2089).

Blocked on #2086. Runs in parallel with #2088.

Acceptance

  • src/composites/terraform-watch-op.ts returns { op, schedule? }; a test pins phases, the Drift outcome attribute, and that omitting schedule returns no schedule.
  • report, issue and pull-request each produce the expected step wiring; report opens nothing.
  • A test asserts the issue/PR body is the -no-color plan text and that no plan JSON reaches it.
  • src/describe-resources.ts maps values.root_module.resources[] addresses to buildRoots() keys, pinned by a recorded show -json fixture.
  • A failed init or show yields not-observed with reason read-failed for every declared entity of that root, never absent.
  • Everything in state reports owned, a resource found outside state reports unknown, both tested.
  • ownershipChannel declared on the plugin and passed to the conformance suite.
  • describeObservationConformance runs green with at least one present, one not-observed and one ownership scenario.
  • An example directory with a scheduled watch Op plus a test asserting the generateOpsPipeline YAML: cron, workflow_dispatch, concurrency group, issues: write, the terraform install line.
  • The lexicon doc page states the state-file ownership position and links concepts/lifecycle-models.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:lifecyclearea:terraformenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions