Skip to content

refactor(simulate): build one health model per run - #663

Draft
jcogilvie wants to merge 28 commits into
crossplane:mainfrom
jcogilvie:jco-simulate-health-run
Draft

jcogilvie wants to merge 28 commits into
crossplane:mainfrom
jcogilvie:jco-simulate-health-run

Conversation

@jcogilvie

@jcogilvie jcogilvie commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Description of your changes

Fixes #612

Stacked on #566; review only the commits after test(simulate): require summary.health before comparing its counts.

Health in simulate output used to be worked out separately for each output. The plan text, xr -o diff, composition -o diff, resource simulate, JSON/YAML and the exit status each walked the results themselves to decide which XR lists an unhealthy resource and what to count. That's how #554 ended up needing three separate fixes for nested XRs. This PR builds one health model per simulate run and has every output read from it.

  • The model. health.Build(composites, shown) returns a health.Run. It records, for each composite, which composed resources it owns once nested XRs are resolved (Owned), plus the run's Counts and Failing. Ownership and counting live only in internal/health. Tally, Entry and OwnedElsewhere are now unexported.
  • Built once. The simulate assembler builds the run and stores it on Result. Health(), Failing(), HealthLine(), MarshalJSON and both -o diff renderers (through DiffOptions.Health) read that copy. Resource simulate builds its own with plan.HealthRun, which also replaces plan.HealthCounts and plan.Failing, and keeps feat(resource): report live health in resource simulate #566's LiveNamespace rule.
  • Renderers. xr diff (flat and grouped) and composition diff take ownership and the Health: line from the run instead of tallying diffs. If the run doesn't match the report they're rendering, they return an error rather than panic. With no run (DiffOptions.Health nil) they print no health at all. The diff-counting fallback is gone.
  • Behaviour. JSON/YAML, exit codes and the plan text are unchanged. In one edge case the human Health: line under -o diff now counts from plans, matching JSON and the exit status, where it used to count diffs. Resource simulate plans that have no kind and no name now each count once, so the count always agrees with exit bit 4.
  • Kept on purpose, pinned by tests for follow-up: composition diff prints an unhealthy XR's message twice. Grouped xr diff lists a nested XR's child twice when that XR itself has no health. Flat xr diff leaves an extra blank line after a healthless XR with no diffs. "failing" doesn't appear in the Health: line when only an XR is failing.
  • Test builders. The health builder moves from internal/diff/testutils to a new internal/health/healthtest package. testutils imports internal/plan, so plan's own tests couldn't use it there.

Tests. The first five commits pin today's output for every path before anything changes. That includes 2- and 3-level nested XRs, XRs shared across compositions and filtered impacts, --full-messages, JSON round-trips and exit codes. The refactor commits leave those expected values alone. New table tests cover health.Build, rendering through the stored run for composition and grouped xr diff, the run-mismatch error, and resource simulate plans with no kind and no name.

I have:

  • Read and followed Crossplane's contribution process.
  • Read and followed Crossplane's AI policy and confirmed a human stands behind this PR.
  • Run ./nix.sh flake check to ensure this PR is ready for review.
  • Added or updated unit tests.
  • Linked a PR or a docs tracking issue to document this change. No user-facing change.
  • Added backport release-x.y labels to auto-backport this PR.

Need help with this checklist? See the cheat sheet.

🤖 Generated with Claude Code

jcogilvie and others added 28 commits December 3, 2026 18:00
Planner.Plan already fetches each managed resource's live counterpart;
it now records that object's health on the plan it returns. A create has
no live object and so no health. No cluster reads are added.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Plan output gives each managed resource that isn't healthy before apply
a ⚠ line under its plan, unchanged inputs included, and adds a Health:
line after Plan: when anything is unhealthy. Healthy runs print what
they printed before. --full-messages prints condition messages in full,
wrapped, rather than cut to one line.

