From 44de08af665107f24167bd0186d2de7e4f6f1e31 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Tue, 17 Nov 2026 23:31:55 -0500 Subject: [PATCH 01/30] feat(health): classify a live object's health from its conditions Add a leaf package that maps a live object to a health tier: paused (the crossplane.io/paused annotation, overriding conditions), failing (Synced=False), notReady (Ready=False) or healthy, carrying the Synced and Ready conditions it was classified from. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/health/health.go | 104 ++++++++++++++++++++++ internal/health/health_test.go | 154 +++++++++++++++++++++++++++++++++ 2 files changed, 258 insertions(+) create mode 100644 internal/health/health.go create mode 100644 internal/health/health_test.go diff --git a/internal/health/health.go b/internal/health/health.go new file mode 100644 index 00000000..8ada72c4 --- /dev/null +++ b/internal/health/health.go @@ -0,0 +1,104 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package health classifies the health of a live cluster object from its +// Synced and Ready conditions and its paused annotation. It is a leaf package +// so that both the diff engine and the simulate result model can use it. +package health + +import ( + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + + xpmeta "github.com/crossplane/crossplane-runtime/v2/pkg/meta" + xpv2 "github.com/crossplane/crossplane/apis/v2/core/v2" + + "github.com/crossplane/cli/v2/pkg/resource" +) + +// State is the health tier of a live object. +type State string + +// The health tiers, in the order Of checks them. +const ( + // Paused means the object carries the crossplane.io/paused: "true" + // annotation, so its conditions are stale. It overrides the conditions. + Paused State = "paused" + // Failing means the object's Synced condition is False. + Failing State = "failing" + // NotReady means the object's Ready condition is False, and it is not + // failing. + NotReady State = "notReady" + // Healthy is anything else, including an object with no Synced or Ready + // condition, or with either Unknown. + Healthy State = "healthy" +) + +// Condition is one condition of a live object, as the cluster reports it. +type Condition struct { + Status corev1.ConditionStatus `json:"status"` + Reason string `json:"reason,omitempty"` + Message string `json:"message,omitempty"` +} + +// Health is the health of one live object: its tier, and the Synced and Ready +// conditions it was classified from, each absent when the object has none. +type Health struct { + State State `json:"state"` + Synced *Condition `json:"synced,omitempty"` + Ready *Condition `json:"ready,omitempty"` +} + +// Of classifies the live object u. It returns nil when there is no live +// object: a resource the plan creates has no health. +func Of(u *unstructured.Unstructured) *Health { + if u == nil { + return nil + } + + r := &resource.Resource{Unstructured: *u} + h := &Health{ + State: Healthy, + Synced: condition(r.GetCondition(xpv2.TypeSynced)), + Ready: condition(r.GetCondition(xpv2.TypeReady)), + } + + switch { + case xpmeta.IsPaused(u): + h.State = Paused + case h.Synced != nil && h.Synced.Status == corev1.ConditionFalse: + h.State = Failing + case h.Ready != nil && h.Ready.Status == corev1.ConditionFalse: + h.State = NotReady + } + + return h +} + +// Unhealthy reports whether h is a live object in any tier but healthy. No +// live object (nil) is not unhealthy. +func (h *Health) Unhealthy() bool { + return h != nil && h.State != Healthy +} + +// condition returns c, or nil when the object has no such condition. +func condition(c xpv2.Condition) *Condition { + if c.Type == "" { + return nil + } + + return &Condition{Status: c.Status, Reason: string(c.Reason), Message: c.Message} +} diff --git a/internal/health/health_test.go b/internal/health/health_test.go new file mode 100644 index 00000000..f4db7064 --- /dev/null +++ b/internal/health/health_test.go @@ -0,0 +1,154 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package health + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" +) + +func TestOf(t *testing.T) { + cond := func(typ, status, reason, msg string) any { + return map[string]any{"type": typ, "status": status, "reason": reason, "message": msg, "lastTransitionTime": "2026-01-01T00:00:00Z"} + } + obj := func(paused string, conds ...any) *unstructured.Unstructured { + u := &unstructured.Unstructured{Object: map[string]any{"apiVersion": "example.org/v1", "kind": "Bucket", "metadata": map[string]any{"name": "b"}}} + if paused != "" { + u.SetAnnotations(map[string]string{"crossplane.io/paused": paused}) + } + if conds != nil { + u.Object["status"] = map[string]any{"conditions": conds} + } + return u + } + syncedFalse := cond("Synced", "False", "ReconcileError", "cannot update: denied") + syncedTrue := cond("Synced", "True", "ReconcileSuccess", "") + readyFalse := cond("Ready", "False", "Creating", "") + readyTrue := cond("Ready", "True", "Available", "") + + cases := map[string]struct { + reason string + in *unstructured.Unstructured + want *Health + }{ + "NilObject": { + reason: "No live object has no health.", + want: nil, + }, + "NoConditions": { + reason: "An object with no Synced or Ready condition, such as a ConfigMap, is healthy.", + in: obj(""), + want: &Health{State: Healthy}, + }, + "SyncedAndReady": { + reason: "Synced=True and Ready=True is healthy, carrying both conditions.", + in: obj("", syncedTrue, readyTrue), + want: &Health{ + State: Healthy, + Synced: &Condition{Status: corev1.ConditionTrue, Reason: "ReconcileSuccess"}, + Ready: &Condition{Status: corev1.ConditionTrue, Reason: "Available"}, + }, + }, + "Unknown": { + reason: "Unknown conditions are healthy.", + in: obj("", cond("Synced", "Unknown", "", ""), cond("Ready", "Unknown", "", "")), + want: &Health{ + State: Healthy, + Synced: &Condition{Status: corev1.ConditionUnknown}, + Ready: &Condition{Status: corev1.ConditionUnknown}, + }, + }, + "SyncedFalse": { + reason: "Synced=False is failing, even when Ready=False too.", + in: obj("", syncedFalse, readyFalse), + want: &Health{ + State: Failing, + Synced: &Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError", Message: "cannot update: denied"}, + Ready: &Condition{Status: corev1.ConditionFalse, Reason: "Creating"}, + }, + }, + "ReadyFalse": { + reason: "Ready=False with Synced not False is not ready.", + in: obj("", syncedTrue, readyFalse), + want: &Health{ + State: NotReady, + Synced: &Condition{Status: corev1.ConditionTrue, Reason: "ReconcileSuccess"}, + Ready: &Condition{Status: corev1.ConditionFalse, Reason: "Creating"}, + }, + }, + "ReadyFalseOnly": { + reason: "Ready=False with no Synced condition is not ready.", + in: obj("", readyFalse), + want: &Health{State: NotReady, Ready: &Condition{Status: corev1.ConditionFalse, Reason: "Creating"}}, + }, + "PausedOverridesFailing": { + reason: "The paused annotation overrides Synced=False; the stale conditions are still reported.", + in: obj("true", syncedFalse), + want: &Health{ + State: Paused, + Synced: &Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError", Message: "cannot update: denied"}, + }, + }, + "PausedNotTrue": { + reason: "Only the value \"true\" pauses an object.", + in: obj("false", syncedFalse), + want: &Health{ + State: Failing, + Synced: &Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError", Message: "cannot update: denied"}, + }, + }, + "MalformedStatus": { + reason: "A status whose conditions cannot be read is treated as having none.", + in: &unstructured.Unstructured{Object: map[string]any{"status": map[string]any{"conditions": "nope"}}}, + want: &Health{State: Healthy}, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + got := Of(tc.in) + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("%s\nOf(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestUnhealthy(t *testing.T) { + cases := map[string]struct { + reason string + in *Health + want bool + }{ + "Nil": {reason: "No live object is not unhealthy.", in: nil, want: false}, + "Healthy": {reason: "Healthy is not unhealthy.", in: &Health{State: Healthy}, want: false}, + "Failing": {reason: "Failing is unhealthy.", in: &Health{State: Failing}, want: true}, + "NotReady": {reason: "Not ready is unhealthy.", in: &Health{State: NotReady}, want: true}, + "Paused": {reason: "Paused is unhealthy.", in: &Health{State: Paused}, want: true}, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + if got := tc.in.Unhealthy(); got != tc.want { + t.Errorf("%s\nUnhealthy(): want %t, got %t", tc.reason, tc.want, got) + } + }) + } +} From 09ffe089c603833161ab518b6cfa7cfe510eaa3c Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 00:29:26 -0500 Subject: [PATCH 02/30] feat(diff): carry live health on XR diff groups and impacts Record the health of each XR's live object and of the composed resources the diff engine already fetched to render it, on XRDiffGroup and XRImpact. No new cluster reads: the XR comes from the resolved composite's cluster copy, and composed resources from the observed resources render consumes, before --eventual-state synthesizes any Ready condition. A nested XR's observed resources merge into its parent's once, keyed by identity. DiffSingleResource now returns the RenderedResources it built, so the composition path gets health along with the unchanged renderings. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/diff/diffprocessor/comp_processor.go | 17 ++- .../diff/diffprocessor/comp_processor_test.go | 138 ++++++++++++++---- internal/diff/diffprocessor/diff_processor.go | 24 ++- .../diff/diffprocessor/diff_processor_test.go | 33 ++++- internal/diff/result/comp.go | 3 + internal/diff/result/types.go | 23 +++ internal/diff/testutils/mocks.go | 6 +- internal/diff/types/types.go | 48 ++++++ internal/diff/types/types_test.go | 110 ++++++++++++++ internal/health/health.go | 1 + 10 files changed, 353 insertions(+), 50 deletions(-) create mode 100644 internal/diff/types/types_test.go diff --git a/internal/diff/diffprocessor/comp_processor.go b/internal/diff/diffprocessor/comp_processor.go index 55cc47e2..b14e1efa 100644 --- a/internal/diff/diffprocessor/comp_processor.go +++ b/internal/diff/diffprocessor/comp_processor.go @@ -53,8 +53,9 @@ type XRDiffResult struct { // Error contains any error that occurred while processing this XR. // nil means processing was successful. Error error - // unchanged holds the unchanged renderings Diffs leaves out, for the overlap check only. - unchanged map[string]*dt.ResourceDiff + // rendered is what the render produced: the unchanged renderings Diffs leaves out, for the overlap + // check, and the live objects the impact's health is read from. + rendered dtypes.RenderedResources } // HasChanges returns true if this XR has downstream resource changes. @@ -600,7 +601,7 @@ func (p *DefaultCompDiffProcessor) processSingleComposition(ctx context.Context, for _, xr := range keptXRs { if r := xrResults[dt.MakeDiffKeyFromResource(xr)]; r != nil && !r.HasError() { group := dt.XRDiffGroup{XR: objectRef(xr), Diffs: r.Diffs} - composites = append(composites, renderedComposite{xr: xr, group: withUnchanged([]dt.XRDiffGroup{group}, []map[string]*dt.ResourceDiff{r.unchanged})[0]}) + composites = append(composites, renderedComposite{xr: xr, group: withUnchanged([]dt.XRDiffGroup{group}, []map[string]*dt.ResourceDiff{r.rendered.Unchanged()})[0]}) } } @@ -815,7 +816,7 @@ func (p *DefaultCompDiffProcessor) collectXRDiffs(ctx context.Context, xrs []*un } xrCtx, cancel := p.config.xrContext(ctx) - diffs, unchanged, err := p.xrProc.DiffSingleResource(xrCtx, xr, compositionProvider) + diffs, rendered, err := p.xrProc.DiffSingleResource(xrCtx, xr, compositionProvider) err = p.config.afterXR(ctx, xrCtx, diffs, err) cancel() if err != nil { @@ -829,8 +830,8 @@ func (p *DefaultCompDiffProcessor) collectXRDiffs(ctx context.Context, xrs []*un } else { // Store successful result with diffs results[resourceID] = &XRDiffResult{ - Diffs: diffs, - unchanged: unchanged, + Diffs: diffs, + rendered: rendered, } } } @@ -1642,6 +1643,7 @@ func (p *DefaultCompDiffProcessor) buildImpactAnalysis(xrs []*un.Unstructured, r case result != nil && result.HasChanges(): impact.Status = dt.XRStatusChanged impact.Diffs = result.Diffs + impact.Health = result.rendered.Health() summary.WithChanges++ default: impact.Status = dt.XRStatusUnchanged @@ -1650,6 +1652,9 @@ func (p *DefaultCompDiffProcessor) buildImpactAnalysis(xrs []*un.Unstructured, r if result != nil && p.config.KeepEqualDiffs != nil { impact.Diffs = result.Diffs } + if result != nil { + impact.Health = result.rendered.Health() + } summary.Unchanged++ } diff --git a/internal/diff/diffprocessor/comp_processor_test.go b/internal/diff/diffprocessor/comp_processor_test.go index abb07075..24814f92 100644 --- a/internal/diff/diffprocessor/comp_processor_test.go +++ b/internal/diff/diffprocessor/comp_processor_test.go @@ -46,6 +46,7 @@ import ( dt "github.com/crossplane/cli/v2/internal/diff/result" tu "github.com/crossplane/cli/v2/internal/diff/testutils" "github.com/crossplane/cli/v2/internal/diff/types" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/pkg/render" ) @@ -1296,12 +1297,12 @@ func TestDefaultCompDiffProcessor_collectXRDiffs_RevisionName(t *testing.T) { processor := &DefaultCompDiffProcessor{ xrProc: &tu.MockDiffProcessor{ - DiffSingleResourceFn: func(ctx context.Context, _ *un.Unstructured, provider types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { + DiffSingleResourceFn: func(ctx context.Context, _ *un.Unstructured, provider types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { var err error got, err = provider(ctx, rendered) - return map[string]*dt.ResourceDiff{}, nil, err + return map[string]*dt.ResourceDiff{}, types.RenderedResources{}, err }, }, config: ProcessorConfig{Logger: tu.TestLogger(t, false), IncludeManual: true}, @@ -1341,12 +1342,12 @@ func TestDefaultCompDiffProcessor_collectXRDiffs_PerXR(t *testing.T) { var hooked []string processor := &DefaultCompDiffProcessor{ xrProc: &tu.MockDiffProcessor{ - DiffSingleResourceFn: func(ctx context.Context, xr *un.Unstructured, _ types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { + DiffSingleResourceFn: func(ctx context.Context, xr *un.Unstructured, _ types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { if xr.GetName() == "slow" { <-ctx.Done() - return nil, nil, ctx.Err() + return nil, types.RenderedResources{}, ctx.Err() } - return map[string]*dt.ResourceDiff{xr.GetName(): diff}, nil, nil + return map[string]*dt.ResourceDiff{xr.GetName(): diff}, types.RenderedResources{}, nil }, }, config: ProcessorConfig{ @@ -1637,11 +1638,11 @@ func TestDefaultCompDiffProcessor_collectXRDiffs_NestedXRCompositionLookup(t *te compositionRequests := make([]string, 0) mockXRProc := &tu.MockDiffProcessor{ - DiffSingleResourceFn: func(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { + DiffSingleResourceFn: func(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { // For the root XR, test what composition is returned comp, err := compositionProvider(ctx, res) if err != nil { - return nil, nil, err + return nil, types.RenderedResources{}, err } compositionRequests = append(compositionRequests, fmt.Sprintf("%s/%s->%s", res.GetKind(), res.GetName(), comp.Composition.GetName())) @@ -1650,12 +1651,12 @@ func TestDefaultCompDiffProcessor_collectXRDiffs_NestedXRCompositionLookup(t *te // This is what ProcessNestedXRs does when it encounters nested XRs nestedComp, err := compositionProvider(ctx, nestedXR) if err != nil { - return nil, nil, err + return nil, types.RenderedResources{}, err } compositionRequests = append(compositionRequests, fmt.Sprintf("%s/%s->%s", nestedXR.GetKind(), nestedXR.GetName(), nestedComp.Composition.GetName())) - return make(map[string]*dt.ResourceDiff), nil, nil + return make(map[string]*dt.ResourceDiff), types.RenderedResources{}, nil }, } @@ -1855,10 +1856,10 @@ func TestDefaultCompDiffProcessor_collectXRDiffs_NestedXRRunComposition(t *testi }). Build(), xrProc: &tu.MockDiffProcessor{ - DiffSingleResourceFn: func(ctx context.Context, _ *un.Unstructured, provider types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { + DiffSingleResourceFn: func(ctx context.Context, _ *un.Unstructured, provider types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { got, gotErr = provider(ctx, tc.args.nested) - return map[string]*dt.ResourceDiff{}, nil, gotErr + return map[string]*dt.ResourceDiff{}, types.RenderedResources{}, gotErr }, }, config: ProcessorConfig{Logger: tu.TestLogger(t, false)}, @@ -1928,12 +1929,12 @@ func TestDefaultCompDiffProcessor_DiffComposition_ReportErrorOutput(t *testing.T // Create mock XR processor that fails for one XR mockXRProc := &tu.MockDiffProcessor{ - DiffSingleResourceFn: func(_ context.Context, res *un.Unstructured, _ types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { + DiffSingleResourceFn: func(_ context.Context, res *un.Unstructured, _ types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { if res.GetName() == "fail-xr" { - return nil, nil, fmt.Errorf("render pipeline failed: function timeout") + return nil, types.RenderedResources{}, fmt.Errorf("render pipeline failed: function timeout") } - return make(map[string]*dt.ResourceDiff), nil, nil + return make(map[string]*dt.ResourceDiff), types.RenderedResources{}, nil }, } @@ -2005,8 +2006,8 @@ func TestDefaultCompDiffProcessor_DiffComposition_LateWarningsReachStructuredOut advisories := NewAdvisoryCollector(&stderr) mockXRProc := &tu.MockDiffProcessor{ - DiffSingleResourceFn: func(context.Context, *un.Unstructured, types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { - return map[string]*dt.ResourceDiff{}, nil, nil + DiffSingleResourceFn: func(context.Context, *un.Unstructured, types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { + return map[string]*dt.ResourceDiff{}, types.RenderedResources{}, nil }, // Stands in for the leftover-container advisory raised from the function provider's teardown, // which reaches comp through DefaultCompDiffProcessor.Cleanup -> xrProc.Cleanup. @@ -2062,8 +2063,8 @@ func newCompProcessorForTest(t *testing.T, compClient xp.CompositionClient, incl config.SetDefaultFactories() mockXR := &tu.MockDiffProcessor{ - DiffSingleResourceFn: func(context.Context, *un.Unstructured, types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { - return map[string]*dt.ResourceDiff{}, nil, nil + DiffSingleResourceFn: func(context.Context, *un.Unstructured, types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { + return map[string]*dt.ResourceDiff{}, types.RenderedResources{}, nil }, } @@ -2657,15 +2658,15 @@ func TestDefaultCompDiffProcessor_processSingleComposition_PinnedXRWithChangedFu proc.xrProc = &tu.MockDiffProcessor{ ResolveCompositeFn: resolveComposite, // Like the real one, the provider is handed the effective composite. - DiffSingleResourceFn: func(ctx context.Context, xr *un.Unstructured, provider types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { + DiffSingleResourceFn: func(ctx context.Context, xr *un.Unstructured, provider types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { composite, err := resolveComposite(ctx, xr) if err != nil { - return nil, nil, err + return nil, types.RenderedResources{}, err } resolved, err := provider(ctx, composite) if err != nil { - return nil, nil, err + return nil, types.RenderedResources{}, err } gotRevision = resolved.RevisionName @@ -2673,7 +2674,7 @@ func TestDefaultCompDiffProcessor_processSingleComposition_PinnedXRWithChangedFu gotPipeline = append(gotPipeline, step.FunctionRef.Name) } - return map[string]*dt.ResourceDiff{}, nil, nil + return map[string]*dt.ResourceDiff{}, types.RenderedResources{}, nil }, } @@ -2765,15 +2766,15 @@ func TestDefaultCompDiffProcessor_processSingleComposition_PinnedBackingXROfFilt return synced, nil }, - DiffSingleResourceFn: func(ctx context.Context, xr *un.Unstructured, provider types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { + DiffSingleResourceFn: func(ctx context.Context, xr *un.Unstructured, provider types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { resolved, err := provider(ctx, xr) if err != nil { - return nil, nil, err + return nil, types.RenderedResources{}, err } rendered[xr.GetName()] = resolved.RevisionName - return map[string]*dt.ResourceDiff{}, nil, nil + return map[string]*dt.ResourceDiff{}, types.RenderedResources{}, nil }, } @@ -2964,18 +2965,15 @@ func TestDefaultCompDiffProcessor_DiffComposition_RenderOverlaps(t *testing.T) { var rendered []string proc := NewCompDiffProcessor(&tu.MockDiffProcessor{ - DiffSingleResourceFn: func(_ context.Context, res *un.Unstructured, _ types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { + DiffSingleResourceFn: func(_ context.Context, res *un.Unstructured, _ types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { rendered = append(rendered, res.GetName()) out := map[string]*dt.ResourceDiff{} for _, d := range append([]*dt.ResourceDiff{self(res)}, tc.renders[res.GetName()]...) { out[d.GetDiffKey()] = d } - var unchanged map[string]*dt.ResourceDiff + var unchanged types.RenderedResources for _, d := range tc.unchanged[res.GetName()] { - if unchanged == nil { - unchanged = map[string]*dt.ResourceDiff{} - } - unchanged[d.GetDiffKey()] = d + unchanged.AddUnchanged(d.GetDiffKey(), d) } return out, unchanged, nil }, @@ -3016,3 +3014,81 @@ func TestDefaultCompDiffProcessor_DiffComposition_RenderOverlaps(t *testing.T) { }) } } + +func TestDefaultCompDiffProcessor_DiffComposition_Health(t *testing.T) { + xr := func(name string) *un.Unstructured { + return tu.NewResource("example.org/v1", "XResource", name).WithNamespace("default").Build() + } + bucket := tu.NewResource("example.org/v1", "Bucket", "bucket").WithCondition("Synced", "False", "ApplyFailed").Build() + failing := &health.Health{State: health.Failing, Synced: &health.Condition{Status: corev1.ConditionFalse, Reason: "ApplyFailed"}} + live := dt.CompositeHealth{ + XR: &health.Health{State: health.Healthy}, + Composed: []dt.ResourceHealth{{ObjectReference: corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "Bucket", Name: "bucket"}, Health: failing}}, + } + + type want struct { + status dt.XRStatus + health dt.CompositeHealth + } + cases := map[string]struct { + reason string + // changed makes the render produce a change; errs makes it fail. + changed, errs bool + want want + }{ + "Changed": { + reason: "A changed composite carries the health its render read.", + changed: true, + want: want{status: dt.XRStatusChanged, health: live}, + }, + "Unchanged": { + reason: "An unchanged composite carries its health too: being unchanged does not make it healthy.", + want: want{status: dt.XRStatusUnchanged, health: live}, + }, + "Errored": { + reason: "A composite whose render failed carries no health.", + errs: true, + want: want{status: dt.XRStatusError}, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + comp := tu.NewComposition("test-composition").WithCompositeTypeRef("example.org/v1", "XResource").WithPipelineMode().Build() + + proc := NewCompDiffProcessor(&tu.MockDiffProcessor{ + DiffSingleResourceFn: func(_ context.Context, res *un.Unstructured, _ types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { + if tc.errs { + return nil, types.RenderedResources{}, errors.New("render failed") + } + var rendered types.RenderedResources + rendered.SetLive(res) + rendered.AddObserved(bucket) + diffs := map[string]*dt.ResourceDiff{} + if tc.changed { + d := &dt.ResourceDiff{Gvk: bucket.GroupVersionKind(), ResourceName: "bucket", DiffType: dt.DiffTypeModified, Current: dt.ResourceViews{Raw: bucket}, Desired: dt.ResourceViews{Raw: bucket}} + diffs[d.GetDiffKey()] = d + } + return diffs, rendered, nil + }, + }, tu.NewMockCompositionClient(). + WithSuccessfulCompositionFetch(comp). + WithResourcesForComposition("test-composition", "default", []*un.Unstructured{xr("a")}). + Build(), WithLogger(tu.TestLogger(t, false))) + + // The input differs from the cluster's composition (the label), so impact analysis runs. + out, _ := proc.DiffComposition(t.Context(), []*un.Unstructured{ + tu.NewComposition("test-composition").WithCompositeTypeRef("example.org/v1", "XResource").WithPipelineMode(). + WithLabels(map[string]string{"version": "2"}).BuildAsUnstructured(), + }, "default", nil) + if out == nil || len(out.Compositions) != 1 || len(out.Compositions[0].ImpactAnalysis) != 1 { + t.Fatalf("%s\nDiffComposition(...): want one composition with one impact, got %+v", tc.reason, out) + } + + impact := out.Compositions[0].ImpactAnalysis[0] + if diff := gcmp.Diff(tc.want, want{status: impact.Status, health: impact.Health}, gcmp.AllowUnexported(want{})); diff != "" { + t.Errorf("%s\nImpactAnalysis[0]: -want, +got:\n%s", tc.reason, diff) + } + }) + } +} diff --git a/internal/diff/diffprocessor/diff_processor.go b/internal/diff/diffprocessor/diff_processor.go index f52482bc..4237b05d 100644 --- a/internal/diff/diffprocessor/diff_processor.go +++ b/internal/diff/diffprocessor/diff_processor.go @@ -51,10 +51,11 @@ type DiffProcessor interface { // error, so the successful inputs' results and the failures' errors can still be rendered. PerformDiff(ctx context.Context, resources []*un.Unstructured, compositionProvider types.CompositionProvider) (*dt.XRReport, error) - // DiffSingleResource processes a single resource and returns its diffs and, by diff key, the - // unchanged renderings its diffs leave out (see DefaultDiffCalculator.CalculateNonRemovalDiffs), - // which only the render-overlap check consumes. - DiffSingleResource(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) + // DiffSingleResource processes a single resource and returns its diffs and what its render + // produced: the unchanged renderings its diffs leave out (see + // DefaultDiffCalculator.CalculateNonRemovalDiffs), which only the render-overlap check consumes, + // and the live objects health is read from. + DiffSingleResource(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) // ResolveComposite returns the composite DiffSingleResource hands its composition provider for res: // the defaulted effective composite (see CompositeResolver), which for a claim is its backing XR. @@ -281,6 +282,7 @@ func (p *DefaultDiffProcessor) PerformDiff(ctx context.Context, resources []*un. // We don't emit partial results for a single XR: on success the // whole diff tree is attached, on failure none of it. group.Diffs = diffs + group.Health = rendered.Health() unchanged[i] = rendered.Unchanged() } } @@ -296,6 +298,7 @@ func (p *DefaultDiffProcessor) PerformDiff(ctx context.Context, resources []*un. } groups[i].Diffs = nil + groups[i].Health = dt.CompositeHealth{} res := toRender[i].Resource resourceID := fmt.Sprintf("%s/%s", groups[i].XR.Kind, groups[i].XR.Name) @@ -364,9 +367,8 @@ func (p *DefaultDiffProcessor) PerformDiff(ctx context.Context, resources []*un. // DiffSingleResource handles one resource at a time and returns its diffs. // The compositionProvider function is called to obtain the composition to use for rendering. // This is the public method for top-level XR diffing, which enables removal detection. -func (p *DefaultDiffProcessor) DiffSingleResource(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { - diffs, rendered, err := p.diffSingleResourceInternal(ctx, res, compositionProvider, nil, nil, true, 0) - return diffs, rendered.Unchanged(), err +func (p *DefaultDiffProcessor) DiffSingleResource(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { + return p.diffSingleResourceInternal(ctx, res, compositionProvider, nil, nil, true, 0) } // ResolveComposite returns the defaulted effective composite of res; see DiffProcessor. @@ -526,6 +528,14 @@ func (p *DefaultDiffProcessor) diffSingleResourceInternal(ctx context.Context, r return nil, types.RenderedResources{}, errors.Wrap(err, "cannot calculate diffs for composed resources") } + // Record the live objects already fetched, for health. The observed resources are the cluster's, + // never the copies --eventual-state synthesizes Ready on (see RenderToStableState). + rendered.SetLive(composite.Cluster) + + for i := range observedResources { + rendered.AddObserved(&observedResources[i].Unstructured) + } + // Check for nested XRs in the composed resources and process them recursively p.config.Logger.Debug("Checking for nested XRs", "resource", resourceID, "composedCount", len(desired.ComposedResources)) diff --git a/internal/diff/diffprocessor/diff_processor_test.go b/internal/diff/diffprocessor/diff_processor_test.go index f046f18e..de257e3a 100644 --- a/internal/diff/diffprocessor/diff_processor_test.go +++ b/internal/diff/diffprocessor/diff_processor_test.go @@ -36,6 +36,7 @@ import ( dt "github.com/crossplane/cli/v2/internal/diff/result" tu "github.com/crossplane/cli/v2/internal/diff/testutils" "github.com/crossplane/cli/v2/internal/diff/types" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/pkg/render" "github.com/crossplane/cli/v2/pkg/resource" ) @@ -3111,8 +3112,10 @@ func TestDefaultDiffProcessor_ProcessNestedXRs(t *testing.T) { func TestDefaultDiffProcessor_DiffSingleResource_WithObservedResources(t *testing.T) { ctx := t.Context() - // Create test XR - xr := tu.NewResource("example.org/v1", "XR", "test-xr").Build() + // Create test XR. Its conditions, and the bucket's, are what the reported health is read from. + xr := tu.NewResource("example.org/v1", "XR", "test-xr"). + WithCondition("Synced", "False", "ReconcileError"). + Build() // Create test observed composed resources observedBucket := tu.NewResource("s3.aws.crossplane.io/v1", "Bucket", "observed-bucket"). @@ -3120,6 +3123,7 @@ func TestDefaultDiffProcessor_DiffSingleResource_WithObservedResources(t *testin "crossplane.io/composition-resource-name": "bucket", }). WithSpecField("bucketName", "my-bucket"). + WithCondition("Ready", "False", "Creating"). Build() observedUser := tu.NewResource("iam.aws.crossplane.io/v1", "User", "observed-user"). @@ -3152,6 +3156,7 @@ func TestDefaultDiffProcessor_DiffSingleResource_WithObservedResources(t *testin wantErr bool wantErrContain string verifyObservedPassed bool + wantHealth dt.CompositeHealth }{ "ObservedResourcesFetchedAndPassedToRender": { setupMocks: func() (k8.Clients, xp.Clients) { @@ -3250,6 +3255,21 @@ func TestDefaultDiffProcessor_DiffSingleResource_WithObservedResources(t *testin wantObservedCount: 2, verifyObservedPassed: true, wantErr: false, + // Health is read from the cluster XR and the observed resources the render already + // fetched, unchanged ones included. + wantHealth: dt.CompositeHealth{ + XR: &health.Health{State: health.Failing, Synced: &health.Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError"}}, + Composed: []dt.ResourceHealth{ + { + ObjectReference: corev1.ObjectReference{APIVersion: "iam.aws.crossplane.io/v1", Kind: "User", Name: "observed-user"}, + Health: &health.Health{State: health.Healthy}, + }, + { + ObjectReference: corev1.ObjectReference{APIVersion: "s3.aws.crossplane.io/v1", Kind: "Bucket", Name: "observed-bucket"}, + Health: &health.Health{State: health.NotReady, Ready: &health.Condition{Status: corev1.ConditionFalse, Reason: "Creating"}}, + }, + }, + }, }, "EmptyObservedResourcesWhenTreeEmpty": { setupMocks: func() (k8.Clients, xp.Clients) { @@ -3324,6 +3344,9 @@ func TestDefaultDiffProcessor_DiffSingleResource_WithObservedResources(t *testin wantObservedCount: 0, verifyObservedPassed: true, wantErr: false, + wantHealth: dt.CompositeHealth{ + XR: &health.Health{State: health.Failing, Synced: &health.Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError"}}, + }, }, "FailedObservedResourceFetchIsFatal": { setupMocks: func() (k8.Clients, xp.Clients) { @@ -3535,7 +3558,7 @@ func TestDefaultDiffProcessor_DiffSingleResource_WithObservedResources(t *testin return xpClients.Composition.FindMatchingComposition(ctx, res) } - diffs, _, err := processor.(*DefaultDiffProcessor).DiffSingleResource(ctx, xr, compositionProvider) + diffs, rendered, err := processor.(*DefaultDiffProcessor).DiffSingleResource(ctx, xr, compositionProvider) // Check error expectations if (err != nil) != tt.wantErr { @@ -3573,6 +3596,10 @@ func TestDefaultDiffProcessor_DiffSingleResource_WithObservedResources(t *testin if diffs == nil { t.Errorf("DiffSingleResource() returned nil diffs, expected non-nil map") } + + if diff := gcmp.Diff(tt.wantHealth, rendered.Health()); diff != "" { + t.Errorf("DiffSingleResource() health: -want, +got:\n%s", diff) + } }) } } diff --git a/internal/diff/result/comp.go b/internal/diff/result/comp.go index c6a1a6d4..bc89b03c 100644 --- a/internal/diff/result/comp.go +++ b/internal/diff/result/comp.go @@ -249,4 +249,7 @@ type XRImpact struct { // OutputError the command lists in its top-level errors. Error *OutputError Diffs map[string]*ResourceDiff // downstream diffs (nil if unchanged/error) + // Health is the live health of the composite and of what it composed, set for a changed or + // unchanged composite and zero for an errored or filtered one. + Health CompositeHealth } diff --git a/internal/diff/result/types.go b/internal/diff/result/types.go index f00022ea..62c86849 100644 --- a/internal/diff/result/types.go +++ b/internal/diff/result/types.go @@ -14,6 +14,8 @@ import ( corev1 "k8s.io/api/core/v1" un "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" + + "github.com/crossplane/cli/v2/internal/health" ) // XRDiffGroup pairs one input XR/claim with the diffs its render produced (or @@ -45,6 +47,27 @@ type XRDiffGroup struct { NameGenerated bool Diffs map[string]*ResourceDiff Err *OutputError + // Health is the live health of the XR and of what it composed, set alongside Diffs. + Health CompositeHealth +} + +// CompositeHealth is the live health of one input composite and of every resource the cluster +// observes it composing, nested composites and their resources included, taken from the objects the +// render already fetched. It describes the cluster, not the desired state: a resource the render +// would create has no entry, and one it would delete has one. +type CompositeHealth struct { + // XR is the health of the input's own cluster object (for a claim, the claim); nil when the + // cluster has none. + XR *health.Health + // Composed holds the health of each observed composed resource, once each, in diff key order. + Composed []ResourceHealth +} + +// ResourceHealth is the live health of one composed resource. +type ResourceHealth struct { + corev1.ObjectReference + + Health *health.Health } // XRReport is what one xr diff run produced: everything a DiffRenderer renders. diff --git a/internal/diff/testutils/mocks.go b/internal/diff/testutils/mocks.go index 6bcd364b..81127099 100644 --- a/internal/diff/testutils/mocks.go +++ b/internal/diff/testutils/mocks.go @@ -258,7 +258,7 @@ type MockDiffProcessor struct { // Function fields for mocking behavior InitializeFn func(ctx context.Context) error PerformDiffFn func(ctx context.Context, resources []*un.Unstructured, compositionProvider types.CompositionProvider) (*dt.XRReport, error) - DiffSingleResourceFn func(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) + DiffSingleResourceFn func(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) ResolveCompositeFn func(ctx context.Context, res *un.Unstructured) (*un.Unstructured, error) CleanupFn func(ctx context.Context) error } @@ -292,12 +292,12 @@ func (m *MockDiffProcessor) PerformDiff(ctx context.Context, resources []*un.Uns } // DiffSingleResource implements the DiffProcessor.DiffSingleResource method. -func (m *MockDiffProcessor) DiffSingleResource(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, map[string]*dt.ResourceDiff, error) { +func (m *MockDiffProcessor) DiffSingleResource(ctx context.Context, res *un.Unstructured, compositionProvider types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { if m.DiffSingleResourceFn != nil { return m.DiffSingleResourceFn(ctx, res, compositionProvider) } - return make(map[string]*dt.ResourceDiff), nil, nil + return make(map[string]*dt.ResourceDiff), types.RenderedResources{}, nil } // Cleanup implements the DiffProcessor.Cleanup method. diff --git a/internal/diff/types/types.go b/internal/diff/types/types.go index b3daf84a..2644d745 100644 --- a/internal/diff/types/types.go +++ b/internal/diff/types/types.go @@ -19,6 +19,8 @@ package types import ( "context" + "maps" + "slices" corev1 "k8s.io/api/core/v1" un "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -27,6 +29,7 @@ import ( apiextensionsv1 "github.com/crossplane/crossplane/apis/v2/apiextensions/v1" dt "github.com/crossplane/cli/v2/internal/diff/result" + "github.com/crossplane/cli/v2/internal/health" ) // Verb is a Kubernetes API verb, as it appears in a SelfSubjectAccessReview's @@ -142,10 +145,51 @@ type CredentialFetchResult struct { // // It also holds, by diff key, the diff of each unchanged rendering the calculator did not keep among // the diffs it returns, so input validation sees every rendering whether or not equal diffs are kept. +// +// Finally it holds the live cluster objects the render already fetched, for health reporting: the +// composite's own cluster copy, and by diff key every composed resource observed beneath it. type RenderedResources struct { keys map[string]bool desired map[string]map[string]bool // composite identity -> composition resource names it desired unchanged map[string]*dt.ResourceDiff + live *un.Unstructured + observed map[string]*un.Unstructured +} + +// SetLive records the composite's own cluster copy (for a claim, the claim). Merge does not carry it: +// a nested composite's cluster copy is one of its parent's observed resources. +func (r *RenderedResources) SetLive(composite *un.Unstructured) { + r.live = composite +} + +// AddObserved records a composed resource's cluster copy. A resource observed twice, as a nested +// composite is (once beneath its parent, once as the composite it is), is recorded once. +func (r *RenderedResources) AddObserved(res *un.Unstructured) { + if r.observed == nil { + r.observed = make(map[string]*un.Unstructured) + } + + r.observed[dt.MakeDiffKeyFromResource(res)] = res +} + +// Health classifies the live objects SetLive and AddObserved recorded. +func (r *RenderedResources) Health() dt.CompositeHealth { + h := dt.CompositeHealth{XR: health.Of(r.live)} + + for _, key := range slices.Sorted(maps.Keys(r.observed)) { + res := r.observed[key] + h.Composed = append(h.Composed, dt.ResourceHealth{ + ObjectReference: corev1.ObjectReference{ + APIVersion: res.GetAPIVersion(), + Kind: res.GetKind(), + Name: res.GetName(), + Namespace: res.GetNamespace(), + }, + Health: health.Of(res), + }) + } + + return h } // AddUnchanged records the diff of an unchanged rendering that is not among the returned diffs. @@ -201,6 +245,10 @@ func (r *RenderedResources) Merge(other RenderedResources) { for key, d := range other.unchanged { r.AddUnchanged(key, d) } + + for _, res := range other.observed { + r.AddObserved(res) + } } // Keys returns the diff keys of every rendered composed resource. The caller must not modify it. diff --git a/internal/diff/types/types_test.go b/internal/diff/types/types_test.go new file mode 100644 index 00000000..851e22a2 --- /dev/null +++ b/internal/diff/types/types_test.go @@ -0,0 +1,110 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package types + +import ( + "testing" + + "github.com/google/go-cmp/cmp" + corev1 "k8s.io/api/core/v1" + un "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + + dt "github.com/crossplane/cli/v2/internal/diff/result" + "github.com/crossplane/cli/v2/internal/health" +) + +func TestRenderedResourcesHealth(t *testing.T) { + obj := func(apiVersion, kind, name, readyStatus string) *un.Unstructured { + u := &un.Unstructured{Object: map[string]any{}} + u.SetAPIVersion(apiVersion) + u.SetKind(kind) + u.SetName(name) + if readyStatus != "" { + _ = un.SetNestedSlice(u.Object, []any{map[string]any{"type": "Ready", "status": readyStatus}}, "status", "conditions") + } + return u + } + ref := func(apiVersion, kind, name string) corev1.ObjectReference { + return corev1.ObjectReference{APIVersion: apiVersion, Kind: kind, Name: name} + } + healthy := &health.Health{State: health.Healthy} + notReady := &health.Health{State: health.NotReady, Ready: &health.Condition{Status: corev1.ConditionFalse}} + + parent := obj("example.org/v1", "XParent", "parent", "") + child := obj("example.org/v1", "XChild", "child", "False") + grandchild := obj("v1", "ConfigMap", "grandchild", "") + + type args struct { + build func() RenderedResources + } + cases := map[string]struct { + reason string + args args + want dt.CompositeHealth + }{ + "Empty": { + reason: "Nothing recorded means no live composite and nothing composed.", + args: args{build: func() RenderedResources { return RenderedResources{} }}, + want: dt.CompositeHealth{}, + }, + "LiveAndObserved": { + reason: "The live composite gives XR, and each observed resource an entry, in diff key order.", + args: args{build: func() RenderedResources { + var r RenderedResources + r.SetLive(parent) + r.AddObserved(grandchild) + r.AddObserved(child) + return r + }}, + want: dt.CompositeHealth{ + XR: healthy, + Composed: []dt.ResourceHealth{ + {ObjectReference: ref("example.org/v1", "XChild", "child"), Health: notReady}, + {ObjectReference: ref("v1", "ConfigMap", "grandchild"), Health: healthy}, + }, + }, + }, + "NestedMergedOnce": { + reason: "Merging a nested composite's render adds what it observed, but not its own live copy as XR; one observed by both levels counts once.", + args: args{build: func() RenderedResources { + var r, nested RenderedResources + r.SetLive(parent) + r.AddObserved(child) + nested.SetLive(child) + nested.AddObserved(grandchild) + nested.AddObserved(child) + r.Merge(nested) + return r + }}, + want: dt.CompositeHealth{ + XR: healthy, + Composed: []dt.ResourceHealth{ + {ObjectReference: ref("example.org/v1", "XChild", "child"), Health: notReady}, + {ObjectReference: ref("v1", "ConfigMap", "grandchild"), Health: healthy}, + }, + }, + }, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + r := tc.args.build() + if diff := cmp.Diff(tc.want, r.Health()); diff != "" { + t.Errorf("\n%s\nHealth(): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} diff --git a/internal/health/health.go b/internal/health/health.go index 8ada72c4..161d5d4b 100644 --- a/internal/health/health.go +++ b/internal/health/health.go @@ -24,6 +24,7 @@ import ( "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" xpmeta "github.com/crossplane/crossplane-runtime/v2/pkg/meta" + xpv2 "github.com/crossplane/crossplane/apis/v2/core/v2" "github.com/crossplane/cli/v2/pkg/resource" From cef0b345755d3b9bdac5e1dd10fb38059cad4d6e Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 00:29:27 -0500 Subject: [PATCH 03/30] feat(simulate): report live health in the structured result Each plan with a live object carries its health; each composite carries the health of its live XR and lists the not-healthy composed resources that have no plan of their own. The summary counts unhealthy composites and failing, notReady and paused resources, each once. Bump the result format version to 0.2 and document the new fields. Text and diff output are unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/plan/output.go | 2 +- internal/plan/types.go | 7 ++ internal/simulate/help/output.md | 12 ++- internal/simulate/report.go | 45 +++++++++- internal/simulate/report_test.go | 143 +++++++++++++++++++++++++++++++ internal/simulate/result.go | 97 ++++++++++++++++++++- internal/simulate/result_test.go | 77 +++++++++++++++++ 7 files changed, 374 insertions(+), 9 deletions(-) diff --git a/internal/plan/output.go b/internal/plan/output.go index f17f67b5..b037465a 100644 --- a/internal/plan/output.go +++ b/internal/plan/output.go @@ -87,7 +87,7 @@ func Summarize(plans []*ResourcePlan) Summary { // ResultFormatVersion versions the serialized result schema so automation // consuming it can detect incompatible changes. The schema is alpha; the // version moves to 1.0 when it stabilizes. -const ResultFormatVersion = "0.1" +const ResultFormatVersion = "0.2" // Result is the serialized form of a simulate run, for -o json|yaml. Errored // is the primary gate for wrapping automation: when true, at least one diff --git a/internal/plan/types.go b/internal/plan/types.go index f3e79fc8..e26b91fa 100644 --- a/internal/plan/types.go +++ b/internal/plan/types.go @@ -27,6 +27,8 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" xpfieldpath "github.com/crossplane/crossplane-runtime/v2/pkg/fieldpath" + + "github.com/crossplane/cli/v2/internal/health" ) // Action is what applying a resource would do. @@ -126,6 +128,11 @@ type ResourcePlan struct { // paths the provider marks sensitive, are redacted (see RedactSecret). Before *unstructured.Unstructured `json:"before,omitempty"` After *unstructured.Unstructured `json:"after,omitempty"` + + // Health is the live object's health, read from its Synced and Ready + // conditions and paused annotation before apply; nil when there is no + // live object, as for a create. + Health *health.Health `json:"health,omitempty"` } // ResourceRef identifies a resource. diff --git a/internal/simulate/help/output.md b/internal/simulate/help/output.md index e6612f69..d48ad27c 100644 --- a/internal/simulate/help/output.md +++ b/internal/simulate/help/output.md @@ -15,7 +15,17 @@ The `-o` flag picks the output format: in the changes. Each composite has a `status`: `changed`, `unchanged`, `error`, `deferred`, `skipped`, or `unknown` when the engine reports a status this version - doesn't recognize. + doesn't recognize. That status is the simulated outcome. Separately, + `health` reports the live object's state before apply: `health.state` is + `failing` (its Synced condition is False), `notReady` (its Ready condition + is False), `paused` (it carries the `crossplane.io/paused: "true"` + annotation, whatever its conditions), or `healthy`, with the `synced` and + `ready` conditions it was read from. A composite or plan has no `health` + when there is no live object, as for a create. Each composite also lists + under `unhealthy` the composed resources, nested composites' included, that + are not healthy and have no plan of their own. `summary` counts the + composites that are not healthy (`unhealthyComposites`), and the other + resources that are `failing`, `notReady` or `paused`, each once. - `diff` prints a line diff of each changed resource's manifest. Every Secret value, and any value a provider marks sensitive, reads `(sensitive value)`; a value the provider computes on apply reads diff --git a/internal/simulate/report.go b/internal/simulate/report.go index faa79db8..218c30fd 100644 --- a/internal/simulate/report.go +++ b/internal/simulate/report.go @@ -25,10 +25,12 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/apimachinery/pkg/runtime/schema" apiextensionsv1 "github.com/crossplane/crossplane/apis/v2/apiextensions/v1" dt "github.com/crossplane/cli/v2/internal/diff/result" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" ) @@ -78,7 +80,7 @@ func FromXRReport(r *dt.XRReport, plans *ProviderPlans, keep func(*unstructured. if a.addDiffs(g.Diffs, &xr) && c.Status == CompositeUnchanged { c.Status = CompositeChanged } - a.composites = append(a.composites, c) + a.addComposite(c, g.Health) } a.addRunErrors(r.Errors, owned) @@ -144,7 +146,7 @@ func FromCompReport(o *dt.CompDiffOutput, plans *ProviderPlans, keep func(*unstr if a.addDiffs(impact.Diffs, &xr) && c.Status == CompositeUnchanged { c.Status = CompositeChanged } - a.composites = append(a.composites, c) + a.addComposite(c, impact.Health) } } @@ -220,7 +222,10 @@ type assembler struct { keep func(*unstructured.Unstructured) bool plans []*plan.ResourcePlan composites []CompositeResult - errors []Error + // composed holds, by composite, the health of what it composed, which + // result reduces to Unhealthy once every plan is known. + composed [][]dt.ResourceHealth + errors []Error // listed holds, per resource, the unredacted changes already planned, // so a change two inputs reach identically is listed once. listed map[string][]listedChange @@ -239,6 +244,37 @@ func newAssembler(provider *ProviderPlans, keep func(*unstructured.Unstructured) return &assembler{provider: provider, keep: keep, plans: []*plan.ResourcePlan{}, listed: map[string][]listedChange{}} } +// addComposite adds c with the live health h the engine read for it. +func (a *assembler) addComposite(c CompositeResult, h dt.CompositeHealth) { + c.Health = h.XR + a.composites = append(a.composites, c) + a.composed = append(a.composed, h.Composed) +} + +// unhealthy sets each composite's Unhealthy: what it composed that is not +// healthy and has no plan in the run, matched across API versions. A +// resource with a plan shows its health there. +func (a *assembler) unhealthy() { + planned := map[schema.GroupKind]map[[2]string]bool{} + for _, p := range a.plans { + gk := p.GroupVersionKind().GroupKind() + if planned[gk] == nil { + planned[gk] = map[[2]string]bool{} + } + planned[gk][[2]string{p.Namespace, p.Name}] = true + } + + for i, composed := range a.composed { + for _, r := range composed { + gk := schema.FromAPIVersionAndKind(r.APIVersion, r.Kind).GroupKind() + if !r.Health.Unhealthy() || planned[gk][[2]string{r.Namespace, r.Name}] { + continue + } + a.composites[i].Unhealthy = append(a.composites[i].Unhealthy, UnhealthyResource{Ref: resourceRef(r.ObjectReference), Health: r.Health}) + } + } +} + // addDiffs adds a plan per diff, in the structured renderer's canonical order. // It reports whether a provider plan changed any of them, which the engine's // verdict alone does not show. @@ -367,6 +403,7 @@ func (a *assembler) addRunErrors(all, owned []dt.OutputError) { } func (a *assembler) result(warnings []dt.OutputWarning) Result { + a.unhealthy() res := NewResult(a.plans, a.composites, a.errors) if a.provider == nil { res.Notes = []string{noteProviderPlanNotRun} @@ -395,6 +432,8 @@ func resourcePlan(d *dt.ResourceDiff) *plan.ResourcePlan { // neither: its Raw views are uncleaned, unlike every other plan's. Before: plan.RedactSecret(d.Current.Clean), After: plan.RedactSecret(d.Desired.Clean), + // Read from the uncleaned live object: Clean has no status. + Health: health.Of(d.Current.Raw), } switch d.DiffType { diff --git a/internal/simulate/report_test.go b/internal/simulate/report_test.go index 241ee7b3..1cba7315 100644 --- a/internal/simulate/report_test.go +++ b/internal/simulate/report_test.go @@ -28,6 +28,7 @@ import ( "k8s.io/apimachinery/pkg/runtime/schema" dt "github.com/crossplane/cli/v2/internal/diff/result" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" ) @@ -610,3 +611,145 @@ func TestProviderPlanOnlyForItsRendering(t *testing.T) { t.Errorf("For(removal) = %v, want no plan", p) } } + +// TestHealth covers how the engine's live health reaches the result: on each +// plan from its live object, on each composite, and in each composite's +// unhealthy list for what has no plan; and how Result.Health counts it. +func TestHealth(t *testing.T) { + cond := func(typ, status string) map[string]any { + return map[string]any{"status": map[string]any{"conditions": []any{map[string]any{"type": typ, "status": status, "reason": "R"}}}} + } + live := func(d *dt.ResourceDiff, fields map[string]any) *dt.ResourceDiff { + d.Current.Raw = obj(d.Gvk, d.ResourceName, fields) + return d + } + failing := &health.Health{State: health.Failing, Synced: &health.Condition{Status: corev1.ConditionFalse, Reason: "R"}} + notReady := &health.Health{State: health.NotReady, Ready: &health.Condition{Status: corev1.ConditionFalse, Reason: "R"}} + healthy := &health.Health{State: health.Healthy} + ref := func(apiVersion, kind, name string) corev1.ObjectReference { + return corev1.ObjectReference{APIVersion: apiVersion, Kind: kind, Name: name} + } + cmRef := func(name string) corev1.ObjectReference { return ref("v1", "ConfigMap", name) } + cmPlan := func(name string) plan.ResourceRef { + return plan.ResourceRef{APIVersion: "v1", Kind: "ConfigMap", Name: name} + } + bucketV1 := ref("s3.example.org/v1", "Bucket", "b") + bucketV2 := ref("s3.example.org/v2", "Bucket", "b") + + type want struct { + plans map[string]*health.Health // plan name -> health + composites []CompositeResult + summary HealthSummary + } + cases := map[string]struct { + reason string + in *dt.XRReport + want want + }{ + "AllHealthy": { + reason: "A healthy run carries health on the composite and plans but emits no unhealthy entries, and counts nothing.", + in: &dt.XRReport{Groups: []dt.XRDiffGroup{{ + XR: xrRef, + Diffs: diffs(live(rdiff(cmGVK, "cm", dt.DiffTypeModified, map[string]any{}, map[string]any{"data": "x"}), nil)), + Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{{ObjectReference: cmRef("cm"), Health: healthy}}}, + }}}, + want: want{ + plans: map[string]*health.Health{"cm": healthy}, + composites: []CompositeResult{{Ref: xrPlan, Status: CompositeChanged, Health: healthy}}, + }, + }, + "CreateHasNoHealth": { + reason: "A created resource has no live object, so its plan carries no health; nor does a composite not in the cluster.", + in: &dt.XRReport{Groups: []dt.XRDiffGroup{{ + XR: xrRef, + Diffs: diffs(rdiff(cmGVK, "new", dt.DiffTypeAdded, nil, map[string]any{})), + }}}, + want: want{ + plans: map[string]*health.Health{"new": nil}, + composites: []CompositeResult{{Ref: xrPlan, Status: CompositeChanged}}, + }, + }, + "PlannedAndUnplanned": { + reason: "An unhealthy resource with a plan shows its health there; one without a plan, under a version the plan does not use either, is listed as unhealthy once.", + in: &dt.XRReport{Groups: []dt.XRDiffGroup{{ + XR: xrRef, + Diffs: diffs(live(rdiff(cmGVK, "cm", dt.DiffTypeRemoved, map[string]any{}, nil), cond("Ready", "False"))), + Health: dt.CompositeHealth{XR: failing, Composed: []dt.ResourceHealth{ + {ObjectReference: cmRef("cm"), Health: notReady}, + {ObjectReference: cmRef("quiet"), Health: healthy}, + {ObjectReference: bucketV1, Health: failing}, + }}, + }}}, + want: want{ + plans: map[string]*health.Health{"cm": notReady}, + composites: []CompositeResult{{ + Ref: xrPlan, Status: CompositeChanged, Health: failing, + Unhealthy: []UnhealthyResource{{Ref: resourceRef(bucketV1), Health: failing}}, + }}, + summary: HealthSummary{Composites: 1, Failing: 1, NotReady: 1}, + }, + }, + "PlannedAcrossVersions": { + reason: "A composed resource observed under another API version than its plan's is matched to the plan, not listed again.", + in: &dt.XRReport{Groups: []dt.XRDiffGroup{{ + XR: xrRef, + Diffs: diffs(live(rdiff(schema.FromAPIVersionAndKind(bucketV2.APIVersion, bucketV2.Kind), "b", dt.DiffTypeModified, + map[string]any{}, map[string]any{"spec": "x"}), cond("Synced", "False"))), + Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{{ObjectReference: bucketV1, Health: failing}}}, + }}}, + want: want{ + plans: map[string]*health.Health{"b": failing}, + composites: []CompositeResult{{Ref: xrPlan, Status: CompositeChanged, Health: healthy}}, + summary: HealthSummary{Failing: 1}, + }, + }, + "NestedCountedOnce": { + reason: "A nested composite that is its own input is counted as a composite, not again as a resource, and a resource two composites reach counts once.", + in: &dt.XRReport{Groups: []dt.XRDiffGroup{ + { + XR: xrRef, + Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{ + {ObjectReference: ref("example.org/v1", "XSubnet", "child"), Health: notReady}, + {ObjectReference: cmRef("deep"), Health: failing}, + }}, + }, + { + XR: ref("example.org/v1", "XSubnet", "child"), + Health: dt.CompositeHealth{XR: notReady, Composed: []dt.ResourceHealth{{ObjectReference: cmRef("deep"), Health: failing}}}, + }, + }}, + want: want{ + plans: map[string]*health.Health{}, + composites: []CompositeResult{ + {Ref: xrPlan, Status: CompositeUnchanged, Health: healthy, Unhealthy: []UnhealthyResource{ + {Ref: plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XSubnet", Name: "child"}, Health: notReady}, + {Ref: cmPlan("deep"), Health: failing}, + }}, + {Ref: plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XSubnet", Name: "child"}, Status: CompositeUnchanged, Health: notReady, Unhealthy: []UnhealthyResource{ + {Ref: cmPlan("deep"), Health: failing}, + }}, + }, + summary: HealthSummary{Composites: 1, Failing: 1}, + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + got := FromXRReport(tc.in, nil, nil) + gotPlans := map[string]*health.Health{} + for _, p := range got.Plans { + gotPlans[p.Name] = p.Health + } + if diff := cmp.Diff(tc.want.plans, gotPlans, cmpopts.EquateEmpty()); diff != "" { + t.Errorf("%s\nFromXRReport(...) plan health: -want, +got:\n%s", tc.reason, diff) + } + if diff := cmp.Diff(tc.want.composites, got.Composites, cmpopts.EquateEmpty()); diff != "" { + t.Errorf("%s\nFromXRReport(...) composites: -want, +got:\n%s", tc.reason, diff) + } + if diff := cmp.Diff(tc.want.summary, got.Health()); diff != "" { + t.Errorf("%s\nHealth(): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} diff --git a/internal/simulate/result.go b/internal/simulate/result.go index dfc2fa09..e686f07f 100644 --- a/internal/simulate/result.go +++ b/internal/simulate/result.go @@ -28,6 +28,7 @@ import ( "github.com/crossplane/crossplane-runtime/v2/pkg/errors" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" ) @@ -86,6 +87,32 @@ type CompositeResult struct { // ComposedBy references the parent composite of a nested XR; nil for a // top-level XR. ComposedBy *plan.ResourceRef `json:"composedBy,omitempty"` + // Health is the live composite's health before apply, which is distinct + // from Status, the simulated outcome; nil when the cluster has no such + // composite, or it was not simulated. + Health *health.Health `json:"health,omitempty"` + // Unhealthy lists the composed resources, nested composites' included, + // that are live, not healthy, and have no plan in the run (so no health + // of their own to show). + Unhealthy []UnhealthyResource `json:"unhealthy,omitempty"` +} + +// UnhealthyResource is a composed resource that is not healthy before apply. +type UnhealthyResource struct { + Ref plan.ResourceRef `json:"ref"` + Health *health.Health `json:"health"` +} + +// HealthSummary counts what is not healthy before apply, serialized among +// the summary's other counts. Composites counts +// the composites whose own health is not healthy; Failing, NotReady and +// Paused count the other resources, each once however many composites or +// plans reach it. +type HealthSummary struct { + Composites int `json:"unhealthyComposites"` + Failing int `json:"failing"` + NotReady int `json:"notReady"` + Paused int `json:"paused"` } // Error is an error from a simulate run, optionally anchored to a resource. @@ -180,14 +207,76 @@ func (r Result) Deferred() int { return n } +// Health counts the unhealthy composites, and the other resources by health +// state, from Composites and Plans. A resource counts once, under the state +// its first live copy reports, matched across API versions. +func (r Result) Health() HealthSummary { + var s HealthSummary + composites := map[schema.GroupKind]map[[2]string]bool{} + isComposite := func(ref plan.ResourceRef) bool { + return composites[groupKind(ref)][[2]string{ref.Namespace, ref.Name}] + } + for _, c := range r.Composites { + gk := groupKind(c.Ref) + if composites[gk] == nil { + composites[gk] = map[[2]string]bool{} + } + composites[gk][[2]string{c.Ref.Namespace, c.Ref.Name}] = true + if c.Health.Unhealthy() { + s.Composites++ + } + } + + seen := map[schema.GroupKind]map[[2]string]bool{} + count := func(ref plan.ResourceRef, h *health.Health) { + if h == nil || isComposite(ref) { + return + } + gk, id := groupKind(ref), [2]string{ref.Namespace, ref.Name} + if seen[gk][id] { + return + } + if seen[gk] == nil { + seen[gk] = map[[2]string]bool{} + } + seen[gk][id] = true + switch h.State { + case health.Failing: + s.Failing++ + case health.NotReady: + s.NotReady++ + case health.Paused: + s.Paused++ + case health.Healthy: + } + } + for _, p := range r.Plans { + count(p.ResourceRef(), p.Health) + } + for _, c := range r.Composites { + for _, u := range c.Unhealthy { + count(u.Ref, u.Health) + } + } + return s +} + +// groupKind is ref's version-independent type. +func groupKind(ref plan.ResourceRef) schema.GroupKind { + return schema.FromAPIVersionAndKind(ref.APIVersion, ref.Kind).GroupKind() +} + // MarshalJSON writes the result with summary, composites and compositions -// ahead of the plans, which can be long, and with the deferred count inside -// summary. YAML goes through it too. Decoding needs no counterpart: summary -// decodes into Result.Summary, and Deferred is counted from Composites. +// ahead of the plans, which can be long, and with the deferred and health +// counts inside summary. YAML goes through it too. Decoding needs no +// counterpart: summary decodes into Result.Summary, Deferred is counted from +// Composites, and Health from Composites and Plans. func (r Result) MarshalJSON() ([]byte, error) { type summary struct { plan.Summary + HealthSummary + Deferred int `json:"deferred"` } return json.Marshal(struct { @@ -203,7 +292,7 @@ func (r Result) MarshalJSON() ([]byte, error) { }{ FormatVersion: r.FormatVersion, Errored: r.Errored, - Summary: summary{Summary: r.Summary, Deferred: r.Deferred()}, + Summary: summary{Summary: r.Summary, HealthSummary: r.Health(), Deferred: r.Deferred()}, Composites: r.Composites, Compositions: r.Compositions, Plans: r.Plans, diff --git a/internal/simulate/result_test.go b/internal/simulate/result_test.go index 8da19d12..7d9038fe 100644 --- a/internal/simulate/result_test.go +++ b/internal/simulate/result_test.go @@ -24,6 +24,7 @@ import ( "github.com/google/go-cmp/cmp" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" ) @@ -303,3 +304,79 @@ func TestNewResult(t *testing.T) { }) } } + +func TestRenderJSONHealth(t *testing.T) { + xr := plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XNetwork", Name: "prod"} + failing := &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: "boom"}} + + type want struct { + composites string + plans string + summary string + } + cases := map[string]struct { + reason string + in Result + want want + }{ + "NoLiveObjects": { + reason: "Without live objects no health or unhealthy key is written, and every health count is zero.", + in: NewResult( + []*plan.ResourcePlan{{APIVersion: "v1", Kind: "ConfigMap", Name: "cm", Action: plan.ActionCreate, Composite: &xr}}, + []CompositeResult{{Ref: xr, Status: CompositeChanged}}, nil), + want: want{ + composites: `[{"ref":{"apiVersion":"example.org/v1","kind":"XNetwork","name":"prod"},"status":"changed"}]`, + plans: `[{"apiVersion":"v1","kind":"ConfigMap","name":"cm","action":"create","composite":{"apiVersion":"example.org/v1","kind":"XNetwork","name":"prod"}}]`, + summary: `{"create":1,"update":0,"destroy":0,"replace":0,"noop":0,"approximate":0,"errored":0,"unhealthyComposites":0,"failing":0,"notReady":0,"paused":0,"deferred":0}`, + }, + }, + "Unhealthy": { + reason: "Composite and plan health, and the unhealthy list, are written under their keys with the conditions they came from.", + in: NewResult( + []*plan.ResourcePlan{{APIVersion: "v1", Kind: "ConfigMap", Name: "cm", Action: plan.ActionNoOp, Composite: &xr, Health: &health.Health{State: health.Paused}}}, + []CompositeResult{{ + Ref: xr, Status: CompositeUnchanged, Health: failing, + Unhealthy: []UnhealthyResource{{Ref: plan.ResourceRef{APIVersion: "v1", Kind: "Secret", Name: "s"}, Health: &health.Health{State: health.NotReady, Ready: &health.Condition{Status: "False"}}}}, + }}, nil), + want: want{ + composites: `[{"ref":{"apiVersion":"example.org/v1","kind":"XNetwork","name":"prod"},"status":"unchanged",` + + `"health":{"state":"failing","synced":{"status":"False","reason":"ReconcileError","message":"boom"}},` + + `"unhealthy":[{"ref":{"apiVersion":"v1","kind":"Secret","name":"s"},"health":{"state":"notReady","ready":{"status":"False"}}}]}]`, + plans: `[{"apiVersion":"v1","kind":"ConfigMap","name":"cm","action":"no-op","composite":{"apiVersion":"example.org/v1","kind":"XNetwork","name":"prod"},"health":{"state":"paused"}}]`, + summary: `{"create":0,"update":0,"destroy":0,"replace":0,"noop":1,"approximate":0,"errored":0,"unhealthyComposites":1,"failing":0,"notReady":1,"paused":1,"deferred":0}`, + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + var buf bytes.Buffer + r, _ := NewRenderer(plan.FormatJSON) + if err := r.Render(&buf, tc.in); err != nil { + t.Fatalf("%s\nRender(json): unexpected error: %v", tc.reason, err) + } + var top map[string]json.RawMessage + if err := json.Unmarshal(buf.Bytes(), &top); err != nil { + t.Fatalf("%s\nRender(json): output is not valid JSON: %v", tc.reason, err) + } + compact := func(raw json.RawMessage) string { + var b bytes.Buffer + _ = json.Compact(&b, raw) + return b.String() + } + got := want{composites: compact(top["composites"]), plans: compact(top["plans"]), summary: compact(top["summary"])} + if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(want{})); diff != "" { + t.Errorf("%s\nRender(json): -want, +got:\n%s", tc.reason, diff) + } + + // What is written decodes back to what was rendered. + var back Result + if err := json.Unmarshal(buf.Bytes(), &back); err != nil { + t.Fatalf("%s\nRender(json): cannot decode: %v", tc.reason, err) + } + if diff := cmp.Diff(tc.in, back); diff != "" { + t.Errorf("%s\nRender(json) round trip: -want, +got:\n%s", tc.reason, diff) + } + }) + } +} From c5abee572109985e6ec9aa2a11c2ed1d0d031b31 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 10:40:34 -0500 Subject: [PATCH 04/30] fix(diff): read a claim's health from its backing XR and list nested health once A claim mirrors only part of its backing XR's conditions, so the composite health of a claim input is now its backing XR's. RenderedResources classifies health as it records the live objects instead of retaining them. Merge now records each nested composite's health, with the nested composite as the via of what it composes directly, so every composed resource is listed once with the composite that composes it. That also covers a nested XR found by name rather than in its parent's observed set, which previously lost its health when it had no plan. The comp path stores each XR's computed health rather than its render. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/diff/diffprocessor/comp_processor.go | 19 +- .../diff/diffprocessor/comp_processor_test.go | 26 +- internal/diff/diffprocessor/diff_processor.go | 8 +- .../diff/diffprocessor/diff_processor_test.go | 232 ++++++++++++++++++ internal/diff/result/types.go | 8 +- internal/diff/types/types.go | 92 ++++--- internal/diff/types/types_test.go | 71 +++++- 7 files changed, 391 insertions(+), 65 deletions(-) diff --git a/internal/diff/diffprocessor/comp_processor.go b/internal/diff/diffprocessor/comp_processor.go index b14e1efa..dd89d35a 100644 --- a/internal/diff/diffprocessor/comp_processor.go +++ b/internal/diff/diffprocessor/comp_processor.go @@ -53,9 +53,11 @@ type XRDiffResult struct { // Error contains any error that occurred while processing this XR. // nil means processing was successful. Error error - // rendered is what the render produced: the unchanged renderings Diffs leaves out, for the overlap - // check, and the live objects the impact's health is read from. - rendered dtypes.RenderedResources + // unchanged holds the unchanged renderings Diffs leaves out, for the overlap check only. + unchanged map[string]*dt.ResourceDiff + // health is the live health the render read, computed as the XR is diffed so that the live objects + // are not retained until impact analysis. + health dt.CompositeHealth } // HasChanges returns true if this XR has downstream resource changes. @@ -601,7 +603,7 @@ func (p *DefaultCompDiffProcessor) processSingleComposition(ctx context.Context, for _, xr := range keptXRs { if r := xrResults[dt.MakeDiffKeyFromResource(xr)]; r != nil && !r.HasError() { group := dt.XRDiffGroup{XR: objectRef(xr), Diffs: r.Diffs} - composites = append(composites, renderedComposite{xr: xr, group: withUnchanged([]dt.XRDiffGroup{group}, []map[string]*dt.ResourceDiff{r.rendered.Unchanged()})[0]}) + composites = append(composites, renderedComposite{xr: xr, group: withUnchanged([]dt.XRDiffGroup{group}, []map[string]*dt.ResourceDiff{r.unchanged})[0]}) } } @@ -830,8 +832,9 @@ func (p *DefaultCompDiffProcessor) collectXRDiffs(ctx context.Context, xrs []*un } else { // Store successful result with diffs results[resourceID] = &XRDiffResult{ - Diffs: diffs, - rendered: rendered, + Diffs: diffs, + unchanged: rendered.Unchanged(), + health: rendered.Health(), } } } @@ -1643,7 +1646,7 @@ func (p *DefaultCompDiffProcessor) buildImpactAnalysis(xrs []*un.Unstructured, r case result != nil && result.HasChanges(): impact.Status = dt.XRStatusChanged impact.Diffs = result.Diffs - impact.Health = result.rendered.Health() + impact.Health = result.health summary.WithChanges++ default: impact.Status = dt.XRStatusUnchanged @@ -1653,7 +1656,7 @@ func (p *DefaultCompDiffProcessor) buildImpactAnalysis(xrs []*un.Unstructured, r impact.Diffs = result.Diffs } if result != nil { - impact.Health = result.rendered.Health() + impact.Health = result.health } summary.Unchanged++ } diff --git a/internal/diff/diffprocessor/comp_processor_test.go b/internal/diff/diffprocessor/comp_processor_test.go index 24814f92..c3466a06 100644 --- a/internal/diff/diffprocessor/comp_processor_test.go +++ b/internal/diff/diffprocessor/comp_processor_test.go @@ -3030,16 +3030,20 @@ func TestDefaultCompDiffProcessor_DiffComposition_Health(t *testing.T) { status dt.XRStatus health dt.CompositeHealth } + type args struct { + // changed makes the render produce a change; renderErr makes it fail. + changed bool + renderErr error + } cases := map[string]struct { reason string - // changed makes the render produce a change; errs makes it fail. - changed, errs bool - want want + args args + want want }{ "Changed": { - reason: "A changed composite carries the health its render read.", - changed: true, - want: want{status: dt.XRStatusChanged, health: live}, + reason: "A changed composite carries the health its render read.", + args: args{changed: true}, + want: want{status: dt.XRStatusChanged, health: live}, }, "Unchanged": { reason: "An unchanged composite carries its health too: being unchanged does not make it healthy.", @@ -3047,7 +3051,7 @@ func TestDefaultCompDiffProcessor_DiffComposition_Health(t *testing.T) { }, "Errored": { reason: "A composite whose render failed carries no health.", - errs: true, + args: args{renderErr: errors.New("render failed")}, want: want{status: dt.XRStatusError}, }, } @@ -3058,14 +3062,14 @@ func TestDefaultCompDiffProcessor_DiffComposition_Health(t *testing.T) { proc := NewCompDiffProcessor(&tu.MockDiffProcessor{ DiffSingleResourceFn: func(_ context.Context, res *un.Unstructured, _ types.CompositionProvider) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { - if tc.errs { - return nil, types.RenderedResources{}, errors.New("render failed") + if tc.args.renderErr != nil { + return nil, types.RenderedResources{}, tc.args.renderErr } var rendered types.RenderedResources - rendered.SetLive(res) + rendered.SetComposite(res) rendered.AddObserved(bucket) diffs := map[string]*dt.ResourceDiff{} - if tc.changed { + if tc.args.changed { d := &dt.ResourceDiff{Gvk: bucket.GroupVersionKind(), ResourceName: "bucket", DiffType: dt.DiffTypeModified, Current: dt.ResourceViews{Raw: bucket}, Desired: dt.ResourceViews{Raw: bucket}} diffs[d.GetDiffKey()] = d } diff --git a/internal/diff/diffprocessor/diff_processor.go b/internal/diff/diffprocessor/diff_processor.go index 4237b05d..f53eb086 100644 --- a/internal/diff/diffprocessor/diff_processor.go +++ b/internal/diff/diffprocessor/diff_processor.go @@ -528,9 +528,11 @@ func (p *DefaultDiffProcessor) diffSingleResourceInternal(ctx context.Context, r return nil, types.RenderedResources{}, errors.Wrap(err, "cannot calculate diffs for composed resources") } - // Record the live objects already fetched, for health. The observed resources are the cluster's, - // never the copies --eventual-state synthesizes Ready on (see RenderToStableState). - rendered.SetLive(composite.Cluster) + // Record the health of the live objects already fetched: the composite that composes (for a claim, + // its backing XR, whose conditions the claim only partly mirrors) and what it composes. The observed + // resources are the cluster's, never the copies --eventual-state synthesizes Ready on (see + // RenderToStableState). + rendered.SetComposite(composite.Composed()) for i := range observedResources { rendered.AddObserved(&observedResources[i].Unstructured) diff --git a/internal/diff/diffprocessor/diff_processor_test.go b/internal/diff/diffprocessor/diff_processor_test.go index de257e3a..b1065416 100644 --- a/internal/diff/diffprocessor/diff_processor_test.go +++ b/internal/diff/diffprocessor/diff_processor_test.go @@ -1117,6 +1117,238 @@ func TestDefaultDiffProcessor_PerformDiff_Groups(t *testing.T) { } } +// TestDefaultDiffProcessor_PerformDiff_Health covers the health each group carries, end to end through +// composite resolution, the observed-resource fetch, the nested XR merge and the input validator. The +// composite reports the cluster XR's own health (for a claim, its backing XR's), and every composed +// resource is listed once, nested composites' own included, each with the nested composite that +// directly composes it. +func TestDefaultDiffProcessor_PerformDiff_Health(t *testing.T) { + ctx := t.Context() + + const apiVersion = testGroup + "/" + testAPIVersion + + parentCluster := tu.NewResource(apiVersion, testKind, "parent").WithUID("parent-uid"). + WithCondition("Synced", "False", "ReconcileError"). + Build() + childCluster := tu.NewResource(apiVersion, "XChild", "child").WithUID("child-uid"). + WithCompositionResourceName("child"). + ControlledBy(parentCluster). + WithCondition("Ready", "False", "Creating"). + Build() + bucketA := tu.NewResource(apiVersion, "Bucket", "bucket-a"). + WithCompositionResourceName("bucket-a"). + ControlledBy(parentCluster). + WithCondition("Ready", "False", "Creating"). + Build() + bucketB := tu.NewResource(apiVersion, "Bucket", "bucket-b"). + WithCompositionResourceName("bucket-b"). + ControlledBy(childCluster). + WithCondition("Synced", "False", "ReconcileError"). + Build() + // The claim is healthy, so only its backing XR (parent) can make the composite failing. + claimCluster := tu.NewResource(apiVersion, "Claim", "my-claim").InNamespace("ns"). + WithSpecField("resourceRef", map[string]any{"apiVersion": apiVersion, "kind": testKind, "name": "parent"}). + WithCondition("Synced", "True", "ReconcileSuccess"). + WithCondition("Ready", "True", "Available"). + Build() + + parentInput := tu.NewResource(apiVersion, testKind, "parent").WithSpecField("coolField", "value").Build() + claimInput := tu.NewResource(apiVersion, "Claim", "my-claim").InNamespace("ns").WithSpecField("coolField", "value").Build() + + fullTree := tu.NewTreeNode(parentCluster).WithChildren( + tu.NewTreeNode(childCluster).WithChildren(tu.NewTreeNode(bucketB)), + tu.NewTreeNode(bucketA), + ).Build() + // The tree without the nested XR, as when the tree client cannot walk to it: the nested XR is then + // found by name instead. + treeWithoutChild := tu.NewTreeNode(parentCluster).WithChildren(tu.NewTreeNode(bucketA)).Build() + childTree := tu.NewTreeNode(childCluster).WithChildren(tu.NewTreeNode(bucketB)).Build() + + xrds := map[string]*un.Unstructured{ + testKind: tu.NewXRD(testCRDName, testGroup, testKind).WithPlural(testPlural).WithSingular(testSingular).BuildAsUnstructured(), + "XChild": tu.NewXRD("xchilds."+testGroup, testGroup, "XChild").WithPlural("xchilds").WithSingular("xchild").BuildAsUnstructured(), + } + crds := map[string]*extv1.CustomResourceDefinition{ + testCRDName: makeTestCRD(testCRDName, testKind, testGroup, testAPIVersion), + "xchilds." + testGroup: makeTestCRD("xchilds."+testGroup, "XChild", testGroup, testAPIVersion), + } + compositions := map[string]*apiextensionsv1.Composition{ + testKind: tu.NewComposition("parent-comp").WithCompositeTypeRef(apiVersion, testKind). + WithPipelineMode().WithPipelineStep("step1", "function-test", nil).Build(), + "XChild": tu.NewComposition("child-comp").WithCompositeTypeRef(apiVersion, "XChild"). + WithPipelineMode().WithPipelineStep("step1", "function-test", nil).Build(), + } + // rendered is what each composite's composition renders, by the composite's kind. + rendered := map[string][]cpd.Unstructured{ + testKind: { + *tu.NewResource(apiVersion, "XChild", "child").WithCompositionResourceName("child").BuildUComposed(), + *tu.NewResource(apiVersion, "Bucket", "bucket-a").WithCompositionResourceName("bucket-a").BuildUComposed(), + }, + "XChild": {*tu.NewResource(apiVersion, "Bucket", "bucket-b").WithCompositionResourceName("bucket-b").BuildUComposed()}, + } + cluster := map[string]*un.Unstructured{} + for _, u := range []*un.Unstructured{parentCluster, childCluster, claimCluster} { + cluster[dt.MakeDiffKeyFromResource(u)] = u + } + + ref := func(kind, name string) corev1.ObjectReference { + return corev1.ObjectReference{APIVersion: apiVersion, Kind: kind, Name: name} + } + via := ref("XChild", "child") + failing := &health.Health{State: health.Failing, Synced: &health.Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError"}} + notReady := &health.Health{State: health.NotReady, Ready: &health.Condition{Status: corev1.ConditionFalse, Reason: "Creating"}} + nested := dt.CompositeHealth{ + XR: failing, + Composed: []dt.ResourceHealth{ + {ObjectReference: ref("Bucket", "bucket-a"), Health: notReady}, + {ObjectReference: ref("Bucket", "bucket-b"), Via: &via, Health: failing}, + {ObjectReference: ref("XChild", "child"), Health: notReady}, + }, + } + + type args struct { + input *un.Unstructured + parentTree *resource.Resource + eventualState bool + verdict error + } + + tests := map[string]struct { + reason string + args args + want dt.CompositeHealth + }{ + "NestedXRResourcesListedOnceWithVia": { + reason: "The nested XR is a resource of its parent, and what it composes is listed once with it as via.", + args: args{input: parentInput, parentTree: fullTree}, + want: nested, + }, + "NestedXRFoundByNameKeepsItsHealth": { + reason: "A nested XR absent from the parent's observed set, found by name instead, is still listed.", + args: args{input: parentInput, parentTree: treeWithoutChild}, + want: nested, + }, + "ClaimReportsItsBackingXR": { + reason: "A claim's health is its backing XR's, which here is failing while the claim is healthy.", + args: args{input: claimInput, parentTree: fullTree}, + want: nested, + }, + "EventualStateLeavesHealthAsObserved": { + reason: "--eventual-state synthesizes Ready only for the next render; bucket-a stays notReady.", + args: args{input: parentInput, parentTree: fullTree, eventualState: true}, + want: nested, + }, + "VerdictErrorClearsHealth": { + reason: "A group the validator fails reports no health, as it reports no diffs.", + args: args{input: parentInput, parentTree: fullTree, verdict: errors.New("rejected")}, + want: dt.CompositeHealth{}, + }, + } + + for name, tc := range tests { + t.Run(name, func(t *testing.T) { + trees := map[string]*resource.Resource{"parent": tc.args.parentTree, "child": childTree} + + k8sClients := k8.Clients{ + Apply: tu.NewMockApplyClient().WithSuccessfulDryRun().Build(), + Resource: tu.NewMockResourceClient().WithResourcesExist(parentCluster, childCluster, claimCluster).Build(), + Schema: tu.NewMockSchemaClient(). + WithNoResourcesRequiringCRDs(). + WithGetCRDByName(func(name string) (*extv1.CustomResourceDefinition, error) { + if crd, ok := crds[name]; ok { + return crd, nil + } + return nil, errors.Errorf("CRD %s not found", name) + }). + Build(), + Type: tu.NewMockTypeConverter().Build(), + } + xpClients := xp.Clients{ + Credential: &tu.MockCredentialClient{}, + Definition: tu.NewMockDefinitionClient(). + WithGetXRDForXR(func(_ context.Context, gvk schema.GroupVersionKind) (*un.Unstructured, error) { + if xrd, ok := xrds[gvk.Kind]; ok { + return xrd, nil + } + return nil, errors.Errorf("no XRD for %s", gvk) + }). + WithIsClaimResource(func(_ context.Context, res *un.Unstructured) bool { return res.GetKind() == "Claim" }). + Build(), + Function: tu.NewMockFunctionClient(). + WithSuccessfulFunctionsFetch([]pkgv1.Function{{ObjectMeta: metav1.ObjectMeta{Name: "function-test"}}}). + Build(), + ResourceTree: tu.NewMockResourceTreeClient(). + WithGetResourceTree(func(_ context.Context, xr *un.Unstructured) (*resource.Resource, error) { + if tree, ok := trees[xr.GetName()]; ok { + return tree, nil + } + return nil, errors.Errorf("no tree for %s", xr.GetName()) + }). + Build(), + } + + opts := append(testProcessorOptions(t), + WithEventualState(tc.args.eventualState), + WithRenderFunc(func(_ context.Context, _ logging.Logger, in RenderInputs) (render.CompositionOutputs, error) { + return render.CompositionOutputs{CompositeResource: in.CompositeResource, ComposedResources: rendered[in.CompositeResource.GetKind()]}, nil + }), + WithSchemaValidatorFactory(func(k8.SchemaClient, k8.ResourceClient, xp.DefinitionClient, logging.Logger) SchemaValidator { + return &tu.MockSchemaValidator{ + ValidateResourcesFn: func(context.Context, *un.Unstructured, []cpd.Unstructured) error { return nil }, + } + }), + WithDiffCalculatorFactory(func(k8.ApplyClient, k8.AccessChecker, xp.ResourceTreeClient, ResourceManager, logging.Logger, compare.Options, DryRunOn, Defaulter, func(*un.Unstructured) bool) DiffCalculator { + return &tu.MockDiffCalculator{ + // An unchanged diff of the composite against its cluster copy, which is what + // nested processing and removal detection read the cluster XR from. + CalculateNonRemovalDiffsFn: func(_ context.Context, xr *cmp.Unstructured, _ render.CompositionOutputs) (map[string]*dt.ResourceDiff, types.RenderedResources, error) { + key := dt.MakeDiffKeyFromResource(xr.GetUnstructured()) + return map[string]*dt.ResourceDiff{key: { + Gvk: xr.GroupVersionKind(), + Namespace: xr.GetNamespace(), + ResourceName: xr.GetName(), + DiffType: dt.DiffTypeEqual, + Current: dt.ResourceViews{Raw: cluster[key]}, + }}, types.RenderedResources{}, nil + }, + } + }), + ) + + if tc.args.verdict != nil { + opts = append(opts, WithInputValidatorFactory(func(dt.Advisories, []*un.Unstructured) InputValidator { + return &tu.MockInputValidator{ + ToRenderFn: func() []types.ValidatedInput { return []types.ValidatedInput{{Resource: tc.args.input}} }, + RecordRenderFn: func(int, map[string]bool, error) {}, + VerdictsFn: func([]dt.XRDiffGroup) []error { return []error{tc.args.verdict} }, + RenderOverlapsFn: func([]dt.XRDiffGroup) []error { return nil }, + } + })) + } + + processor := NewDiffProcessor(k8sClients, xpClients, opts...) + + report, err := processor.PerformDiff(ctx, []*un.Unstructured{tc.args.input}, func(_ context.Context, res *un.Unstructured) (types.ResolvedComposition, error) { + if comp, ok := compositions[res.GetKind()]; ok { + return types.ResolvedComposition{Composition: comp}, nil + } + return types.ResolvedComposition{}, xp.ErrNoMatchingComposition + }) + if err != nil && tc.args.verdict == nil { + t.Fatalf("\n%s\nPerformDiff(...): unexpected error: %v", tc.reason, err) + } + + if len(report.Groups) != 1 { + t.Fatalf("\n%s\nPerformDiff(...): got %d groups, want 1", tc.reason, len(report.Groups)) + } + + if diff := gcmp.Diff(tc.want, report.Groups[0].Health); diff != "" { + t.Errorf("\n%s\nPerformDiff(...) group health: -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + // TestDefaultDiffProcessor_PerformDiff_LateWarningsReachStructuredOutput pins the warning channel's // contract for advisories that can only be discovered during teardown. The leftover-function-container // advisory is raised from FunctionProvider.Cleanup, which used to run in the command layer's defer — diff --git a/internal/diff/result/types.go b/internal/diff/result/types.go index 62c86849..ed0cc2f0 100644 --- a/internal/diff/result/types.go +++ b/internal/diff/result/types.go @@ -56,8 +56,8 @@ type XRDiffGroup struct { // render already fetched. It describes the cluster, not the desired state: a resource the render // would create has no entry, and one it would delete has one. type CompositeHealth struct { - // XR is the health of the input's own cluster object (for a claim, the claim); nil when the - // cluster has none. + // XR is the health of the cluster object the input composes: the XR itself or, for a claim, its + // backing XR. It is nil when the cluster has none. XR *health.Health // Composed holds the health of each observed composed resource, once each, in diff key order. Composed []ResourceHealth @@ -67,6 +67,10 @@ type CompositeHealth struct { type ResourceHealth struct { corev1.ObjectReference + // Via is the nested composite that directly composes the resource, or nil when the input's own + // composite does. + Via *corev1.ObjectReference + Health *health.Health } diff --git a/internal/diff/types/types.go b/internal/diff/types/types.go index 2644d745..97f98333 100644 --- a/internal/diff/types/types.go +++ b/internal/diff/types/types.go @@ -146,52 +146,74 @@ type CredentialFetchResult struct { // It also holds, by diff key, the diff of each unchanged rendering the calculator did not keep among // the diffs it returns, so input validation sees every rendering whether or not equal diffs are kept. // -// Finally it holds the live cluster objects the render already fetched, for health reporting: the -// composite's own cluster copy, and by diff key every composed resource observed beneath it. +// Finally it holds, for health reporting, the health of the live objects the render already fetched, +// classified as they are recorded so the objects themselves are not retained: the composite's own, and +// by diff key that of every composed resource observed beneath it, with the nested composite that +// directly composes each. type RenderedResources struct { keys map[string]bool desired map[string]map[string]bool // composite identity -> composition resource names it desired unchanged map[string]*dt.ResourceDiff - live *un.Unstructured - observed map[string]*un.Unstructured + composite *dt.ResourceHealth + composed map[string]dt.ResourceHealth } -// SetLive records the composite's own cluster copy (for a claim, the claim). Merge does not carry it: -// a nested composite's cluster copy is one of its parent's observed resources. -func (r *RenderedResources) SetLive(composite *un.Unstructured) { - r.live = composite +// SetComposite records the cluster object of the composite rendered: the XR itself or, for a claim, its +// backing XR. A nil object, for a composite the cluster does not have yet, records nothing. +func (r *RenderedResources) SetComposite(live *un.Unstructured) { + if live == nil { + return + } + + h := resourceHealth(live) + r.composite = &h } -// AddObserved records a composed resource's cluster copy. A resource observed twice, as a nested -// composite is (once beneath its parent, once as the composite it is), is recorded once. +// AddObserved records a composed resource's cluster copy, which the composite rendered composes +// directly. A resource recorded twice keeps its first record. func (r *RenderedResources) AddObserved(res *un.Unstructured) { - if r.observed == nil { - r.observed = make(map[string]*un.Unstructured) + r.addComposed(resourceHealth(res)) +} + +func (r *RenderedResources) addComposed(h dt.ResourceHealth) { + key := dt.MakeDiffKey(h.APIVersion, h.Kind, h.Namespace, h.Name) + if _, ok := r.composed[key]; ok { + return } - r.observed[dt.MakeDiffKeyFromResource(res)] = res + if r.composed == nil { + r.composed = make(map[string]dt.ResourceHealth) + } + + r.composed[key] = h } -// Health classifies the live objects SetLive and AddObserved recorded. +// Health returns the health SetComposite and AddObserved recorded, and Merge carried. func (r *RenderedResources) Health() dt.CompositeHealth { - h := dt.CompositeHealth{XR: health.Of(r.live)} - - for _, key := range slices.Sorted(maps.Keys(r.observed)) { - res := r.observed[key] - h.Composed = append(h.Composed, dt.ResourceHealth{ - ObjectReference: corev1.ObjectReference{ - APIVersion: res.GetAPIVersion(), - Kind: res.GetKind(), - Name: res.GetName(), - Namespace: res.GetNamespace(), - }, - Health: health.Of(res), - }) + var h dt.CompositeHealth + if r.composite != nil { + h.XR = r.composite.Health + } + + for _, key := range slices.Sorted(maps.Keys(r.composed)) { + h.Composed = append(h.Composed, r.composed[key]) } return h } +func resourceHealth(u *un.Unstructured) dt.ResourceHealth { + return dt.ResourceHealth{ + ObjectReference: corev1.ObjectReference{ + APIVersion: u.GetAPIVersion(), + Kind: u.GetKind(), + Name: u.GetName(), + Namespace: u.GetNamespace(), + }, + Health: health.Of(u), + } +} + // AddUnchanged records the diff of an unchanged rendering that is not among the returned diffs. func (r *RenderedResources) AddUnchanged(key string, d *dt.ResourceDiff) { if r.unchanged == nil { @@ -246,8 +268,20 @@ func (r *RenderedResources) Merge(other RenderedResources) { r.AddUnchanged(key, d) } - for _, res := range other.observed { - r.AddObserved(res) + // What other's composite composed directly, it composed via that composite; and the composite + // itself is composed by r's, which records it already when it observed it (but not when the + // composite was found by name instead). + for _, h := range other.composed { + if h.Via == nil && other.composite != nil { + via := other.composite.ObjectReference + h.Via = &via + } + + r.addComposed(h) + } + + if other.composite != nil { + r.addComposed(*other.composite) } } diff --git a/internal/diff/types/types_test.go b/internal/diff/types/types_test.go index 851e22a2..3347a5de 100644 --- a/internal/diff/types/types_test.go +++ b/internal/diff/types/types_test.go @@ -47,6 +47,10 @@ func TestRenderedResourcesHealth(t *testing.T) { parent := obj("example.org/v1", "XParent", "parent", "") child := obj("example.org/v1", "XChild", "child", "False") grandchild := obj("v1", "ConfigMap", "grandchild", "") + leaf := obj("v1", "Secret", "leaf", "") + childRef := ref("example.org/v1", "XChild", "child") + grandchildRef := ref("v1", "ConfigMap", "grandchild") + leafRef := ref("v1", "Secret", "leaf") type args struct { build func() RenderedResources @@ -61,11 +65,11 @@ func TestRenderedResourcesHealth(t *testing.T) { args: args{build: func() RenderedResources { return RenderedResources{} }}, want: dt.CompositeHealth{}, }, - "LiveAndObserved": { - reason: "The live composite gives XR, and each observed resource an entry, in diff key order.", + "CompositeAndObserved": { + reason: "The composite gives XR, and each observed resource an entry with no via, in diff key order.", args: args{build: func() RenderedResources { var r RenderedResources - r.SetLive(parent) + r.SetComposite(parent) r.AddObserved(grandchild) r.AddObserved(child) return r @@ -73,28 +77,71 @@ func TestRenderedResourcesHealth(t *testing.T) { want: dt.CompositeHealth{ XR: healthy, Composed: []dt.ResourceHealth{ - {ObjectReference: ref("example.org/v1", "XChild", "child"), Health: notReady}, - {ObjectReference: ref("v1", "ConfigMap", "grandchild"), Health: healthy}, + {ObjectReference: childRef, Health: notReady}, + {ObjectReference: grandchildRef, Health: healthy}, }, }, }, - "NestedMergedOnce": { - reason: "Merging a nested composite's render adds what it observed, but not its own live copy as XR; one observed by both levels counts once.", + "NoComposite": { + reason: "A composite the cluster does not have yet records no XR health.", + args: args{build: func() RenderedResources { + var r RenderedResources + r.SetComposite(nil) + return r + }}, + want: dt.CompositeHealth{}, + }, + "NestedMergedOnceWithVia": { + reason: "Merging a nested composite's render lists what it composed with the nested composite as via, and the nested composite itself once, as the parent observed it.", args: args{build: func() RenderedResources { var r, nested RenderedResources - r.SetLive(parent) + r.SetComposite(parent) r.AddObserved(child) - nested.SetLive(child) + nested.SetComposite(child) nested.AddObserved(grandchild) - nested.AddObserved(child) r.Merge(nested) return r }}, want: dt.CompositeHealth{ XR: healthy, Composed: []dt.ResourceHealth{ - {ObjectReference: ref("example.org/v1", "XChild", "child"), Health: notReady}, - {ObjectReference: ref("v1", "ConfigMap", "grandchild"), Health: healthy}, + {ObjectReference: childRef, Health: notReady}, + {ObjectReference: grandchildRef, Via: &childRef, Health: healthy}, + }, + }, + }, + "NestedNotObservedByParent": { + reason: "A nested composite the parent did not observe (found by name instead) is still listed, from its own render.", + args: args{build: func() RenderedResources { + var r, nested RenderedResources + r.SetComposite(parent) + nested.SetComposite(child) + r.Merge(nested) + return r + }}, + want: dt.CompositeHealth{ + XR: healthy, + Composed: []dt.ResourceHealth{{ObjectReference: childRef, Health: notReady}}, + }, + }, + "DeeplyNestedKeepsDirectParent": { + reason: "Via names the composite that directly composes a resource, however deep, not an intermediate one.", + args: args{build: func() RenderedResources { + var r, nested, deeper RenderedResources + r.SetComposite(parent) + nested.SetComposite(child) + deeper.SetComposite(grandchild) + deeper.AddObserved(leaf) + nested.Merge(deeper) + r.Merge(nested) + return r + }}, + want: dt.CompositeHealth{ + XR: healthy, + Composed: []dt.ResourceHealth{ + {ObjectReference: childRef, Health: notReady}, + {ObjectReference: grandchildRef, Via: &childRef, Health: healthy}, + {ObjectReference: leafRef, Via: &grandchildRef, Health: healthy}, }, }, }, From d3deddc35501472919796a224812739e3440e97e Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 10:40:34 -0500 Subject: [PATCH 05/30] feat(simulate): name the via of nested unhealthy resources and nest summary health Each unhealthy entry names the nested composite that composes it under via. The health counts move under summary.health, and a composite in two entries is counted once. The help documents via, summary.health, a claim's health, and that a composite that was not simulated has no health. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/simulate/help/output.md | 15 +++--- internal/simulate/report.go | 7 ++- internal/simulate/report_test.go | 87 +++++++++++++++++++------------- internal/simulate/result.go | 36 +++++++------ internal/simulate/result_test.go | 47 ++++++++++------- 5 files changed, 117 insertions(+), 75 deletions(-) diff --git a/internal/simulate/help/output.md b/internal/simulate/help/output.md index d48ad27c..bcd9c413 100644 --- a/internal/simulate/help/output.md +++ b/internal/simulate/help/output.md @@ -20,12 +20,15 @@ The `-o` flag picks the output format: `failing` (its Synced condition is False), `notReady` (its Ready condition is False), `paused` (it carries the `crossplane.io/paused: "true"` annotation, whatever its conditions), or `healthy`, with the `synced` and - `ready` conditions it was read from. A composite or plan has no `health` - when there is no live object, as for a create. Each composite also lists - under `unhealthy` the composed resources, nested composites' included, that - are not healthy and have no plan of their own. `summary` counts the - composites that are not healthy (`unhealthyComposites`), and the other - resources that are `failing`, `notReady` or `paused`, each once. + `ready` conditions it was read from. A claim's `health` is its backing + composite resource's. A composite or plan has no `health` when there is no + live object, as for a create, or, for a composite, when it was not + simulated (`error`, `skipped`, `deferred`). Each composite also lists under + `unhealthy` the composed resources that are not healthy and have no plan of + their own, each once. That includes nested composites and what they compose; + an entry a nested composite composes names it under `via`. `summary.health` + counts the composites that are not healthy (`unhealthyComposites`), and the + other resources that are `failing`, `notReady` or `paused`, each once. - `diff` prints a line diff of each changed resource's manifest. Every Secret value, and any value a provider marks sensitive, reads `(sensitive value)`; a value the provider computes on apply reads diff --git a/internal/simulate/report.go b/internal/simulate/report.go index 218c30fd..2e978109 100644 --- a/internal/simulate/report.go +++ b/internal/simulate/report.go @@ -270,7 +270,12 @@ func (a *assembler) unhealthy() { if !r.Health.Unhealthy() || planned[gk][[2]string{r.Namespace, r.Name}] { continue } - a.composites[i].Unhealthy = append(a.composites[i].Unhealthy, UnhealthyResource{Ref: resourceRef(r.ObjectReference), Health: r.Health}) + u := UnhealthyResource{Ref: resourceRef(r.ObjectReference), Health: r.Health} + if r.Via != nil { + via := resourceRef(*r.Via) + u.Via = &via + } + a.composites[i].Unhealthy = append(a.composites[i].Unhealthy, u) } } } diff --git a/internal/simulate/report_test.go b/internal/simulate/report_test.go index 1cba7315..257acf50 100644 --- a/internal/simulate/report_test.go +++ b/internal/simulate/report_test.go @@ -635,24 +635,34 @@ func TestHealth(t *testing.T) { } bucketV1 := ref("s3.example.org/v1", "Bucket", "b") bucketV2 := ref("s3.example.org/v2", "Bucket", "b") + child := ref("example.org/v1", "XSubnet", "child") + childPlan := resourceRef(child) + compRef := func(name string) *plan.ResourceRef { + return &plan.ResourceRef{APIVersion: "apiextensions.crossplane.io/v1", Kind: "Composition", Name: name} + } + unhealthyXR := dt.CompositeHealth{XR: failing, Composed: []dt.ResourceHealth{{ObjectReference: cmRef("cm"), Health: notReady}}} type want struct { plans map[string]*health.Health // plan name -> health composites []CompositeResult summary HealthSummary } + type args struct { + xr *dt.XRReport + comp *dt.CompDiffOutput + } cases := map[string]struct { reason string - in *dt.XRReport + args args want want }{ "AllHealthy": { reason: "A healthy run carries health on the composite and plans but emits no unhealthy entries, and counts nothing.", - in: &dt.XRReport{Groups: []dt.XRDiffGroup{{ + args: args{xr: &dt.XRReport{Groups: []dt.XRDiffGroup{{ XR: xrRef, Diffs: diffs(live(rdiff(cmGVK, "cm", dt.DiffTypeModified, map[string]any{}, map[string]any{"data": "x"}), nil)), Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{{ObjectReference: cmRef("cm"), Health: healthy}}}, - }}}, + }}}}, want: want{ plans: map[string]*health.Health{"cm": healthy}, composites: []CompositeResult{{Ref: xrPlan, Status: CompositeChanged, Health: healthy}}, @@ -660,10 +670,10 @@ func TestHealth(t *testing.T) { }, "CreateHasNoHealth": { reason: "A created resource has no live object, so its plan carries no health; nor does a composite not in the cluster.", - in: &dt.XRReport{Groups: []dt.XRDiffGroup{{ + args: args{xr: &dt.XRReport{Groups: []dt.XRDiffGroup{{ XR: xrRef, Diffs: diffs(rdiff(cmGVK, "new", dt.DiffTypeAdded, nil, map[string]any{})), - }}}, + }}}}, want: want{ plans: map[string]*health.Health{"new": nil}, composites: []CompositeResult{{Ref: xrPlan, Status: CompositeChanged}}, @@ -671,7 +681,7 @@ func TestHealth(t *testing.T) { }, "PlannedAndUnplanned": { reason: "An unhealthy resource with a plan shows its health there; one without a plan, under a version the plan does not use either, is listed as unhealthy once.", - in: &dt.XRReport{Groups: []dt.XRDiffGroup{{ + args: args{xr: &dt.XRReport{Groups: []dt.XRDiffGroup{{ XR: xrRef, Diffs: diffs(live(rdiff(cmGVK, "cm", dt.DiffTypeRemoved, map[string]any{}, nil), cond("Ready", "False"))), Health: dt.CompositeHealth{XR: failing, Composed: []dt.ResourceHealth{ @@ -679,7 +689,7 @@ func TestHealth(t *testing.T) { {ObjectReference: cmRef("quiet"), Health: healthy}, {ObjectReference: bucketV1, Health: failing}, }}, - }}}, + }}}}, want: want{ plans: map[string]*health.Health{"cm": notReady}, composites: []CompositeResult{{ @@ -691,61 +701,68 @@ func TestHealth(t *testing.T) { }, "PlannedAcrossVersions": { reason: "A composed resource observed under another API version than its plan's is matched to the plan, not listed again.", - in: &dt.XRReport{Groups: []dt.XRDiffGroup{{ + args: args{xr: &dt.XRReport{Groups: []dt.XRDiffGroup{{ XR: xrRef, Diffs: diffs(live(rdiff(schema.FromAPIVersionAndKind(bucketV2.APIVersion, bucketV2.Kind), "b", dt.DiffTypeModified, map[string]any{}, map[string]any{"spec": "x"}), cond("Synced", "False"))), Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{{ObjectReference: bucketV1, Health: failing}}}, - }}}, + }}}}, want: want{ plans: map[string]*health.Health{"b": failing}, composites: []CompositeResult{{Ref: xrPlan, Status: CompositeChanged, Health: healthy}}, summary: HealthSummary{Failing: 1}, }, }, - "NestedCountedOnce": { - reason: "A nested composite that is its own input is counted as a composite, not again as a resource, and a resource two composites reach counts once.", - in: &dt.XRReport{Groups: []dt.XRDiffGroup{ - { - XR: xrRef, - Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{ - {ObjectReference: ref("example.org/v1", "XSubnet", "child"), Health: notReady}, - {ObjectReference: cmRef("deep"), Health: failing}, - }}, - }, - { - XR: ref("example.org/v1", "XSubnet", "child"), - Health: dt.CompositeHealth{XR: notReady, Composed: []dt.ResourceHealth{{ObjectReference: cmRef("deep"), Health: failing}}}, - }, - }}, + "NestedVia": { + reason: "A nested composite is listed as a resource of its top-level composite, and what it composes is listed there too, once each, with via naming it.", + args: args{xr: &dt.XRReport{Groups: []dt.XRDiffGroup{{ + XR: xrRef, + Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{ + {ObjectReference: child, Health: notReady}, + {ObjectReference: cmRef("deep"), Via: &child, Health: failing}, + }}, + }}}}, want: want{ plans: map[string]*health.Health{}, + composites: []CompositeResult{{Ref: xrPlan, Status: CompositeUnchanged, Health: healthy, Unhealthy: []UnhealthyResource{ + {Ref: resourceRef(child), Health: notReady}, + {Ref: cmPlan("deep"), Via: &childPlan, Health: failing}, + }}}, + summary: HealthSummary{Failing: 1, NotReady: 1}, + }, + }, + "CompImpactsCountedOnce": { + reason: "A composition run carries each impact's health; an XR two compositions reach is one composite, so it counts once, and so does what it composes.", + args: args{comp: &dt.CompDiffOutput{Compositions: []dt.CompositionDiff{ + {Name: "a", ImpactAnalysis: []dt.XRImpact{{ObjectReference: xrRef, Status: dt.XRStatusUnchanged, Health: unhealthyXR}}}, + {Name: "b", ImpactAnalysis: []dt.XRImpact{{ObjectReference: xrRef, Status: dt.XRStatusUnchanged, Health: unhealthyXR}}}, + }}}, + want: want{ + plans: map[string]*health.Health{"a": nil, "b": nil}, composites: []CompositeResult{ - {Ref: xrPlan, Status: CompositeUnchanged, Health: healthy, Unhealthy: []UnhealthyResource{ - {Ref: plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XSubnet", Name: "child"}, Health: notReady}, - {Ref: cmPlan("deep"), Health: failing}, - }}, - {Ref: plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XSubnet", Name: "child"}, Status: CompositeUnchanged, Health: notReady, Unhealthy: []UnhealthyResource{ - {Ref: cmPlan("deep"), Health: failing}, - }}, + {Ref: xrPlan, Composition: compRef("a"), Status: CompositeUnchanged, Health: failing, Unhealthy: []UnhealthyResource{{Ref: cmPlan("cm"), Health: notReady}}}, + {Ref: xrPlan, Composition: compRef("b"), Status: CompositeUnchanged, Health: failing, Unhealthy: []UnhealthyResource{{Ref: cmPlan("cm"), Health: notReady}}}, }, - summary: HealthSummary{Composites: 1, Failing: 1}, + summary: HealthSummary{Composites: 1, NotReady: 1}, }, }, } for name, tc := range cases { t.Run(name, func(t *testing.T) { - got := FromXRReport(tc.in, nil, nil) + got := FromXRReport(tc.args.xr, nil, nil) + if tc.args.comp != nil { + got = FromCompReport(tc.args.comp, nil, nil) + } gotPlans := map[string]*health.Health{} for _, p := range got.Plans { gotPlans[p.Name] = p.Health } if diff := cmp.Diff(tc.want.plans, gotPlans, cmpopts.EquateEmpty()); diff != "" { - t.Errorf("%s\nFromXRReport(...) plan health: -want, +got:\n%s", tc.reason, diff) + t.Errorf("%s\nFrom...Report(...) plan health: -want, +got:\n%s", tc.reason, diff) } if diff := cmp.Diff(tc.want.composites, got.Composites, cmpopts.EquateEmpty()); diff != "" { - t.Errorf("%s\nFromXRReport(...) composites: -want, +got:\n%s", tc.reason, diff) + t.Errorf("%s\nFrom...Report(...) composites: -want, +got:\n%s", tc.reason, diff) } if diff := cmp.Diff(tc.want.summary, got.Health()); diff != "" { t.Errorf("%s\nHealth(): -want, +got:\n%s", tc.reason, diff) diff --git a/internal/simulate/result.go b/internal/simulate/result.go index e686f07f..6ebbb31a 100644 --- a/internal/simulate/result.go +++ b/internal/simulate/result.go @@ -91,23 +91,25 @@ type CompositeResult struct { // from Status, the simulated outcome; nil when the cluster has no such // composite, or it was not simulated. Health *health.Health `json:"health,omitempty"` - // Unhealthy lists the composed resources, nested composites' included, - // that are live, not healthy, and have no plan in the run (so no health - // of their own to show). + // Unhealthy lists the composed resources, nested composites and theirs + // included, that are live, not healthy, and have no plan in the run (so + // no health of their own to show). Unhealthy []UnhealthyResource `json:"unhealthy,omitempty"` } // UnhealthyResource is a composed resource that is not healthy before apply. type UnhealthyResource struct { - Ref plan.ResourceRef `json:"ref"` - Health *health.Health `json:"health"` + Ref plan.ResourceRef `json:"ref"` + // Via is the nested composite that directly composes the resource; nil + // when the listing composite does. + Via *plan.ResourceRef `json:"via,omitempty"` + Health *health.Health `json:"health"` } -// HealthSummary counts what is not healthy before apply, serialized among -// the summary's other counts. Composites counts +// HealthSummary counts what is not healthy before apply. Composites counts // the composites whose own health is not healthy; Failing, NotReady and -// Paused count the other resources, each once however many composites or -// plans reach it. +// Paused count the other resources. Each is counted once however many +// composites or plans reach it. type HealthSummary struct { Composites int `json:"unhealthyComposites"` Failing int `json:"failing"` @@ -217,11 +219,14 @@ func (r Result) Health() HealthSummary { return composites[groupKind(ref)][[2]string{ref.Namespace, ref.Name}] } for _, c := range r.Composites { - gk := groupKind(c.Ref) + gk, id := groupKind(c.Ref), [2]string{c.Ref.Namespace, c.Ref.Name} + if composites[gk][id] { + continue + } if composites[gk] == nil { composites[gk] = map[[2]string]bool{} } - composites[gk][[2]string{c.Ref.Namespace, c.Ref.Name}] = true + composites[gk][id] = true if c.Health.Unhealthy() { s.Composites++ } @@ -268,16 +273,15 @@ func groupKind(ref plan.ResourceRef) schema.GroupKind { // MarshalJSON writes the result with summary, composites and compositions // ahead of the plans, which can be long, and with the deferred and health -// counts inside summary. YAML goes through it too. Decoding needs no +// counts inside summary (the health ones under summary.health). YAML goes through it too. Decoding needs no // counterpart: summary decodes into Result.Summary, Deferred is counted from // Composites, and Health from Composites and Plans. func (r Result) MarshalJSON() ([]byte, error) { type summary struct { plan.Summary - HealthSummary - - Deferred int `json:"deferred"` + Health HealthSummary `json:"health"` + Deferred int `json:"deferred"` } return json.Marshal(struct { FormatVersion string `json:"formatVersion"` @@ -292,7 +296,7 @@ func (r Result) MarshalJSON() ([]byte, error) { }{ FormatVersion: r.FormatVersion, Errored: r.Errored, - Summary: summary{Summary: r.Summary, HealthSummary: r.Health(), Deferred: r.Deferred()}, + Summary: summary{Summary: r.Summary, Health: r.Health(), Deferred: r.Deferred()}, Composites: r.Composites, Compositions: r.Compositions, Plans: r.Plans, diff --git a/internal/simulate/result_test.go b/internal/simulate/result_test.go index 7d9038fe..6f72031c 100644 --- a/internal/simulate/result_test.go +++ b/internal/simulate/result_test.go @@ -215,7 +215,8 @@ func TestRenderJSONProjectFields(t *testing.T) { } // The top-level keys keep summary, composites and compositions ahead of - // the plans, and summary carries the deferred count. + // the plans, and summary carries the deferred count. Summary is not a flat + // map of counts: the health counts nest under summary.health. dec := json.NewDecoder(&buf) var keys []string if _, err := dec.Token(); err != nil { @@ -232,12 +233,16 @@ func TestRenderJSONProjectFields(t *testing.T) { t.Fatalf("Render(json): cannot read value of %q: %v", k, err) } if k == "summary" { - var sum map[string]int + var sum struct { + Destroy int `json:"destroy"` + Deferred int `json:"deferred"` + Health map[string]int `json:"health"` + } if err := json.Unmarshal(v, &sum); err != nil { - t.Fatalf("Render(json): summary is not a map of counts: %v", err) + t.Fatalf("Render(json): summary does not hold counts: %v", err) } - if sum["deferred"] != 1 || sum["destroy"] != 1 { - t.Errorf("Render(json): summary: want destroy 1 and deferred 1, got %v", sum) + if sum.Deferred != 1 || sum.Destroy != 1 || sum.Health == nil { + t.Errorf("Render(json): summary: want destroy 1, deferred 1 and health counts, got %+v", sum) } } } @@ -307,6 +312,7 @@ func TestNewResult(t *testing.T) { func TestRenderJSONHealth(t *testing.T) { xr := plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XNetwork", Name: "prod"} + nested := plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XSubnet", Name: "a"} failing := &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: "boom"}} type want struct { @@ -314,36 +320,43 @@ func TestRenderJSONHealth(t *testing.T) { plans string summary string } + type args struct { + in Result + } cases := map[string]struct { reason string - in Result + args args want want }{ "NoLiveObjects": { reason: "Without live objects no health or unhealthy key is written, and every health count is zero.", - in: NewResult( + args: args{in: NewResult( []*plan.ResourcePlan{{APIVersion: "v1", Kind: "ConfigMap", Name: "cm", Action: plan.ActionCreate, Composite: &xr}}, - []CompositeResult{{Ref: xr, Status: CompositeChanged}}, nil), + []CompositeResult{{Ref: xr, Status: CompositeChanged}}, nil)}, want: want{ composites: `[{"ref":{"apiVersion":"example.org/v1","kind":"XNetwork","name":"prod"},"status":"changed"}]`, plans: `[{"apiVersion":"v1","kind":"ConfigMap","name":"cm","action":"create","composite":{"apiVersion":"example.org/v1","kind":"XNetwork","name":"prod"}}]`, - summary: `{"create":1,"update":0,"destroy":0,"replace":0,"noop":0,"approximate":0,"errored":0,"unhealthyComposites":0,"failing":0,"notReady":0,"paused":0,"deferred":0}`, + summary: `{"create":1,"update":0,"destroy":0,"replace":0,"noop":0,"approximate":0,"errored":0,"health":{"unhealthyComposites":0,"failing":0,"notReady":0,"paused":0},"deferred":0}`, }, }, "Unhealthy": { - reason: "Composite and plan health, and the unhealthy list, are written under their keys with the conditions they came from.", - in: NewResult( + reason: "Composite and plan health, and the unhealthy list with each entry's via, are written under their keys with the conditions they came from.", + args: args{in: NewResult( []*plan.ResourcePlan{{APIVersion: "v1", Kind: "ConfigMap", Name: "cm", Action: plan.ActionNoOp, Composite: &xr, Health: &health.Health{State: health.Paused}}}, []CompositeResult{{ Ref: xr, Status: CompositeUnchanged, Health: failing, - Unhealthy: []UnhealthyResource{{Ref: plan.ResourceRef{APIVersion: "v1", Kind: "Secret", Name: "s"}, Health: &health.Health{State: health.NotReady, Ready: &health.Condition{Status: "False"}}}}, - }}, nil), + Unhealthy: []UnhealthyResource{ + {Ref: plan.ResourceRef{APIVersion: "v1", Kind: "Secret", Name: "s"}, Health: &health.Health{State: health.NotReady, Ready: &health.Condition{Status: "False"}}}, + {Ref: plan.ResourceRef{APIVersion: "v1", Kind: "Secret", Name: "t"}, Via: &nested, Health: &health.Health{State: health.Paused}}, + }, + }}, nil)}, want: want{ composites: `[{"ref":{"apiVersion":"example.org/v1","kind":"XNetwork","name":"prod"},"status":"unchanged",` + `"health":{"state":"failing","synced":{"status":"False","reason":"ReconcileError","message":"boom"}},` + - `"unhealthy":[{"ref":{"apiVersion":"v1","kind":"Secret","name":"s"},"health":{"state":"notReady","ready":{"status":"False"}}}]}]`, + `"unhealthy":[{"ref":{"apiVersion":"v1","kind":"Secret","name":"s"},"health":{"state":"notReady","ready":{"status":"False"}}},` + + `{"ref":{"apiVersion":"v1","kind":"Secret","name":"t"},"via":{"apiVersion":"example.org/v1","kind":"XSubnet","name":"a"},"health":{"state":"paused"}}]}]`, plans: `[{"apiVersion":"v1","kind":"ConfigMap","name":"cm","action":"no-op","composite":{"apiVersion":"example.org/v1","kind":"XNetwork","name":"prod"},"health":{"state":"paused"}}]`, - summary: `{"create":0,"update":0,"destroy":0,"replace":0,"noop":1,"approximate":0,"errored":0,"unhealthyComposites":1,"failing":0,"notReady":1,"paused":1,"deferred":0}`, + summary: `{"create":0,"update":0,"destroy":0,"replace":0,"noop":1,"approximate":0,"errored":0,"health":{"unhealthyComposites":1,"failing":0,"notReady":1,"paused":2},"deferred":0}`, }, }, } @@ -352,7 +365,7 @@ func TestRenderJSONHealth(t *testing.T) { t.Run(name, func(t *testing.T) { var buf bytes.Buffer r, _ := NewRenderer(plan.FormatJSON) - if err := r.Render(&buf, tc.in); err != nil { + if err := r.Render(&buf, tc.args.in); err != nil { t.Fatalf("%s\nRender(json): unexpected error: %v", tc.reason, err) } var top map[string]json.RawMessage @@ -374,7 +387,7 @@ func TestRenderJSONHealth(t *testing.T) { if err := json.Unmarshal(buf.Bytes(), &back); err != nil { t.Fatalf("%s\nRender(json): cannot decode: %v", tc.reason, err) } - if diff := cmp.Diff(tc.in, back); diff != "" { + if diff := cmp.Diff(tc.args.in, back); diff != "" { t.Errorf("%s\nRender(json) round trip: -want, +got:\n%s", tc.reason, diff) } }) From 4d137570c2560de241d9e3f6d28f3075c1258780 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 10:40:34 -0500 Subject: [PATCH 06/30] test(health): use args tables in the classifier tests Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/health/health_test.go | 42 +++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/internal/health/health_test.go b/internal/health/health_test.go index f4db7064..136ff663 100644 --- a/internal/health/health_test.go +++ b/internal/health/health_test.go @@ -43,9 +43,12 @@ func TestOf(t *testing.T) { readyFalse := cond("Ready", "False", "Creating", "") readyTrue := cond("Ready", "True", "Available", "") + type args struct { + u *unstructured.Unstructured + } cases := map[string]struct { reason string - in *unstructured.Unstructured + args args want *Health }{ "NilObject": { @@ -54,12 +57,12 @@ func TestOf(t *testing.T) { }, "NoConditions": { reason: "An object with no Synced or Ready condition, such as a ConfigMap, is healthy.", - in: obj(""), + args: args{u: obj("")}, want: &Health{State: Healthy}, }, "SyncedAndReady": { reason: "Synced=True and Ready=True is healthy, carrying both conditions.", - in: obj("", syncedTrue, readyTrue), + args: args{u: obj("", syncedTrue, readyTrue)}, want: &Health{ State: Healthy, Synced: &Condition{Status: corev1.ConditionTrue, Reason: "ReconcileSuccess"}, @@ -68,7 +71,7 @@ func TestOf(t *testing.T) { }, "Unknown": { reason: "Unknown conditions are healthy.", - in: obj("", cond("Synced", "Unknown", "", ""), cond("Ready", "Unknown", "", "")), + args: args{u: obj("", cond("Synced", "Unknown", "", ""), cond("Ready", "Unknown", "", ""))}, want: &Health{ State: Healthy, Synced: &Condition{Status: corev1.ConditionUnknown}, @@ -77,7 +80,7 @@ func TestOf(t *testing.T) { }, "SyncedFalse": { reason: "Synced=False is failing, even when Ready=False too.", - in: obj("", syncedFalse, readyFalse), + args: args{u: obj("", syncedFalse, readyFalse)}, want: &Health{ State: Failing, Synced: &Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError", Message: "cannot update: denied"}, @@ -86,7 +89,7 @@ func TestOf(t *testing.T) { }, "ReadyFalse": { reason: "Ready=False with Synced not False is not ready.", - in: obj("", syncedTrue, readyFalse), + args: args{u: obj("", syncedTrue, readyFalse)}, want: &Health{ State: NotReady, Synced: &Condition{Status: corev1.ConditionTrue, Reason: "ReconcileSuccess"}, @@ -95,12 +98,12 @@ func TestOf(t *testing.T) { }, "ReadyFalseOnly": { reason: "Ready=False with no Synced condition is not ready.", - in: obj("", readyFalse), + args: args{u: obj("", readyFalse)}, want: &Health{State: NotReady, Ready: &Condition{Status: corev1.ConditionFalse, Reason: "Creating"}}, }, "PausedOverridesFailing": { reason: "The paused annotation overrides Synced=False; the stale conditions are still reported.", - in: obj("true", syncedFalse), + args: args{u: obj("true", syncedFalse)}, want: &Health{ State: Paused, Synced: &Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError", Message: "cannot update: denied"}, @@ -108,7 +111,7 @@ func TestOf(t *testing.T) { }, "PausedNotTrue": { reason: "Only the value \"true\" pauses an object.", - in: obj("false", syncedFalse), + args: args{u: obj("false", syncedFalse)}, want: &Health{ State: Failing, Synced: &Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError", Message: "cannot update: denied"}, @@ -116,14 +119,14 @@ func TestOf(t *testing.T) { }, "MalformedStatus": { reason: "A status whose conditions cannot be read is treated as having none.", - in: &unstructured.Unstructured{Object: map[string]any{"status": map[string]any{"conditions": "nope"}}}, + args: args{u: &unstructured.Unstructured{Object: map[string]any{"status": map[string]any{"conditions": "nope"}}}}, want: &Health{State: Healthy}, }, } for name, tc := range cases { t.Run(name, func(t *testing.T) { - got := Of(tc.in) + got := Of(tc.args.u) if diff := cmp.Diff(tc.want, got); diff != "" { t.Errorf("%s\nOf(...): -want, +got:\n%s", tc.reason, diff) } @@ -132,21 +135,24 @@ func TestOf(t *testing.T) { } func TestUnhealthy(t *testing.T) { + type args struct { + h *Health + } cases := map[string]struct { reason string - in *Health + args args want bool }{ - "Nil": {reason: "No live object is not unhealthy.", in: nil, want: false}, - "Healthy": {reason: "Healthy is not unhealthy.", in: &Health{State: Healthy}, want: false}, - "Failing": {reason: "Failing is unhealthy.", in: &Health{State: Failing}, want: true}, - "NotReady": {reason: "Not ready is unhealthy.", in: &Health{State: NotReady}, want: true}, - "Paused": {reason: "Paused is unhealthy.", in: &Health{State: Paused}, want: true}, + "Nil": {reason: "No live object is not unhealthy.", args: args{h: nil}, want: false}, + "Healthy": {reason: "Healthy is not unhealthy.", args: args{h: &Health{State: Healthy}}, want: false}, + "Failing": {reason: "Failing is unhealthy.", args: args{h: &Health{State: Failing}}, want: true}, + "NotReady": {reason: "Not ready is unhealthy.", args: args{h: &Health{State: NotReady}}, want: true}, + "Paused": {reason: "Paused is unhealthy.", args: args{h: &Health{State: Paused}}, want: true}, } for name, tc := range cases { t.Run(name, func(t *testing.T) { - if got := tc.in.Unhealthy(); got != tc.want { + if got := tc.args.h.Unhealthy(); got != tc.want { t.Errorf("%s\nUnhealthy(): want %t, got %t", tc.reason, tc.want, got) } }) From ffc70db67197d8fca261ae297be56dcbf3484ada Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 10:53:40 -0500 Subject: [PATCH 07/30] docs(simulate): reflow the MarshalJSON comment Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/simulate/result.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/simulate/result.go b/internal/simulate/result.go index 6ebbb31a..adf0483a 100644 --- a/internal/simulate/result.go +++ b/internal/simulate/result.go @@ -273,9 +273,10 @@ func groupKind(ref plan.ResourceRef) schema.GroupKind { // MarshalJSON writes the result with summary, composites and compositions // ahead of the plans, which can be long, and with the deferred and health -// counts inside summary (the health ones under summary.health). YAML goes through it too. Decoding needs no -// counterpart: summary decodes into Result.Summary, Deferred is counted from -// Composites, and Health from Composites and Plans. +// counts inside summary (the health ones under summary.health). YAML goes +// through it too. Decoding needs no counterpart: summary decodes into +// Result.Summary, Deferred is counted from Composites, and Health from +// Composites and Plans. func (r Result) MarshalJSON() ([]byte, error) { type summary struct { plan.Summary From 601f3563e94abd65d50246471e2fc156938a7ade Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 15:10:24 -0500 Subject: [PATCH 08/30] test(project): expect healthy composites in project simulate JSON PR 1 reports each composite's live health, so the project simulate integration cases now see Health: healthy on every composite. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- cmd/crossplane/project/integration_project_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cmd/crossplane/project/integration_project_test.go b/cmd/crossplane/project/integration_project_test.go index e1a2d3e9..b1283f23 100644 --- a/cmd/crossplane/project/integration_project_test.go +++ b/cmd/crossplane/project/integration_project_test.go @@ -29,6 +29,7 @@ import ( "github.com/google/go-cmp/cmp" dp "github.com/crossplane/cli/v2/internal/diff/diffprocessor" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" "github.com/crossplane/cli/v2/internal/simulate" "github.com/crossplane/cli/v2/internal/testutils/integration" @@ -71,7 +72,7 @@ func TestProjectSimulateIntegration(t *testing.T) { changed := func(refs ...plan.ResourceRef) []simulate.CompositeResult { out := make([]simulate.CompositeResult, 0, len(refs)) for _, r := range refs { - out = append(out, simulate.CompositeResult{Ref: r, Status: simulate.CompositeChanged, Composition: &comp}) + out = append(out, simulate.CompositeResult{Ref: r, Status: simulate.CompositeChanged, Composition: &comp, Health: &health.Health{State: health.Healthy}}) } return out } From 2ef649840428c0f40822fe590743ed93df43e5ba Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 15:31:00 -0500 Subject: [PATCH 09/30] test(project): expect healthy composites in project plan output JSON Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- .../project/integration_plan_output_test.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cmd/crossplane/project/integration_plan_output_test.go b/cmd/crossplane/project/integration_plan_output_test.go index 1c0d1e4f..657d89a2 100644 --- a/cmd/crossplane/project/integration_plan_output_test.go +++ b/cmd/crossplane/project/integration_plan_output_test.go @@ -32,6 +32,7 @@ import ( dp "github.com/crossplane/cli/v2/internal/diff/diffprocessor" "github.com/crossplane/cli/v2/internal/exitcode" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" "github.com/crossplane/cli/v2/internal/simulate" "github.com/crossplane/cli/v2/internal/testutils/integration" @@ -73,6 +74,8 @@ func TestSimulatePlanOutput(t *testing.T) { another := xnop another.Name = "another-resource" comp := plan.ResourceRef{APIVersion: "apiextensions.crossplane.io/v1", Kind: "Composition", Name: "xnopresources.diff.example.org"} + // healthy is a live composite's health with no failing condition. + healthy := &health.Health{State: health.Healthy} xrArgs := []string{"-f", "../../../internal/testutils/integration/testdata/diff/modified-xr.yaml"} compArgs := []string{"-f", "../../../internal/testutils/integration/testdata/comp/updated-composition.yaml", "--namespace=default"} @@ -140,7 +143,7 @@ Note: Provider plan not run: no provider diff server planned these resources. args: append([]string{"-o", "json"}, xrArgs...), wantResult: &planShape{ Summary: plan.Summary{Update: 2}, - Composites: []simulate.CompositeResult{{Ref: xnop, Status: simulate.CompositeChanged}}, + Composites: []simulate.CompositeResult{{Ref: xnop, Status: simulate.CompositeChanged, Health: healthy}}, Plans: []planEntry{ {Kind: "XDownstreamResource", Name: "test-resource", Action: plan.ActionUpdate, Composite: "test-resource"}, {Kind: "XNopResource", Name: "test-resource", Action: plan.ActionUpdate, Composite: "test-resource"}, @@ -184,7 +187,7 @@ Note: Provider plan not run: no provider diff server planned these resources. args: append([]string{"-o", "json"}, compArgs...), wantResult: &planShape{ Summary: plan.Summary{Update: 3, NoOp: 2}, - Composites: []simulate.CompositeResult{{Ref: another, Status: simulate.CompositeChanged, Composition: &comp}, {Ref: xnop, Status: simulate.CompositeChanged, Composition: &comp}}, + Composites: []simulate.CompositeResult{{Ref: another, Status: simulate.CompositeChanged, Composition: &comp, Health: healthy}, {Ref: xnop, Status: simulate.CompositeChanged, Composition: &comp, Health: healthy}}, Compositions: []simulate.CompositionResult{{ Ref: comp, RevisionImpact: &simulate.RevisionImpact{ChangeScope: "spec", CreatesRevision: true, RepointedComposites: 2}, @@ -203,7 +206,7 @@ Note: Provider plan not run: no provider diff server planned these resources. args: []string{"-o", "json", "-n", "default", "--name=another-resource", "--name=test-resource"}, wantResult: &planShape{ Summary: plan.Summary{Update: 3, NoOp: 2}, - Composites: []simulate.CompositeResult{{Ref: another, Status: simulate.CompositeChanged, Composition: &comp}, {Ref: xnop, Status: simulate.CompositeChanged, Composition: &comp}}, + Composites: []simulate.CompositeResult{{Ref: another, Status: simulate.CompositeChanged, Composition: &comp, Health: healthy}, {Ref: xnop, Status: simulate.CompositeChanged, Composition: &comp, Health: healthy}}, Compositions: []simulate.CompositionResult{{ Ref: comp, RevisionImpact: &simulate.RevisionImpact{ChangeScope: "spec", CreatesRevision: true, RepointedComposites: 2}, @@ -222,7 +225,7 @@ Note: Provider plan not run: no provider diff server planned these resources. args: []string{"-o", "json", "--name=default/test-resource"}, wantResult: &planShape{ Summary: plan.Summary{Update: 2, NoOp: 1}, - Composites: []simulate.CompositeResult{{Ref: xnop, Status: simulate.CompositeChanged, Composition: &comp}}, + Composites: []simulate.CompositeResult{{Ref: xnop, Status: simulate.CompositeChanged, Composition: &comp, Health: healthy}}, Compositions: []simulate.CompositionResult{{ Ref: comp, RevisionImpact: &simulate.RevisionImpact{ChangeScope: "spec", CreatesRevision: true, RepointedComposites: 1}, From 4ce9bb1f2d18c001397af089c6758fe00599a1db Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 14:02:48 -0500 Subject: [PATCH 10/30] docs(simulate): use contractions in the output help Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/simulate/help/output.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/simulate/help/output.md b/internal/simulate/help/output.md index bcd9c413..f9bcabb6 100644 --- a/internal/simulate/help/output.md +++ b/internal/simulate/help/output.md @@ -22,12 +22,12 @@ The `-o` flag picks the output format: annotation, whatever its conditions), or `healthy`, with the `synced` and `ready` conditions it was read from. A claim's `health` is its backing composite resource's. A composite or plan has no `health` when there is no - live object, as for a create, or, for a composite, when it was not + live object, as for a create, or, for a composite, when it wasn't simulated (`error`, `skipped`, `deferred`). Each composite also lists under - `unhealthy` the composed resources that are not healthy and have no plan of + `unhealthy` the composed resources that aren't healthy and have no plan of their own, each once. That includes nested composites and what they compose; an entry a nested composite composes names it under `via`. `summary.health` - counts the composites that are not healthy (`unhealthyComposites`), and the + counts the composites that aren't healthy (`unhealthyComposites`), and the other resources that are `failing`, `notReady` or `paused`, each once. - `diff` prints a line diff of each changed resource's manifest. Every Secret value, and any value a provider marks sensitive, reads From a1a5d400354a6c9d303e40bd32ac0e5dd87a98f8 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 15:10:48 -0500 Subject: [PATCH 11/30] fix(simulate): list a nested composite's health on its own entry A composition run gives a nested composite of the diffed composition's type an entry of its own, now marked composedBy. Its parent's health also lists it and what it composes, so each of those appeared in two unhealthy lists. A nested composite with an entry and health of its own now shows its health there and lists what it composes itself; its parent lists neither. One whose entry has no health, because it wasn't simulated, stays listed under its parent and counts there as a resource. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/simulate/help/output.md | 10 +++++-- internal/simulate/report.go | 48 +++++++++++++++++++++++++++----- internal/simulate/report_test.go | 42 ++++++++++++++++++++++++++++ internal/simulate/result.go | 15 ++++++---- 4 files changed, 100 insertions(+), 15 deletions(-) diff --git a/internal/simulate/help/output.md b/internal/simulate/help/output.md index f9bcabb6..f1275ac9 100644 --- a/internal/simulate/help/output.md +++ b/internal/simulate/help/output.md @@ -26,9 +26,13 @@ The `-o` flag picks the output format: simulated (`error`, `skipped`, `deferred`). Each composite also lists under `unhealthy` the composed resources that aren't healthy and have no plan of their own, each once. That includes nested composites and what they compose; - an entry a nested composite composes names it under `via`. `summary.health` - counts the composites that aren't healthy (`unhealthyComposites`), and the - other resources that are `failing`, `notReady` or `paused`, each once. + an entry a nested composite composes names it under `via`. A nested + composite that has its own entry with `health` (one with `composedBy`) + shows its health there and lists what it composes, so its parent lists + neither. + `summary.health` counts the composites that aren't healthy + (`unhealthyComposites`), and the other resources that are `failing`, + `notReady` or `paused`, each once. - `diff` prints a line diff of each changed resource's manifest. Every Secret value, and any value a provider marks sensitive, reads `(sensitive value)`; a value the provider computes on apply reads diff --git a/internal/simulate/report.go b/internal/simulate/report.go index 2e978109..d742e935 100644 --- a/internal/simulate/report.go +++ b/internal/simulate/report.go @@ -254,20 +254,44 @@ func (a *assembler) addComposite(c CompositeResult, h dt.CompositeHealth) { // unhealthy sets each composite's Unhealthy: what it composed that is not // healthy and has no plan in the run, matched across API versions. A // resource with a plan shows its health there. +// +// A nested composite with an entry and health of its own, as a composition +// run gives one of a diffed composition's type, shows its health on that +// entry and lists what it composes itself; its parent lists neither. So a +// resource is listed under the most deeply nested composite entry that +// composes it, once. func (a *assembler) unhealthy() { - planned := map[schema.GroupKind]map[[2]string]bool{} + planned := map[objectID]bool{} for _, p := range a.plans { - gk := p.GroupVersionKind().GroupKind() - if planned[gk] == nil { - planned[gk] = map[[2]string]bool{} + planned[idOf(p.APIVersion, p.Kind, p.Namespace, p.Name)] = true + } + + composes := make([]map[objectID]bool, len(a.composed)) + entries := map[objectID][]int{} + for i, composed := range a.composed { + composes[i] = map[objectID]bool{} + for _, r := range composed { + composes[i][idOf(r.APIVersion, r.Kind, r.Namespace, r.Name)] = true + } + if c := a.composites[i]; c.Health != nil { + id := idOf(c.Ref.APIVersion, c.Ref.Kind, c.Ref.Namespace, c.Ref.Name) + entries[id] = append(entries[id], i) } - planned[gk][[2]string{p.Namespace, p.Name}] = true } for i, composed := range a.composed { + // What a nested composite entry lists, and the entry itself. + ownedElsewhere := map[objectID]bool{} + for id := range composes[i] { + for _, j := range entries[id] { + ownedElsewhere[id] = true + maps.Copy(ownedElsewhere, composes[j]) + } + } + for _, r := range composed { - gk := schema.FromAPIVersionAndKind(r.APIVersion, r.Kind).GroupKind() - if !r.Health.Unhealthy() || planned[gk][[2]string{r.Namespace, r.Name}] { + id := idOf(r.APIVersion, r.Kind, r.Namespace, r.Name) + if !r.Health.Unhealthy() || planned[id] || ownedElsewhere[id] { continue } u := UnhealthyResource{Ref: resourceRef(r.ObjectReference), Health: r.Health} @@ -280,6 +304,16 @@ func (a *assembler) unhealthy() { } } +// objectID is an object's identity across API versions. +type objectID struct { + gk schema.GroupKind + namespace, name string +} + +func idOf(apiVersion, kind, namespace, name string) objectID { + return objectID{gk: schema.FromAPIVersionAndKind(apiVersion, kind).GroupKind(), namespace: namespace, name: name} +} + // addDiffs adds a plan per diff, in the structured renderer's canonical order. // It reports whether a provider plan changed any of them, which the engine's // verdict alone does not show. diff --git a/internal/simulate/report_test.go b/internal/simulate/report_test.go index 257acf50..0bc24cec 100644 --- a/internal/simulate/report_test.go +++ b/internal/simulate/report_test.go @@ -731,6 +731,48 @@ func TestHealth(t *testing.T) { summary: HealthSummary{Failing: 1, NotReady: 1}, }, }, + "NestedEntryListsItsOwn": { + reason: "A nested composite with an impact and health of its own shows its health there and lists what it composes; its parent lists neither, and the summary counts each once.", + args: args{comp: &dt.CompDiffOutput{Compositions: []dt.CompositionDiff{{Name: "a", ImpactAnalysis: []dt.XRImpact{ + {ObjectReference: xrRef, Status: dt.XRStatusUnchanged, Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{ + {ObjectReference: child, Health: notReady}, + {ObjectReference: cmRef("deep"), Via: &child, Health: failing}, + {ObjectReference: cmRef("cm"), Health: failing}, + }}}, + {ObjectReference: child, ComposedBy: &xrRef, Status: dt.XRStatusUnchanged, Health: dt.CompositeHealth{XR: notReady, Composed: []dt.ResourceHealth{ + {ObjectReference: cmRef("deep"), Health: failing}, + }}}, + }}}}}, + want: want{ + plans: map[string]*health.Health{"a": nil}, + composites: []CompositeResult{ + {Ref: xrPlan, Composition: compRef("a"), Status: CompositeUnchanged, Health: healthy, Unhealthy: []UnhealthyResource{{Ref: cmPlan("cm"), Health: failing}}}, + {Ref: childPlan, Composition: compRef("a"), ComposedBy: &xrPlan, Status: CompositeUnchanged, Health: notReady, Unhealthy: []UnhealthyResource{{Ref: cmPlan("deep"), Health: failing}}}, + }, + summary: HealthSummary{Composites: 1, Failing: 2}, + }, + }, + "NestedEntryWithoutHealth": { + reason: "A nested composite whose own impact has no health, as when it errored, is listed under its parent with what it composes, and counted there as a resource.", + args: args{comp: &dt.CompDiffOutput{Compositions: []dt.CompositionDiff{{Name: "a", ImpactAnalysis: []dt.XRImpact{ + {ObjectReference: xrRef, Status: dt.XRStatusUnchanged, Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{ + {ObjectReference: child, Health: notReady}, + {ObjectReference: cmRef("deep"), Via: &child, Health: failing}, + }}}, + {ObjectReference: child, ComposedBy: &xrRef, Status: dt.XRStatusError}, + }}}}}, + want: want{ + plans: map[string]*health.Health{"a": nil}, + composites: []CompositeResult{ + {Ref: xrPlan, Composition: compRef("a"), Status: CompositeUnchanged, Health: healthy, Unhealthy: []UnhealthyResource{ + {Ref: childPlan, Health: notReady}, + {Ref: cmPlan("deep"), Via: &childPlan, Health: failing}, + }}, + {Ref: childPlan, Composition: compRef("a"), ComposedBy: &xrPlan, Status: CompositeError}, + }, + summary: HealthSummary{Failing: 1, NotReady: 1}, + }, + }, "CompImpactsCountedOnce": { reason: "A composition run carries each impact's health; an XR two compositions reach is one composite, so it counts once, and so does what it composes.", args: args{comp: &dt.CompDiffOutput{Compositions: []dt.CompositionDiff{ diff --git a/internal/simulate/result.go b/internal/simulate/result.go index adf0483a..8ae6f792 100644 --- a/internal/simulate/result.go +++ b/internal/simulate/result.go @@ -93,7 +93,9 @@ type CompositeResult struct { Health *health.Health `json:"health,omitempty"` // Unhealthy lists the composed resources, nested composites and theirs // included, that are live, not healthy, and have no plan in the run (so - // no health of their own to show). + // no health of their own to show). A nested composite with an entry and + // health of its own lists what it composes there instead, and its + // parent lists neither it nor that. Unhealthy []UnhealthyResource `json:"unhealthy,omitempty"` } @@ -107,9 +109,10 @@ type UnhealthyResource struct { } // HealthSummary counts what is not healthy before apply. Composites counts -// the composites whose own health is not healthy; Failing, NotReady and -// Paused count the other resources. Each is counted once however many -// composites or plans reach it. +// the composite entries whose own health is not healthy; Failing, NotReady +// and Paused count the other resources, a nested composite without health of +// its own on an entry included. Each is counted once however many composites +// or plans reach it. type HealthSummary struct { Composites int `json:"unhealthyComposites"` Failing int `json:"failing"` @@ -220,7 +223,9 @@ func (r Result) Health() HealthSummary { } for _, c := range r.Composites { gk, id := groupKind(c.Ref), [2]string{c.Ref.Namespace, c.Ref.Name} - if composites[gk][id] { + // A composite with no health of its own here (one not simulated) is + // counted as a resource where its parent lists it, if anywhere. + if c.Health == nil || composites[gk][id] { continue } if composites[gk] == nil { From 50e8fad37a3bd829324d4fdf97e969aed30ea7d1 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 14:01:55 -0500 Subject: [PATCH 12/30] feat(simulate): mark unhealthy resources in plan output An unhealthy XR's header and a plan whose live resource is not healthy now carry a one-line health description: the tier, the condition it was read from with its reason, and its message cut to 120 runes. Under each XR an "Unhealthy resources without changes:" block lists the composed resources that are unhealthy and have no plan, nesting each under the nested XR that composes it. A Health: line after Plan: summarizes the counts. Healthy runs print what they did before. --full-messages prints the messages in full, wrapped and indented, and is ignored, with a warning, for -o json and -o yaml. The formatting lives in internal/health so the diff renderer can share it. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/health/format.go | 368 +++++++++++++++++++++++++++++++ internal/health/format_test.go | 331 +++++++++++++++++++++++++++ internal/plan/output.go | 21 +- internal/simulate/flags.go | 1 + internal/simulate/result.go | 178 +++++++++------ internal/simulate/result_test.go | 237 +++++++++++++++++++- internal/simulate/run.go | 14 +- internal/simulate/run_test.go | 25 ++- 8 files changed, 1093 insertions(+), 82 deletions(-) create mode 100644 internal/health/format.go create mode 100644 internal/health/format_test.go diff --git a/internal/health/format.go b/internal/health/format.go new file mode 100644 index 00000000..0b3ae866 --- /dev/null +++ b/internal/health/format.go @@ -0,0 +1,368 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package health + +import ( + "fmt" + "strings" + "unicode/utf8" + + "k8s.io/apimachinery/pkg/runtime/schema" +) + +// Marker flags an object that is not healthy in text output. The diff +// renderer's text output already uses non-ASCII status glyphs, and the marker +// is not a colour, so it stays with colours off. +const Marker = "⚠" + +// BlockHeading heads the block listing a composite's resources that are not +// healthy and have no changes. +const BlockHeading = "Unhealthy resources without changes:" + +const ( + // messageRunes is how many runes of a condition message a one-line + // description keeps, its ellipsis included. + messageRunes = 120 + // wrapRunes is the width a full message is wrapped to. + wrapRunes = 100 +) + +// Lines describes h for text output, or returns nil when h is nil or healthy. +// The first line is the marker, the tier, the condition it was read from with +// that condition's reason, and its message cut to one line, e.g. +// "⚠ failing (Synced=False, ReconcileError): cannot update". With full, the +// message instead follows in full, wrapped, one element per line. A paused +// object's conditions are stale, so it is described by its tier alone. +func Lines(h *Health, full bool) []string { + if !h.Unhealthy() { + return nil + } + + var ( + line string + c *Condition + ) + + switch h.State { + case Failing: + line, c = Marker+" failing (Synced=False", h.Synced + case NotReady: + line, c = Marker+" not ready (Ready=False", h.Ready + case Paused, Healthy: + return []string{Marker + " paused"} + } + + if c == nil { + return []string{line + ")"} + } + + if c.Reason != "" { + line += ", " + c.Reason + } + + line += ")" + + switch { + case strings.TrimSpace(c.Message) == "": + return []string{line} + case full: + return append([]string{line}, wrap(c.Message)...) + default: + return []string{line + ": " + truncate(c.Message)} + } +} + +// Item is a resource a block of unhealthy resources lists. +type Item struct { + // ID identifies the resource, to match another item's Via against. + ID string + // Name is how the block prints the resource. + Name string + // Via is the ID of the nested composite that composes the resource, and + // ViaName how the block prints it. Both are empty when the block's own + // composite composes it. + Via, ViaName string + + Health *Health +} + +// Block returns the lines of the block listing items, a composite's resources +// that are not healthy and have no changes, or nil when there are none. The +// heading is unindented and each item is indented two spaces per level below +// it, with any wrapped message four more. +// +// An item a nested composite composes is nested under that composite: under +// its own item, or, for a composite listed elsewhere with changes, under a +// line naming it. listed maps the IDs of the resources listed elsewhere to that +// line. An item whose nested composite is neither names it, as "(via ...)". +func Block(items []Item, listed map[string]string, full bool) []string { + if len(items) == 0 { + return nil + } + + ids := make(map[string]bool, len(items)) + for _, it := range items { + ids[it.ID] = true + } + + nested := func(it Item) bool { + return it.Via != "" && (ids[it.Via] || listed[it.Via] != "") + } + + children := map[string][]Item{} + + for _, it := range items { + if nested(it) { + children[it.Via] = append(children[it.Via], it) + } + } + + lines := []string{BlockHeading} + done := map[string]bool{} + + var add func(it Item, depth int) + + add = func(it Item, depth int) { + if done[it.ID] { + return + } + + done[it.ID] = true + indent := strings.Repeat(" ", depth+1) + + name := it.Name + if it.Via != "" && !nested(it) { + name += " (via " + it.ViaName + ")" + } + + desc := Lines(it.Health, full) + if len(desc) == 0 { + lines = append(lines, indent+name) + } else { + lines = append(lines, indent+name+" "+desc[0]) + for _, l := range desc[1:] { + lines = append(lines, indent+" "+l) + } + } + + for _, c := range children[it.ID] { + add(c, depth+1) + } + } + + shown := map[string]bool{} + + for _, it := range items { + switch { + case !nested(it): + add(it, 0) + case !ids[it.Via] && !shown[it.Via]: + // A composite listed elsewhere heads its resources here too. + shown[it.Via] = true + lines = append(lines, " "+listed[it.Via]) + + for _, c := range children[it.Via] { + add(c, 1) + } + } + } + + // Items whose nesting loops back on itself have no root; list them + // rather than lose them. + for _, it := range items { + add(it, 0) + } + + return lines +} + +// truncate returns msg on one line, whitespace collapsed to single spaces, +// cut to messageRunes with an ellipsis. +func truncate(msg string) string { + msg = strings.Join(strings.Fields(msg), " ") + if utf8.RuneCountInString(msg) <= messageRunes { + return msg + } + + return string([]rune(msg)[:messageRunes-1]) + "…" +} + +// wrap returns msg's lines, blank ones dropped, each wrapped at word +// boundaries to wrapRunes. A word longer than that has a line to itself. +func wrap(msg string) []string { + var out []string + + for line := range strings.SplitSeq(msg, "\n") { + cur := "" + + for w := range strings.FieldsSeq(line) { + if cur != "" && utf8.RuneCountInString(cur)+1+utf8.RuneCountInString(w) > wrapRunes { + out = append(out, cur) + cur = "" + } + + if cur != "" { + cur += " " + } + + cur += w + } + + if cur != "" { + out = append(out, cur) + } + } + + return out +} + +// Key identifies a resource across API versions, for matching an Item's Via +// against the resource it names. +func Key(apiVersion, kind, namespace, name string) string { + gk := schema.FromAPIVersionAndKind(apiVersion, kind).GroupKind() + return gk.String() + "/" + namespace + "/" + name +} + +// Tally counts a run's health: the composites it lists, and the other +// resources by state, each once. Resources are matched by Key, and the first +// health a resource is added with wins. A composite added as a resource is +// counted only as a composite, whichever was added first. +type Tally struct { + composites map[string]bool + resources map[string]State + order []string +} + +// Composite adds a composite with key and health h. +func (t *Tally) Composite(key string, h *Health) { + if t.composites == nil { + t.composites = map[string]bool{} + } + + if _, ok := t.composites[key]; !ok { + t.composites[key] = h.Unhealthy() + } +} + +// Resource adds a resource with key and health h. A resource with no health +// is not counted. +func (t *Tally) Resource(key string, h *Health) { + if h == nil { + return + } + + if t.resources == nil { + t.resources = map[string]State{} + } + + if _, ok := t.resources[key]; !ok { + t.resources[key] = h.State + t.order = append(t.order, key) + } +} + +// Counts returns what t counted. +func (t *Tally) Counts() Counts { + c := Counts{Composites: len(t.composites)} + + for _, unhealthy := range t.composites { + if unhealthy { + c.UnhealthyComposites++ + } + } + + for _, key := range t.order { + if _, ok := t.composites[key]; ok { + continue + } + + switch t.resources[key] { + case Failing: + c.Failing++ + case NotReady: + c.NotReady++ + case Paused: + c.Paused++ + case Healthy: + } + } + + return c +} + +// Counts are a run's health counts, for its summary line. +type Counts struct { + // Composites is how many composites the run listed, and + // UnhealthyComposites how many of those are not healthy. + Composites, UnhealthyComposites int + // Failing, NotReady and Paused count the other resources by state. + Failing, NotReady, Paused int +} + +// SummaryLine returns the line summarizing c, e.g. "Health: 1 of 2 XRs +// unhealthy before apply; 12 resources failing (Synced=False), 2 not ready, +// 1 paused.", with only the non-zero parts, or "" when nothing is unhealthy. +func SummaryLine(c Counts) string { + var parts []string + + if c.UnhealthyComposites > 0 { + noun := "XRs" + if c.Composites == 1 { + noun = "XR" + } + + parts = append(parts, fmt.Sprintf("%d of %d %s unhealthy before apply", c.UnhealthyComposites, c.Composites, noun)) + } + + var resources []string + + for _, n := range []struct { + count int + what string + }{ + {c.Failing, "failing (Synced=False)"}, + {c.NotReady, "not ready"}, + {c.Paused, "paused"}, + } { + if n.count == 0 { + continue + } + + if len(resources) == 0 { + noun := "resources" + if n.count == 1 { + noun = "resource" + } + + resources = append(resources, fmt.Sprintf("%d %s %s", n.count, noun, n.what)) + + continue + } + + resources = append(resources, fmt.Sprintf("%d %s", n.count, n.what)) + } + + if len(resources) > 0 { + parts = append(parts, strings.Join(resources, ", ")) + } + + if len(parts) == 0 { + return "" + } + + return "Health: " + strings.Join(parts, "; ") + "." +} diff --git a/internal/health/format_test.go b/internal/health/format_test.go new file mode 100644 index 00000000..1b3764af --- /dev/null +++ b/internal/health/format_test.go @@ -0,0 +1,331 @@ +/* +Copyright 2026 The Crossplane Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package health + +import ( + "strings" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestLines(t *testing.T) { + long := strings.Repeat("word ", 40) // 200 runes + failing := func(msg string) *Health { + return &Health{State: Failing, Synced: &Condition{Status: "False", Reason: "ReconcileError", Message: msg}} + } + + type args struct { + h *Health + full bool + } + cases := map[string]struct { + reason string + args args + want []string + }{ + "Nil": { + reason: "No live object prints nothing.", + }, + "Healthy": { + reason: "A healthy object prints nothing, so healthy output is unchanged.", + args: args{h: &Health{State: Healthy}}, + }, + "Failing": { + reason: "A failing object names the tier, the Synced condition's reason and its message.", + args: args{h: failing("cannot update: denied")}, + want: []string{"⚠ failing (Synced=False, ReconcileError): cannot update: denied"}, + }, + "NotReady": { + reason: "A not-ready object is described by its Ready condition.", + args: args{h: &Health{State: NotReady, Ready: &Condition{Status: "False", Reason: "Creating"}}}, + want: []string{"⚠ not ready (Ready=False, Creating)"}, + }, + "Paused": { + reason: "A paused object's conditions are stale, so it is described by its tier alone.", + args: args{h: &Health{State: Paused, Synced: &Condition{Status: "False", Reason: "ReconcileError", Message: "x"}}}, + want: []string{"⚠ paused"}, + }, + "NoReason": { + reason: "A condition without a reason prints none.", + args: args{h: &Health{State: Failing, Synced: &Condition{Status: "False", Message: "boom"}}}, + want: []string{"⚠ failing (Synced=False): boom"}, + }, + "Collapsed": { + reason: "A multi-line message prints on one line, whitespace collapsed.", + args: args{h: failing("first line\n second\tline\n")}, + want: []string{"⚠ failing (Synced=False, ReconcileError): first line second line"}, + }, + "Truncated": { + reason: "A message over 120 runes is cut to 120, its ellipsis included.", + args: args{h: failing(long)}, + want: []string{"⚠ failing (Synced=False, ReconcileError): " + strings.Repeat("word ", 23) + "word…"}, + }, + "TruncatedRunes": { + reason: "Truncation counts runes, not bytes, so a multi-byte message isn't split mid-rune.", + args: args{h: failing(strings.Repeat("é", 130))}, + want: []string{"⚠ failing (Synced=False, ReconcileError): " + strings.Repeat("é", 119) + "…"}, + }, + "Full": { + reason: "With full, the message follows the first line in full, wrapped at word boundaries, its own line breaks kept.", + args: args{h: failing(long + "\nsecond"), full: true}, + want: []string{ + "⚠ failing (Synced=False, ReconcileError)", + strings.TrimSpace(strings.Repeat("word ", 20)), + strings.TrimSpace(strings.Repeat("word ", 20)), + "second", + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + got := Lines(tc.args.h, tc.args.full) + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("\n%s\nLines(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestBlock(t *testing.T) { + failing := &Health{State: Failing, Synced: &Condition{Status: "False", Reason: "ReconcileError", Message: "boom"}} + notReady := &Health{State: NotReady, Ready: &Condition{Status: "False", Reason: "Creating"}} + + type args struct { + items []Item + listed map[string]string + } + cases := map[string]struct { + reason string + args args + want []string + }{ + "None": { + reason: "No unhealthy resources print no block.", + }, + "Direct": { + reason: "Each resource prints on one line under the heading, in order.", + args: args{items: []Item{ + {ID: "a", Name: "RouteTable.ec2 rt", Health: failing}, + {ID: "b", Name: "Subnet.ec2 sn", Health: notReady}, + }}, + want: []string{ + "Unhealthy resources without changes:", + " RouteTable.ec2 rt ⚠ failing (Synced=False, ReconcileError): boom", + " Subnet.ec2 sn ⚠ not ready (Ready=False, Creating)", + }, + }, + "NestedUnderListedComposite": { + reason: "A resource a nested composite composes is indented under that composite when the block lists it too.", + args: args{items: []Item{ + {ID: "rt", Name: "RouteTable.ec2 rt", Via: "x", ViaName: "XSubnet.example.org x", Health: failing}, + {ID: "x", Name: "XSubnet.example.org x", Health: notReady}, + }}, + want: []string{ + "Unhealthy resources without changes:", + " XSubnet.example.org x ⚠ not ready (Ready=False, Creating)", + " RouteTable.ec2 rt ⚠ failing (Synced=False, ReconcileError): boom", + }, + }, + "NestedUnderPlannedComposite": { + reason: "A resource whose nested composite has a plan is indented under a line naming that composite.", + args: args{ + items: []Item{{ID: "rt", Name: "RouteTable.ec2 rt", Via: "x", ViaName: "XSubnet.example.org x", Health: failing}}, + listed: map[string]string{"x": "XSubnet.example.org x"}, + }, + want: []string{ + "Unhealthy resources without changes:", + " XSubnet.example.org x", + " RouteTable.ec2 rt ⚠ failing (Synced=False, ReconcileError): boom", + }, + }, + "ViaNotListed": { + reason: "A resource whose nested composite is not listed names it on its line.", + args: args{items: []Item{ + {ID: "rt", Name: "RouteTable.ec2 rt", Via: "x", ViaName: "XSubnet.example.org x", Health: failing}, + }}, + want: []string{ + "Unhealthy resources without changes:", + " RouteTable.ec2 rt (via XSubnet.example.org x) ⚠ failing (Synced=False, ReconcileError): boom", + }, + }, + "DeeplyNested": { + reason: "Nesting follows each via, however deep.", + args: args{items: []Item{ + {ID: "y", Name: "XInner y", Via: "x", ViaName: "XOuter x", Health: notReady}, + {ID: "rt", Name: "RouteTable rt", Via: "y", ViaName: "XInner y", Health: failing}, + {ID: "x", Name: "XOuter x", Health: notReady}, + }}, + want: []string{ + "Unhealthy resources without changes:", + " XOuter x ⚠ not ready (Ready=False, Creating)", + " XInner y ⚠ not ready (Ready=False, Creating)", + " RouteTable rt ⚠ failing (Synced=False, ReconcileError): boom", + }, + }, + "Cycle": { + reason: "Resources whose vias loop back on each other are still listed, once each.", + args: args{items: []Item{ + {ID: "a", Name: "A", Via: "b", ViaName: "B", Health: failing}, + {ID: "b", Name: "B", Via: "a", ViaName: "A", Health: notReady}, + }}, + want: []string{ + "Unhealthy resources without changes:", + " A ⚠ failing (Synced=False, ReconcileError): boom", + " B ⚠ not ready (Ready=False, Creating)", + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + got := Block(tc.args.items, tc.args.listed, false) + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("\n%s\nBlock(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestBlockFullMessages(t *testing.T) { + type args struct { + items []Item + } + cases := map[string]struct { + reason string + args args + want []string + }{ + "Wrapped": { + reason: "With full messages, a message prints in full beneath its resource, indented four more.", + args: args{items: []Item{{ID: "rt", Name: "RouteTable rt", Health: &Health{ + State: Failing, Synced: &Condition{Status: "False", Reason: "ReconcileError", Message: "line one\nline two"}, + }}}}, + want: []string{ + "Unhealthy resources without changes:", + " RouteTable rt ⚠ failing (Synced=False, ReconcileError)", + " line one", + " line two", + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + got := Block(tc.args.items, nil, true) + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("\n%s\nBlock(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestTally(t *testing.T) { + failing := &Health{State: Failing} + notReady := &Health{State: NotReady} + + type args struct { + add func(*Tally) + } + cases := map[string]struct { + reason string + args args + want Counts + }{ + "Empty": { + reason: "Nothing added counts nothing.", + args: args{add: func(*Tally) {}}, + }, + "EachOnce": { + reason: "A resource added twice counts once, under the health it was first added with.", + args: args{add: func(t *Tally) { + t.Resource("a", failing) + t.Resource("a", notReady) + t.Resource("b", &Health{State: Paused}) + t.Resource("c", nil) + }}, + want: Counts{Failing: 1, Paused: 1}, + }, + "CompositesCountedAsComposites": { + reason: "Composites count once each, the unhealthy ones apart, and not again as resources, whichever is added first.", + args: args{add: func(t *Tally) { + t.Resource("x", failing) + t.Composite("x", failing) + t.Composite("y", nil) + t.Composite("y", failing) + t.Resource("y", failing) + t.Resource("r", notReady) + }}, + want: Counts{Composites: 2, UnhealthyComposites: 1, NotReady: 1}, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + var tl Tally + tc.args.add(&tl) + if diff := cmp.Diff(tc.want, tl.Counts()); diff != "" { + t.Errorf("\n%s\nCounts(): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestSummaryLine(t *testing.T) { + type args struct { + c Counts + } + cases := map[string]struct { + reason string + args args + want string + }{ + "AllHealthy": { + reason: "With nothing unhealthy there is no line.", + args: args{c: Counts{Composites: 2}}, + }, + "Everything": { + reason: "Every non-zero part prints, in the documented wording.", + args: args{c: Counts{Composites: 2, UnhealthyComposites: 1, Failing: 12, NotReady: 2, Paused: 1}}, + want: "Health: 1 of 2 XRs unhealthy before apply; 12 resources failing (Synced=False), 2 not ready, 1 paused.", + }, + "OnlyResources": { + reason: "Healthy composites are left out, and one resource is singular.", + args: args{c: Counts{Composites: 1, NotReady: 1}}, + want: "Health: 1 resource not ready.", + }, + "OnlyComposite": { + reason: "One composite is singular, and zero resource counts are left out.", + args: args{c: Counts{Composites: 1, UnhealthyComposites: 1}}, + want: "Health: 1 of 1 XR unhealthy before apply.", + }, + "PausedOnly": { + reason: "The first non-zero resource count carries the noun.", + args: args{c: Counts{Paused: 3}}, + want: "Health: 3 resources paused.", + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + if diff := cmp.Diff(tc.want, SummaryLine(tc.args.c)); diff != "" { + t.Errorf("\n%s\nSummaryLine(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} diff --git a/internal/plan/output.go b/internal/plan/output.go index b037465a..ab81b0e8 100644 --- a/internal/plan/output.go +++ b/internal/plan/output.go @@ -28,6 +28,8 @@ import ( "sigs.k8s.io/yaml" "github.com/crossplane/crossplane-runtime/v2/pkg/errors" + + "github.com/crossplane/cli/v2/internal/health" ) // The output formats simulate commands share. A unified diff format @@ -161,7 +163,7 @@ type planRenderer struct{} // Render writes the plans to w in the human-readable plan format. func (planRenderer) Render(w io.Writer, plans []*ResourcePlan) error { for _, p := range plans { - RenderPlan(w, p, "") + RenderPlan(w, p, "", false) } s := Summarize(plans) RenderSummaryLine(w, s) @@ -169,11 +171,20 @@ func (planRenderer) Render(w io.Writer, plans []*ResourcePlan) error { return nil } -// RenderPlan prints one resource's header, note when not empty, and change -// tree. A delete prints only its header and note, Terraform-style, though it -// carries actual-only changes. -func RenderPlan(w io.Writer, p *ResourcePlan, note string) { +// RenderPlan prints one resource's header, its health when it is not +// healthy, note when not empty, and change tree. A delete prints only its +// header, health and note, Terraform-style, though it carries actual-only +// changes. With fullMessages a health condition message prints in full, +// wrapped, rather than cut to one line. +func RenderPlan(w io.Writer, p *ResourcePlan, note string, fullMessages bool) { _, _ = fmt.Fprintln(w, header(p)) + for i, l := range health.Lines(p.Health, fullMessages) { + indent := " " + if i > 0 { + indent += " " + } + _, _ = fmt.Fprintln(w, indent+l) + } if note != "" { _, _ = fmt.Fprintf(w, " %s\n", note) } diff --git a/internal/simulate/flags.go b/internal/simulate/flags.go index 1e727fa8..9031b67c 100644 --- a/internal/simulate/flags.go +++ b/internal/simulate/flags.go @@ -45,6 +45,7 @@ type Flags struct { NoColor bool `help:"Disable colorized output."` Compact bool `help:"Show compact diffs with minimal context."` + FullMessages bool `help:"Print health condition messages in full in plan and diff output, rather than cut to one line."` MaxNestedDepth int `default:"10" help:"Maximum depth for nested XR recursion."` IgnorePaths []string `help:"Paths to ignore in diffs (e.g., 'metadata.annotations[argocd.argoproj.io/tracking-id]')."` FunctionCredentials string `help:"A YAML file or directory of YAML files specifying Secret credentials to pass to Functions." placeholder:"PATH" predictor:"yaml_file_or_directory" type:"path"` diff --git a/internal/simulate/result.go b/internal/simulate/result.go index 8ae6f792..1e219010 100644 --- a/internal/simulate/result.go +++ b/internal/simulate/result.go @@ -216,64 +216,54 @@ func (r Result) Deferred() int { // state, from Composites and Plans. A resource counts once, under the state // its first live copy reports, matched across API versions. func (r Result) Health() HealthSummary { - var s HealthSummary - composites := map[schema.GroupKind]map[[2]string]bool{} - isComposite := func(ref plan.ResourceRef) bool { - return composites[groupKind(ref)][[2]string{ref.Namespace, ref.Name}] - } - for _, c := range r.Composites { - gk, id := groupKind(c.Ref), [2]string{c.Ref.Namespace, c.Ref.Name} - // A composite with no health of its own here (one not simulated) is - // counted as a resource where its parent lists it, if anywhere. - if c.Health == nil || composites[gk][id] { - continue - } - if composites[gk] == nil { - composites[gk] = map[[2]string]bool{} - } - composites[gk][id] = true - if c.Health.Unhealthy() { - s.Composites++ - } - } + c := r.healthTally().Counts() + return HealthSummary{Composites: c.UnhealthyComposites, Failing: c.Failing, NotReady: c.NotReady, Paused: c.Paused} +} - seen := map[schema.GroupKind]map[[2]string]bool{} - count := func(ref plan.ResourceRef, h *health.Health) { - if h == nil || isComposite(ref) { - return - } - gk, id := groupKind(ref), [2]string{ref.Namespace, ref.Name} - if seen[gk][id] { - return - } - if seen[gk] == nil { - seen[gk] = map[[2]string]bool{} - } - seen[gk][id] = true - switch h.State { - case health.Failing: - s.Failing++ - case health.NotReady: - s.NotReady++ - case health.Paused: - s.Paused++ - case health.Healthy: +// healthTally adds r's composites and resources to a health.Tally. A +// composite with no health of its own here (one not simulated) isn't added as +// a composite, so it counts as a resource where its parent lists it, if +// anywhere. +func (r Result) healthTally() *health.Tally { + t := &health.Tally{} + for _, c := range r.Composites { + if c.Health != nil { + t.Composite(healthKey(c.Ref), c.Health) } } for _, p := range r.Plans { - count(p.ResourceRef(), p.Health) + t.Resource(healthKey(p.ResourceRef()), p.Health) } for _, c := range r.Composites { for _, u := range c.Unhealthy { - count(u.Ref, u.Health) + t.Resource(healthKey(u.Ref), u.Health) } } - return s + return t +} + +// Failing reports whether a composite or any resource in r is failing, which +// sets exit bit 4. +func (r Result) Failing() bool { + failing := func(h *health.Health) bool { return h != nil && h.State == health.Failing } + for _, c := range r.Composites { + if failing(c.Health) || slices.ContainsFunc(c.Unhealthy, func(u UnhealthyResource) bool { return failing(u.Health) }) { + return true + } + } + return slices.ContainsFunc(r.Plans, func(p *plan.ResourcePlan) bool { return failing(p.Health) }) +} + +// HealthLine returns the Health: line summarizing r, or "" when nothing in +// it is unhealthy. Its XRs are the composites with health of their own, each +// once. +func (r Result) HealthLine() string { + return health.SummaryLine(r.healthTally().Counts()) } -// groupKind is ref's version-independent type. -func groupKind(ref plan.ResourceRef) schema.GroupKind { - return schema.FromAPIVersionAndKind(ref.APIVersion, ref.Kind).GroupKind() +// healthKey identifies ref across API versions, as health.Key does. +func healthKey(ref plan.ResourceRef) string { + return health.Key(ref.APIVersion, ref.Kind, ref.Namespace, ref.Name) } // MarshalJSON writes the result with summary, composites and compositions @@ -319,10 +309,13 @@ type Renderer interface { // NewRenderer returns the Renderer for the given output format: one of // plan.FormatPlan, plan.FormatJSON or plan.FormatYAML. -func NewRenderer(format string) (Renderer, error) { +// With fullMessages the plan format prints health condition messages in full, +// wrapped, rather than cut to one line; the structured formats always carry +// them in full. +func NewRenderer(format string, fullMessages bool) (Renderer, error) { switch format { case plan.FormatPlan: - return planRenderer{}, nil + return planRenderer{fullMessages: fullMessages}, nil case plan.FormatJSON: return jsonRenderer{}, nil case plan.FormatYAML: @@ -361,21 +354,28 @@ func (yamlRenderer) Render(w io.Writer, r Result) error { // tree per resource, then a summary with warnings, then any Notes. When any // plan or composite names a composite, plans print grouped under an // "XR: Kind.group name" header. A Composition's revision impact prints as -// one line under its plan. Run-level Errors and Warnings are not printed; -// the command reports them on stderr. -type planRenderer struct{} +// one line under its plan. What is not healthy before apply is marked where +// it is listed, and summarized after the "Plan:" line. Run-level Errors and +// Warnings are not printed; the command reports them on stderr. +type planRenderer struct { + // fullMessages prints health condition messages in full. + fullMessages bool +} // Render writes the result to w in the human-readable plan format. -func (planRenderer) Render(w io.Writer, r Result) error { +func (pr planRenderer) Render(w io.Writer, r Result) error { impacts := revisionImpactLines(r.Compositions) if len(r.Composites) == 0 && !slices.ContainsFunc(r.Plans, func(p *plan.ResourcePlan) bool { return p.Composite != nil }) { for _, p := range r.Plans { - plan.RenderPlan(w, p, impacts[p.ResourceRef()]) + plan.RenderPlan(w, p, impacts[p.ResourceRef()], pr.fullMessages) } } else { - renderGrouped(w, r, impacts) + pr.renderGrouped(w, r, impacts) } plan.RenderSummaryLine(w, r.Summary) + if line := r.HealthLine(); line != "" { + _, _ = fmt.Fprintln(w, line) + } if d := r.Deferred(); d > 0 { _, _ = fmt.Fprintf(w, "%s deferred (pinned to a composition revision).\n", pluralize(d, "composite resource", "composite resources")) } @@ -417,7 +417,7 @@ func revisionImpactLines(comps []CompositionResult) map[plan.ResourceRef]string // renderGrouped prints ungrouped plans first, then each composite (in // Composites order, then any only plans name) with its plans indented // beneath. A composite's own plan is omitted when it is a no-op. -func renderGrouped(w io.Writer, r Result, impacts map[plan.ResourceRef]string) { +func (pr planRenderer) renderGrouped(w io.Writer, r Result, impacts map[plan.ResourceRef]string) { var refs []plan.ResourceRef byRef := map[plan.ResourceRef][]*plan.ResourcePlan{} status := map[plan.ResourceRef]CompositeResult{} @@ -429,7 +429,7 @@ func renderGrouped(w io.Writer, r Result, impacts map[plan.ResourceRef]string) { } for _, p := range r.Plans { if p.Composite == nil { - plan.RenderPlan(w, p, impacts[p.ResourceRef()]) + plan.RenderPlan(w, p, impacts[p.ResourceRef()], pr.fullMessages) continue } ref := *p.Composite @@ -442,14 +442,23 @@ func renderGrouped(w io.Writer, r Result, impacts map[plan.ResourceRef]string) { } for _, ref := range refs { - renderComposite(w, status[ref], ref, byRef[ref]) + pr.renderComposite(w, status[ref], ref, byRef[ref]) } } // renderComposite prints one composite's header, its deferral or skip note, -// and its plans indented beneath. -func renderComposite(w io.Writer, c CompositeResult, ref plan.ResourceRef, plans []*plan.ResourcePlan) { - _, _ = fmt.Fprintln(w, compositeHeader(c, ref)) +// and its plans indented beneath, then the resources it composes that are not +// healthy and have no plan. +func (pr planRenderer) renderComposite(w io.Writer, c CompositeResult, ref plan.ResourceRef, plans []*plan.ResourcePlan) { + h := compositeHeader(c, ref) + desc := health.Lines(c.Health, pr.fullMessages) + if len(desc) > 0 { + h += " " + desc[0] + } + _, _ = fmt.Fprintln(w, h) + for _, l := range desc[min(1, len(desc)):] { + _, _ = fmt.Fprintf(w, " %s\n", l) + } if c.ComposedBy != nil { gvk := schema.FromAPIVersionAndKind(c.ComposedBy.APIVersion, c.ComposedBy.Kind) name := c.ComposedBy.Name @@ -477,7 +486,17 @@ func renderComposite(w io.Writer, c CompositeResult, ref plan.ResourceRef, plans if p.Action == plan.ActionNoOp && p.Error == "" && p.ResourceRef() == ref { continue } - plan.RenderPlan(&block, p, "") + if p.ResourceRef() == ref { + // The header already describes the composite's health. + own := *p + own.Health = nil + p = &own + } + plan.RenderPlan(&block, p, "", pr.fullMessages) + } + if lines := unhealthyBlock(c.Unhealthy, plans, pr.fullMessages); len(lines) > 0 { + _, _ = fmt.Fprintln(&block, strings.Join(lines, "\n")) + _, _ = fmt.Fprintln(&block) } for line := range strings.Lines(block.String()) { if line != "\n" { @@ -490,14 +509,43 @@ func renderComposite(w io.Writer, c CompositeResult, ref plan.ResourceRef, plans } } -// compositeHeader returns e.g. "XR: XNetwork.example.org prod-network". -func compositeHeader(c CompositeResult, ref plan.ResourceRef) string { +// unhealthyBlock returns the lines listing a composite's resources that are +// not healthy and have no plan, or nil when there are none. A resource a +// nested composite composes is nested under that composite's line: its own, +// or, when the composite has a plan in plans, one naming it. +func unhealthyBlock(unhealthy []UnhealthyResource, plans []*plan.ResourcePlan, full bool) []string { + if len(unhealthy) == 0 { + return nil + } + listed := map[string]string{} + for _, p := range plans { + listed[healthKey(p.ResourceRef())] = displayName(p.ResourceRef()) + } + items := make([]health.Item, 0, len(unhealthy)) + for _, u := range unhealthy { + it := health.Item{ID: healthKey(u.Ref), Name: displayName(u.Ref), Health: u.Health} + if u.Via != nil { + it.Via, it.ViaName = healthKey(*u.Via), displayName(*u.Via) + } + items = append(items, it) + } + return health.Block(items, listed, full) +} + +// displayName returns e.g. "XNetwork.example.org prod/network", as a plan's +// header names its resource. +func displayName(ref plan.ResourceRef) string { gvk := schema.FromAPIVersionAndKind(ref.APIVersion, ref.Kind) name := ref.Name if ref.Namespace != "" { name = ref.Namespace + "/" + name } - h := fmt.Sprintf("XR: %s.%s %s", gvk.Kind, gvk.Group, name) + return gvk.Kind + "." + gvk.Group + " " + name +} + +// compositeHeader returns e.g. "XR: XNetwork.example.org prod-network". +func compositeHeader(c CompositeResult, ref plan.ResourceRef) string { + h := "XR: " + displayName(ref) switch c.Status { case CompositeDeferred: h += " (deferred)" diff --git a/internal/simulate/result_test.go b/internal/simulate/result_test.go index 6f72031c..ac7de1dc 100644 --- a/internal/simulate/result_test.go +++ b/internal/simulate/result_test.go @@ -70,7 +70,7 @@ Plan: 0 to add, 1 to change, 1 to destroy. 1 composite resource deferred (pinned to a composition revision). ` var buf bytes.Buffer - r2, _ := NewRenderer(plan.FormatPlan) + r2, _ := NewRenderer(plan.FormatPlan, false) if err := r2.Render(&buf, r); err != nil { t.Fatalf("Render(plan): unexpected error: %v", err) } @@ -91,7 +91,7 @@ func TestRenderPlanCompositeDetail(t *testing.T) { Plan: 0 to add, 0 to change, 0 to destroy. ` var buf bytes.Buffer - r2, _ := NewRenderer(plan.FormatPlan) + r2, _ := NewRenderer(plan.FormatPlan, false) if err := r2.Render(&buf, r); err != nil { t.Fatalf("Render(plan): unexpected error: %v", err) } @@ -103,7 +103,7 @@ Plan: 0 to add, 0 to change, 0 to destroy. func TestRenderPlanPinnedHeader(t *testing.T) { xr := plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XNetwork", Name: "prod-network"} var buf bytes.Buffer - r, _ := NewRenderer(plan.FormatPlan) + r, _ := NewRenderer(plan.FormatPlan, false) if err := r.Render(&buf, Result{Composites: []CompositeResult{{Ref: xr, Status: CompositeUnchanged, Pinned: true}}}); err != nil { t.Fatalf("Render(plan): unexpected error: %v", err) } @@ -164,7 +164,7 @@ Plan: 0 to add, 2 to change, 0 to destroy. Note: Provider plan not run. ` var buf bytes.Buffer - pr, _ := NewRenderer(plan.FormatPlan) + pr, _ := NewRenderer(plan.FormatPlan, false) if err := pr.Render(&buf, r); err != nil { t.Fatalf("Render(plan): unexpected error: %v", err) } @@ -202,7 +202,7 @@ func TestRenderJSONProjectFields(t *testing.T) { }}, } var buf bytes.Buffer - r, _ := NewRenderer(plan.FormatJSON) + r, _ := NewRenderer(plan.FormatJSON, false) if err := r.Render(&buf, in); err != nil { t.Fatalf("Render(json): unexpected error: %v", err) } @@ -256,7 +256,7 @@ func TestRenderYAMLSummaryDeferred(t *testing.T) { xr := plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XNetwork", Name: "prod-network"} in := NewResult(nil, []CompositeResult{{Ref: xr, Status: CompositeDeferred}}, nil) var buf bytes.Buffer - r, _ := NewRenderer(plan.FormatYAML) + r, _ := NewRenderer(plan.FormatYAML, false) if err := r.Render(&buf, in); err != nil { t.Fatalf("Render(yaml): unexpected error: %v", err) } @@ -364,7 +364,7 @@ func TestRenderJSONHealth(t *testing.T) { for name, tc := range cases { t.Run(name, func(t *testing.T) { var buf bytes.Buffer - r, _ := NewRenderer(plan.FormatJSON) + r, _ := NewRenderer(plan.FormatJSON, false) if err := r.Render(&buf, tc.args.in); err != nil { t.Fatalf("%s\nRender(json): unexpected error: %v", tc.reason, err) } @@ -393,3 +393,226 @@ func TestRenderJSONHealth(t *testing.T) { }) } } + +func TestRenderPlanHealth(t *testing.T) { + xr := plan.ResourceRef{APIVersion: "aws.platform.upbound.io/v1alpha1", Kind: "Network", Namespace: "crossplane-system", Name: "net"} + other := plan.ResourceRef{APIVersion: "aws.platform.upbound.io/v1alpha1", Kind: "Network", Namespace: "crossplane-system", Name: "other"} + nested := plan.ResourceRef{APIVersion: "aws.platform.upbound.io/v1alpha1", Kind: "XSubnet", Namespace: "crossplane-system", Name: "net-sn"} + rt := plan.ResourceRef{APIVersion: "ec2.aws.m.upbound.io/v1beta1", Kind: "RouteTable", Namespace: "crossplane-system", Name: "net-rt"} + sn := plan.ResourceRef{APIVersion: "ec2.aws.m.upbound.io/v1beta1", Kind: "Subnet", Namespace: "crossplane-system", Name: "net-sn-a"} + failing := func(msg string) *health.Health { + return &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: msg}} + } + notReady := &health.Health{State: health.NotReady, Ready: &health.Condition{Status: "False", Reason: "Creating", Message: "14/15 unready"}} + update := func(name string, h *health.Health) *plan.ResourcePlan { + return &plan.ResourcePlan{ + APIVersion: "ec2.aws.m.upbound.io/v1beta1", Kind: "InternetGateway", Namespace: "crossplane-system", Name: name, + Action: plan.ActionUpdate, Composite: &xr, Health: h, + Changes: []plan.FieldChange{{Path: "spec.forProvider.region", Actual: &plan.Value{Value: "us-east-1"}, Planned: &plan.Value{Value: "us-west-2"}}}, + } + } + vpcMsg := "The vpc ID 'vpc-048777c3b8a8ee173' does not exist\nstatus code: 400" + + type args struct { + r Result + full bool + } + cases := map[string]struct { + reason string + args args + want string + }{ + "BrokenXR": { + reason: "An unhealthy XR's header, a failing plan, and the composed resources that are unhealthy without changes are marked, nested under their nested XR, and summarized.", + args: args{r: NewResult( + []*plan.ResourcePlan{ + // The XR's own no-op plan is not printed; its header carries its health. + {APIVersion: xr.APIVersion, Kind: xr.Kind, Namespace: xr.Namespace, Name: xr.Name, Action: plan.ActionNoOp, Composite: &xr, Health: notReady}, + update("net-igw", failing("cannot update: "+vpcMsg)), + }, + []CompositeResult{ + {Ref: xr, Status: CompositeChanged, Health: notReady, Unhealthy: []UnhealthyResource{ + {Ref: rt, Health: failing(vpcMsg)}, + {Ref: nested, Health: notReady}, + {Ref: sn, Via: &nested, Health: &health.Health{State: health.Paused}}, + }}, + {Ref: other, Status: CompositeUnchanged, Health: &health.Health{State: health.Healthy}}, + }, nil)}, + want: `XR: Network.aws.platform.upbound.io crossplane-system/net ⚠ not ready (Ready=False, Creating): 14/15 unready + [~] InternetGateway.ec2.aws.m.upbound.io crossplane-system/net-igw (update) + ⚠ failing (Synced=False, ReconcileError): cannot update: The vpc ID 'vpc-048777c3b8a8ee173' does not exist status code: 400 + spec: + forProvider: + ~ region: "us-east-1" -> "us-west-2" + + Unhealthy resources without changes: + RouteTable.ec2.aws.m.upbound.io crossplane-system/net-rt ⚠ failing (Synced=False, ReconcileError): The vpc ID 'vpc-048777c3b8a8ee173' does not exist status code: 400 + XSubnet.aws.platform.upbound.io crossplane-system/net-sn ⚠ not ready (Ready=False, Creating): 14/15 unready + Subnet.ec2.aws.m.upbound.io crossplane-system/net-sn-a ⚠ paused + +XR: Network.aws.platform.upbound.io crossplane-system/other + +Plan: 0 to add, 1 to change, 0 to destroy. +Health: 1 of 2 XRs unhealthy before apply; 2 resources failing (Synced=False), 1 not ready, 1 paused. +`, + }, + "FullMessages": { + reason: "With full messages, each message prints in full beneath its line, wrapped and indented.", + args: args{full: true, r: NewResult( + []*plan.ResourcePlan{update("net-igw", failing(vpcMsg))}, + []CompositeResult{{Ref: xr, Status: CompositeChanged, Health: failing("xr broken"), Unhealthy: []UnhealthyResource{{Ref: rt, Health: failing(vpcMsg)}}}}, + nil)}, + want: `XR: Network.aws.platform.upbound.io crossplane-system/net ⚠ failing (Synced=False, ReconcileError) + xr broken + [~] InternetGateway.ec2.aws.m.upbound.io crossplane-system/net-igw (update) + ⚠ failing (Synced=False, ReconcileError) + The vpc ID 'vpc-048777c3b8a8ee173' does not exist + status code: 400 + spec: + forProvider: + ~ region: "us-east-1" -> "us-west-2" + + Unhealthy resources without changes: + RouteTable.ec2.aws.m.upbound.io crossplane-system/net-rt ⚠ failing (Synced=False, ReconcileError) + The vpc ID 'vpc-048777c3b8a8ee173' does not exist + status code: 400 + +Plan: 0 to add, 1 to change, 0 to destroy. +Health: 1 of 1 XR unhealthy before apply; 2 resources failing (Synced=False). +`, + }, + "NestedUnderPlannedXR": { + reason: "A resource whose nested XR has a plan of its own is nested under a line naming that XR.", + args: args{r: NewResult( + []*plan.ResourcePlan{{APIVersion: nested.APIVersion, Kind: nested.Kind, Namespace: nested.Namespace, Name: nested.Name, Action: plan.ActionUpdate, Composite: &xr}}, + []CompositeResult{{Ref: xr, Status: CompositeChanged, Unhealthy: []UnhealthyResource{{Ref: sn, Via: &nested, Health: notReady}}}}, + nil)}, + want: `XR: Network.aws.platform.upbound.io crossplane-system/net + [~] XSubnet.aws.platform.upbound.io crossplane-system/net-sn (update) + + Unhealthy resources without changes: + XSubnet.aws.platform.upbound.io crossplane-system/net-sn + Subnet.ec2.aws.m.upbound.io crossplane-system/net-sn-a ⚠ not ready (Ready=False, Creating): 14/15 unready + +Plan: 0 to add, 1 to change, 0 to destroy. +Health: 1 resource not ready. +`, + }, + "NestedEntryListsItsOwn": { + reason: "A nested XR with an entry and health of its own is marked on its own header and lists what it composes; it counts as an XR, not a resource.", + args: args{r: NewResult(nil, + []CompositeResult{ + {Ref: xr, Status: CompositeUnchanged, Health: &health.Health{State: health.Healthy}, Unhealthy: []UnhealthyResource{{Ref: rt, Health: failing("gone")}}}, + {Ref: nested, ComposedBy: &xr, Status: CompositeUnchanged, Health: notReady, Unhealthy: []UnhealthyResource{{Ref: sn, Health: notReady}}}, + }, nil)}, + want: `XR: Network.aws.platform.upbound.io crossplane-system/net + Unhealthy resources without changes: + RouteTable.ec2.aws.m.upbound.io crossplane-system/net-rt ⚠ failing (Synced=False, ReconcileError): gone + +XR: XSubnet.aws.platform.upbound.io crossplane-system/net-sn ⚠ not ready (Ready=False, Creating): 14/15 unready + ↳ composed by Network.aws.platform.upbound.io crossplane-system/net + Unhealthy resources without changes: + Subnet.ec2.aws.m.upbound.io crossplane-system/net-sn-a ⚠ not ready (Ready=False, Creating): 14/15 unready + +Plan: 0 to add, 0 to change, 0 to destroy. +Health: 1 of 2 XRs unhealthy before apply; 1 resource failing (Synced=False), 1 not ready. +`, + }, + "NestedEntryWithoutHealth": { + reason: "A nested XR whose entry has no health is listed under its parent and counts as a resource, not an XR.", + args: args{r: NewResult(nil, + []CompositeResult{ + {Ref: xr, Status: CompositeUnchanged, Health: notReady, Unhealthy: []UnhealthyResource{ + {Ref: nested, Health: failing("broken")}, + {Ref: sn, Via: &nested, Health: notReady}, + }}, + {Ref: nested, ComposedBy: &xr, Status: CompositeError}, + }, nil)}, + want: `XR: Network.aws.platform.upbound.io crossplane-system/net ⚠ not ready (Ready=False, Creating): 14/15 unready + Unhealthy resources without changes: + XSubnet.aws.platform.upbound.io crossplane-system/net-sn ⚠ failing (Synced=False, ReconcileError): broken + Subnet.ec2.aws.m.upbound.io crossplane-system/net-sn-a ⚠ not ready (Ready=False, Creating): 14/15 unready + +XR: XSubnet.aws.platform.upbound.io crossplane-system/net-sn + ↳ composed by Network.aws.platform.upbound.io crossplane-system/net + +Plan: 0 to add, 0 to change, 0 to destroy. +Health: 1 of 1 XR unhealthy before apply; 1 resource failing (Synced=False), 1 not ready. +`, + }, + "UngroupedDelete": { + reason: "A failing resource the plan deletes is marked like any other.", + args: args{r: NewResult( + []*plan.ResourcePlan{{APIVersion: rt.APIVersion, Kind: rt.Kind, Namespace: rt.Namespace, Name: rt.Name, Action: plan.ActionDelete, Health: failing("gone")}}, + nil, nil)}, + want: `[-] RouteTable.ec2.aws.m.upbound.io crossplane-system/net-rt (delete) + ⚠ failing (Synced=False, ReconcileError): gone + +Plan: 0 to add, 0 to change, 1 to destroy. +Health: 1 resource failing (Synced=False). +`, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + r, _ := NewRenderer(plan.FormatPlan, tc.args.full) + var buf bytes.Buffer + if err := r.Render(&buf, tc.args.r); err != nil { + t.Fatalf("\n%s\nRender(plan): unexpected error: %v", tc.reason, err) + } + if diff := cmp.Diff(tc.want, buf.String()); diff != "" { + t.Errorf("\n%s\nRender(plan): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestResultFailing(t *testing.T) { + xr := plan.ResourceRef{APIVersion: "example.org/v1", Kind: "XNetwork", Name: "prod"} + failing := &health.Health{State: health.Failing} + notReady := &health.Health{State: health.NotReady} + + type args struct { + r Result + } + cases := map[string]struct { + reason string + args args + want bool + }{ + "Healthy": { + reason: "A run with nothing failing sets no exit bit.", + args: args{r: NewResult([]*plan.ResourcePlan{{Kind: "ConfigMap", Name: "cm", Action: plan.ActionUpdate}}, []CompositeResult{{Ref: xr}}, nil)}, + }, + "NotReadyAndPaused": { + reason: "Only failing counts: not ready and paused resources don't.", + args: args{r: NewResult( + []*plan.ResourcePlan{{Kind: "ConfigMap", Name: "cm", Health: notReady}}, + []CompositeResult{{Ref: xr, Health: notReady, Unhealthy: []UnhealthyResource{{Ref: xr, Health: &health.Health{State: health.Paused}}}}}, nil)}, + }, + "FailingComposite": { + reason: "A failing composite counts.", + args: args{r: NewResult(nil, []CompositeResult{{Ref: xr, Health: failing}}, nil)}, + want: true, + }, + "FailingUnchanged": { + reason: "A failing resource without changes counts.", + args: args{r: NewResult(nil, []CompositeResult{{Ref: xr, Unhealthy: []UnhealthyResource{{Ref: xr, Health: failing}}}}, nil)}, + want: true, + }, + "FailingDelete": { + reason: "A failing resource the plan deletes counts.", + args: args{r: NewResult([]*plan.ResourcePlan{{Kind: "ConfigMap", Name: "cm", Action: plan.ActionDelete, Health: failing}}, nil, nil)}, + want: true, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + if got := tc.args.r.Failing(); got != tc.want { + t.Errorf("\n%s\nFailing(): want %t, got %t", tc.reason, tc.want, got) + } + }) + } +} diff --git a/internal/simulate/run.go b/internal/simulate/run.go index b8a01424..273f2bcf 100644 --- a/internal/simulate/run.go +++ b/internal/simulate/run.go @@ -55,6 +55,7 @@ import ( "github.com/crossplane/cli/v2/internal/docker" "github.com/crossplane/cli/v2/internal/exitcode" "github.com/crossplane/cli/v2/internal/kube" + "github.com/crossplane/cli/v2/internal/plan" "github.com/crossplane/cli/v2/internal/project" "github.com/crossplane/cli/v2/internal/terminal" clixpkg "github.com/crossplane/cli/v2/internal/xpkg" @@ -514,7 +515,7 @@ func (c *Runner) renderReport(k *kong.Context, logger logging.Logger, runErr err // engine's renderers do. The run's other warnings went to stderr when they // were raised. func (c *Runner) renderPlan(k *kong.Context, res Result, derived []dt.OutputWarning) error { - r, err := NewRenderer(c.Output) + r, err := NewRenderer(c.Output, c.FullMessages) if err != nil { return err } @@ -583,7 +584,7 @@ func (c *Runner) Run(sigCtx context.Context, k *kong.Context, logger logging.Log c.sel = *in.Selection } project := in.Build != nil - warnIgnoredFlags(k.Stderr, setFlags(k), c.SkipPlan, project) + warnIgnoredFlags(k.Stderr, setFlags(k), c.Output, c.SkipPlan, project) if err := docker.Check(sigCtx); err != nil { return errors.Wrap(err, "cannot connect to Docker, which simulate needs to run functions") @@ -687,8 +688,10 @@ func (c *Flags) flagImageConfigs() ([]pkgv1beta1.ImageConfig, error) { // warnIgnoredFlags prints a warning to stderr for each flag set on the // command line that does nothing in this run: --skip-plan runs no diff // servers, so --providers and --session-id do nothing with it, and -// --cache-dir caches nothing with --skip-plan unless the run builds a project. -func warnIgnoredFlags(stderr io.Writer, set map[string]bool, skipPlan, project bool) { +// --cache-dir caches nothing with --skip-plan unless the run builds a project, +// and --full-messages changes nothing in json or yaml output, which always +// carries messages in full. +func warnIgnoredFlags(stderr io.Writer, set map[string]bool, output string, skipPlan, project bool) { for _, f := range []string{"providers", "session-id"} { if skipPlan && set[f] { _, _ = fmt.Fprintf(stderr, "Warning: ignoring --%s; --skip-plan runs no diff servers.\n", f) @@ -697,6 +700,9 @@ func warnIgnoredFlags(stderr io.Writer, set map[string]bool, skipPlan, project b if !project && skipPlan && set["cache-dir"] { _, _ = fmt.Fprintln(stderr, "Warning: ignoring --cache-dir; with --skip-plan and no project file there is nothing to cache.") } + if set["full-messages"] && (output == plan.FormatJSON || output == plan.FormatYAML) { + _, _ = fmt.Fprintf(stderr, "Warning: ignoring --full-messages; -o %s always prints messages in full.\n", output) + } } // Built is a built project: what Inputs.Build returns. diff --git a/internal/simulate/run_test.go b/internal/simulate/run_test.go index ad267985..43ce7c41 100644 --- a/internal/simulate/run_test.go +++ b/internal/simulate/run_test.go @@ -745,6 +745,7 @@ func TestWarnIgnoredFlags(t *testing.T) { cases := map[string]struct { reason string args []string + output string skipPlan bool project bool warn string @@ -783,6 +784,28 @@ func TestWarnIgnoredFlags(t *testing.T) { skipPlan: true, warn: "ignoring --session-id", }, + "FullMessagesPlan": { + reason: "--full-messages applies to plan output.", + args: []string{"--full-messages"}, + output: "plan", + }, + "FullMessagesDiff": { + reason: "--full-messages applies to diff output.", + args: []string{"--full-messages"}, + output: "diff", + }, + "FullMessagesJSON": { + reason: "json always carries messages in full, so --full-messages is ignored with a warning.", + args: []string{"--full-messages"}, + output: "json", + warn: "ignoring --full-messages; -o json", + }, + "FullMessagesYAML": { + reason: "yaml always carries messages in full, so --full-messages is ignored with a warning.", + args: []string{"--full-messages"}, + output: "yaml", + warn: "ignoring --full-messages; -o yaml", + }, "DefaultsAreNotSet": { reason: "Flags left at their defaults, or set from the environment, are not warned about.", skipPlan: true, @@ -809,7 +832,7 @@ func TestWarnIgnoredFlags(t *testing.T) { } var stderr bytes.Buffer - warnIgnoredFlags(&stderr, setFlags(k), tc.skipPlan, tc.project) + warnIgnoredFlags(&stderr, setFlags(k), tc.output, tc.skipPlan, tc.project) if tc.warn == "" && stderr.Len() > 0 || !strings.Contains(stderr.String(), tc.warn) { t.Errorf("\n%s\nwarnIgnoredFlags(...): want warning containing %q, got %q", tc.reason, tc.warn, stderr.String()) } From f06445c020222e024c5a721924ad6a79eddb4881 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 14:14:03 -0500 Subject: [PATCH 13/30] feat(diff): mark unhealthy resources in diff output -o diff now shows what plan output does. An XR's health heads its section, or the output for a single XR. A diffed resource that is not healthy is marked on its header. Each XR's unhealthy resources without changes are listed after its diffs, or after "No changes.". A Health: line closes the run. The composition diff marks each affected XR's line and lists those resources after the impact analysis. Both use the formatting in internal/health and honour --full-messages. Healthy runs print what they did before. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/diff/renderer/comp_diff_renderer.go | 73 +++++- internal/diff/renderer/diff_formatter.go | 4 + internal/diff/renderer/diff_renderer.go | 173 ++++++++++--- internal/diff/renderer/health.go | 120 +++++++++ internal/diff/renderer/health_test.go | 242 +++++++++++++++++++ internal/simulate/run.go | 1 + internal/simulate/run_test.go | 8 + 7 files changed, 584 insertions(+), 37 deletions(-) create mode 100644 internal/diff/renderer/health.go create mode 100644 internal/diff/renderer/health_test.go diff --git a/internal/diff/renderer/comp_diff_renderer.go b/internal/diff/renderer/comp_diff_renderer.go index 2e473c2f..050cca6b 100644 --- a/internal/diff/renderer/comp_diff_renderer.go +++ b/internal/diff/renderer/comp_diff_renderer.go @@ -25,6 +25,7 @@ import ( "github.com/crossplane/crossplane-runtime/v2/pkg/logging" dt "github.com/crossplane/cli/v2/internal/diff/result" + "github.com/crossplane/cli/v2/internal/health" ) const ( @@ -78,6 +79,21 @@ func (r *DefaultCompDiffRenderer) RenderCompDiff(output *dt.CompDiffOutput) erro } } + // What is not healthy before apply is summarized once for the run, as in plan output. + var t health.Tally + + for _, comp := range output.Compositions { + for _, impact := range comp.ImpactAnalysis { + tallyComposite(&t, impact.ObjectReference, impact.Health, impact.Diffs) + } + } + + if line := health.SummaryLine(t.Counts()); line != "" { + if _, err := fmt.Fprintf(stdout, "\n%s\n", line); err != nil { + return errors.Wrap(err, "cannot write health summary") + } + } + // One run-wide summary of unverified additions; see DiffRenderer.RenderDiffs for why it is derived // at render time, and why the per-composition reuse of the diff renderer above does not emit it. return writeWarnings(r.opts.Stderr, dt.CompDryRunWarnings(output)) @@ -438,7 +454,44 @@ func (r *DefaultCompDiffRenderer) renderImpactAnalysis(comp *dt.CompositionDiff) } } - return nil + return r.renderUnhealthyBlocks(comp, len(allDiffs) > 0) +} + +// renderUnhealthyBlocks writes, for each composite the composition affects, +// the resources it composes that are not healthy and have no changes, under a +// line naming the composite. afterDiffs separates the first from the diffs +// above it. +func (r *DefaultCompDiffRenderer) renderUnhealthyBlocks(comp *dt.CompositionDiff, afterDiffs bool) error { + var sb strings.Builder + + for _, impact := range listedImpacts(comp) { + block := unhealthyBlock(impact.Health, impact.Diffs, r.opts.FullMessages) + if len(block) == 0 { + continue + } + + if afterDiffs || sb.Len() > 0 { + sb.WriteString("\n") + } + + sb.WriteString("XR: " + impactTitle(impact) + "\n") + + for _, l := range block { + sb.WriteString(" " + l + "\n") + } + } + + return write(r.opts.Stdout, sb.String()) +} + +// impactTitle names an affected composite, e.g. "XNetwork/prod/net", as the +// xr diff names its sections. +func impactTitle(impact dt.XRImpact) string { + if impact.Namespace != "" { + return impact.Kind + "/" + impact.Namespace + "/" + impact.Name + } + + return impact.Kind + "/" + impact.Name } // totalFiltered sums the per-reason filter counters. Kept in one place so adding a reason cannot @@ -568,12 +621,26 @@ func (r *DefaultCompDiffRenderer) buildXRStatusList(impacts []dt.XRImpact) strin suffix = filteredSuffix(impact) } - fmt.Fprintf(&sb, "%s %s %s/%s (%s)%s%s\n", + // What is not healthy before apply follows, uncoloured: its marker is + // not the status indicator. + desc := healthDescription(impact.Health.XR, r.opts.FullMessages) + if len(desc) > 0 { + desc[0] = " " + desc[0] + } else { + desc = []string{""} + } + + fmt.Fprintf(&sb, "%s %s %s/%s (%s)%s%s%s\n", color, indicator, impact.Kind, impact.Name, scope, suffix, - colorReset) + colorReset, + desc[0]) + + for _, l := range desc[1:] { + fmt.Fprintf(&sb, " %s\n", l) + } // Include error details for XRStatusError impacts so users can diagnose issues. if impact.Status == dt.XRStatusError && impact.Error != nil { diff --git a/internal/diff/renderer/diff_formatter.go b/internal/diff/renderer/diff_formatter.go index 4a3caefe..0164944b 100644 --- a/internal/diff/renderer/diff_formatter.go +++ b/internal/diff/renderer/diff_formatter.go @@ -50,6 +50,10 @@ type DiffOptions struct { // Replaced, when set, reports whether a diff's resource would be replaced; // its header and the summary say so. Nil reports none. Replaced func(*t.ResourceDiff) bool + + // FullMessages prints health condition messages in full, wrapped, rather + // than cut to one line. + FullMessages bool } // OutputFormat represents the desired output format for diffs. diff --git a/internal/diff/renderer/diff_renderer.go b/internal/diff/renderer/diff_renderer.go index b6c86bee..ed30fd0e 100644 --- a/internal/diff/renderer/diff_renderer.go +++ b/internal/diff/renderer/diff_renderer.go @@ -11,6 +11,7 @@ import ( "github.com/crossplane/crossplane-runtime/v2/pkg/logging" dt "github.com/crossplane/cli/v2/internal/diff/result" + "github.com/crossplane/cli/v2/internal/health" ) // flattenGroups merges every group's diffs into a single map, for the human @@ -130,6 +131,11 @@ func (r *DefaultDiffRenderer) renderDiffList(diffs map[string]*dt.ResourceDiff) header += " (requires replacement)" } + // A resource the cluster has is marked when it is not healthy. + if desc := healthDescription(health.Of(diff.Current.Raw), r.diffOpts.FullMessages); len(desc) > 0 { + header = strings.Join(append([]string{header + " " + desc[0]}, desc[1:]...), "\n") + } + content := FormatDiff(diff.LineDiffs, r.diffOpts) if content != "" { @@ -257,7 +263,7 @@ func (r *DefaultDiffRenderer) RenderDiffs(groups []dt.XRDiffGroup, errs []dt.Out if len(xrGroups) > 1 { err = r.renderGrouped(groups) } else { - err = r.renderFlat(flattenGroups(groups)) + err = r.renderFlat(flattenGroups(groups), xrGroups) } if err != nil { @@ -268,8 +274,35 @@ func (r *DefaultDiffRenderer) RenderDiffs(groups []dt.XRDiffGroup, errs []dt.Out } // renderFlat renders a single flat block of diffs plus a "Summary:" line. This -// is the pre-grouping behavior, used for identity-less groups (comp reuse). -func (r *DefaultDiffRenderer) renderFlat(diffs map[string]*dt.ResourceDiff) error { +// is the pre-grouping behavior, used for identity-less groups (comp reuse) and +// a single input XR. That XR, in xrGroups when there is one, is described +// when it or what it composes is not healthy: its health on a line before the +// diffs, its unhealthy resources without changes after them, and a Health: +// line after the summary. +func (r *DefaultDiffRenderer) renderFlat(diffs map[string]*dt.ResourceDiff, xrGroups []dt.XRDiffGroup) error { + stdout := r.diffOpts.Stdout + full := r.diffOpts.FullMessages + + var ( + head, block string + t health.Tally + ) + + if len(xrGroups) == 1 { + g := xrGroups[0] + if desc := healthDescription(g.Health.XR, full); len(desc) > 0 { + desc[0] = "XR: " + groupTitle(g) + " " + desc[0] + head = joinLines(desc) + "\n" + } + + block = joinLines(unhealthyBlock(g.Health, g.Diffs, full)) + tallyComposite(&t, g.XR, g.Health, g.Diffs) + } + + if err := write(stdout, head); err != nil { + return err + } + counts, err := r.renderDiffList(diffs) if err != nil { return err @@ -277,17 +310,52 @@ func (r *DefaultDiffRenderer) renderFlat(diffs map[string]*dt.ResourceDiff) erro r.logger.Debug("Diff rendering complete", "counts", counts) + summary := "" if counts.output > 0 { - if line := counts.summaryLine(); line != "" { - if _, err := fmt.Fprintf(r.diffOpts.Stdout, "\nSummary: %s\n", line); err != nil { - return errors.Wrap(err, "failed to write summary to output") - } + summary = counts.summaryLine() + } + + // The block, Summary and Health lines are separated by blank lines from + // whatever precedes them. + written := head != "" || counts.output > 0 + for _, s := range []string{block, summaryText(summary), joinLines([]string{health.SummaryLine(t.Counts())})} { + if s == "" || s == "\n" { + continue } + + if written { + s = "\n" + s + } + + if err := write(stdout, s); err != nil { + return err + } + + written = true } return nil } +// summaryText returns the "Summary:" line for line, or "" when it is empty. +func summaryText(line string) string { + if line == "" { + return "" + } + + return "Summary: " + line + "\n" +} + +// groupTitle names a group's XR, e.g. "XNetwork/prod/net". The namespace is +// part of the identity: same-named XRs in different namespaces are distinct. +func groupTitle(g dt.XRDiffGroup) string { + if g.XR.Namespace != "" { + return g.XR.Kind + "/" + g.XR.Namespace + "/" + g.XR.Name + } + + return g.XR.Kind + "/" + g.XR.Name +} + // renderGrouped renders each group as a per-input-XR section (header + diffs + // per-section summary, or an inline error / "No changes."), then an aggregate // footer. Only called when there is more than one input XR (a single XR renders @@ -300,6 +368,7 @@ func (r *DefaultDiffRenderer) renderGrouped(groups []dt.XRDiffGroup) error { xrCount int unchangedXR int errorXR int + t health.Tally ) for _, g := range groups { @@ -319,51 +388,87 @@ func (r *DefaultDiffRenderer) renderGrouped(groups []dt.XRDiffGroup) error { xrCount++ - // The namespace is part of the identity: same-named XRs in different namespaces are distinct. - title := g.XR.Kind + "/" + g.XR.Name - if g.XR.Namespace != "" { - title = g.XR.Kind + "/" + g.XR.Namespace + "/" + g.XR.Name - } + tallyComposite(&t, g.XR, g.Health, g.Diffs) - if _, err := fmt.Fprintf(stdout, "=== %s ===\n\n", title); err != nil { - return errors.Wrap(err, "failed to write XR section header") + counts, err := r.renderSection(g) + if err != nil { + return err } - if g.Err != nil { + total = total.plus(counts) + + switch { + case g.Err != nil: errorXR++ + case counts.output == 0: + unchangedXR++ + } + } - if _, err := fmt.Fprintf(stdout, "Error: %s\n\n", g.Err.Message); err != nil { - return errors.Wrap(err, "failed to write XR error") - } + if err := r.renderAggregateFooter(xrCount, total, unchangedXR, errorXR); err != nil { + return err + } - continue + return writeHealthLine(stdout, &t) +} + +// renderSection renders one input XR's section: its header and health, then +// its error, or its diffs, unhealthy resources without changes and summary, +// or "No changes." with those resources. It returns the section's counts, +// zero for an errored XR. +func (r *DefaultDiffRenderer) renderSection(g dt.XRDiffGroup) (diffCounts, error) { + stdout := r.diffOpts.Stdout + + if _, err := fmt.Fprintf(stdout, "=== %s ===\n\n", groupTitle(g)); err != nil { + return diffCounts{}, errors.Wrap(err, "failed to write XR section header") + } + + if s := joinLines(healthDescription(g.Health.XR, r.diffOpts.FullMessages)); s != "" { + if err := write(stdout, s+"\n"); err != nil { + return diffCounts{}, err } + } - counts, err := r.renderDiffList(g.Diffs) - if err != nil { - return err + if g.Err != nil { + if _, err := fmt.Fprintf(stdout, "Error: %s\n\n", g.Err.Message); err != nil { + return diffCounts{}, errors.Wrap(err, "failed to write XR error") } - total = total.plus(counts) + return diffCounts{}, nil + } - if counts.output == 0 { - unchangedXR++ + counts, err := r.renderDiffList(g.Diffs) + if err != nil { + return counts, err + } - if _, err := fmt.Fprintf(stdout, "No changes.\n\n"); err != nil { - return errors.Wrap(err, "failed to write no-changes message") - } + block := joinLines(unhealthyBlock(g.Health, g.Diffs, r.diffOpts.FullMessages)) - continue + if counts.output == 0 { + if _, err := fmt.Fprintf(stdout, "No changes.\n\n"); err != nil { + return counts, errors.Wrap(err, "failed to write no-changes message") } - if line := counts.summaryLine(); line != "" { - if _, err := fmt.Fprintf(stdout, "\nSummary: %s\n\n", line); err != nil { - return errors.Wrap(err, "failed to write section summary") - } + if block != "" { + return counts, write(stdout, block+"\n") + } + + return counts, nil + } + + if block != "" { + if err := write(stdout, "\n"+block); err != nil { + return counts, err } } - return r.renderAggregateFooter(xrCount, total, unchangedXR, errorXR) + if line := counts.summaryLine(); line != "" { + if _, err := fmt.Fprintf(stdout, "\nSummary: %s\n\n", line); err != nil { + return counts, errors.Wrap(err, "failed to write section summary") + } + } + + return counts, nil } // renderAggregateFooter writes the cross-XR totals line shown when more than one diff --git a/internal/diff/renderer/health.go b/internal/diff/renderer/health.go new file mode 100644 index 00000000..61eb9972 --- /dev/null +++ b/internal/diff/renderer/health.go @@ -0,0 +1,120 @@ +package renderer + +import ( + "fmt" + "io" + "strings" + + corev1 "k8s.io/api/core/v1" + + "github.com/crossplane/crossplane-runtime/v2/pkg/errors" + + dt "github.com/crossplane/cli/v2/internal/diff/result" + "github.com/crossplane/cli/v2/internal/health" +) + +// refKey identifies ref across API versions, as health.Key does. +func refKey(ref corev1.ObjectReference) string { + return health.Key(ref.APIVersion, ref.Kind, ref.Namespace, ref.Name) +} + +// diffKey identifies d's resource across API versions, as health.Key does. +func diffKey(d *dt.ResourceDiff) string { + return health.Key(d.Gvk.GroupVersion().String(), d.Gvk.Kind, d.Namespace, d.ResourceName) +} + +// refName returns how the diff list names ref, e.g. "Bucket/my-bucket". +func refName(ref corev1.ObjectReference) string { + return ref.Kind + "/" + ref.Name +} + +// unhealthyBlock returns the lines listing what a composite composes that is +// not healthy and has no changes in diffs, or nil when there is nothing. A +// resource a nested composite composes is nested under that composite, as in +// plan output. +func unhealthyBlock(h dt.CompositeHealth, diffs map[string]*dt.ResourceDiff, full bool) []string { + listed := map[string]string{} + + for _, d := range diffs { + if d.DiffType != dt.DiffTypeEqual { + listed[diffKey(d)] = getKindName(d) + } + } + + var items []health.Item + + for _, r := range h.Composed { + id := refKey(r.ObjectReference) + if !r.Health.Unhealthy() || listed[id] != "" { + continue + } + + it := health.Item{ID: id, Name: refName(r.ObjectReference), Health: r.Health} + if r.Via != nil { + it.Via, it.ViaName = refKey(*r.Via), refName(*r.Via) + } + + items = append(items, it) + } + + return health.Block(items, listed, full) +} + +// joinLines returns lines, each ended by a newline, or "" when there are +// none. +func joinLines(lines []string) string { + if len(lines) == 0 { + return "" + } + + return strings.Join(lines, "\n") + "\n" +} + +// write writes s to w, or nothing when s is empty. +func write(w io.Writer, s string) error { + if s == "" { + return nil + } + + _, err := fmt.Fprint(w, s) + + return errors.Wrap(err, "cannot write health") +} + +// healthDescription returns h's description for a line of its own: the first +// line of health.Lines, with any wrapped message indented beneath it. +func healthDescription(h *health.Health, full bool) []string { + lines := health.Lines(h, full) + for i := 1; i < len(lines); i++ { + lines[i] = " " + lines[i] + } + + return lines +} + +// tallyComposite adds a top-level composite with ref and health h, and what +// it composes, to t. +func tallyComposite(t *health.Tally, ref corev1.ObjectReference, h dt.CompositeHealth, diffs map[string]*dt.ResourceDiff) { + t.Composite(refKey(ref), h.XR) + + for _, d := range diffs { + t.Resource(diffKey(d), health.Of(d.Current.Raw)) + } + + for _, r := range h.Composed { + t.Resource(refKey(r.ObjectReference), r.Health) + } +} + +// writeHealthLine writes the run's Health: line to w, or nothing when nothing +// in it is unhealthy. +func writeHealthLine(w io.Writer, t *health.Tally) error { + line := health.SummaryLine(t.Counts()) + if line == "" { + return nil + } + + _, err := fmt.Fprintln(w, line) + + return errors.Wrap(err, "cannot write health summary") +} diff --git a/internal/diff/renderer/health_test.go b/internal/diff/renderer/health_test.go new file mode 100644 index 00000000..6f62294c --- /dev/null +++ b/internal/diff/renderer/health_test.go @@ -0,0 +1,242 @@ +package renderer + +import ( + "bytes" + "testing" + + "github.com/google/go-cmp/cmp" + corev1 "k8s.io/api/core/v1" + un "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + + dt "github.com/crossplane/cli/v2/internal/diff/result" + tu "github.com/crossplane/cli/v2/internal/diff/testutils" + "github.com/crossplane/cli/v2/internal/health" +) + +// liveWith returns d with a live object carrying a Synced=False condition. +func liveWith(d *dt.ResourceDiff, msg string) *dt.ResourceDiff { + d.Current.Raw = &un.Unstructured{Object: map[string]any{ + "apiVersion": d.Gvk.GroupVersion().String(), "kind": d.Gvk.Kind, + "metadata": map[string]any{"name": d.ResourceName}, + "status": map[string]any{"conditions": []any{map[string]any{ + "type": "Synced", "status": "False", "reason": "ReconcileError", "message": msg, + "lastTransitionTime": "2026-01-01T00:00:00Z", + }}}, + }} + + return d +} + +func TestRenderDiffsHealth(t *testing.T) { + failing := &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: "vpc does not exist"}} + notReady := &health.Health{State: health.NotReady, Ready: &health.Condition{Status: "False", Reason: "Creating"}} + ref := func(kind, name string) corev1.ObjectReference { + return corev1.ObjectReference{APIVersion: "example.org/v1", Kind: kind, Name: name} + } + group := func(name string, xr *health.Health, composed []dt.ResourceHealth, diffs ...*dt.ResourceDiff) dt.XRDiffGroup { + g := xrGroup("XNetwork", name, map[string]*dt.ResourceDiff{}) + for _, d := range diffs { + g.Diffs[d.GetDiffKey()] = d + } + + g.Health = dt.CompositeHealth{XR: xr, Composed: composed} + + return g + } + + type args struct { + groups []dt.XRDiffGroup + full bool + } + cases := map[string]struct { + reason string + args args + want string + }{ + "SingleXR": { + reason: "A single XR prints its health before its diffs, marks a failing resource, lists the unhealthy resources without changes after them, nested by via, and the Health: line after the summary.", + args: args{groups: []dt.XRDiffGroup{group("net", notReady, []dt.ResourceHealth{ + {ObjectReference: ref("Bucket", "b"), Health: failing}, + {ObjectReference: ref("RouteTable", "rt"), Health: failing}, + {ObjectReference: ref("Subnet", "sn"), Via: new(ref("XSubnet", "x")), Health: notReady}, + {ObjectReference: ref("Healthy", "h"), Health: &health.Health{State: health.Healthy}}, + }, liveWith(modifiedDiffFor("Bucket", "b"), "vpc does not exist"))}}, + want: `XR: XNetwork/net ⚠ not ready (Ready=False, Creating) + +~~~ Bucket/b ⚠ failing (Synced=False, ReconcileError): vpc does not exist +- spec: +- field: old ++ spec: ++ field: new + +--- + +Unhealthy resources without changes: + RouteTable/rt ⚠ failing (Synced=False, ReconcileError): vpc does not exist + Subnet/sn (via XSubnet/x) ⚠ not ready (Ready=False, Creating) + +Summary: 1 modified + +Health: 1 of 1 XR unhealthy before apply; 2 resources failing (Synced=False), 1 not ready. +`, + }, + "SingleXRNoChanges": { + reason: "A single XR without changes still prints its unhealthy resources and the Health: line.", + args: args{groups: []dt.XRDiffGroup{group("net", nil, []dt.ResourceHealth{ + {ObjectReference: ref("RouteTable", "rt"), Health: notReady}, + })}}, + want: `Unhealthy resources without changes: + RouteTable/rt ⚠ not ready (Ready=False, Creating) + +Health: 1 resource not ready. +`, + }, + "Grouped": { + reason: "Each XR section carries its health under its header and its unhealthy resources, changed or not, and the Health: line follows the footer.", + args: args{groups: []dt.XRDiffGroup{ + group("a", failing, nil, modifiedDiffFor("Bucket", "b")), + group("c", nil, []dt.ResourceHealth{{ObjectReference: ref("RouteTable", "rt"), Health: notReady}}), + }}, + want: `=== XNetwork/a === + +⚠ failing (Synced=False, ReconcileError): vpc does not exist + +~~~ Bucket/b +- spec: +- field: old ++ spec: ++ field: new + +--- + +Summary: 1 modified + +=== XNetwork/c === + +No changes. + +Unhealthy resources without changes: + RouteTable/rt ⚠ not ready (Ready=False, Creating) + +================================================================================ +Total: 1 modified across 2 XRs (1 unchanged) +Health: 1 of 2 XRs unhealthy before apply; 1 resource not ready. +`, + }, + "FullMessages": { + reason: "With full messages, a message prints in full beneath its line.", + args: args{full: true, groups: []dt.XRDiffGroup{group("net", nil, nil, liveWith(modifiedDiffFor("Bucket", "b"), "line one\nline two"))}}, + want: `~~~ Bucket/b ⚠ failing (Synced=False, ReconcileError) + line one + line two +- spec: +- field: old ++ spec: ++ field: new + +--- + +Summary: 1 modified + +Health: 1 resource failing (Synced=False). +`, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + var buf bytes.Buffer + + opts := DefaultDiffOptions() + opts.UseColors = false + opts.FullMessages = tc.args.full + opts.Stdout = &buf + opts.Stderr = &bytes.Buffer{} + + if err := NewDiffRenderer(tu.TestLogger(t, false), opts).RenderDiffs(tc.args.groups, nil, nil); err != nil { + t.Fatalf("\n%s\nRenderDiffs(...): unexpected error: %v", tc.reason, err) + } + + if diff := cmp.Diff(tc.want, buf.String()); diff != "" { + t.Errorf("\n%s\nRenderDiffs(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestRenderCompDiffHealth(t *testing.T) { + failing := &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: "boom"}} + notReady := &health.Health{State: health.NotReady, Ready: &health.Condition{Status: "False", Reason: "Creating"}} + rt := corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "RouteTable", Name: "rt"} + + type args struct { + impacts []dt.XRImpact + } + cases := map[string]struct { + reason string + args args + want string + }{ + "Unhealthy": { + reason: "An unhealthy XR's affected line carries its health, each XR's unhealthy resources without changes follow the impact analysis under its name, and the Health: line ends the run.", + args: args{impacts: []dt.XRImpact{ + { + ObjectReference: corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "XBucket", Namespace: "ns", Name: "a"}, + Status: dt.XRStatusUnchanged, + Health: dt.CompositeHealth{XR: failing, Composed: []dt.ResourceHealth{{ObjectReference: rt, Health: notReady}}}, + }, + { + ObjectReference: corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "XBucket", Name: "b"}, + Status: dt.XRStatusUnchanged, + }, + }}, + want: `=== Composition Changes === + +No changes detected in composition xbuckets.example.org + +=== Affected Composite Resources === + + ✓ XBucket/a (namespace: ns) ⚠ failing (Synced=False, ReconcileError): boom + ✓ XBucket/b (cluster-scoped) + +Summary: 2 resources unchanged + +=== Impact Analysis === + +All composite resources are up-to-date. No downstream resource changes detected. + +XR: XBucket/ns/a + Unhealthy resources without changes: + RouteTable/rt ⚠ not ready (Ready=False, Creating) + +Health: 1 of 2 XRs unhealthy before apply; 1 resource not ready. +`, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + var stdout bytes.Buffer + + opts := DefaultDiffOptions() + opts.Stdout = &stdout + opts.Stderr = &bytes.Buffer{} + opts.UseColors = false + + r := NewDefaultCompDiffRenderer(tu.TestLogger(t, false), NewDiffRenderer(tu.TestLogger(t, false), opts), opts) + comp := dt.CompositionDiff{ + Name: "xbuckets.example.org", + AffectedResources: dt.AffectedResourcesSummary{Total: 2, Unchanged: 2}, + ImpactAnalysis: tc.args.impacts, + } + + if err := r.RenderCompDiff(&dt.CompDiffOutput{Compositions: []dt.CompositionDiff{comp}}); err != nil { + t.Fatalf("\n%s\nRenderCompDiff(...): unexpected error: %v", tc.reason, err) + } + + if diff := cmp.Diff(tc.want, stdout.String()); diff != "" { + t.Errorf("\n%s\nRenderCompDiff(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} diff --git a/internal/simulate/run.go b/internal/simulate/run.go index 273f2bcf..6494dfd9 100644 --- a/internal/simulate/run.go +++ b/internal/simulate/run.go @@ -422,6 +422,7 @@ func (c *Runner) diffOptions(k *kong.Context) renderer.DiffOptions { opts := renderer.DefaultDiffOptions() opts.UseColors = !c.NoColor && (c.Hooks != nil && c.Hooks.Terminal || isTerminal(k.Stdout)) opts.Compact = c.Compact + opts.FullMessages = c.FullMessages opts.Stdout = k.Stdout opts.Stderr = k.Stderr return opts diff --git a/internal/simulate/run_test.go b/internal/simulate/run_test.go index 43ce7c41..645d6019 100644 --- a/internal/simulate/run_test.go +++ b/internal/simulate/run_test.go @@ -312,6 +312,14 @@ func TestSimulateDiffOptions(t *testing.T) { o.Compact = true }, }, + "FullMessagesCarryThrough": { + reason: "--full-messages should reach the renderer.", + cmd: Flags{NoColor: true, FullMessages: true}, + want: func(o *renderer.DiffOptions) { + o.UseColors = false + o.FullMessages = true + }, + }, "HooksColorizeAsTerminal": { reason: "The test hooks should colorize as for a terminal.", cmd: Flags{Hooks: &Hooks{Terminal: true}}, From 57629508d1c33c00065dbf98ecf38b4ab43bd343 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 14:19:30 -0500 Subject: [PATCH 14/30] feat(simulate): exit with bit 4 when a resource is failing Without an error, a simulate run's exit status now adds 4 when a composite resource, or a resource it composes, was failing (Synced=False) before apply, including one the change would delete. So 4 means no changes and something failing, and 7 means both. Errors still come first: 130 over 1 over 2. DetermineExitCode takes the failing state and composes the bits in place of returning the first match. The exit help and the harness's exit-code comment describe the new statuses. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- .../diff/diffprocessor/comp_processor_test.go | 2 +- internal/diff/diffprocessor/errors.go | 25 ++++-- internal/diff/diffprocessor/errors_test.go | 28 +++++- internal/diff/result/comp.go | 2 +- internal/simulate/exit.go | 5 ++ internal/simulate/help/exit.md | 11 ++- internal/simulate/run.go | 86 ++++++++++++------- internal/simulate/run_test.go | 42 +++++++++ .../testutils/integration/simharness/cases.go | 2 +- 9 files changed, 158 insertions(+), 45 deletions(-) diff --git a/internal/diff/diffprocessor/comp_processor_test.go b/internal/diff/diffprocessor/comp_processor_test.go index c3466a06..66207bc6 100644 --- a/internal/diff/diffprocessor/comp_processor_test.go +++ b/internal/diff/diffprocessor/comp_processor_test.go @@ -3008,7 +3008,7 @@ func TestDefaultCompDiffProcessor_DiffComposition_RenderOverlaps(t *testing.T) { t.Errorf("%s\nDiffComposition(...): want one run-level report error containing %q, got %+v", tc.reason, tc.wantErr, out) } - if code := DetermineExitCode(err, true); code != ExitCodeToolError { + if code := DetermineExitCode(err, true, false); code != ExitCodeToolError { t.Errorf("%s\nDetermineExitCode(...) = %d, want %d", tc.reason, code, ExitCodeToolError) } }) diff --git a/internal/diff/diffprocessor/errors.go b/internal/diff/diffprocessor/errors.go index f721f15a..8ff64d29 100644 --- a/internal/diff/diffprocessor/errors.go +++ b/internal/diff/diffprocessor/errors.go @@ -41,6 +41,11 @@ const ( // ExitCodeDiffDetected indicates that differences were detected. ExitCodeDiffDetected = 3 + // ExitCodeFailing indicates that a resource was failing (Synced=False) before apply. It is a bit + // that composes with ExitCodeDiffDetected: a run that finds changes and a failing resource exits + // 7. + ExitCodeFailing = 4 + // ExitCodeInterrupted indicates the run was cancelled by SIGINT (Ctrl+C) or SIGTERM. It is 130 by // the shell convention for SIGINT (128 + 2); the signal handler does not report which signal // arrived, so SIGTERM uses the same code. @@ -345,11 +350,12 @@ func isOnlySchemaValidationErrors(err error) bool { return false } -// DetermineExitCode determines the appropriate exit code based on the error and diff status. -// Priority: interrupted (130) > tool error (1) > schema validation error (2) > diff detected (3) > -// success (0). An interruption outranks everything because the other errors of an interrupted run are -// mostly the cancelled calls it caused. -func DetermineExitCode(err error, hasDiffs bool) int { +// DetermineExitCode determines the appropriate exit code based on the error, diff and health status. +// Errors come first, by priority: interrupted (130) > tool error (1) > schema validation error (2). +// An interruption outranks everything because the other errors of an interrupted run are mostly the +// cancelled calls it caused. A run without errors exits with a bitmask: ExitCodeDiffDetected (3) when +// it found differences, ORed with ExitCodeFailing (4) when a resource was failing, so 0, 3, 4 or 7. +func DetermineExitCode(err error, hasDiffs, failing bool) int { if errors.Is(err, ErrInterrupted) { return ExitCodeInterrupted } @@ -366,9 +372,14 @@ func DetermineExitCode(err error, hasDiffs bool) int { return ExitCodeToolError } + code := ExitCodeSuccess if hasDiffs { - return ExitCodeDiffDetected + code |= ExitCodeDiffDetected + } + + if failing { + code |= ExitCodeFailing } - return ExitCodeSuccess + return code } diff --git a/internal/diff/diffprocessor/errors_test.go b/internal/diff/diffprocessor/errors_test.go index 9ec3fda0..d35cb3c8 100644 --- a/internal/diff/diffprocessor/errors_test.go +++ b/internal/diff/diffprocessor/errors_test.go @@ -650,8 +650,34 @@ func TestDetermineExitCode(t *testing.T) { tests := map[string]struct { err error hasDiffs bool + failing bool want int }{ + "NoErrorFailing": { + failing: true, + want: ExitCodeFailing, + }, + "NoErrorWithDiffFailing": { + hasDiffs: true, + failing: true, + want: ExitCodeDiffDetected | ExitCodeFailing, + }, + "ToolErrorOutranksFailing": { + err: errors.New("connection refused"), + hasDiffs: true, + failing: true, + want: ExitCodeToolError, + }, + "SchemaValidationErrorOutranksFailing": { + err: &SchemaValidationError{ResourceID: "Kind/res", Message: "invalid"}, + failing: true, + want: ExitCodeSchemaValidation, + }, + "InterruptedOutranksFailing": { + err: ErrInterrupted, + failing: true, + want: ExitCodeInterrupted, + }, "NoErrorNoDiff": { err: nil, hasDiffs: false, @@ -780,7 +806,7 @@ func TestDetermineExitCode(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { - got := DetermineExitCode(tc.err, tc.hasDiffs) + got := DetermineExitCode(tc.err, tc.hasDiffs, tc.failing) if got != tc.want { t.Errorf("DetermineExitCode() = %v, want %v", got, tc.want) } diff --git a/internal/diff/result/comp.go b/internal/diff/result/comp.go index bc89b03c..af2d97a8 100644 --- a/internal/diff/result/comp.go +++ b/internal/diff/result/comp.go @@ -167,7 +167,7 @@ func (i RevisionImpact) Determined() bool { } // HasChanges returns true if this composition diff has any changes, which is what drives -// ExitCodeDiffDetected. +// ExitCodeDiffDetected's bit. // // Deliberately excluded: RevisionImpact. A composition whose only difference is in masked fields // creates a CompositionRevision but has nothing to show and nothing rendering differently, and a diff --git a/internal/simulate/exit.go b/internal/simulate/exit.go index 29937e77..a4a3f773 100644 --- a/internal/simulate/exit.go +++ b/internal/simulate/exit.go @@ -23,4 +23,9 @@ const ( // ExitCodeChanges is the status a simulate command exits with when the // simulation succeeded and found changes. ExitCodeChanges = 3 + // ExitCodeFailing is the bit a simulate command's exit status carries + // when the simulation succeeded and a resource was failing (Synced=False) + // before apply. It composes with ExitCodeChanges: a run that finds + // changes and a failing resource exits 7. + ExitCodeFailing = 4 ) diff --git a/internal/simulate/help/exit.md b/internal/simulate/help/exit.md index ecc54afc..255f60bd 100644 --- a/internal/simulate/help/exit.md +++ b/internal/simulate/help/exit.md @@ -12,7 +12,16 @@ the result: - 2: only schema validation failed. - 3: the simulation succeeded and found changes, including drift a provider plan found that the cluster diff didn't. +- 4: the simulation succeeded, found no changes, and a composite resource or + a resource it composes was failing (Synced=False) before apply, including + one the change would delete. A resource that is only not ready, or is + paused, doesn't set it. +- 7: the simulation succeeded, found changes, and a resource was failing: + 3 and 4 combined. - 80: a flag was unknown or had an invalid value, so nothing was simulated. - 130: the simulation was interrupted. -When more than one applies, the lowest of 1, 2 and 3 wins. +An error wins over what the simulation found: 130 over 1 over 2, and any of +them over 3, 4 and 7. Without an error, the status adds 4 for a failing +resource to 3 for changes, so 3 and 7 both mean changes, and 4 and 7 both mean +a failing resource. diff --git a/internal/simulate/run.go b/internal/simulate/run.go index 6494dfd9..8210c23b 100644 --- a/internal/simulate/run.go +++ b/internal/simulate/run.go @@ -216,18 +216,18 @@ func changedFunctions(ctx context.Context, rc k8client.ResourceClient, digests m // diffCompositions returns the diff simulate runs for Compositions: their // impact on the composite resources in scope, rendered to k's writers. // Errors are wrapped with msg. -func (c *Runner) diffCompositions(k *kong.Context, logger logging.Logger, proc dp.CompDiffProcessor, comps []*un.Unstructured, msg string) func(context.Context) (bool, error) { - return func(ctx context.Context) (bool, error) { +func (c *Runner) diffCompositions(k *kong.Context, logger logging.Logger, proc dp.CompDiffProcessor, comps []*un.Unstructured, msg string) func(context.Context) (outcome, error) { + return func(ctx context.Context) (outcome, error) { output, err := proc.DiffComposition(ctx, comps, c.sel.ns, c.sel.refs) // Render the report even when there are errors (e.g. every // composition failed), so structured output stays valid. A render // failure is joined with the processing error, so neither is lost. - changed := output != nil && output.HasChanges() + var o outcome if output != nil { - changed, err = c.renderCompReport(k, logger, output, err) + o, err = c.renderCompReport(k, logger, output, err) } - return changed, errors.Wrap(err, msg) + return o, errors.Wrap(err, msg) } } @@ -236,21 +236,21 @@ func (c *Runner) diffCompositions(k *kong.Context, logger logging.Logger, proc d // crossplane-diff's xr command. A claim is simulated as its XR, but the // report names the input, so its result is listed under the claim's name // (crossplane/cli#367). -func (c *Runner) diffComposites(k *kong.Context, logger logging.Logger, proc dp.DiffProcessor, xp *xpclient.Clients, ns string, xrs []*un.Unstructured) func(context.Context) (bool, error) { - return func(ctx context.Context) (bool, error) { +func (c *Runner) diffComposites(k *kong.Context, logger logging.Logger, proc dp.DiffProcessor, xp *xpclient.Clients, ns string, xrs []*un.Unstructured) func(context.Context) (outcome, error) { + return func(ctx context.Context) (outcome, error) { if err := checkComposites(ctx, xp.Definition, ns, xrs); err != nil { - return false, err + return outcome{}, err } report, err := proc.PerformDiff(ctx, xrs, xp.Composition.FindMatchingComposition) // Render the report even when there are errors, so successful XRs // are shown and structured output stays valid. The renderer // includes the errors in structured output. - changed := report != nil && report.HasChanges() + var o outcome if report != nil { - changed, err = c.renderXRReport(k, logger, report, err) + o, err = c.renderXRReport(k, logger, report, err) } - return changed, errors.Wrap(err, "cannot simulate composite resources") + return o, errors.Wrap(err, "cannot simulate composite resources") } } @@ -266,18 +266,29 @@ func setFlags(k *kong.Context) map[string]bool { return set } +// outcome is what a diff found that sets the exit status of a run without +// errors. +type outcome struct { + // changed is whether the run changed anything, provider plans included. + changed bool + // failing is whether a composite or a resource it composes was failing + // (Synced=False) before apply. + failing bool +} + // processor is what simulate initializes and cleans up around a diff. type processor interface { Initialize(ctx context.Context) error dp.Cleaner } -// simulate initializes the clients and processor and runs diff. A run that -// finds changes and fails nothing returns a silent ExitError that -// exits 3; one that fails only schema validation returns one that exits 2. +// simulate initializes the clients and processor and runs diff. A run without +// errors that finds changes or a failing resource returns a silent ExitError +// that exits 3, 4 or 7 (see DetermineExitCode); one that fails only schema +// validation returns one that exits 2. // ctx must not be cancelled by its owner until simulate returns, so a // cancellation reads as an interrupt. -func (c *Runner) simulate(ctx context.Context, logger logging.Logger, initClients func(context.Context, logging.Logger) error, proc processor, diff func(context.Context) (bool, error)) (err error) { +func (c *Runner) simulate(ctx context.Context, logger logging.Logger, initClients func(context.Context, logging.Logger) error, proc processor, diff func(context.Context) (outcome, error)) (err error) { // An interrupted run reports the interruption, not the cancelled calls it // caused. main exits 130 for it. defer func() { @@ -296,18 +307,26 @@ func (c *Runner) simulate(ctx context.Context, logger logging.Logger, initClient return errors.Wrap(err, "cannot initialize diff processor") } - // Exit codes rank by severity: a tool error (1) over schema - // validation (2) over changes found (3) over success (0). - hasDiffs, err := diff(ctx) - switch dp.DetermineExitCode(err, hasDiffs) { + // Errors rank by severity: a tool error (1) over schema validation (2). + // Without one, changes found (3) and a failing resource (4) are bits. + o, err := diff(ctx) + switch code := dp.DetermineExitCode(err, o.changed, o.failing); code { + case dp.ExitCodeSuccess, dp.ExitCodeToolError, dp.ExitCodeInterrupted: + return err case dp.ExitCodeSchemaValidation: return exitcode.ExitError{Err: err, Code: ExitCodeSchemaValidation} - case dp.ExitCodeDiffDetected: - // Changes found are a result, not an error, so nothing is printed. - return exitcode.ExitError{Code: ExitCodeChanges} + default: + // Changes found and failing resources are results, not errors, so + // nothing is printed. + ec := 0 + if code&dp.ExitCodeDiffDetected != 0 { + ec |= ExitCodeChanges + } + if code&dp.ExitCodeFailing != 0 { + ec |= ExitCodeFailing + } + return exitcode.ExitError{Code: ec} } - - return err } // scope returns the namespace and composite resource refs DiffComposition @@ -459,7 +478,7 @@ func (c *Runner) notes() []string { // renderXRReport writes an xr run's report in the format -o selects, as // renderReport does. -func (c *Runner) renderXRReport(k *kong.Context, logger logging.Logger, r *dt.XRReport, runErr error) (bool, error) { +func (c *Runner) renderXRReport(k *kong.Context, logger logging.Logger, r *dt.XRReport, runErr error) (outcome, error) { plans := c.providerPlans() return c.renderReport(k, logger, runErr, FromXRReport(r, plans, c.keep), r.HasChanges(), dt.XRDryRunWarnings(r.Groups), func(opts renderer.DiffOptions) error { // Annotated even without plans, so a Secret's values are redacted. @@ -470,7 +489,7 @@ func (c *Runner) renderXRReport(k *kong.Context, logger logging.Logger, r *dt.XR // renderCompReport writes a composition run's report in the format -o // selects, as renderReport does. -func (c *Runner) renderCompReport(k *kong.Context, logger logging.Logger, o *dt.CompDiffOutput, runErr error) (bool, error) { +func (c *Runner) renderCompReport(k *kong.Context, logger logging.Logger, o *dt.CompDiffOutput, runErr error) (outcome, error) { plans := c.providerPlans() return c.renderReport(k, logger, runErr, FromCompReport(o, plans, c.keep), o.HasChanges(), dt.CompDryRunWarnings(o), func(opts renderer.DiffOptions) error { opts.MinimizeComposition = c.sel.MinimizeComposition @@ -482,10 +501,11 @@ func (c *Runner) renderCompReport(k *kong.Context, logger logging.Logger, o *dt. // renderReport writes a run's report in the format -o selects: renderDiff's // human diff for diff, and res otherwise, followed by warnings. It returns -// whether the run changed anything, provider plans included, and runErr -// with any render failure joined to it, so neither is lost. changed is -// whether the report itself has changes. -func (c *Runner) renderReport(k *kong.Context, logger logging.Logger, runErr error, res Result, changed bool, warnings []dt.OutputWarning, renderDiff func(renderer.DiffOptions) error) (bool, error) { +// the run's outcome: whether it changed anything, provider plans included, +// and whether anything was failing; and runErr with any render failure +// joined to it, so neither is lost. changed is whether the report itself +// has changes. +func (c *Runner) renderReport(k *kong.Context, logger logging.Logger, runErr error, res Result, changed bool, warnings []dt.OutputWarning, renderDiff func(renderer.DiffOptions) error) (outcome, error) { opts := c.diffOptions(k) plans := c.providerPlans() if plans != nil { @@ -496,7 +516,7 @@ func (c *Runner) renderReport(k *kong.Context, logger logging.Logger, runErr err res.Notes = n } c.noteNoServers(k, n) - changed = changed || (plans != nil && res.Summary.Changes() > 0) + o := outcome{changed: changed || (plans != nil && res.Summary.Changes() > 0), failing: res.Failing()} var err error if c.Output == string(renderer.OutputFormatDiff) { @@ -506,9 +526,9 @@ func (c *Runner) renderReport(k *kong.Context, logger logging.Logger, runErr err } if err != nil { logger.Debug("Failed to render diffs", "error", err) - return changed, withRenderError(runErr, errors.Wrap(err, "failed to render diffs")) + return o, withRenderError(runErr, errors.Wrap(err, "failed to render diffs")) } - return changed, runErr + return o, runErr } // renderPlan writes res to stdout in the plan, json or yaml format, then diff --git a/internal/simulate/run_test.go b/internal/simulate/run_test.go index 645d6019..7f16be30 100644 --- a/internal/simulate/run_test.go +++ b/internal/simulate/run_test.go @@ -52,6 +52,7 @@ import ( "github.com/crossplane/cli/v2/internal/diff/testutils" difftypes "github.com/crossplane/cli/v2/internal/diff/types" "github.com/crossplane/cli/v2/internal/exitcode" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" xprender "github.com/crossplane/cli/v2/pkg/render" ) @@ -582,6 +583,19 @@ func TestSimulateRun(t *testing.T) { }}} } + // failing is a report of one Composition whose affected composite, in + // status, is failing before apply. + failing := func(status dt.XRStatus) *dt.CompDiffOutput { + return &dt.CompDiffOutput{Compositions: []dt.CompositionDiff{{ + Name: "comp", + ImpactAnalysis: []dt.XRImpact{{ + ObjectReference: corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "XDB", Name: "db"}, + Status: status, + Health: dt.CompositeHealth{XR: &health.Health{State: health.Failing, Synced: &health.Condition{Status: corev1.ConditionFalse}}}, + }}, + }}} + } + type args struct { cmd Runner initClients func(context.Context, logging.Logger) error @@ -616,6 +630,34 @@ func TestSimulateRun(t *testing.T) { }}}, want: want{code: dp.ExitCodeDiffDetected, diffed: true}, }, + "Failing": { + reason: "A run without changes whose composite is failing should exit 4, silently.", + args: args{initClients: noInit, proc: &fakeCompProc{diffFn: func(context.Context) (*dt.CompDiffOutput, error) { + return failing(dt.XRStatusUnchanged), nil + }}}, + want: want{code: ExitCodeFailing, diffed: true}, + }, + "ChangesAndFailing": { + reason: "A run with changes whose composite is failing should exit 7, silently.", + args: args{initClients: noInit, proc: &fakeCompProc{diffFn: func(context.Context) (*dt.CompDiffOutput, error) { + return failing(dt.XRStatusChanged), nil + }}}, + want: want{code: ExitCodeChanges | ExitCodeFailing, diffed: true}, + }, + "FailingDiffOutput": { + reason: "The failing bit should not depend on the output format.", + args: args{cmd: Runner{Flags: &Flags{Output: "diff"}}, initClients: noInit, proc: &fakeCompProc{diffFn: func(context.Context) (*dt.CompDiffOutput, error) { + return failing(dt.XRStatusUnchanged), nil + }}}, + want: want{code: ExitCodeFailing, diffed: true}, + }, + "ErrorOutranksFailing": { + reason: "A tool error should exit 1 even when a composite is failing.", + args: args{initClients: noInit, proc: &fakeCompProc{diffFn: func(context.Context) (*dt.CompDiffOutput, error) { + return failing(dt.XRStatusChanged), errBoom + }}}, + want: want{code: dp.ExitCodeToolError, err: errBoom, diffed: true}, + }, "ErrorWithChanges": { reason: "A tool error should exit 1, not 3, even when there are changes.", args: args{initClients: noInit, proc: &fakeCompProc{diffFn: func(context.Context) (*dt.CompDiffOutput, error) { diff --git a/internal/testutils/integration/simharness/cases.go b/internal/testutils/integration/simharness/cases.go index 4fe44816..8f2e07ff 100644 --- a/internal/testutils/integration/simharness/cases.go +++ b/internal/testutils/integration/simharness/cases.go @@ -76,7 +76,7 @@ type IntegrationTestCase struct { ExpectedError bool ExpectedErrorContains string ExpectedStderrContains []string // substrings that must appear on stderr (warnings, error lines) - ExpectedExitCode int // Expected exit code (0=success, 1=tool error, 2=schema validation, 3=diff detected) + ExpectedExitCode int // Expected exit code (0=success, 1=tool error, 2=schema validation; without an error, 3=diff detected, 4=failing resource, 7=both) NoColor bool Namespace string // For composition tests (optional) XrdAPIVersion integration.XRDAPIVersion // For XR tests (optional) From 023993510df546729a18c87dc243a323fedb3b1a Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 15:10:24 -0500 Subject: [PATCH 15/30] test(simulate): cover unhealthy XRs in xr, composition and project integration Fixtures carry Synced=False, Ready=False and paused. The cases assert the text output, the JSON health fields and summary.health, and exit codes 4 and 7. Under envtest no controller writes status, so the fixture loader now writes a fixture's status through the status subresource. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- .../composition/integration_test.go | 74 +++++++++++++++ .../project/integration_plan_output_test.go | 95 ++++++++++++++++++- cmd/crossplane/xr/integration_test.go | 82 ++++++++++++++++ internal/simulate/simtest/plan_assertions.go | 57 +++++++++++ .../simulate/simtest/structured_assertions.go | 36 +++++++ internal/testutils/integration/fixtures.go | 23 +++++ .../existing-downstream-1-not-ready.yaml | 18 ++++ .../existing-downstream-2-paused.yaml | 13 +++ .../comp/resources/existing-xr-1-failing.yaml | 19 ++++ ...xisting-downstream-resource-not-ready.yaml | 20 ++++ .../diff/resources/existing-xr-failing.yaml | 20 ++++ 11 files changed, 455 insertions(+), 2 deletions(-) create mode 100644 internal/testutils/integration/testdata/comp/resources/existing-downstream-1-not-ready.yaml create mode 100644 internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml create mode 100644 internal/testutils/integration/testdata/comp/resources/existing-xr-1-failing.yaml create mode 100644 internal/testutils/integration/testdata/diff/resources/existing-downstream-resource-not-ready.yaml create mode 100644 internal/testutils/integration/testdata/diff/resources/existing-xr-failing.yaml diff --git a/cmd/crossplane/composition/integration_test.go b/cmd/crossplane/composition/integration_test.go index 881cd4e0..1adfb572 100644 --- a/cmd/crossplane/composition/integration_test.go +++ b/cmd/crossplane/composition/integration_test.go @@ -300,6 +300,80 @@ Summary: 2 modified`, WithDownstreamSummary(0, 0, 1). WithDownstreamResource("removed", "XDownstreamResource", "resource-to-be-removed", "default"), }, + "UnhealthyXRWithChangesExits7": { + Reason: "A composition change on a failing XR prints the XR's health and its not-ready composed resource's in the impact analysis, then the Health: line, and exits 7: changes (3) and failing (4).", + SetupFiles: []string{ + "../../../internal/testutils/integration/testdata/comp/resources/xrd.yaml", + "../../../internal/testutils/integration/testdata/comp/resources/original-composition.yaml", + "../../../internal/testutils/integration/testdata/comp/resources/functions.yaml", + }, + CrossplaneManagedResources: []integration.HierarchicalOwnershipRelation{ + integration.Composes( + "../../../internal/testutils/integration/testdata/comp/resources/existing-xr-1-failing.yaml", + "../../../internal/testutils/integration/testdata/comp/resources/existing-downstream-1-not-ready.yaml", + ), + }, + InputFiles: []string{"../../../internal/testutils/integration/testdata/comp/updated-composition.yaml"}, + Namespace: "default", + NoColor: true, + ExpectedOutput: ` +=== Affected Composite Resources === + + ⚠ XNopResource/test-resource (namespace: default) ⚠ failing (Synced=False, ReconcileError): cannot compose resources: the vpc does not exist + +Summary: 1 resource with changes + +=== Impact Analysis === + +~~~ XDownstreamResource/test-resource ⚠ not ready (Ready=False, Creating) + apiVersion: ns.nop.example.org/v1alpha1 + kind: XDownstreamResource + metadata: + annotations: + crossplane.io/composition-resource-name: nop-resource + labels: + crossplane.io/composite: test-resource + name: test-resource + namespace: default + spec: + forProvider: +- configData: existing-value +- resourceTier: basic ++ configData: updated-existing-value ++ resourceTier: premium + +--- + +Summary: 1 modified + +Health: 1 of 1 XR unhealthy before apply; 1 resource not ready.`, + ExpectedExitCode: dp.ExitCodeDiffDetected | dp.ExitCodeFailing, + }, + "UnhealthyXRWithoutChangesExits4": { + Reason: "An unchanged composition analysed on a failing XR reports its health in -o json and exits 4: failing, no changes.", + SetupFiles: []string{ + "../../../internal/testutils/integration/testdata/comp/resources/xrd.yaml", + "../../../internal/testutils/integration/testdata/comp/resources/original-composition.yaml", + "../../../internal/testutils/integration/testdata/comp/resources/functions.yaml", + }, + CrossplaneManagedResources: []integration.HierarchicalOwnershipRelation{ + integration.Composes( + "../../../internal/testutils/integration/testdata/comp/resources/existing-xr-1-failing.yaml", + "../../../internal/testutils/integration/testdata/comp/resources/existing-downstream-1-not-ready.yaml", + ), + }, + InputFiles: []string{"testdata/comp/composition-no-changes.yaml"}, + Namespace: "default", + AnalyzeUnchanged: true, + OutputFormat: "json", + ExpectedExitCode: dp.ExitCodeFailing, + ExpectedStructured: simtest.Expect(). + WithHealthSummary(1, 0, 1, 0). + WithXRs(simtest.XR("XNopResource", "test-resource", "default"). + Status("unchanged"). + Health("failing"). + Unhealthy("XDownstreamResource", "test-resource", "notReady")), + }, "CompositionDiffIgnorePaths": { Reason: "Validates that ArgoCD annotations are ignored in composition diffs", SetupFiles: []string{ diff --git a/cmd/crossplane/project/integration_plan_output_test.go b/cmd/crossplane/project/integration_plan_output_test.go index 657d89a2..8d935203 100644 --- a/cmd/crossplane/project/integration_plan_output_test.go +++ b/cmd/crossplane/project/integration_plan_output_test.go @@ -27,6 +27,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + corev1 "k8s.io/api/core/v1" un "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/controller-runtime/pkg/client" @@ -77,6 +78,15 @@ func TestSimulatePlanOutput(t *testing.T) { // healthy is a live composite's health with no failing condition. healthy := &health.Health{State: health.Healthy} + // unhealthyCase is compCase with test-resource failing (Synced=False) and + // its composed resource not ready (Ready=False), and the composed resource + // of another-resource paused. + unhealthyCase := compCase + unhealthyCase.CrossplaneManagedResources = []integration.HierarchicalOwnershipRelation{ + integration.Composes("../../../internal/testutils/integration/testdata/comp/resources/existing-xr-1-failing.yaml", "../../../internal/testutils/integration/testdata/comp/resources/existing-downstream-1-not-ready.yaml"), + integration.Composes("../../../internal/testutils/integration/testdata/comp/resources/existing-xr-2.yaml", "../../../internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml"), + } + xrArgs := []string{"-f", "../../../internal/testutils/integration/testdata/diff/modified-xr.yaml"} compArgs := []string{"-f", "../../../internal/testutils/integration/testdata/comp/updated-composition.yaml", "--namespace=default"} @@ -89,6 +99,11 @@ func TestSimulatePlanOutput(t *testing.T) { // tt, when set, runs `xr simulate` with args against its cluster // instead of compCase's. tt *integration.Setup + // cluster, when set, replaces compCase's cluster for a project or + // composition run. + cluster *integration.Setup + // wantExitCode is the exit code; zero means dp.ExitCodeDiffDetected. + wantExitCode int // wantText is the -o plan output; empty for a json case. wantText string // wantResult is the -o json output, reduced by summarize. @@ -201,6 +216,70 @@ Note: Provider plan not run: no provider diff server planned these resources. }, }, }, + "UnhealthyPlanText": { + reason: "-o plan marks the failing XR's header and its not-ready and paused composed resources' plan entries, prints the Health: line after Plan:, and exits 7: changes (3) and failing (4).", + cluster: &unhealthyCase, + wantExitCode: dp.ExitCodeDiffDetected | dp.ExitCodeFailing, + wantText: `[~] Composition.apiextensions.crossplane.io xnopresources.diff.example.org (update) + Applying this creates a new CompositionRevision, which 2 composites would adopt. + spec: + pipeline[0]: + input: + inline: + ~ template: "apiVersion: ns.nop.example.org/v1alpha1\nkind: XDownstreamResource\nmetadata:\n name: {{ .observed.composite.resource.metadata.name }}\n namespace: {{ .observed.composite.resource.metadata.namespace }}\n annotations:\n gotemplating.fn.crossplane.io/composition-resource-name: nop-resource\nspec:\n forProvider:\n configData: {{ .observed.composite.resource.spec.coolField }}\n resourceTier: basic\n" -> "apiVersion: ns.nop.example.org/v1alpha1\nkind: XDownstreamResource\nmetadata:\n name: {{ .observed.composite.resource.metadata.name }}\n namespace: {{ .observed.composite.resource.metadata.namespace }}\n annotations:\n gotemplating.fn.crossplane.io/composition-resource-name: nop-resource\nspec:\n forProvider:\n configData: updated-{{ .observed.composite.resource.spec.coolField }}\n resourceTier: premium\n" + +XR: XNopResource.ns.diff.example.org default/another-resource + [~] XDownstreamResource.ns.nop.example.org default/another-resource (update) + ⚠ paused + metadata: + annotations: + - crossplane.io/paused: "true" + spec: + forProvider: + ~ configData: "another-existing-value" -> "updated-another-existing-value" + ~ resourceTier: "basic" -> "premium" + +XR: XNopResource.ns.diff.example.org default/test-resource ⚠ failing (Synced=False, ReconcileError): cannot compose resources: the vpc does not exist + [~] XDownstreamResource.ns.nop.example.org default/test-resource (update) + ⚠ not ready (Ready=False, Creating) + spec: + forProvider: + ~ configData: "existing-value" -> "updated-existing-value" + ~ resourceTier: "basic" -> "premium" + +Plan: 0 to add, 3 to change, 0 to destroy. +Health: 1 of 2 XRs unhealthy before apply; 1 resource not ready, 1 paused. + +Note: Provider plan not run: no provider diff server planned these resources. +`, + }, + "UnhealthyPlanJSON": { + reason: "-o json carries the failing XR's health and each unhealthy plan entry's, and exits 7.", + cluster: &unhealthyCase, + args: []string{"-o", "json"}, + wantExitCode: dp.ExitCodeDiffDetected | dp.ExitCodeFailing, + wantResult: &planShape{ + Summary: plan.Summary{Update: 3, NoOp: 2}, + Composites: []simulate.CompositeResult{ + {Ref: another, Status: simulate.CompositeChanged, Composition: &comp, Health: healthy}, + {Ref: xnop, Status: simulate.CompositeChanged, Composition: &comp, Health: &health.Health{ + State: health.Failing, + Synced: &health.Condition{Status: corev1.ConditionFalse, Reason: "ReconcileError", Message: "cannot compose resources: the vpc does not exist"}, + }}, + }, + Compositions: []simulate.CompositionResult{{ + Ref: comp, + RevisionImpact: &simulate.RevisionImpact{ChangeScope: "spec", CreatesRevision: true, RepointedComposites: 2}, + }}, + Plans: []planEntry{ + {Kind: "Composition", Name: comp.Name, Action: plan.ActionUpdate}, + {Kind: "XDownstreamResource", Name: "another-resource", Action: plan.ActionUpdate, Composite: "another-resource", Health: health.Paused}, + {Kind: "XNopResource", Name: "another-resource", Action: plan.ActionNoOp, Composite: "another-resource"}, + {Kind: "XDownstreamResource", Name: "test-resource", Action: plan.ActionUpdate, Composite: "test-resource", Health: health.NotReady}, + {Kind: "XNopResource", Name: "test-resource", Action: plan.ActionNoOp, Composite: "test-resource", Health: health.Failing}, + }, + }, + }, "NameRepeatedScopesToEach": { reason: "repeated --name with --namespace scopes the run to each named composite, as the unscoped run covers both.", args: []string{"-o", "json", "-n", "default", "--name=another-resource", "--name=test-resource"}, @@ -251,6 +330,9 @@ Note: Provider plan not run: no provider diff server planned these resources. if tc.tt != nil { setup = *tc.tt } + if tc.cluster != nil { + setup = *tc.cluster + } cfg := integration.Start(t, setup).Config var stdout, stderr bytes.Buffer @@ -265,8 +347,12 @@ Note: Provider plan not run: no provider diff server planned these resources. if err != nil { t.Fatalf("\n%s\nsimulate: unexpected error: %v\nstderr:\n%s", tc.reason, err, stderr.String()) } - if code != dp.ExitCodeDiffDetected { - t.Errorf("\n%s\nsimulate: want exit code %d, got %d", tc.reason, dp.ExitCodeDiffDetected, code) + wantCode := dp.ExitCodeDiffDetected + if tc.wantExitCode != 0 { + wantCode = tc.wantExitCode + } + if code != wantCode { + t.Errorf("\n%s\nsimulate: want exit code %d, got %d", tc.reason, wantCode, code) } if tc.wantResult == nil { @@ -305,6 +391,8 @@ type planEntry struct { Kind, Name string Action plan.Action Composite string + // Health is the live object's state when it is not healthy, else empty. + Health health.State } func summarize(r simulate.Result) planShape { @@ -314,6 +402,9 @@ func summarize(r simulate.Result) planShape { if p.Composite != nil { e.Composite = p.Composite.Name } + if p.Health.Unhealthy() { + e.Health = p.Health.State + } s.Plans = append(s.Plans, e) } // The revision's predicted name is a hash of the Composition's content. diff --git a/cmd/crossplane/xr/integration_test.go b/cmd/crossplane/xr/integration_test.go index c60bc0d3..544f96dd 100644 --- a/cmd/crossplane/xr/integration_test.go +++ b/cmd/crossplane/xr/integration_test.go @@ -563,6 +563,88 @@ Summary: 2 modified`, ExpectedError: false, ExpectedExitCode: dp.ExitCodeDiffDetected, }, + "UnhealthyXRWithChangesExits7": { + Reason: "A failing XR with changes prints its health and its not-ready composed resource's, then the Health: line, and exits 7: changes (3) and failing (4).", + SetupFiles: []string{ + "../../../internal/testutils/integration/testdata/diff/resources/xrd.yaml", + "../../../internal/testutils/integration/testdata/diff/resources/composition.yaml", + "../../../internal/testutils/integration/testdata/diff/resources/functions.yaml", + }, + CrossplaneManagedResources: []integration.HierarchicalOwnershipRelation{ + integration.Composes( + "../../../internal/testutils/integration/testdata/diff/resources/existing-xr-failing.yaml", + "../../../internal/testutils/integration/testdata/diff/resources/existing-downstream-resource-not-ready.yaml", + ), + }, + InputFiles: []string{"../../../internal/testutils/integration/testdata/diff/modified-xr.yaml"}, + NoColor: true, + ExpectedOutput: `XR: XNopResource/default/test-resource ⚠ failing (Synced=False, ReconcileError): cannot compose resources: the vpc does not exist + +~~~ XDownstreamResource/test-resource ⚠ not ready (Ready=False, Creating) + apiVersion: ns.nop.example.org/v1alpha1 + kind: XDownstreamResource + metadata: + annotations: + crossplane.io/composition-resource-name: nop-resource + labels: + crossplane.io/composite: test-resource + name: test-resource + namespace: default + spec: + forProvider: +- configData: existing-value ++ configData: modified-value + +--- +~~~ XNopResource/test-resource ⚠ failing (Synced=False, ReconcileError): cannot compose resources: the vpc does not exist + apiVersion: ns.diff.example.org/v1alpha1 + kind: XNopResource + metadata: + labels: + crossplane.io/composite: test-resource + name: test-resource + namespace: default + spec: +- coolField: existing-value ++ coolField: modified-value + crossplane: + compositionRef: + name: xnopresources.diff.example.org + compositionRevisionRef: + name: xnopresources.diff.example.org-b616215 + compositionUpdatePolicy: Automatic + +--- + +Summary: 2 modified + +Health: 1 of 1 XR unhealthy before apply; 1 resource not ready.`, + ExpectedExitCode: dp.ExitCodeDiffDetected | dp.ExitCodeFailing, + }, + "UnhealthyXRWithoutChangesExits4": { + Reason: "A failing XR without changes reports its health and its not-ready composed resource in -o json, and exits 4: failing, no changes.", + OutputFormat: "json", + SetupFiles: []string{ + "../../../internal/testutils/integration/testdata/diff/resources/xrd.yaml", + "../../../internal/testutils/integration/testdata/diff/resources/composition.yaml", + "../../../internal/testutils/integration/testdata/diff/resources/functions.yaml", + }, + CrossplaneManagedResources: []integration.HierarchicalOwnershipRelation{ + integration.Composes( + "../../../internal/testutils/integration/testdata/diff/resources/existing-xr-failing.yaml", + "../../../internal/testutils/integration/testdata/diff/resources/existing-downstream-resource-not-ready.yaml", + ), + }, + InputFiles: []string{"../../../internal/testutils/integration/testdata/diff/unmodified-xr.yaml"}, + ExpectedStructured: simtest.Expect(). + WithSummary(0, 0, 0). + WithHealthSummary(1, 0, 1, 0). + WithXRs(simtest.XR("XNopResource", "test-resource", "default"). + Status("unchanged"). + Health("failing"). + Unhealthy("XDownstreamResource", "test-resource", "notReady")), + ExpectedExitCode: dp.ExitCodeFailing, + }, "IgnorePathsArgoCD": { Reason: "Ignores ArgoCD annotations and labels when --ignore-paths is specified", SetupFiles: []string{ diff --git a/internal/simulate/simtest/plan_assertions.go b/internal/simulate/simtest/plan_assertions.go index e3582028..e40cb309 100644 --- a/internal/simulate/simtest/plan_assertions.go +++ b/internal/simulate/simtest/plan_assertions.go @@ -103,6 +103,61 @@ func AssertStructured(t *testing.T, jsonOutput string, e Expectation) { for _, want := range expected.compositions { assertComposition(t, res, want) } + + if want := expected.health; want != nil { + assertHealthSummary(t, jsonOutput, *want) + } +} + +// assertHealthSummary checks summary.health, which simulate.Result computes on marshal and so does not +// read back. +func assertHealthSummary(t *testing.T, jsonOutput string, want expectedHealth) { + t.Helper() + + var out struct { + Summary struct { + Health simulate.HealthSummary `json:"health"` + } `json:"summary"` + } + if err := json.Unmarshal([]byte(jsonOutput), &out); err != nil { + t.Fatalf("failed to parse summary.health: %v", err) + } + + got := out.Summary.Health + if g := (expectedHealth{composites: got.Composites, failing: got.Failing, notReady: got.NotReady, paused: got.Paused}); g != want { + t.Errorf("summary.health: expected %+v, got %+v", want, g) + } +} + +// assertXRHealth checks an xrs[] entry's health state and its unhealthy[] entries. +func assertXRHealth(t *testing.T, label string, got simulate.CompositeResult, want *XRExpectation) { + t.Helper() + + if want.health != "" { + state := "" + if got.Health != nil { + state = string(got.Health.State) + } + if state != want.health { + t.Errorf("xr %s: health: expected %q, got %q", label, want.health, state) + } + } + + if len(want.unhealthy) == 0 { + return + } + + gotU := make([]unhealthyExpectation, 0, len(got.Unhealthy)) + for _, u := range got.Unhealthy { + e := unhealthyExpectation{kind: u.Ref.Kind, name: u.Ref.Name} + if u.Health != nil { + e.state = string(u.Health.State) + } + gotU = append(gotU, e) + } + if !slices.Equal(gotU, want.unhealthy) { + t.Errorf("xr %s: unhealthy: expected %+v, got %+v", label, want.unhealthy, gotU) + } } func assertComposition(t *testing.T, res simulate.Result, want *CompositionExpectation) { @@ -357,6 +412,8 @@ func assertXRExpectations(t *testing.T, res simulate.Result, expected []*XRExpec t.Errorf("xr %s: status: expected %q, got %q", label, want.status, got.Status) } + assertXRHealth(t, label, got, want) + changes := changePlans(res.Plans, groupedUnder(got.Ref)) if s := want.summary; s != nil { diff --git a/internal/simulate/simtest/structured_assertions.go b/internal/simulate/simtest/structured_assertions.go index f59f9cbf..829898ca 100644 --- a/internal/simulate/simtest/structured_assertions.go +++ b/internal/simulate/simtest/structured_assertions.go @@ -30,6 +30,18 @@ type Expected struct { // noWarnings asserts warnings[] is empty. Separate from len(warnings)==0, which means "no opinion". noWarnings bool xrs []*XRExpectation + // health is summary.health, asserted when set. + health *expectedHealth +} + +// expectedHealth is summary.health's counts. +type expectedHealth struct { + composites, failing, notReady, paused int +} + +// unhealthyExpectation is one entry of an xrs[] entry's unhealthy[]. +type unhealthyExpectation struct { + kind, name, state string } // warningMatch describes one expected entry in warnings[], shared by the xr and comp builders. Matched @@ -436,6 +448,10 @@ type XRExpectation struct { summary *expectedSummary changes []*ResourceExpectation errorIDs []string + // health is the entry's live health state, asserted when set. + health string + // unhealthy pins the entry's unhealthy[] when set. + unhealthy []unhealthyExpectation } // XR begins a standalone expectation for one xrs[] entry, keyed by the input @@ -509,6 +525,26 @@ func (x *XRExpectation) Change(changeType, kind, name, namespace string) *Resour return r } +// WithHealthSummary asserts summary.health: the unhealthy top-level composites, and the failing, not +// ready and paused resources. +func (e *Expected) WithHealthSummary(composites, failing, notReady, paused int) *Expected { + e.health = &expectedHealth{composites: composites, failing: failing, notReady: notReady, paused: paused} + return e +} + +// Health asserts the entry's live health state (paused, failing, notReady or healthy). +func (x *XRExpectation) Health(state string) *XRExpectation { + x.health = state + return x +} + +// Unhealthy asserts the entry's unhealthy[] holds a resource of this kind and name in this health +// state. Pinning any pins the count too. +func (x *XRExpectation) Unhealthy(kind, name, state string) *XRExpectation { + x.unhealthy = append(x.unhealthy, unhealthyExpectation{kind: kind, name: name, state: state}) + return x +} + // WithErrorID asserts the top-level errors[] contains an error with the given resourceID. Unlike // WithError it pins neither the error's details nor the error count. func (e *Expected) WithErrorID(resourceID string) *Expected { diff --git a/internal/testutils/integration/fixtures.go b/internal/testutils/integration/fixtures.go index 3e065048..fe344b15 100644 --- a/internal/testutils/integration/fixtures.go +++ b/internal/testutils/integration/fixtures.go @@ -32,6 +32,7 @@ import ( authorizationv1 "k8s.io/api/authorization/v1" rbacv1 "k8s.io/api/rbac/v1" extv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/apimachinery/pkg/api/equality" apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" un "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -640,12 +641,34 @@ func createResourceFromFile(ctx context.Context, c client.Client, d *crossplaneD } } + if err := writeStatus(ctx, c, resource, serverResource); err != nil { + return nil, fmt.Errorf("%s: %w", path, err) + } + // Store and return the created resource createdResources[path] = serverResource return serverResource, nil } +// writeStatus writes want's status to server, the object as created from want, through the status +// subresource when creating it dropped the status, as it does for a type with that subresource, such as +// an XR. Under envtest no controller writes status, so this is how a fixture carries live conditions. +// server is updated in place. +func writeStatus(ctx context.Context, c client.Client, want, server *un.Unstructured) error { + status, ok := want.Object["status"] + if !ok || equality.Semantic.DeepEqual(status, server.Object["status"]) { + return nil + } + + server.Object["status"] = status + if err := c.Status().Update(ctx, server); err != nil { + return fmt.Errorf("cannot write status: %w", err) + } + + return nil +} + // withoutCompositeReconcilerFields returns a copy of resource without the compositeReconcilerFields it // sets, and those fields' values keyed by their index in compositeReconcilerFields. resource is not // mutated. diff --git a/internal/testutils/integration/testdata/comp/resources/existing-downstream-1-not-ready.yaml b/internal/testutils/integration/testdata/comp/resources/existing-downstream-1-not-ready.yaml new file mode 100644 index 00000000..596383ba --- /dev/null +++ b/internal/testutils/integration/testdata/comp/resources/existing-downstream-1-not-ready.yaml @@ -0,0 +1,18 @@ +# existing-downstream-1.yaml while its provider is still creating it: Ready=False, so it is not ready. +apiVersion: ns.nop.example.org/v1alpha1 +kind: XDownstreamResource +metadata: + name: test-resource + namespace: default + annotations: + crossplane.io/composition-resource-name: nop-resource +spec: + forProvider: + configData: existing-value + resourceTier: basic +status: + conditions: + - type: Ready + status: "False" + reason: Creating + lastTransitionTime: "2024-01-01T00:00:00Z" diff --git a/internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml b/internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml new file mode 100644 index 00000000..18511b25 --- /dev/null +++ b/internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml @@ -0,0 +1,13 @@ +# existing-downstream-2.yaml with reconciliation paused, so it is paused. +apiVersion: ns.nop.example.org/v1alpha1 +kind: XDownstreamResource +metadata: + name: another-resource + namespace: default + annotations: + crossplane.io/composition-resource-name: nop-resource + crossplane.io/paused: "true" +spec: + forProvider: + configData: another-existing-value + resourceTier: basic diff --git a/internal/testutils/integration/testdata/comp/resources/existing-xr-1-failing.yaml b/internal/testutils/integration/testdata/comp/resources/existing-xr-1-failing.yaml new file mode 100644 index 00000000..b1a2fdca --- /dev/null +++ b/internal/testutils/integration/testdata/comp/resources/existing-xr-1-failing.yaml @@ -0,0 +1,19 @@ +# existing-xr-1.yaml as Crossplane reports it once its composed resources cannot sync: Synced=False, +# so the XR is failing. Fixture setup writes the status through the status subresource. +apiVersion: ns.diff.example.org/v1alpha1 +kind: XNopResource +metadata: + name: test-resource + namespace: default +spec: + coolField: existing-value + crossplane: + compositionRef: + name: xnopresources.diff.example.org +status: + conditions: + - type: Synced + status: "False" + reason: ReconcileError + message: "cannot compose resources: the vpc does not exist" + lastTransitionTime: "2024-01-01T00:00:00Z" diff --git a/internal/testutils/integration/testdata/diff/resources/existing-downstream-resource-not-ready.yaml b/internal/testutils/integration/testdata/diff/resources/existing-downstream-resource-not-ready.yaml new file mode 100644 index 00000000..9421bab8 --- /dev/null +++ b/internal/testutils/integration/testdata/diff/resources/existing-downstream-resource-not-ready.yaml @@ -0,0 +1,20 @@ +# existing-downstream-resource.yaml while its provider is still creating it: Ready=False, so it is +# not ready. +apiVersion: ns.nop.example.org/v1alpha1 +kind: XDownstreamResource +metadata: + annotations: + crossplane.io/composition-resource-name: nop-resource + labels: + crossplane.io/composite: test-resource + name: test-resource + namespace: default +spec: + forProvider: + configData: existing-value +status: + conditions: + - type: Ready + status: "False" + reason: Creating + lastTransitionTime: "2024-01-01T00:00:00Z" diff --git a/internal/testutils/integration/testdata/diff/resources/existing-xr-failing.yaml b/internal/testutils/integration/testdata/diff/resources/existing-xr-failing.yaml new file mode 100644 index 00000000..0fa35e38 --- /dev/null +++ b/internal/testutils/integration/testdata/diff/resources/existing-xr-failing.yaml @@ -0,0 +1,20 @@ +# existing-xr.yaml as Crossplane reports it once its composed resources cannot sync: Synced=False, so +# the XR is failing. Fixture setup writes the status through the status subresource. +apiVersion: ns.diff.example.org/v1alpha1 +kind: XNopResource +metadata: + name: test-resource + namespace: default +spec: + coolField: "existing-value" + crossplane: + compositionRef: + name: xnopresources.diff.example.org + compositionUpdatePolicy: Automatic +status: + conditions: + - type: Synced + status: "False" + reason: ReconcileError + message: "cannot compose resources: the vpc does not exist" + lastTransitionTime: "2024-01-01T00:00:00Z" From 86ed91d4664d78f773f310948b885e6ca93099e0 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 16:59:19 -0500 Subject: [PATCH 16/30] test(simulate): apply the paused annotation under a user field manager The paused fixture carried crossplane.io/paused under the Crossplane field manager, so the dry-run apply removed it and the plan golden showed the annotation being dropped. Apply it under kubectl instead, as a user would. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- .../project/integration_plan_output_test.go | 8 ++++---- .../existing-downstream-2-paused-annotation.yaml | 9 +++++++++ .../resources/existing-downstream-2-paused.yaml | 13 ------------- 3 files changed, 13 insertions(+), 17 deletions(-) create mode 100644 internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused-annotation.yaml delete mode 100644 internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml diff --git a/cmd/crossplane/project/integration_plan_output_test.go b/cmd/crossplane/project/integration_plan_output_test.go index 8d935203..fc8d13ca 100644 --- a/cmd/crossplane/project/integration_plan_output_test.go +++ b/cmd/crossplane/project/integration_plan_output_test.go @@ -84,7 +84,10 @@ func TestSimulatePlanOutput(t *testing.T) { unhealthyCase := compCase unhealthyCase.CrossplaneManagedResources = []integration.HierarchicalOwnershipRelation{ integration.Composes("../../../internal/testutils/integration/testdata/comp/resources/existing-xr-1-failing.yaml", "../../../internal/testutils/integration/testdata/comp/resources/existing-downstream-1-not-ready.yaml"), - integration.Composes("../../../internal/testutils/integration/testdata/comp/resources/existing-xr-2.yaml", "../../../internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml"), + integration.Composes("../../../internal/testutils/integration/testdata/comp/resources/existing-xr-2.yaml", "../../../internal/testutils/integration/testdata/comp/resources/existing-downstream-2.yaml"), + } + unhealthyCase.FieldManagerApplies = []integration.FieldManagerApply{ + {File: "../../../internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused-annotation.yaml", FieldManager: "kubectl"}, } xrArgs := []string{"-f", "../../../internal/testutils/integration/testdata/diff/modified-xr.yaml"} @@ -231,9 +234,6 @@ Note: Provider plan not run: no provider diff server planned these resources. XR: XNopResource.ns.diff.example.org default/another-resource [~] XDownstreamResource.ns.nop.example.org default/another-resource (update) ⚠ paused - metadata: - annotations: - - crossplane.io/paused: "true" spec: forProvider: ~ configData: "another-existing-value" -> "updated-another-existing-value" diff --git a/internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused-annotation.yaml b/internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused-annotation.yaml new file mode 100644 index 00000000..568bd163 --- /dev/null +++ b/internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused-annotation.yaml @@ -0,0 +1,9 @@ +# Pauses existing-downstream-2.yaml's resource. Applied under a non-Crossplane field manager, as a +# user or tool would, so Crossplane's dry-run apply does not remove the annotation. +apiVersion: ns.nop.example.org/v1alpha1 +kind: XDownstreamResource +metadata: + name: another-resource + namespace: default + annotations: + crossplane.io/paused: "true" diff --git a/internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml b/internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml deleted file mode 100644 index 18511b25..00000000 --- a/internal/testutils/integration/testdata/comp/resources/existing-downstream-2-paused.yaml +++ /dev/null @@ -1,13 +0,0 @@ -# existing-downstream-2.yaml with reconciliation paused, so it is paused. -apiVersion: ns.nop.example.org/v1alpha1 -kind: XDownstreamResource -metadata: - name: another-resource - namespace: default - annotations: - crossplane.io/composition-resource-name: nop-resource - crossplane.io/paused: "true" -spec: - forProvider: - configData: another-existing-value - resourceTier: basic From 63b411e38bf68187e31b3a092843f6a9f255b9f3 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 16:59:19 -0500 Subject: [PATCH 17/30] fix(diff): print an unhealthy XR's message once in diff output When the XR's health line is printed for its group, the XR's own diff entry carries only its tier marker, without the condition and message again. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- cmd/crossplane/xr/integration_test.go | 2 +- internal/diff/renderer/diff_renderer.go | 25 ++++++++++++----- internal/diff/renderer/diff_renderer_test.go | 2 +- internal/diff/renderer/health_test.go | 29 ++++++++++++++++++++ 4 files changed, 49 insertions(+), 9 deletions(-) diff --git a/cmd/crossplane/xr/integration_test.go b/cmd/crossplane/xr/integration_test.go index 544f96dd..8f937857 100644 --- a/cmd/crossplane/xr/integration_test.go +++ b/cmd/crossplane/xr/integration_test.go @@ -596,7 +596,7 @@ Summary: 2 modified`, + configData: modified-value --- -~~~ XNopResource/test-resource ⚠ failing (Synced=False, ReconcileError): cannot compose resources: the vpc does not exist +~~~ XNopResource/test-resource ⚠ failing apiVersion: ns.diff.example.org/v1alpha1 kind: XNopResource metadata: diff --git a/internal/diff/renderer/diff_renderer.go b/internal/diff/renderer/diff_renderer.go index ed30fd0e..a8df1110 100644 --- a/internal/diff/renderer/diff_renderer.go +++ b/internal/diff/renderer/diff_renderer.go @@ -83,8 +83,10 @@ type diffCounts struct { // renderDiffList renders a flat set of diffs (sorted by kind/name) to stdout in // the human-readable +++/~~~/--- form and returns the per-type counts. It does // not print a summary; callers decide whether to emit a per-section or -// aggregate summary from the returned counts. Equal diffs are skipped. -func (r *DefaultDiffRenderer) renderDiffList(diffs map[string]*dt.ResourceDiff) (diffCounts, error) { +// aggregate summary from the returned counts. Equal diffs are skipped. The +// diff whose diffKey is described, an XR whose health line was already +// printed, is marked with its tier alone. +func (r *DefaultDiffRenderer) renderDiffList(diffs map[string]*dt.ResourceDiff, described string) (diffCounts, error) { stdout := r.diffOpts.Stdout // Sort by GetKindName, which is how it's displayed to the user. The same kind and name in two @@ -133,6 +135,11 @@ func (r *DefaultDiffRenderer) renderDiffList(diffs map[string]*dt.ResourceDiff) // A resource the cluster has is marked when it is not healthy. if desc := healthDescription(health.Of(diff.Current.Raw), r.diffOpts.FullMessages); len(desc) > 0 { + if described != "" && diffKey(diff) == described { + tier, _, _ := strings.Cut(desc[0], " (") + desc = []string{tier} + } + header = strings.Join(append([]string{header + " " + desc[0]}, desc[1:]...), "\n") } @@ -284,8 +291,8 @@ func (r *DefaultDiffRenderer) renderFlat(diffs map[string]*dt.ResourceDiff, xrGr full := r.diffOpts.FullMessages var ( - head, block string - t health.Tally + head, block, described string + t health.Tally ) if len(xrGroups) == 1 { @@ -293,6 +300,7 @@ func (r *DefaultDiffRenderer) renderFlat(diffs map[string]*dt.ResourceDiff, xrGr if desc := healthDescription(g.Health.XR, full); len(desc) > 0 { desc[0] = "XR: " + groupTitle(g) + " " + desc[0] head = joinLines(desc) + "\n" + described = refKey(g.XR) } block = joinLines(unhealthyBlock(g.Health, g.Diffs, full)) @@ -303,7 +311,7 @@ func (r *DefaultDiffRenderer) renderFlat(diffs map[string]*dt.ResourceDiff, xrGr return err } - counts, err := r.renderDiffList(diffs) + counts, err := r.renderDiffList(diffs, described) if err != nil { return err } @@ -376,7 +384,7 @@ func (r *DefaultDiffRenderer) renderGrouped(groups []dt.XRDiffGroup) error { // owning XR to head a section, so fold its diffs into the aggregate // without a header or per-section summary. if !hasIdentity(g) { - counts, err := r.renderDiffList(g.Diffs) + counts, err := r.renderDiffList(g.Diffs, "") if err != nil { return err } @@ -423,10 +431,13 @@ func (r *DefaultDiffRenderer) renderSection(g dt.XRDiffGroup) (diffCounts, error return diffCounts{}, errors.Wrap(err, "failed to write XR section header") } + described := "" if s := joinLines(healthDescription(g.Health.XR, r.diffOpts.FullMessages)); s != "" { if err := write(stdout, s+"\n"); err != nil { return diffCounts{}, err } + + described = refKey(g.XR) } if g.Err != nil { @@ -437,7 +448,7 @@ func (r *DefaultDiffRenderer) renderSection(g dt.XRDiffGroup) (diffCounts, error return diffCounts{}, nil } - counts, err := r.renderDiffList(g.Diffs) + counts, err := r.renderDiffList(g.Diffs, described) if err != nil { return counts, err } diff --git a/internal/diff/renderer/diff_renderer_test.go b/internal/diff/renderer/diff_renderer_test.go index 46630026..2de6315e 100644 --- a/internal/diff/renderer/diff_renderer_test.go +++ b/internal/diff/renderer/diff_renderer_test.go @@ -485,7 +485,7 @@ func TestDefaultDiffRenderer_renderDiffList_SameKindNameDifferentNamespaces(t *t opts := DefaultDiffOptions() opts.Stdout, opts.Stderr = &stdout, &bytes.Buffer{} - if _, err := NewDiffRenderer(tu.TestLogger(t, false), opts).(*DefaultDiffRenderer).renderDiffList(diffs); err != nil { + if _, err := NewDiffRenderer(tu.TestLogger(t, false), opts).(*DefaultDiffRenderer).renderDiffList(diffs, ""); err != nil { t.Fatalf("renderDiffList(): %v", err) } diff --git a/internal/diff/renderer/health_test.go b/internal/diff/renderer/health_test.go index 6f62294c..eda6a639 100644 --- a/internal/diff/renderer/health_test.go +++ b/internal/diff/renderer/health_test.go @@ -139,6 +139,35 @@ Health: 1 of 2 XRs unhealthy before apply; 1 resource not ready. Summary: 1 modified Health: 1 resource failing (Synced=False). +`, + }, + "XRMessageOnce": { + reason: "An unhealthy XR's message prints once, on its XR: line, with full messages too, and its own diff entry carries only its tier; a composed resource keeps its whole marker.", + args: args{full: true, groups: []dt.XRDiffGroup{group("net", failing, nil, + liveWith(modifiedDiffFor("XNetwork", "net"), "vpc does not exist"), + liveWith(modifiedDiffFor("Bucket", "b"), "boom"))}}, + want: `XR: XNetwork/net ⚠ failing (Synced=False, ReconcileError) + vpc does not exist + +~~~ Bucket/b ⚠ failing (Synced=False, ReconcileError) + boom +- spec: +- field: old ++ spec: ++ field: new + +--- +~~~ XNetwork/net ⚠ failing +- spec: +- field: old ++ spec: ++ field: new + +--- + +Summary: 2 modified + +Health: 1 of 1 XR unhealthy before apply; 1 resource failing (Synced=False). `, }, } From 71b214f57bc3804996edc98abb756c9a87ea5fc9 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Wed, 18 Nov 2026 17:06:30 -0500 Subject: [PATCH 18/30] refactor(health): name a health's tier without parsing its line Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/diff/renderer/diff_renderer.go | 6 ++-- internal/health/format.go | 44 ++++++++++++++++++------- internal/health/format_test.go | 39 ++++++++++++++++++++++ 3 files changed, 74 insertions(+), 15 deletions(-) diff --git a/internal/diff/renderer/diff_renderer.go b/internal/diff/renderer/diff_renderer.go index a8df1110..750acd39 100644 --- a/internal/diff/renderer/diff_renderer.go +++ b/internal/diff/renderer/diff_renderer.go @@ -134,10 +134,10 @@ func (r *DefaultDiffRenderer) renderDiffList(diffs map[string]*dt.ResourceDiff, } // A resource the cluster has is marked when it is not healthy. - if desc := healthDescription(health.Of(diff.Current.Raw), r.diffOpts.FullMessages); len(desc) > 0 { + h := health.Of(diff.Current.Raw) + if desc := healthDescription(h, r.diffOpts.FullMessages); len(desc) > 0 { if described != "" && diffKey(diff) == described { - tier, _, _ := strings.Cut(desc[0], " (") - desc = []string{tier} + desc = []string{health.Tier(h)} } header = strings.Join(append([]string{header + " " + desc[0]}, desc[1:]...), "\n") diff --git a/internal/health/format.go b/internal/health/format.go index 0b3ae866..1476548d 100644 --- a/internal/health/format.go +++ b/internal/health/format.go @@ -41,6 +41,33 @@ const ( wrapRunes = 100 ) +// Tier returns the marker and the tier of h alone, e.g. "⚠ failing", or "" when +// h is nil or healthy. +func Tier(h *Health) string { + if !h.Unhealthy() { + return "" + } + + name, _, _ := tier(h) + + return Marker + " " + name +} + +// tier returns the name of h's tier, and the condition it was read from as +// "Type=Status" with that condition. A paused object has no condition, as its +// conditions are stale. +func tier(h *Health) (name, cond string, c *Condition) { + switch h.State { + case Failing: + return "failing", "Synced=False", h.Synced + case NotReady: + return "not ready", "Ready=False", h.Ready + case Paused, Healthy: + } + + return "paused", "", nil +} + // Lines describes h for text output, or returns nil when h is nil or healthy. // The first line is the marker, the tier, the condition it was read from with // that condition's reason, and its message cut to one line, e.g. @@ -52,20 +79,13 @@ func Lines(h *Health, full bool) []string { return nil } - var ( - line string - c *Condition - ) - - switch h.State { - case Failing: - line, c = Marker+" failing (Synced=False", h.Synced - case NotReady: - line, c = Marker+" not ready (Ready=False", h.Ready - case Paused, Healthy: - return []string{Marker + " paused"} + name, cond, c := tier(h) + if cond == "" { + return []string{Marker + " " + name} } + line := Marker + " " + name + " (" + cond + if c == nil { return []string{line + ")"} } diff --git a/internal/health/format_test.go b/internal/health/format_test.go index 1b3764af..3d44d86f 100644 --- a/internal/health/format_test.go +++ b/internal/health/format_test.go @@ -102,6 +102,45 @@ func TestLines(t *testing.T) { } } +func TestTier(t *testing.T) { + cases := map[string]struct { + reason string + h *Health + want string + }{ + "Nil": { + reason: "No live object has no tier.", + }, + "Healthy": { + reason: "A healthy object has no tier.", + h: &Health{State: Healthy}, + }, + "Failing": { + reason: "A failing object is named by its tier alone, without condition, reason or message.", + h: &Health{State: Failing, Synced: &Condition{Status: "False", Reason: "ReconcileError", Message: "cannot update"}}, + want: "⚠ failing", + }, + "NotReady": { + reason: "A not-ready object is named by its tier alone.", + h: &Health{State: NotReady, Ready: &Condition{Status: "False", Reason: "Creating"}}, + want: "⚠ not ready", + }, + "Paused": { + reason: "A paused object is named by its tier.", + h: &Health{State: Paused}, + want: "⚠ paused", + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + if diff := cmp.Diff(tc.want, Tier(tc.h)); diff != "" { + t.Errorf("\n%s\nTier(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + func TestBlock(t *testing.T) { failing := &Health{State: Failing, Synced: &Condition{Status: "False", Reason: "ReconcileError", Message: "boom"}} notReady := &Health{State: NotReady, Ready: &Condition{Status: "False", Reason: "Creating"}} From 04878ef8777416c765fa09d2c2ad162df6262d68 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 14:02:48 -0500 Subject: [PATCH 19/30] docs(simulate): contract 'that is' in the exit help Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/simulate/help/exit.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/simulate/help/exit.md b/internal/simulate/help/exit.md index 255f60bd..ca71a130 100644 --- a/internal/simulate/help/exit.md +++ b/internal/simulate/help/exit.md @@ -14,7 +14,7 @@ the result: plan found that the cluster diff didn't. - 4: the simulation succeeded, found no changes, and a composite resource or a resource it composes was failing (Synced=False) before apply, including - one the change would delete. A resource that is only not ready, or is + one the change would delete. A resource that's only not ready, or is paused, doesn't set it. - 7: the simulation succeeded, found changes, and a resource was failing: 3 and 4 combined. From 693cc46c7caf4dca8ac0e0e888f246e395b5e197 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 16:21:57 -0500 Subject: [PATCH 20/30] fix(diff): list a nested XR's unhealthy resources once in composition diff A nested XR with its own impact and health listed what it composes, and its parent listed the same resources again. Share the plan output's rule as health.OwnedElsewhere, used by both the simulate assembler and the composition diff renderer across every composition in the run: each resource is listed under the most deeply nested composite entry that has health of its own. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/diff/renderer/comp_diff_renderer.go | 45 ++++++++++++++++---- internal/diff/renderer/diff_renderer.go | 4 +- internal/diff/renderer/health.go | 6 +-- internal/diff/renderer/health_test.go | 41 ++++++++++++++++++ internal/health/format.go | 45 ++++++++++++++++++++ internal/simulate/report.go | 41 ++++-------------- 6 files changed, 136 insertions(+), 46 deletions(-) diff --git a/internal/diff/renderer/comp_diff_renderer.go b/internal/diff/renderer/comp_diff_renderer.go index 050cca6b..be3b5555 100644 --- a/internal/diff/renderer/comp_diff_renderer.go +++ b/internal/diff/renderer/comp_diff_renderer.go @@ -66,6 +66,7 @@ func NewDefaultCompDiffRenderer(logger logging.Logger, diffRenderer DiffRenderer // to r.opts.Stdout. Top-level errors are not printed; see CompDiffRenderer. func (r *DefaultCompDiffRenderer) RenderCompDiff(output *dt.CompDiffOutput) error { stdout := r.opts.Stdout + ownedElsewhere := unhealthyOwnedElsewhere(output.Compositions) for i, comp := range output.Compositions { if i > 0 { @@ -74,7 +75,7 @@ func (r *DefaultCompDiffRenderer) RenderCompDiff(output *dt.CompDiffOutput) erro } } - if err := r.renderComposition(&comp); err != nil { + if err := r.renderComposition(&comp, ownedElsewhere); err != nil { return err } } @@ -103,7 +104,7 @@ func (r *DefaultCompDiffRenderer) RenderCompDiff(output *dt.CompDiffOutput) erro // followed — only if it processed successfully — by the sections describing its effect on composites. // A composition that failed to process has no such sections to show; renderCompositionChanges // reported the error in their place. -func (r *DefaultCompDiffRenderer) renderComposition(comp *dt.CompositionDiff) error { +func (r *DefaultCompDiffRenderer) renderComposition(comp *dt.CompositionDiff, ownedElsewhere map[string]map[string]bool) error { switch { case r.opts.MinimizeComposition: if err := r.renderMinimizedCompositionChanges(comp); err != nil { @@ -116,7 +117,7 @@ func (r *DefaultCompDiffRenderer) renderComposition(comp *dt.CompositionDiff) er } if comp.Error == nil { - return r.renderCompositionImpact(comp) + return r.renderCompositionImpact(comp, ownedElsewhere) } return nil @@ -125,7 +126,7 @@ func (r *DefaultCompDiffRenderer) renderComposition(comp *dt.CompositionDiff) er // renderCompositionImpact renders the sections describing how a composition affects its composites: // either the note standing in for them when their evaluation was skipped, or the affected-XR list and // the downstream impact analysis. -func (r *DefaultCompDiffRenderer) renderCompositionImpact(comp *dt.CompositionDiff) error { +func (r *DefaultCompDiffRenderer) renderCompositionImpact(comp *dt.CompositionDiff, ownedElsewhere map[string]map[string]bool) error { if comp.ImpactAnalysisSkipped { return r.renderSkippedComposition(comp) } @@ -142,7 +143,7 @@ func (r *DefaultCompDiffRenderer) renderCompositionImpact(comp *dt.CompositionDi } // Render impact analysis (downstream diffs) - return r.renderImpactAnalysis(comp) + return r.renderImpactAnalysis(comp, ownedElsewhere) } // renderSkippedComposition stands in for the affected-XR and impact-analysis sections when a @@ -419,7 +420,7 @@ func (r *DefaultCompDiffRenderer) renderAffectedResourcesList(comp *dt.Compositi } // renderImpactAnalysis renders the impact analysis section with downstream diffs. -func (r *DefaultCompDiffRenderer) renderImpactAnalysis(comp *dt.CompositionDiff) error { +func (r *DefaultCompDiffRenderer) renderImpactAnalysis(comp *dt.CompositionDiff, ownedElsewhere map[string]map[string]bool) error { stdout := r.opts.Stdout if _, err := fmt.Fprintf(stdout, headerImpactAnalysis+"\n\n"); err != nil { @@ -454,18 +455,18 @@ func (r *DefaultCompDiffRenderer) renderImpactAnalysis(comp *dt.CompositionDiff) } } - return r.renderUnhealthyBlocks(comp, len(allDiffs) > 0) + return r.renderUnhealthyBlocks(comp, ownedElsewhere, len(allDiffs) > 0) } // renderUnhealthyBlocks writes, for each composite the composition affects, // the resources it composes that are not healthy and have no changes, under a // line naming the composite. afterDiffs separates the first from the diffs // above it. -func (r *DefaultCompDiffRenderer) renderUnhealthyBlocks(comp *dt.CompositionDiff, afterDiffs bool) error { +func (r *DefaultCompDiffRenderer) renderUnhealthyBlocks(comp *dt.CompositionDiff, ownedElsewhere map[string]map[string]bool, afterDiffs bool) error { var sb strings.Builder for _, impact := range listedImpacts(comp) { - block := unhealthyBlock(impact.Health, impact.Diffs, r.opts.FullMessages) + block := unhealthyBlock(impact.Health, impact.Diffs, ownedElsewhere[refKey(impact.ObjectReference)], r.opts.FullMessages) if len(block) == 0 { continue } @@ -484,6 +485,32 @@ func (r *DefaultCompDiffRenderer) renderUnhealthyBlocks(comp *dt.CompositionDiff return write(r.opts.Stdout, sb.String()) } +// unhealthyOwnedElsewhere returns, by the key of each composite the run lists, +// the keys its unhealthy block skips: a nested XR the run lists with health +// of its own, in any composition, lists what it composes, so its parent lists +// neither, as in plan output. +func unhealthyOwnedElsewhere(comps []dt.CompositionDiff) map[string]map[string]bool { + var entries []health.Entry + + for i := range comps { + for _, impact := range listedImpacts(&comps[i]) { + e := health.Entry{Key: refKey(impact.ObjectReference), HasHealth: impact.Health.XR != nil} + for _, c := range impact.Health.Composed { + e.Composes = append(e.Composes, refKey(c.ObjectReference)) + } + + entries = append(entries, e) + } + } + + out := map[string]map[string]bool{} + for i, skip := range health.OwnedElsewhere(entries) { + out[entries[i].Key] = skip + } + + return out +} + // impactTitle names an affected composite, e.g. "XNetwork/prod/net", as the // xr diff names its sections. func impactTitle(impact dt.XRImpact) string { diff --git a/internal/diff/renderer/diff_renderer.go b/internal/diff/renderer/diff_renderer.go index 750acd39..eb58a453 100644 --- a/internal/diff/renderer/diff_renderer.go +++ b/internal/diff/renderer/diff_renderer.go @@ -303,7 +303,7 @@ func (r *DefaultDiffRenderer) renderFlat(diffs map[string]*dt.ResourceDiff, xrGr described = refKey(g.XR) } - block = joinLines(unhealthyBlock(g.Health, g.Diffs, full)) + block = joinLines(unhealthyBlock(g.Health, g.Diffs, nil, full)) tallyComposite(&t, g.XR, g.Health, g.Diffs) } @@ -453,7 +453,7 @@ func (r *DefaultDiffRenderer) renderSection(g dt.XRDiffGroup) (diffCounts, error return counts, err } - block := joinLines(unhealthyBlock(g.Health, g.Diffs, r.diffOpts.FullMessages)) + block := joinLines(unhealthyBlock(g.Health, g.Diffs, nil, r.diffOpts.FullMessages)) if counts.output == 0 { if _, err := fmt.Fprintf(stdout, "No changes.\n\n"); err != nil { diff --git a/internal/diff/renderer/health.go b/internal/diff/renderer/health.go index 61eb9972..22740c6a 100644 --- a/internal/diff/renderer/health.go +++ b/internal/diff/renderer/health.go @@ -31,8 +31,8 @@ func refName(ref corev1.ObjectReference) string { // unhealthyBlock returns the lines listing what a composite composes that is // not healthy and has no changes in diffs, or nil when there is nothing. A // resource a nested composite composes is nested under that composite, as in -// plan output. -func unhealthyBlock(h dt.CompositeHealth, diffs map[string]*dt.ResourceDiff, full bool) []string { +// plan output. It skips the keys in skip, which another entry lists. +func unhealthyBlock(h dt.CompositeHealth, diffs map[string]*dt.ResourceDiff, skip map[string]bool, full bool) []string { listed := map[string]string{} for _, d := range diffs { @@ -45,7 +45,7 @@ func unhealthyBlock(h dt.CompositeHealth, diffs map[string]*dt.ResourceDiff, ful for _, r := range h.Composed { id := refKey(r.ObjectReference) - if !r.Health.Unhealthy() || listed[id] != "" { + if !r.Health.Unhealthy() || listed[id] != "" || skip[id] { continue } diff --git a/internal/diff/renderer/health_test.go b/internal/diff/renderer/health_test.go index eda6a639..a2efd389 100644 --- a/internal/diff/renderer/health_test.go +++ b/internal/diff/renderer/health_test.go @@ -196,7 +196,11 @@ Health: 1 of 1 XR unhealthy before apply; 1 resource failing (Synced=False). func TestRenderCompDiffHealth(t *testing.T) { failing := &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: "boom"}} notReady := &health.Health{State: health.NotReady, Ready: &health.Condition{Status: "False", Reason: "Creating"}} + healthy := &health.Health{State: health.Healthy} rt := corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "RouteTable", Name: "rt"} + p := corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "XBucket", Name: "p"} + c := corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "XBucket", Name: "c"} + sn := corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "Subnet", Name: "sn"} type args struct { impacts []dt.XRImpact @@ -239,6 +243,43 @@ XR: XBucket/ns/a RouteTable/rt ⚠ not ready (Ready=False, Creating) Health: 1 of 2 XRs unhealthy before apply; 1 resource not ready. +`, + }, + "NestedListsItsOwn": { + reason: "A nested XR listed with health of its own lists what it composes; its parent lists neither, so each resource is listed once, as in plan output.", + args: args{impacts: []dt.XRImpact{ + { + ObjectReference: p, + Status: dt.XRStatusUnchanged, + Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{{ObjectReference: c, Health: notReady}, {ObjectReference: sn, Via: &c, Health: failing}}}, + }, + { + ObjectReference: c, + Status: dt.XRStatusUnchanged, + ComposedBy: &p, + Health: dt.CompositeHealth{XR: notReady, Composed: []dt.ResourceHealth{{ObjectReference: sn, Health: failing}}}, + }, + }}, + want: `=== Composition Changes === + +No changes detected in composition xbuckets.example.org + +=== Affected Composite Resources === + + ✓ XBucket/p (cluster-scoped) + ✓ XBucket/c (cluster-scoped) ⚠ not ready (Ready=False, Creating) + +Summary: 2 resources unchanged + +=== Impact Analysis === + +All composite resources are up-to-date. No downstream resource changes detected. + +XR: XBucket/c + Unhealthy resources without changes: + Subnet/sn ⚠ failing (Synced=False, ReconcileError): boom + +Health: 1 of 2 XRs unhealthy before apply; 1 resource failing (Synced=False). `, }, } diff --git a/internal/health/format.go b/internal/health/format.go index 1476548d..20837c39 100644 --- a/internal/health/format.go +++ b/internal/health/format.go @@ -18,6 +18,7 @@ package health import ( "fmt" + "maps" "strings" "unicode/utf8" @@ -386,3 +387,47 @@ func SummaryLine(c Counts) string { return "Health: " + strings.Join(parts, "; ") + "." } + +// Entry is a composite entry in a run's output, for OwnedElsewhere: its Key, +// whether it has health of its own, and the Keys of what it composes. +type Entry struct { + Key string + HasHealth bool + Composes []string +} + +// OwnedElsewhere returns, for each of entries, the Keys it must not list +// because a nested composite entry with health of its own lists them: that +// nested composite, and what it composes. So a resource is listed under the +// most deeply nested composite entry that composes it, once. An entry without +// health is listed by its parent, as a resource. +func OwnedElsewhere(entries []Entry) []map[string]bool { + composes := make([]map[string]bool, len(entries)) + withHealth := map[string][]int{} + + for i, e := range entries { + composes[i] = map[string]bool{} + for _, k := range e.Composes { + composes[i][k] = true + } + + if e.HasHealth { + withHealth[e.Key] = append(withHealth[e.Key], i) + } + } + + out := make([]map[string]bool, len(entries)) + + for i := range entries { + out[i] = map[string]bool{} + + for k := range composes[i] { + for _, j := range withHealth[k] { + out[i][k] = true + maps.Copy(out[i], composes[j]) + } + } + } + + return out +} diff --git a/internal/simulate/report.go b/internal/simulate/report.go index d742e935..a0b1e89b 100644 --- a/internal/simulate/report.go +++ b/internal/simulate/report.go @@ -25,7 +25,6 @@ import ( corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/apimachinery/pkg/runtime/schema" apiextensionsv1 "github.com/crossplane/crossplane/apis/v2/apiextensions/v1" @@ -261,37 +260,25 @@ func (a *assembler) addComposite(c CompositeResult, h dt.CompositeHealth) { // resource is listed under the most deeply nested composite entry that // composes it, once. func (a *assembler) unhealthy() { - planned := map[objectID]bool{} + planned := map[string]bool{} for _, p := range a.plans { - planned[idOf(p.APIVersion, p.Kind, p.Namespace, p.Name)] = true + planned[health.Key(p.APIVersion, p.Kind, p.Namespace, p.Name)] = true } - composes := make([]map[objectID]bool, len(a.composed)) - entries := map[objectID][]int{} + entries := make([]health.Entry, len(a.composed)) for i, composed := range a.composed { - composes[i] = map[objectID]bool{} + c := a.composites[i] + entries[i] = health.Entry{Key: healthKey(c.Ref), HasHealth: c.Health != nil} for _, r := range composed { - composes[i][idOf(r.APIVersion, r.Kind, r.Namespace, r.Name)] = true - } - if c := a.composites[i]; c.Health != nil { - id := idOf(c.Ref.APIVersion, c.Ref.Kind, c.Ref.Namespace, c.Ref.Name) - entries[id] = append(entries[id], i) + entries[i].Composes = append(entries[i].Composes, health.Key(r.APIVersion, r.Kind, r.Namespace, r.Name)) } } + ownedElsewhere := health.OwnedElsewhere(entries) for i, composed := range a.composed { - // What a nested composite entry lists, and the entry itself. - ownedElsewhere := map[objectID]bool{} - for id := range composes[i] { - for _, j := range entries[id] { - ownedElsewhere[id] = true - maps.Copy(ownedElsewhere, composes[j]) - } - } - for _, r := range composed { - id := idOf(r.APIVersion, r.Kind, r.Namespace, r.Name) - if !r.Health.Unhealthy() || planned[id] || ownedElsewhere[id] { + id := health.Key(r.APIVersion, r.Kind, r.Namespace, r.Name) + if !r.Health.Unhealthy() || planned[id] || ownedElsewhere[i][id] { continue } u := UnhealthyResource{Ref: resourceRef(r.ObjectReference), Health: r.Health} @@ -304,16 +291,6 @@ func (a *assembler) unhealthy() { } } -// objectID is an object's identity across API versions. -type objectID struct { - gk schema.GroupKind - namespace, name string -} - -func idOf(apiVersion, kind, namespace, name string) objectID { - return objectID{gk: schema.FromAPIVersionAndKind(apiVersion, kind).GroupKind(), namespace: namespace, name: name} -} - // addDiffs adds a plan per diff, in the structured renderer's canonical order. // It reports whether a provider plan changed any of them, which the engine's // verdict alone does not show. From 79ac845681e8a15adbf890dac9c80a586328a937 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 16:23:00 -0500 Subject: [PATCH 21/30] fix(diff): count only XRs with health in the diff Health: line tallyComposite counted an XR without health, as one not yet in the cluster or whose entry errored or was filtered, as a healthy composite. That inflated the XR count past plan output's, and hid a nested XR's failure: added as a composite, it was then skipped as a resource, so a run that exits 4 printed no Health: line. Count a composite only when it has health, as simulate's healthTally does. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/diff/renderer/health.go | 8 +++-- internal/diff/renderer/health_test.go | 44 ++++++++++++++++++++++++--- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/internal/diff/renderer/health.go b/internal/diff/renderer/health.go index 22740c6a..0bfd18d0 100644 --- a/internal/diff/renderer/health.go +++ b/internal/diff/renderer/health.go @@ -93,9 +93,13 @@ func healthDescription(h *health.Health, full bool) []string { } // tallyComposite adds a top-level composite with ref and health h, and what -// it composes, to t. +// it composes, to t. A composite without health, as one not yet in the +// cluster or whose processing errored, is not counted as a composite, as in +// plan output; where its parent composes it, it counts as a resource. func tallyComposite(t *health.Tally, ref corev1.ObjectReference, h dt.CompositeHealth, diffs map[string]*dt.ResourceDiff) { - t.Composite(refKey(ref), h.XR) + if h.XR != nil { + t.Composite(refKey(ref), h.XR) + } for _, d := range diffs { t.Resource(diffKey(d), health.Of(d.Current.Raw)) diff --git a/internal/diff/renderer/health_test.go b/internal/diff/renderer/health_test.go index a2efd389..b9112707 100644 --- a/internal/diff/renderer/health_test.go +++ b/internal/diff/renderer/health_test.go @@ -92,7 +92,7 @@ Health: 1 resource not ready. `, }, "Grouped": { - reason: "Each XR section carries its health under its header and its unhealthy resources, changed or not, and the Health: line follows the footer.", + reason: "Each XR section carries its health under its header and its unhealthy resources, changed or not, and the Health: line follows the footer. XNetwork/c has no health, so the Health: line counts only XNetwork/a, as plan output does.", args: args{groups: []dt.XRDiffGroup{ group("a", failing, nil, modifiedDiffFor("Bucket", "b")), group("c", nil, []dt.ResourceHealth{{ObjectReference: ref("RouteTable", "rt"), Health: notReady}}), @@ -120,7 +120,7 @@ Unhealthy resources without changes: ================================================================================ Total: 1 modified across 2 XRs (1 unchanged) -Health: 1 of 2 XRs unhealthy before apply; 1 resource not ready. +Health: 1 of 1 XR unhealthy before apply; 1 resource not ready. `, }, "FullMessages": { @@ -211,7 +211,7 @@ func TestRenderCompDiffHealth(t *testing.T) { want string }{ "Unhealthy": { - reason: "An unhealthy XR's affected line carries its health, each XR's unhealthy resources without changes follow the impact analysis under its name, and the Health: line ends the run.", + reason: "An unhealthy XR's affected line carries its health, each XR's unhealthy resources without changes follow the impact analysis under its name, and the Health: line ends the run, counting only XBucket/a since XBucket/b has no health.", args: args{impacts: []dt.XRImpact{ { ObjectReference: corev1.ObjectReference{APIVersion: "example.org/v1", Kind: "XBucket", Namespace: "ns", Name: "a"}, @@ -242,7 +242,7 @@ XR: XBucket/ns/a Unhealthy resources without changes: RouteTable/rt ⚠ not ready (Ready=False, Creating) -Health: 1 of 2 XRs unhealthy before apply; 1 resource not ready. +Health: 1 of 1 XR unhealthy before apply; 1 resource not ready. `, }, "NestedListsItsOwn": { @@ -280,6 +280,42 @@ XR: XBucket/c Subnet/sn ⚠ failing (Synced=False, ReconcileError): boom Health: 1 of 2 XRs unhealthy before apply; 1 resource failing (Synced=False). +`, + }, + "NestedEntryWithoutHealth": { + reason: "A nested XR whose own entry errored has no health, so it counts as a resource where its parent composes it, and the Health: line shows the failure the run exits 4 for, as plan output does.", + args: args{impacts: []dt.XRImpact{ + { + ObjectReference: p, + Status: dt.XRStatusUnchanged, + Health: dt.CompositeHealth{XR: healthy, Composed: []dt.ResourceHealth{{ObjectReference: c, Health: failing}}}, + }, + { + ObjectReference: c, + Status: dt.XRStatusError, + ComposedBy: &p, + }, + }}, + want: `=== Composition Changes === + +No changes detected in composition xbuckets.example.org + +=== Affected Composite Resources === + + ✓ XBucket/p (cluster-scoped) + ✗ XBucket/c (cluster-scoped) + +Summary: 2 resources unchanged + +=== Impact Analysis === + +All composite resources are up-to-date. No downstream resource changes detected. + +XR: XBucket/p + Unhealthy resources without changes: + XBucket/c ⚠ failing (Synced=False, ReconcileError): boom + +Health: 1 resource failing (Synced=False). `, }, } From bd96340fd9c620e7a30f1dad8d5b356b96b53270 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 14:42:40 -0500 Subject: [PATCH 22/30] feat(resource): carry the live resource's health on each plan 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) Signed-off-by: Jonathan Ogilvie --- internal/simulate/simulate.go | 10 +++-- internal/simulate/simulate_test.go | 61 ++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) diff --git a/internal/simulate/simulate.go b/internal/simulate/simulate.go index cede9d2b..cf998f16 100644 --- a/internal/simulate/simulate.go +++ b/internal/simulate/simulate.go @@ -38,6 +38,7 @@ import ( pkgv1 "github.com/crossplane/crossplane/apis/v2/pkg/v1" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" ) @@ -80,8 +81,8 @@ func NewPlanner(kubeClient client.Client, rmapper meta.RESTMapper, crds CRDs, de } // Plan plans a single resource: fetch its live counterpart, then plan it -// with PlanWithLive. Failures become per-resource errors so one resource -// never fails the whole run. +// with PlanWithLive, carrying the live counterpart's health. Failures become +// per-resource errors so one resource never fails the whole run. func (p *Planner) Plan(ctx context.Context, desired *unstructured.Unstructured) *plan.ResourcePlan { if desired.GetName() == "" { rp := plan.NewResourcePlan(desired) @@ -95,7 +96,10 @@ func (p *Planner) Plan(ctx context.Context, desired *unstructured.Unstructured) rp.Error = fmt.Sprintf("cannot fetch live resource: %s", err) return rp } - return p.PlanWithLive(ctx, desired, live) + rp := p.PlanWithLive(ctx, desired, live) + // Health before apply, read from the live resource; nil for a create. + rp.Health = health.Of(live) + return rp } // PlanWithLive plans a single resource against an already-fetched live diff --git a/internal/simulate/simulate_test.go b/internal/simulate/simulate_test.go index e6daf85e..eaeda9a9 100644 --- a/internal/simulate/simulate_test.go +++ b/internal/simulate/simulate_test.go @@ -42,6 +42,7 @@ import ( "github.com/crossplane/crossplane/apis/v2/pkg" pkgv1 "github.com/crossplane/crossplane/apis/v2/pkg/v1" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" ) @@ -527,3 +528,63 @@ func TestPlanner(t *testing.T) { } }) } + +func TestPlannerHealth(t *testing.T) { + ctx := context.Background() + + s := runtime.NewScheme() + s.AddKnownTypeWithName(vpcGVK, &unstructured.Unstructured{}) + s.AddKnownTypeWithName(vpcGVK.GroupVersion().WithKind("VPCList"), &unstructured.UnstructuredList{}) + + rm := meta.NewDefaultRESTMapper([]schema.GroupVersion{vpcGVK.GroupVersion()}) + rm.Add(vpcGVK, meta.RESTScopeRoot) + + withConditions := func(u *unstructured.Unstructured, conds ...any) *unstructured.Unstructured { + u.Object["status"] = map[string]any{"conditions": conds} + return u + } + syncedFalse := map[string]any{"type": "Synced", "status": "False", "reason": "ReconcileError", "message": "boom"} + + type args struct { + live *unstructured.Unstructured + router Router + } + cases := map[string]struct { + reason string + args args + want *health.Health + }{ + "Create": { + reason: "A resource with no live counterpart carries no health.", + args: args{router: &fakeRouter{client: &fakePlanClient{rsp: &diffv1alpha1.PlanResponse{Action: diffv1alpha1.Action_ACTION_CREATE}}}}, + want: nil, + }, + "HealthyWithoutConditions": { + reason: "A live resource with no Synced or Ready condition is healthy.", + args: args{live: vpc("my-vpc", "10.0.0.0/16"), router: &fakeRouter{client: &fakePlanClient{rsp: &diffv1alpha1.PlanResponse{Action: diffv1alpha1.Action_ACTION_NO_OP}}}}, + want: &health.Health{State: health.Healthy}, + }, + "FailingRouted": { + reason: "A diff server plan carries the live resource's Synced=False as failing.", + args: args{live: withConditions(vpc("my-vpc", "10.0.0.0/16"), syncedFalse), router: &fakeRouter{client: &fakePlanClient{rsp: &diffv1alpha1.PlanResponse{Action: diffv1alpha1.Action_ACTION_UPDATE}}}}, + want: &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: "boom"}}, + }, + "FailingApproximate": { + reason: "An approximate plan carries the live resource's health too.", + args: args{live: withConditions(vpc("my-vpc", "10.0.0.0/16"), syncedFalse), router: &fakeRouter{}}, + want: &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: "boom"}}, + }, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + b := fake.NewClientBuilder().WithScheme(s) + if tc.args.live != nil { + b = b.WithObjects(tc.args.live) + } + got := NewPlanner(b.Build(), rm, noCRDs(t), "default", tc.args.router, time.Second).Plan(ctx, vpc("my-vpc", "10.1.0.0/16")) + if diff := cmp.Diff(tc.want, got.Health); diff != "" { + t.Errorf("\n%s\nPlan(...).Health: -want, +got:\n%s", tc.reason, diff) + } + }) + } +} From 939fae6aee0f91bd95a89fcbb73a3c819e622c89 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 14:43:12 -0500 Subject: [PATCH 23/30] feat(resource): mark unhealthy resources in resource simulate output MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) Signed-off-by: Jonathan Ogilvie --- cmd/crossplane/simulate/simulate.go | 3 +- internal/plan/output.go | 75 ++++++- internal/plan/output_test.go | 208 ++++++++++++++++++- internal/simulate/report_test.go | 14 +- internal/simulate/result.go | 20 +- internal/simulate/simtest/plan_assertions.go | 2 +- 6 files changed, 283 insertions(+), 39 deletions(-) diff --git a/cmd/crossplane/simulate/simulate.go b/cmd/crossplane/simulate/simulate.go index 16f9692d..fbaab655 100644 --- a/cmd/crossplane/simulate/simulate.go +++ b/cmd/crossplane/simulate/simulate.go @@ -67,6 +67,7 @@ type Cmd struct { CacheDir string `env:"CROSSPLANE_XPKG_CACHE" help:"Directory for cached xpkg package contents (providers)." name:"cache-dir" predictor:"directory"` Timeout time.Duration `default:"2m" help:"Max time to plan one managed resource."` Output string `default:"plan" enum:"plan,json,yaml" help:"Output format. One of: plan, json, yaml." name:"output" short:"o"` + FullMessages bool `help:"Print health condition messages in full in plan output, rather than cut to one line."` Impersonation kube.ImpersonationFlags `embed:""` } @@ -92,7 +93,7 @@ func (c *Cmd) Run(ctx context.Context, k *kong.Context, logger logging.Logger, s return errors.Errorf("no resources found in %q", c.Filename) } - renderer, err := plan.NewRenderer(c.Output) + renderer, err := plan.NewRenderer(c.Output, c.FullMessages) if err != nil { return err } diff --git a/internal/plan/output.go b/internal/plan/output.go index ab81b0e8..d2501324 100644 --- a/internal/plan/output.go +++ b/internal/plan/output.go @@ -101,6 +101,57 @@ type Result struct { Plans []*ResourcePlan `json:"plans"` } +// HealthSummary counts what is not healthy before apply. Composites counts +// the composite entries whose own health is not healthy; Failing, NotReady +// and Paused count the other resources, a nested composite without health of +// its own on an entry included. Each is counted once however many composites +// or plans reach it. +type HealthSummary struct { + Composites int `json:"unhealthyComposites"` + Failing int `json:"failing"` + NotReady int `json:"notReady"` + Paused int `json:"paused"` +} + +// HealthCounts returns the health counts of plans: each resource once, under +// the state its first plan reports, matched across API versions. +func HealthCounts(plans []*ResourcePlan) health.Counts { + t := &health.Tally{} + for _, p := range plans { + t.Resource(health.Key(p.APIVersion, p.Kind, p.Namespace, p.Name), p.Health) + } + return t.Counts() +} + +// Failing reports whether any plan's resource is failing, which sets exit +// bit 4. +func Failing(plans []*ResourcePlan) bool { + return slices.ContainsFunc(plans, func(p *ResourcePlan) bool { return p.Health != nil && p.Health.State == health.Failing }) +} + +// MarshalJSON writes the result with its plans' health counts under +// summary.health. YAML goes through it too. Decoding needs no counterpart: +// summary decodes into Summary, and the health counts come from Plans. +func (r Result) MarshalJSON() ([]byte, error) { + type summary struct { + Summary + + Health HealthSummary `json:"health"` + } + c := HealthCounts(r.Plans) + return json.Marshal(struct { + FormatVersion string `json:"formatVersion"` + Errored bool `json:"errored"` + Summary summary `json:"summary"` + Plans []*ResourcePlan `json:"plans"` + }{ + FormatVersion: r.FormatVersion, + Errored: r.Errored, + Summary: summary{Summary: r.Summary, Health: HealthSummary{Failing: c.Failing, NotReady: c.NotReady, Paused: c.Paused}}, + Plans: r.Plans, + }) +} + // NewResult assembles the serializable Result for the given plans. func NewResult(plans []*ResourcePlan) Result { s := Summarize(plans) @@ -117,11 +168,14 @@ type Renderer interface { Render(w io.Writer, plans []*ResourcePlan) error } -// NewRenderer returns the Renderer for the given output format. -func NewRenderer(format string) (Renderer, error) { +// NewRenderer returns the Renderer for the given output format. With +// fullMessages the plan format prints health condition messages in full, +// wrapped, rather than cut to one line; the structured formats always carry +// them in full. +func NewRenderer(format string, fullMessages bool) (Renderer, error) { switch format { case FormatPlan: - return planRenderer{}, nil + return planRenderer{fullMessages: fullMessages}, nil case FormatJSON: return jsonRenderer{}, nil case FormatYAML: @@ -157,16 +211,23 @@ func (yamlRenderer) Render(w io.Writer, plans []*ResourcePlan) error { } // planRenderer renders the human-readable plan format: a header and change -// tree per resource, then a summary with warnings. -type planRenderer struct{} +// tree per resource, marked when its resource is not healthy before apply, +// then a summary, a Health: line when anything is not healthy, and warnings. +type planRenderer struct { + // fullMessages prints health condition messages in full. + fullMessages bool +} // Render writes the plans to w in the human-readable plan format. -func (planRenderer) Render(w io.Writer, plans []*ResourcePlan) error { +func (pr planRenderer) Render(w io.Writer, plans []*ResourcePlan) error { for _, p := range plans { - RenderPlan(w, p, "", false) + RenderPlan(w, p, "", pr.fullMessages) } s := Summarize(plans) RenderSummaryLine(w, s) + if line := health.SummaryLine(HealthCounts(plans)); line != "" { + _, _ = fmt.Fprintln(w, line) + } RenderSummaryWarnings(w, plans, s) return nil } diff --git a/internal/plan/output_test.go b/internal/plan/output_test.go index 10f29295..e728e61b 100644 --- a/internal/plan/output_test.go +++ b/internal/plan/output_test.go @@ -22,6 +22,8 @@ import ( "testing" "github.com/google/go-cmp/cmp" + + "github.com/crossplane/cli/v2/internal/health" ) func testPlans() []*ResourcePlan { @@ -105,7 +107,7 @@ Warning: 1 resource could not be planned. ` var buf bytes.Buffer - r, err := NewRenderer(FormatPlan) + r, err := NewRenderer(FormatPlan, false) if err != nil { t.Fatalf("NewRenderer(plan): unexpected error: %v", err) } @@ -159,7 +161,7 @@ Plan: 0 to add, 1 to change, 0 to destroy. ` var buf bytes.Buffer - r, err := NewRenderer(FormatPlan) + r, err := NewRenderer(FormatPlan, false) if err != nil { t.Fatalf("NewRenderer(plan): unexpected error: %v", err) } @@ -217,7 +219,7 @@ Applying this leaves the resource unsynced until it is replaced manually. ` var buf bytes.Buffer - r, _ := NewRenderer(FormatPlan) + r, _ := NewRenderer(FormatPlan, false) if err := r.Render(&buf, plans); err != nil { t.Fatalf("Render(plan): unexpected error: %v", err) } @@ -238,14 +240,14 @@ Applying this leaves the resource unsynced until it is replaced manually. } func TestNewRendererUnknownFormat(t *testing.T) { - if _, err := NewRenderer("diff"); err == nil { + if _, err := NewRenderer("diff", false); err == nil { t.Errorf("NewRenderer(diff): want an error for an unsupported format") } } func TestRenderJSON(t *testing.T) { var buf bytes.Buffer - r, err := NewRenderer(FormatJSON) + r, err := NewRenderer(FormatJSON, false) if err != nil { t.Fatalf("NewRenderer(json): unexpected error: %v", err) } @@ -284,7 +286,7 @@ func TestSummarize(t *testing.T) { func TestRenderPlanEmptyReplacePath(t *testing.T) { plans := []*ResourcePlan{{APIVersion: "v1", Kind: "ConfigMap", Name: "cm", Action: ActionReplace, ReplacePaths: []string{"", "..", "spec.x"}}} var buf bytes.Buffer - r, _ := NewRenderer(FormatPlan) + r, _ := NewRenderer(FormatPlan, false) if err := r.Render(&buf, plans); err != nil { t.Fatalf("Render(plan): unexpected error: %v", err) } @@ -292,7 +294,7 @@ func TestRenderPlanEmptyReplacePath(t *testing.T) { func TestRenderJSONOmitsProjectFields(t *testing.T) { var buf bytes.Buffer - r, _ := NewRenderer(FormatJSON) + r, _ := NewRenderer(FormatJSON, false) if err := r.Render(&buf, testPlans()); err != nil { t.Fatalf("Render(json): unexpected error: %v", err) } @@ -302,3 +304,195 @@ func TestRenderJSONOmitsProjectFields(t *testing.T) { } } } + +func TestRenderPlanHealth(t *testing.T) { + failing := func(msg string) *health.Health { + return &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: msg}} + } + vpcMsg := "The vpc ID 'vpc-048777c3b8a8ee173' does not exist\nstatus code: 400" + update := &ResourcePlan{ + APIVersion: "ec2.aws.upbound.io/v1beta1", Kind: "Subnet", Name: "my-subnet", Action: ActionUpdate, + Changes: []FieldChange{{Path: "spec.forProvider.cidrBlock", Actual: &Value{Value: "10.0.1.0/24"}, Planned: &Value{Value: "10.0.2.0/24"}}}, + Health: failing(vpcMsg), + } + noop := func(name string, h *health.Health) *ResourcePlan { + return &ResourcePlan{APIVersion: "ec2.aws.upbound.io/v1beta1", Kind: "VPC", Name: name, Action: ActionNoOp, Health: h} + } + + type args struct { + plans []*ResourcePlan + full bool + } + cases := map[string]struct { + reason string + args args + want string + }{ + "Healthy": { + reason: "Healthy resources and creates print no health line and no Health: line.", + args: args{plans: []*ResourcePlan{ + noop("my-vpc", &health.Health{State: health.Healthy}), + {APIVersion: "ec2.aws.upbound.io/v1beta1", Kind: "VPC", Name: "new-vpc", Action: ActionCreate}, + }}, + want: `[=] VPC.ec2.aws.upbound.io my-vpc (no-op) + +[+] VPC.ec2.aws.upbound.io new-vpc (create) + +Plan: 1 to add, 0 to change, 0 to destroy. +`, + }, + "Unhealthy": { + reason: "Each unhealthy resource, unchanged ones included, gets a ⚠ line with its message cut to one line, and the Health: line counts them.", + args: args{plans: []*ResourcePlan{ + update, + noop("my-vpc", &health.Health{State: health.NotReady, Ready: &health.Condition{Status: "False", Reason: "Creating"}}), + noop("paused-vpc", &health.Health{State: health.Paused}), + }}, + want: `[~] Subnet.ec2.aws.upbound.io my-subnet (update) + ⚠ failing (Synced=False, ReconcileError): The vpc ID 'vpc-048777c3b8a8ee173' does not exist status code: 400 + spec: + forProvider: + ~ cidrBlock: "10.0.1.0/24" -> "10.0.2.0/24" + +[=] VPC.ec2.aws.upbound.io my-vpc (no-op) + ⚠ not ready (Ready=False, Creating) + +[=] VPC.ec2.aws.upbound.io paused-vpc (no-op) + ⚠ paused + +Plan: 0 to add, 1 to change, 0 to destroy. +Health: 1 resource failing (Synced=False), 1 not ready, 1 paused. +`, + }, + "FullMessages": { + reason: "With full messages, the message prints in full beneath the ⚠ line, wrapped and indented.", + args: args{plans: []*ResourcePlan{update}, full: true}, + want: `[~] Subnet.ec2.aws.upbound.io my-subnet (update) + ⚠ failing (Synced=False, ReconcileError) + The vpc ID 'vpc-048777c3b8a8ee173' does not exist + status code: 400 + spec: + forProvider: + ~ cidrBlock: "10.0.1.0/24" -> "10.0.2.0/24" + +Plan: 0 to add, 1 to change, 0 to destroy. +Health: 1 resource failing (Synced=False). +`, + }, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + r, _ := NewRenderer(FormatPlan, tc.args.full) + var buf bytes.Buffer + if err := r.Render(&buf, tc.args.plans); err != nil { + t.Fatalf("\n%s\nRender(plan): unexpected error: %v", tc.reason, err) + } + if diff := cmp.Diff(tc.want, buf.String()); diff != "" { + t.Errorf("\n%s\nRender(plan): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestRenderJSONHealth(t *testing.T) { + failing := &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: "boom"}} + + type want struct { + plans string + summary string + } + type args struct { + plans []*ResourcePlan + } + cases := map[string]struct { + reason string + args args + want want + }{ + "NoLiveObject": { + reason: "A create writes no health, and every health count is zero.", + args: args{plans: []*ResourcePlan{{APIVersion: "v1", Kind: "ConfigMap", Name: "cm", Action: ActionCreate}}}, + want: want{ + plans: `[{"apiVersion":"v1","kind":"ConfigMap","name":"cm","action":"create"}]`, + summary: `{"create":1,"update":0,"destroy":0,"replace":0,"noop":0,"approximate":0,"errored":0,"health":{"unhealthyComposites":0,"failing":0,"notReady":0,"paused":0}}`, + }, + }, + "Unhealthy": { + reason: "Each plan's health is written with the conditions it came from, and summary.health counts each resource once.", + args: args{plans: []*ResourcePlan{ + {APIVersion: "v1", Kind: "ConfigMap", Name: "cm", Action: ActionNoOp, Health: failing}, + {APIVersion: "v1", Kind: "ConfigMap", Name: "pm", Action: ActionNoOp, Health: &health.Health{State: health.Paused}}, + }}, + want: want{ + plans: `[{"apiVersion":"v1","kind":"ConfigMap","name":"cm","action":"no-op","health":{"state":"failing","synced":{"status":"False","reason":"ReconcileError","message":"boom"}}},` + + `{"apiVersion":"v1","kind":"ConfigMap","name":"pm","action":"no-op","health":{"state":"paused"}}]`, + summary: `{"create":0,"update":0,"destroy":0,"replace":0,"noop":2,"approximate":0,"errored":0,"health":{"unhealthyComposites":0,"failing":1,"notReady":0,"paused":1}}`, + }, + }, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + r, _ := NewRenderer(FormatJSON, false) + var buf bytes.Buffer + if err := r.Render(&buf, tc.args.plans); err != nil { + t.Fatalf("\n%s\nRender(json): unexpected error: %v", tc.reason, err) + } + var top map[string]json.RawMessage + if err := json.Unmarshal(buf.Bytes(), &top); err != nil { + t.Fatalf("\n%s\nRender(json): output is not valid JSON: %v", tc.reason, err) + } + compact := func(raw json.RawMessage) string { + var b bytes.Buffer + _ = json.Compact(&b, raw) + return b.String() + } + got := want{plans: compact(top["plans"]), summary: compact(top["summary"])} + if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(want{})); diff != "" { + t.Errorf("\n%s\nRender(json): -want, +got:\n%s", tc.reason, diff) + } + + // What is written decodes back to what was rendered. + var back Result + if err := json.Unmarshal(buf.Bytes(), &back); err != nil { + t.Fatalf("\n%s\nRender(json): cannot decode: %v", tc.reason, err) + } + if diff := cmp.Diff(NewResult(tc.args.plans), back); diff != "" { + t.Errorf("\n%s\nRender(json) round trip: -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestFailing(t *testing.T) { + type args struct { + plans []*ResourcePlan + } + cases := map[string]struct { + reason string + args args + want bool + }{ + "NoHealth": { + reason: "Plans without health are not failing.", + args: args{plans: []*ResourcePlan{{Action: ActionCreate}}}, + want: false, + }, + "NotReadyAndPaused": { + reason: "Not ready and paused resources don't set the failing bit.", + args: args{plans: []*ResourcePlan{{Health: &health.Health{State: health.NotReady}}, {Health: &health.Health{State: health.Paused}}}}, + want: false, + }, + "FailingDelete": { + reason: "A failing resource sets the bit, even one the plan deletes.", + args: args{plans: []*ResourcePlan{{Action: ActionDelete, Health: &health.Health{State: health.Failing}}}}, + want: true, + }, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + if diff := cmp.Diff(tc.want, Failing(tc.args.plans)); diff != "" { + t.Errorf("\n%s\nFailing(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} diff --git a/internal/simulate/report_test.go b/internal/simulate/report_test.go index 0bc24cec..57444b9d 100644 --- a/internal/simulate/report_test.go +++ b/internal/simulate/report_test.go @@ -645,7 +645,7 @@ func TestHealth(t *testing.T) { type want struct { plans map[string]*health.Health // plan name -> health composites []CompositeResult - summary HealthSummary + summary plan.HealthSummary } type args struct { xr *dt.XRReport @@ -696,7 +696,7 @@ func TestHealth(t *testing.T) { Ref: xrPlan, Status: CompositeChanged, Health: failing, Unhealthy: []UnhealthyResource{{Ref: resourceRef(bucketV1), Health: failing}}, }}, - summary: HealthSummary{Composites: 1, Failing: 1, NotReady: 1}, + summary: plan.HealthSummary{Composites: 1, Failing: 1, NotReady: 1}, }, }, "PlannedAcrossVersions": { @@ -710,7 +710,7 @@ func TestHealth(t *testing.T) { want: want{ plans: map[string]*health.Health{"b": failing}, composites: []CompositeResult{{Ref: xrPlan, Status: CompositeChanged, Health: healthy}}, - summary: HealthSummary{Failing: 1}, + summary: plan.HealthSummary{Failing: 1}, }, }, "NestedVia": { @@ -728,7 +728,7 @@ func TestHealth(t *testing.T) { {Ref: resourceRef(child), Health: notReady}, {Ref: cmPlan("deep"), Via: &childPlan, Health: failing}, }}}, - summary: HealthSummary{Failing: 1, NotReady: 1}, + summary: plan.HealthSummary{Failing: 1, NotReady: 1}, }, }, "NestedEntryListsItsOwn": { @@ -749,7 +749,7 @@ func TestHealth(t *testing.T) { {Ref: xrPlan, Composition: compRef("a"), Status: CompositeUnchanged, Health: healthy, Unhealthy: []UnhealthyResource{{Ref: cmPlan("cm"), Health: failing}}}, {Ref: childPlan, Composition: compRef("a"), ComposedBy: &xrPlan, Status: CompositeUnchanged, Health: notReady, Unhealthy: []UnhealthyResource{{Ref: cmPlan("deep"), Health: failing}}}, }, - summary: HealthSummary{Composites: 1, Failing: 2}, + summary: plan.HealthSummary{Composites: 1, Failing: 2}, }, }, "NestedEntryWithoutHealth": { @@ -770,7 +770,7 @@ func TestHealth(t *testing.T) { }}, {Ref: childPlan, Composition: compRef("a"), ComposedBy: &xrPlan, Status: CompositeError}, }, - summary: HealthSummary{Failing: 1, NotReady: 1}, + summary: plan.HealthSummary{Failing: 1, NotReady: 1}, }, }, "CompImpactsCountedOnce": { @@ -785,7 +785,7 @@ func TestHealth(t *testing.T) { {Ref: xrPlan, Composition: compRef("a"), Status: CompositeUnchanged, Health: failing, Unhealthy: []UnhealthyResource{{Ref: cmPlan("cm"), Health: notReady}}}, {Ref: xrPlan, Composition: compRef("b"), Status: CompositeUnchanged, Health: failing, Unhealthy: []UnhealthyResource{{Ref: cmPlan("cm"), Health: notReady}}}, }, - summary: HealthSummary{Composites: 1, NotReady: 1}, + summary: plan.HealthSummary{Composites: 1, NotReady: 1}, }, }, } diff --git a/internal/simulate/result.go b/internal/simulate/result.go index 1e219010..b56a33fb 100644 --- a/internal/simulate/result.go +++ b/internal/simulate/result.go @@ -108,18 +108,6 @@ type UnhealthyResource struct { Health *health.Health `json:"health"` } -// HealthSummary counts what is not healthy before apply. Composites counts -// the composite entries whose own health is not healthy; Failing, NotReady -// and Paused count the other resources, a nested composite without health of -// its own on an entry included. Each is counted once however many composites -// or plans reach it. -type HealthSummary struct { - Composites int `json:"unhealthyComposites"` - Failing int `json:"failing"` - NotReady int `json:"notReady"` - Paused int `json:"paused"` -} - // Error is an error from a simulate run, optionally anchored to a resource. type Error struct { Ref *plan.ResourceRef `json:"ref,omitempty"` @@ -215,9 +203,9 @@ func (r Result) Deferred() int { // Health counts the unhealthy composites, and the other resources by health // state, from Composites and Plans. A resource counts once, under the state // its first live copy reports, matched across API versions. -func (r Result) Health() HealthSummary { +func (r Result) Health() plan.HealthSummary { c := r.healthTally().Counts() - return HealthSummary{Composites: c.UnhealthyComposites, Failing: c.Failing, NotReady: c.NotReady, Paused: c.Paused} + return plan.HealthSummary{Composites: c.UnhealthyComposites, Failing: c.Failing, NotReady: c.NotReady, Paused: c.Paused} } // healthTally adds r's composites and resources to a health.Tally. A @@ -276,8 +264,8 @@ func (r Result) MarshalJSON() ([]byte, error) { type summary struct { plan.Summary - Health HealthSummary `json:"health"` - Deferred int `json:"deferred"` + Health plan.HealthSummary `json:"health"` + Deferred int `json:"deferred"` } return json.Marshal(struct { FormatVersion string `json:"formatVersion"` diff --git a/internal/simulate/simtest/plan_assertions.go b/internal/simulate/simtest/plan_assertions.go index e40cb309..f6892770 100644 --- a/internal/simulate/simtest/plan_assertions.go +++ b/internal/simulate/simtest/plan_assertions.go @@ -116,7 +116,7 @@ func assertHealthSummary(t *testing.T, jsonOutput string, want expectedHealth) { var out struct { Summary struct { - Health simulate.HealthSummary `json:"health"` + Health plan.HealthSummary `json:"health"` } `json:"summary"` } if err := json.Unmarshal([]byte(jsonOutput), &out); err != nil { From aa8f4248660ea99dff6131714c9ba054cf9e726b Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 14:43:16 -0500 Subject: [PATCH 24/30] feat(resource): exit with bit 4 when a managed resource is failing 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) Signed-off-by: Jonathan Ogilvie --- cmd/crossplane/simulate/simulate.go | 16 +++++++++++---- cmd/crossplane/simulate/simulate_test.go | 26 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/cmd/crossplane/simulate/simulate.go b/cmd/crossplane/simulate/simulate.go index fbaab655..ac0d97a5 100644 --- a/cmd/crossplane/simulate/simulate.go +++ b/cmd/crossplane/simulate/simulate.go @@ -160,17 +160,25 @@ func (c *Cmd) Run(ctx context.Context, k *kong.Context, logger logging.Logger, s } // exitStatus ranks the plans' outcome: an error, which exits 1, when any -// plan errored; otherwise a silent exitcode.ExitError that exits 3 when a plan -// would change anything; otherwise nil. +// plan errored; otherwise a silent exitcode.ExitError whose code adds 4 when +// a resource was failing before apply to 3 when a plan would change +// anything, so 3, 4 or 7; otherwise nil. func exitStatus(plans []*plan.ResourcePlan) error { s := plan.Summarize(plans) if s.Errored > 0 { return errors.Errorf("cannot plan %d of %d resources; see their errors in the output", s.Errored, len(plans)) } + code := 0 if s.Changes() > 0 { - return exitcode.ExitError{Code: isimulate.ExitCodeChanges} + code |= isimulate.ExitCodeChanges } - return nil + if plan.Failing(plans) { + code |= isimulate.ExitCodeFailing + } + if code == 0 { + return nil + } + return exitcode.ExitError{Code: code} } // setupKubeClient builds the client stack resource simulate plans with: the diff --git a/cmd/crossplane/simulate/simulate_test.go b/cmd/crossplane/simulate/simulate_test.go index a613f2a1..e6185559 100644 --- a/cmd/crossplane/simulate/simulate_test.go +++ b/cmd/crossplane/simulate/simulate_test.go @@ -23,10 +23,13 @@ import ( "github.com/google/go-cmp/cmp" "github.com/crossplane/cli/v2/internal/exitcode" + "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" ) func TestExitStatus(t *testing.T) { + failing := &health.Health{State: health.Failing} + type args struct { plans []*plan.ResourcePlan } @@ -55,6 +58,29 @@ func TestExitStatus(t *testing.T) { args: args{plans: []*plan.ResourcePlan{{Action: plan.ActionUpdate}, {Error: "boom"}}}, want: 1, }, + "FailingNoChanges": { + reason: "A failing resource with no changes should exit 4.", + args: args{plans: []*plan.ResourcePlan{{Action: plan.ActionNoOp, Health: failing}}}, + want: 4, + }, + "FailingWithChanges": { + reason: "Changes and a failing resource should exit 7, 3 and 4 combined.", + args: args{plans: []*plan.ResourcePlan{{Action: plan.ActionUpdate}, {Action: plan.ActionNoOp, Health: failing}}}, + want: 7, + }, + "NotReadyAndPaused": { + reason: "Not ready and paused resources should not set exit bit 4.", + args: args{plans: []*plan.ResourcePlan{ + {Action: plan.ActionNoOp, Health: &health.Health{State: health.NotReady}}, + {Action: plan.ActionNoOp, Health: &health.Health{State: health.Paused}}, + }}, + want: 0, + }, + "ErroredWithFailing": { + reason: "An errored plan should exit 1, not 4 or 7, even when a resource is failing.", + args: args{plans: []*plan.ResourcePlan{{Action: plan.ActionUpdate, Health: failing}, {Error: "boom"}}}, + want: 1, + }, } for name, tc := range cases { t.Run(name, func(t *testing.T) { From 5d86795b776edbe9b2626a67436fc71f7ecd2613 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 14:43:17 -0500 Subject: [PATCH 25/30] docs(resource): document health output and exit codes 4 and 7 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) Signed-off-by: Jonathan Ogilvie --- cmd/crossplane/simulate/help/simulate.md | 38 +++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/cmd/crossplane/simulate/help/simulate.md b/cmd/crossplane/simulate/help/simulate.md index 86b0fc30..c381c606 100644 --- a/cmd/crossplane/simulate/help/simulate.md +++ b/cmd/crossplane/simulate/help/simulate.md @@ -86,13 +86,43 @@ Provider packages are cached in `--cache-dir`, which defaults to `$CROSSPLANE_XPKG_CACHE` or the same xpkg cache directory as `project simulate` and `render`. +The `-o` flag picks the output format: + +- `plan`, the default, prints each resource's field-level plan, including + the resources it leaves unchanged. A resource whose live object isn't + healthy before apply gets a `⚠` line under its plan: `failing` (its Synced + condition is False), `not ready` (its Ready condition is False) or `paused` + (it carries the `crossplane.io/paused: "true"` annotation, whatever its + conditions), with the condition's reason and message. A `Health:` line + after the `Plan:` line counts them, and is left out when every resource is + healthy. Messages are cut to one line; `--full-messages` prints them in + full, wrapped and indented. +- `json` and `yaml` print the same plans as a structured result, for CI and + other tools, with messages always in full. Each plan with a live object + carries its `health`: `health.state` is `failing`, `notReady`, `paused` or + `healthy`, with the `synced` and `ready` conditions it was read from. A + plan has no `health` when there is no live object, as for a create. + `summary.health` counts the resources that are `failing`, `notReady` or + `paused`; `unhealthyComposites` is always 0, since this command takes no + composite resources. + The command exits with one of these statuses, so a CI pipeline can gate on the result: -- 0: the simulation found no changes. +- 0: the simulation succeeded and found no changes. - 1: the simulation failed, including when any resource couldn't be planned (its error is in the output). -- 3: the simulation found changes: a plan would create, update, destroy or - replace a resource. -- 80: the flags were invalid, so nothing was simulated. +- 3: the simulation succeeded and found changes: a plan would create, + update, destroy or replace a resource. +- 4: the simulation succeeded, found no changes, and a resource was failing + (Synced=False) before apply. A resource that's only not ready, or is + paused, doesn't set it. +- 7: the simulation succeeded, found changes, and a resource was failing: + 3 and 4 combined. +- 80: a flag was unknown or had an invalid value, so nothing was simulated. - 130: the simulation was interrupted. + +An error wins over what the simulation found: 130 over 1, and either of them +over 3, 4 and 7. Without an error, the status adds 4 for a failing resource +to 3 for changes, so 3 and 7 both mean changes, and 4 and 7 both mean a +failing resource. From af11efea7598e55f72b425b6603fa274382e3838 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 15:40:40 -0500 Subject: [PATCH 26/30] feat(resource): warn that --full-messages has no effect with -o json|yaml Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- cmd/crossplane/simulate/simulate.go | 1 + cmd/crossplane/simulate/simulate_test.go | 55 ++++++++++++++++++++++++ internal/simulate/run.go | 13 ++++-- internal/simulate/run_test.go | 2 +- 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/cmd/crossplane/simulate/simulate.go b/cmd/crossplane/simulate/simulate.go index ac0d97a5..81599e0a 100644 --- a/cmd/crossplane/simulate/simulate.go +++ b/cmd/crossplane/simulate/simulate.go @@ -97,6 +97,7 @@ func (c *Cmd) Run(ctx context.Context, k *kong.Context, logger logging.Logger, s if err != nil { return err } + isimulate.WarnIgnoredFullMessages(k.Stderr, isimulate.SetFlags(k), c.Output) var inputs []diffserver.Input if c.Providers != "" { diff --git a/cmd/crossplane/simulate/simulate_test.go b/cmd/crossplane/simulate/simulate_test.go index e6185559..e8ba8239 100644 --- a/cmd/crossplane/simulate/simulate_test.go +++ b/cmd/crossplane/simulate/simulate_test.go @@ -17,14 +17,17 @@ limitations under the License. package simulate import ( + "bytes" "strings" "testing" + "github.com/alecthomas/kong" "github.com/google/go-cmp/cmp" "github.com/crossplane/cli/v2/internal/exitcode" "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" + isimulate "github.com/crossplane/cli/v2/internal/simulate" ) func TestExitStatus(t *testing.T) { @@ -129,3 +132,55 @@ func TestHelp(t *testing.T) { }) } } + +func TestWarnIgnoredFullMessages(t *testing.T) { + type args struct { + args []string + } + cases := map[string]struct { + reason string + args args + want string + }{ + "SetWithJSON": { + reason: "json always carries messages in full, so --full-messages is ignored with a warning.", + args: args{args: []string{"-f", "x.yaml", "--full-messages", "-o", "json"}}, + want: "Warning: ignoring --full-messages; -o json always prints messages in full.\n", + }, + "SetWithYAML": { + reason: "yaml always carries messages in full, so --full-messages is ignored with a warning.", + args: args{args: []string{"-f", "x.yaml", "--full-messages", "-o", "yaml"}}, + want: "Warning: ignoring --full-messages; -o yaml always prints messages in full.\n", + }, + "SetWithPlan": { + reason: "--full-messages applies to the default plan output, so there's no warning.", + args: args{args: []string{"-f", "x.yaml", "--full-messages"}}, + }, + "UnsetWithJSON": { + reason: "--full-messages left at its default isn't warned about.", + args: args{args: []string{"-f", "x.yaml", "-o", "json"}}, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + var cli struct { + Simulate Cmd `cmd:""` + } + parser, err := kong.New(&cli, kong.Vars{"project_file": "crossplane-project.yaml"}, kong.Exit(func(int) {})) + if err != nil { + t.Fatalf("kong.New(...): %v", err) + } + k, err := parser.Parse(append([]string{"simulate"}, tc.args.args...)) + if err != nil { + t.Fatalf("Parse(%v): %v", tc.args.args, err) + } + + var stderr bytes.Buffer + isimulate.WarnIgnoredFullMessages(&stderr, isimulate.SetFlags(k), cli.Simulate.Output) + if diff := cmp.Diff(tc.want, stderr.String()); diff != "" { + t.Errorf("\n%s\nWarnIgnoredFullMessages(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} diff --git a/internal/simulate/run.go b/internal/simulate/run.go index 8210c23b..40170c70 100644 --- a/internal/simulate/run.go +++ b/internal/simulate/run.go @@ -254,9 +254,9 @@ func (c *Runner) diffComposites(k *kong.Context, logger logging.Logger, proc dp. } } -// setFlags returns the names of the flags set on the command line, not +// SetFlags returns the names of the flags set on the command line, not // those left at their defaults or set from the environment. -func setFlags(k *kong.Context) map[string]bool { +func SetFlags(k *kong.Context) map[string]bool { set := map[string]bool{} for _, p := range k.Path { if p.Flag != nil { @@ -605,7 +605,7 @@ func (c *Runner) Run(sigCtx context.Context, k *kong.Context, logger logging.Log c.sel = *in.Selection } project := in.Build != nil - warnIgnoredFlags(k.Stderr, setFlags(k), c.Output, c.SkipPlan, project) + warnIgnoredFlags(k.Stderr, SetFlags(k), c.Output, c.SkipPlan, project) if err := docker.Check(sigCtx); err != nil { return errors.Wrap(err, "cannot connect to Docker, which simulate needs to run functions") @@ -721,6 +721,13 @@ func warnIgnoredFlags(stderr io.Writer, set map[string]bool, output string, skip if !project && skipPlan && set["cache-dir"] { _, _ = fmt.Fprintln(stderr, "Warning: ignoring --cache-dir; with --skip-plan and no project file there is nothing to cache.") } + WarnIgnoredFullMessages(stderr, set, output) +} + +// WarnIgnoredFullMessages prints a warning to stderr if --full-messages was +// set on the command line with json or yaml output, which always carries +// messages in full. +func WarnIgnoredFullMessages(stderr io.Writer, set map[string]bool, output string) { if set["full-messages"] && (output == plan.FormatJSON || output == plan.FormatYAML) { _, _ = fmt.Fprintf(stderr, "Warning: ignoring --full-messages; -o %s always prints messages in full.\n", output) } diff --git a/internal/simulate/run_test.go b/internal/simulate/run_test.go index 7f16be30..bc3b0074 100644 --- a/internal/simulate/run_test.go +++ b/internal/simulate/run_test.go @@ -882,7 +882,7 @@ func TestWarnIgnoredFlags(t *testing.T) { } var stderr bytes.Buffer - warnIgnoredFlags(&stderr, setFlags(k), tc.output, tc.skipPlan, tc.project) + warnIgnoredFlags(&stderr, SetFlags(k), tc.output, tc.skipPlan, tc.project) if tc.warn == "" && stderr.Len() > 0 || !strings.Contains(stderr.String(), tc.warn) { t.Errorf("\n%s\nwarnIgnoredFlags(...): want warning containing %q, got %q", tc.reason, tc.warn, stderr.String()) } From 3cc6e729dc27593608dc9fb02e14483250483bc3 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 16:30:21 -0500 Subject: [PATCH 27/30] refactor(resource): split rendering and exit status out of Run Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- cmd/crossplane/simulate/simulate.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/cmd/crossplane/simulate/simulate.go b/cmd/crossplane/simulate/simulate.go index 81599e0a..a91a32f0 100644 --- a/cmd/crossplane/simulate/simulate.go +++ b/cmd/crossplane/simulate/simulate.go @@ -93,11 +93,10 @@ func (c *Cmd) Run(ctx context.Context, k *kong.Context, logger logging.Logger, s return errors.Errorf("no resources found in %q", c.Filename) } - renderer, err := plan.NewRenderer(c.Output, c.FullMessages) + renderer, err := c.renderer(k) if err != nil { return err } - isimulate.WarnIgnoredFullMessages(k.Stderr, isimulate.SetFlags(k), c.Output) var inputs []diffserver.Input if c.Providers != "" { @@ -154,7 +153,23 @@ func (c *Cmd) Run(ctx context.Context, k *kong.Context, logger logging.Logger, s plans = append(plans, planner.Plan(ctx, desired)) } - if err := renderer.Render(k.Stdout, plans); err != nil { + return report(k, renderer, plans) +} + +// renderer builds the renderer for --output and --full-messages, and warns on +// k.Stderr when --full-messages is set but has no effect. +func (c *Cmd) renderer(k *kong.Context) (plan.Renderer, error) { + r, err := plan.NewRenderer(c.Output, c.FullMessages) + if err != nil { + return nil, err + } + isimulate.WarnIgnoredFullMessages(k.Stderr, isimulate.SetFlags(k), c.Output) + return r, nil +} + +// report renders plans to k.Stdout and returns the run's exit status. +func report(k *kong.Context, r plan.Renderer, plans []*plan.ResourcePlan) error { + if err := r.Render(k.Stdout, plans); err != nil { return err } return exitStatus(plans) From b085ded6e9aafef4a232b48daa2b4d8fd3313515 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 16:31:55 -0500 Subject: [PATCH 28/30] test(resource): cover the renderer, warning and exit status Run uses 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) Signed-off-by: Jonathan Ogilvie --- cmd/crossplane/simulate/simulate_test.go | 57 ++++++++++++++++++------ 1 file changed, 43 insertions(+), 14 deletions(-) diff --git a/cmd/crossplane/simulate/simulate_test.go b/cmd/crossplane/simulate/simulate_test.go index e8ba8239..c16147cd 100644 --- a/cmd/crossplane/simulate/simulate_test.go +++ b/cmd/crossplane/simulate/simulate_test.go @@ -27,7 +27,6 @@ import ( "github.com/crossplane/cli/v2/internal/exitcode" "github.com/crossplane/cli/v2/internal/health" "github.com/crossplane/cli/v2/internal/plan" - isimulate "github.com/crossplane/cli/v2/internal/simulate" ) func TestExitStatus(t *testing.T) { @@ -133,32 +132,51 @@ func TestHelp(t *testing.T) { } } -func TestWarnIgnoredFullMessages(t *testing.T) { +func TestRendererAndReport(t *testing.T) { + // A failing resource whose message is too long for one line: plan output + // cuts off its tail unless --full-messages is set. + plans := []*plan.ResourcePlan{{ + APIVersion: "v1", Kind: "ConfigMap", Name: "cm", Action: plan.ActionNoOp, + Health: &health.Health{State: health.Failing, Synced: &health.Condition{Status: "False", Reason: "ReconcileError", Message: strings.Repeat("x ", 200) + "tail"}}, + }} + type args struct { args []string } + type want struct { + stderr string + fullOutput bool + code int + } cases := map[string]struct { reason string args args - want string + want want }{ + "PlanCut": { + reason: "Plan output cuts the message to one line and exits 4 for the failing resource.", + args: args{args: []string{"-f", "x.yaml"}}, + want: want{code: 4}, + }, + "PlanFullMessages": { + reason: "--full-messages reaches the plan renderer, which prints the message in full.", + args: args{args: []string{"-f", "x.yaml", "--full-messages"}}, + want: want{fullOutput: true, code: 4}, + }, "SetWithJSON": { reason: "json always carries messages in full, so --full-messages is ignored with a warning.", args: args{args: []string{"-f", "x.yaml", "--full-messages", "-o", "json"}}, - want: "Warning: ignoring --full-messages; -o json always prints messages in full.\n", + want: want{stderr: "Warning: ignoring --full-messages; -o json always prints messages in full.\n", fullOutput: true, code: 4}, }, "SetWithYAML": { reason: "yaml always carries messages in full, so --full-messages is ignored with a warning.", args: args{args: []string{"-f", "x.yaml", "--full-messages", "-o", "yaml"}}, - want: "Warning: ignoring --full-messages; -o yaml always prints messages in full.\n", - }, - "SetWithPlan": { - reason: "--full-messages applies to the default plan output, so there's no warning.", - args: args{args: []string{"-f", "x.yaml", "--full-messages"}}, + want: want{stderr: "Warning: ignoring --full-messages; -o yaml always prints messages in full.\n", fullOutput: true, code: 4}, }, "UnsetWithJSON": { reason: "--full-messages left at its default isn't warned about.", args: args{args: []string{"-f", "x.yaml", "-o", "json"}}, + want: want{fullOutput: true, code: 4}, }, } @@ -167,7 +185,8 @@ func TestWarnIgnoredFullMessages(t *testing.T) { var cli struct { Simulate Cmd `cmd:""` } - parser, err := kong.New(&cli, kong.Vars{"project_file": "crossplane-project.yaml"}, kong.Exit(func(int) {})) + var stdout, stderr bytes.Buffer + parser, err := kong.New(&cli, kong.Vars{"project_file": "crossplane-project.yaml"}, kong.Exit(func(int) {}), kong.Writers(&stdout, &stderr)) if err != nil { t.Fatalf("kong.New(...): %v", err) } @@ -176,10 +195,20 @@ func TestWarnIgnoredFullMessages(t *testing.T) { t.Fatalf("Parse(%v): %v", tc.args.args, err) } - var stderr bytes.Buffer - isimulate.WarnIgnoredFullMessages(&stderr, isimulate.SetFlags(k), cli.Simulate.Output) - if diff := cmp.Diff(tc.want, stderr.String()); diff != "" { - t.Errorf("\n%s\nWarnIgnoredFullMessages(...): -want, +got:\n%s", tc.reason, diff) + r, err := cli.Simulate.renderer(k) + if err != nil { + t.Fatalf("\n%s\nrenderer(...): unexpected error: %v", tc.reason, err) + } + err = report(k, r, plans) + code := 0 + if c, ok := exitcode.SilentExit(err); ok { + code = c + } else if err != nil { + code = 1 + } + got := want{stderr: stderr.String(), fullOutput: strings.Contains(stdout.String(), "tail"), code: code} + if diff := cmp.Diff(tc.want, got, cmp.AllowUnexported(want{})); diff != "" { + t.Errorf("\n%s\nrenderer(...) and report(...): -want, +got:\n%s", tc.reason, diff) } }) } From 4fe52b87bd91129b414ccb9a3f44619824597a51 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 16:32:20 -0500 Subject: [PATCH 29/30] test(resource): check summary.health in YAML output Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- internal/plan/output_test.go | 41 ++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/internal/plan/output_test.go b/internal/plan/output_test.go index e728e61b..821ad053 100644 --- a/internal/plan/output_test.go +++ b/internal/plan/output_test.go @@ -22,6 +22,7 @@ import ( "testing" "github.com/google/go-cmp/cmp" + "sigs.k8s.io/yaml" "github.com/crossplane/cli/v2/internal/health" ) @@ -463,6 +464,46 @@ func TestRenderJSONHealth(t *testing.T) { } } +func TestRenderYAMLHealth(t *testing.T) { + type args struct { + plans []*ResourcePlan + } + cases := map[string]struct { + reason string + args args + want map[string]any + }{ + "Unhealthy": { + reason: "summary.health counts each unhealthy resource in YAML output as it does in JSON.", + args: args{plans: []*ResourcePlan{ + {APIVersion: "v1", Kind: "ConfigMap", Name: "cm", Action: ActionNoOp, Health: &health.Health{State: health.Failing}}, + {APIVersion: "v1", Kind: "ConfigMap", Name: "pm", Action: ActionNoOp, Health: &health.Health{State: health.Paused}}, + }}, + want: map[string]any{"unhealthyComposites": float64(0), "failing": float64(1), "notReady": float64(0), "paused": float64(1)}, + }, + } + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + r, _ := NewRenderer(FormatYAML, false) + var buf bytes.Buffer + if err := r.Render(&buf, tc.args.plans); err != nil { + t.Fatalf("\n%s\nRender(yaml): unexpected error: %v", tc.reason, err) + } + var got struct { + Summary struct { + Health map[string]any `json:"health"` + } `json:"summary"` + } + if err := yaml.Unmarshal(buf.Bytes(), &got); err != nil { + t.Fatalf("\n%s\nRender(yaml): output is not valid YAML: %v", tc.reason, err) + } + if diff := cmp.Diff(tc.want, got.Summary.Health); diff != "" { + t.Errorf("\n%s\nRender(yaml) summary.health: -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + func TestFailing(t *testing.T) { type args struct { plans []*ResourcePlan From 5a052e017e59892c44e38f6c56438bde6d995a72 Mon Sep 17 00:00:00 2001 From: Jonathan Ogilvie Date: Thu, 19 Nov 2026 16:32:20 -0500 Subject: [PATCH 30/30] docs(resource): say the health line prints under the resource header Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Jonathan Ogilvie --- cmd/crossplane/simulate/help/simulate.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/crossplane/simulate/help/simulate.md b/cmd/crossplane/simulate/help/simulate.md index c381c606..5e6e8143 100644 --- a/cmd/crossplane/simulate/help/simulate.md +++ b/cmd/crossplane/simulate/help/simulate.md @@ -90,7 +90,7 @@ The `-o` flag picks the output format: - `plan`, the default, prints each resource's field-level plan, including the resources it leaves unchanged. A resource whose live object isn't - healthy before apply gets a `⚠` line under its plan: `failing` (its Synced + healthy before apply gets a `⚠` line under its header: `failing` (its Synced condition is False), `not ready` (its Ready condition is False) or `paused` (it carries the `crossplane.io/paused: "true"` annotation, whatever its conditions), with the condition's reason and message. A `Health:` line