The JSON and YAML result carries summary.health, counted from the plans'
health, beside the per-plan health. HealthSummary moves to plan so both
results share it. formatVersion stays 0.2, which plan.ResultFormatVersion
already shares with the other simulate commands.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
resource simulate's exit status is now the same bitmask the other
simulate commands use: 3 for changes, 4 when a resource was failing
(Synced=False) before apply, 7 for both. Not ready and paused resources
don't set bit 4. An errored plan still exits 1 whatever else was found.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Add an output section to resource simulate's help covering the ⚠ line,
the Health: line, --full-messages and the JSON health fields, and
rewrite its exit statuses to match the bitmask.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
…yaml

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Replaces TestWarnIgnoredFullMessages, which called the warning directly,
with a test of the renderer and report functions Run calls.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
…space differently

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Add a HealthBuilder in internal/health/healthtest for *health.Health, for the health ownership tests that follow.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Pin today's behaviour of Key and OwnedElsewhere before the health model refactor.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Add TestRenderDiffsHealth cases for 2- and 3-level nesting in flat and grouped layouts, a nested input XR without health, and an unchanged diff counted in the Health: line. Composed health is flattened with Via set, as RenderedResources.Merge builds it.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
…tput

Add TestRenderCompDiffHealth cases for 2- and 3-level nesting, truncated and full messages, a nested XR shared across compositions, the changed XR's repeated message, and a filtered impact.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Pin plan text, JSON and assembled composites for 2- and 3-level nested XRs
built through FromCompReport, assert Failing() agrees with what the Health:
line counts, and pin the equal-diff edge where -o diff counts a resource
that summary.health and exit bit 4 do not.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Pin the plan text health lines and Health: line for every health state, long message cutting and --full-messages wrapping, Failing per state, and the 0/3/4/7 exit bits, each checked against the Health: line's failing count.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Add health.Run and health.Build, which resolve a run's health ownership and tally its counts and failing bit in one pass, in simulate's order. Shown resources with no kind and no name never share a key. Nothing calls Build yet.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
The assembler builds a health.Run once with health.Build and lists each
composite's Unhealthy from Run.Owned. Result's Health, Failing and
HealthLine read a Run built from its resolved lists, so healthTally goes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Resource simulate builds one health.Run from its plans with health.Build. The Health: line, summary.health and exit bit 4 read it; plan.HealthCounts and plan.Failing stay for their test callers until they are removed.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
The xr -o diff renderer reads one health.Run, passed in through the new
DiffOptions.Health, for which group lists each unhealthy resource and for
the Health: line, in both the flat and grouped layouts. renderXRReport
builds it from the Result's composites and plans, so the Health: line now
counts plans, as summary.health and exit bit 4 do (D-1): an unhealthy
equal diff dropped for a missing provider plan is no longer counted.
groupsOwnedElsewhere loses its last caller and goes.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
The composition -o diff renderer reads one health.Run, passed in through
DiffOptions.Health, for which XR lists each unhealthy resource and for
the Health: line, with one composite per impact of every composition in
run order. renderCompReport builds it with compRun from the Result's
composites and plans. tallyComposite, unhealthyOwnedElsewhere and the
renderer's unhealthyBlock lose their last callers and go.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Delete plan.HealthCounts and plan.Failing; tests read plan.HealthRun instead. Pin that plans with no kind and no name each count on their own.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
The assembler stores the Run it resolved Unhealthy from on Result, and the diff renderers get that exact Run. The second walk over the report (xrRun, compRun) is gone; Results without a stored Run still rebuild it from Composites.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
With DiffOptions.Health nil the diff renderers print no Health: line and no unhealthy blocks, so the run's model is the only count source. The groupsRun and compRun fallbacks are deleted; the renderer tests build the run's health with health.Build.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Render multi-composition and grouped xr reports through the assembler's stored Run, and pin that a Run of the wrong length is an error. The composition diff's inner renderer now gets no health, since its groups are not the run's composites.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
Unexport tally, entry and ownedElsewhere, which only Build uses. Share
composition diff's listed-impact rule through one helper, fix two doc
comments, and tidy two tests' names and fields.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Signed-off-by: Jonathan Ogilvie <jonathan.ogilvie@sumologic.com>
@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 95.33679% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.71%. Comparing base (4f4952a) to head (482d685).
⚠️ Report is 16 commits behind head on main.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(simulate): separate health state, summary and display

1 participant