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 1c0d1e4f..fc8d13ca 100644 --- a/cmd/crossplane/project/integration_plan_output_test.go +++ b/cmd/crossplane/project/integration_plan_output_test.go @@ -27,11 +27,13 @@ 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" 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 +75,20 @@ 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} + + // 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.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"} compArgs := []string{"-f", "../../../internal/testutils/integration/testdata/comp/updated-composition.yaml", "--namespace=default"} @@ -86,6 +102,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. @@ -140,7 +161,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 +205,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}, @@ -198,12 +219,73 @@ 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 + 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"}, 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 +304,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}, @@ -248,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 @@ -262,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 { @@ -302,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 { @@ -311,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/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 } diff --git a/cmd/crossplane/xr/integration_test.go b/cmd/crossplane/xr/integration_test.go index c60bc0d3..8f937857 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 + 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/diff/diffprocessor/comp_processor.go b/internal/diff/diffprocessor/comp_processor.go index 55cc47e2..dd89d35a 100644 --- a/internal/diff/diffprocessor/comp_processor.go +++ b/internal/diff/diffprocessor/comp_processor.go @@ -55,6 +55,9 @@ type XRDiffResult struct { Error error // 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. @@ -815,7 +818,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 { @@ -830,7 +833,8 @@ func (p *DefaultCompDiffProcessor) collectXRDiffs(ctx context.Context, xrs []*un // Store successful result with diffs results[resourceID] = &XRDiffResult{ Diffs: diffs, - unchanged: unchanged, + unchanged: rendered.Unchanged(), + health: rendered.Health(), } } } @@ -1642,6 +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.health summary.WithChanges++ default: impact.Status = dt.XRStatusUnchanged @@ -1650,6 +1655,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.health + } summary.Unchanged++ } diff --git a/internal/diff/diffprocessor/comp_processor_test.go b/internal/diff/diffprocessor/comp_processor_test.go index abb07075..66207bc6 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 }, @@ -3010,9 +3008,91 @@ 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) } }) } } + +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 + } + type args struct { + // changed makes the render produce a change; renderErr makes it fail. + changed bool + renderErr error + } + cases := map[string]struct { + reason string + args args + want want + }{ + "Changed": { + 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.", + want: want{status: dt.XRStatusUnchanged, health: live}, + }, + "Errored": { + reason: "A composite whose render failed carries no health.", + args: args{renderErr: errors.New("render failed")}, + 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.args.renderErr != nil { + return nil, types.RenderedResources{}, tc.args.renderErr + } + var rendered types.RenderedResources + rendered.SetComposite(res) + rendered.AddObserved(bucket) + diffs := map[string]*dt.ResourceDiff{} + 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 + } + 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..f53eb086 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,16 @@ func (p *DefaultDiffProcessor) diffSingleResourceInternal(ctx context.Context, r return nil, types.RenderedResources{}, errors.Wrap(err, "cannot calculate diffs for composed resources") } + // 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) + } + // 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..b1065416 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" ) @@ -1116,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 — @@ -3111,8 +3344,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 +3355,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 +3388,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 +3487,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 +3576,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 +3790,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 +3828,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/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/renderer/comp_diff_renderer.go b/internal/diff/renderer/comp_diff_renderer.go index 2e473c2f..be3b5555 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 ( @@ -65,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 { @@ -73,11 +75,26 @@ 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 } } + // 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)) @@ -87,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 { @@ -100,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 @@ -109,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) } @@ -126,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 @@ -403,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 { @@ -438,7 +455,70 @@ func (r *DefaultCompDiffRenderer) renderImpactAnalysis(comp *dt.CompositionDiff) } } - return nil + 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, ownedElsewhere map[string]map[string]bool, afterDiffs bool) error { + var sb strings.Builder + + for _, impact := range listedImpacts(comp) { + block := unhealthyBlock(impact.Health, impact.Diffs, ownedElsewhere[refKey(impact.ObjectReference)], 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()) +} + +// 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 { + 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 +648,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..eb58a453 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 @@ -82,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 @@ -130,6 +133,16 @@ func (r *DefaultDiffRenderer) renderDiffList(diffs map[string]*dt.ResourceDiff) header += " (requires replacement)" } + // A resource the cluster has is marked when it is not healthy. + h := health.Of(diff.Current.Raw) + if desc := healthDescription(h, r.diffOpts.FullMessages); len(desc) > 0 { + if described != "" && diffKey(diff) == described { + desc = []string{health.Tier(h)} + } + + header = strings.Join(append([]string{header + " " + desc[0]}, desc[1:]...), "\n") + } + content := FormatDiff(diff.LineDiffs, r.diffOpts) if content != "" { @@ -257,7 +270,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,26 +281,89 @@ 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 { - counts, err := r.renderDiffList(diffs) +// 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, described 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" + described = refKey(g.XR) + } + + block = joinLines(unhealthyBlock(g.Health, g.Diffs, nil, full)) + tallyComposite(&t, g.XR, g.Health, g.Diffs) + } + + if err := write(stdout, head); err != nil { + return err + } + + counts, err := r.renderDiffList(diffs, described) if err != nil { return err } 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 +376,7 @@ func (r *DefaultDiffRenderer) renderGrouped(groups []dt.XRDiffGroup) error { xrCount int unchangedXR int errorXR int + t health.Tally ) for _, g := range groups { @@ -307,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 } @@ -319,51 +396,90 @@ 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") + } + + described := "" + 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 + described = refKey(g.XR) + } + + 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, described) + 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, nil, 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/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.go b/internal/diff/renderer/health.go new file mode 100644 index 00000000..0bfd18d0 --- /dev/null +++ b/internal/diff/renderer/health.go @@ -0,0 +1,124 @@ +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. 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 { + 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] != "" || skip[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. 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) { + if h.XR != nil { + 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..b9112707 --- /dev/null +++ b/internal/diff/renderer/health_test.go @@ -0,0 +1,348 @@ +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. 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}}), + }}, + 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 1 XR 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). +`, + }, + "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). +`, + }, + } + + 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"}} + 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 + } + 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, 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"}, + 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 1 XR 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). +`, + }, + "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). +`, + }, + } + + 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/diff/result/comp.go b/internal/diff/result/comp.go index c6a1a6d4..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 @@ -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..ed0cc2f0 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,31 @@ 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 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 +} + +// ResourceHealth is the live health of one composed resource. +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 } // 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..97f98333 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,73 @@ 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, 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 + composite *dt.ResourceHealth + composed map[string]dt.ResourceHealth +} + +// 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, which the composite rendered composes +// directly. A resource recorded twice keeps its first record. +func (r *RenderedResources) AddObserved(res *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 + } + + if r.composed == nil { + r.composed = make(map[string]dt.ResourceHealth) + } + + r.composed[key] = h +} + +// Health returns the health SetComposite and AddObserved recorded, and Merge carried. +func (r *RenderedResources) Health() dt.CompositeHealth { + 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. @@ -201,6 +267,22 @@ func (r *RenderedResources) Merge(other RenderedResources) { for key, d := range other.unchanged { r.AddUnchanged(key, d) } + + // 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) + } } // 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..3347a5de --- /dev/null +++ b/internal/diff/types/types_test.go @@ -0,0 +1,157 @@ +/* +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", "") + 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 + } + 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{}, + }, + "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.SetComposite(parent) + r.AddObserved(grandchild) + r.AddObserved(child) + return r + }}, + want: dt.CompositeHealth{ + XR: healthy, + Composed: []dt.ResourceHealth{ + {ObjectReference: childRef, Health: notReady}, + {ObjectReference: grandchildRef, Health: healthy}, + }, + }, + }, + "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.SetComposite(parent) + r.AddObserved(child) + nested.SetComposite(child) + nested.AddObserved(grandchild) + r.Merge(nested) + return r + }}, + want: dt.CompositeHealth{ + XR: healthy, + Composed: []dt.ResourceHealth{ + {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}, + }, + }, + }, + } + 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/format.go b/internal/health/format.go new file mode 100644 index 00000000..20837c39 --- /dev/null +++ b/internal/health/format.go @@ -0,0 +1,433 @@ +/* +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" + "maps" + "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 +) + +// 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. +// "⚠ 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 + } + + name, cond, c := tier(h) + if cond == "" { + return []string{Marker + " " + name} + } + + line := Marker + " " + name + " (" + cond + + 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, "; ") + "." +} + +// 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/health/format_test.go b/internal/health/format_test.go new file mode 100644 index 00000000..3d44d86f --- /dev/null +++ b/internal/health/format_test.go @@ -0,0 +1,370 @@ +/* +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 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"}} + + 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/health/health.go b/internal/health/health.go new file mode 100644 index 00000000..161d5d4b --- /dev/null +++ b/internal/health/health.go @@ -0,0 +1,105 @@ +/* +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..136ff663 --- /dev/null +++ b/internal/health/health_test.go @@ -0,0 +1,160 @@ +/* +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", "") + + type args struct { + u *unstructured.Unstructured + } + cases := map[string]struct { + reason string + args args + 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.", + args: args{u: obj("")}, + want: &Health{State: Healthy}, + }, + "SyncedAndReady": { + reason: "Synced=True and Ready=True is healthy, carrying both conditions.", + args: args{u: 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.", + args: args{u: 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.", + args: args{u: 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.", + args: args{u: 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.", + 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.", + args: args{u: 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.", + args: args{u: 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.", + 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.args.u) + if diff := cmp.Diff(tc.want, got); diff != "" { + t.Errorf("%s\nOf(...): -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +func TestUnhealthy(t *testing.T) { + type args struct { + h *Health + } + cases := map[string]struct { + reason string + args args + want bool + }{ + "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.args.h.Unhealthy(); got != tc.want { + t.Errorf("%s\nUnhealthy(): want %t, got %t", tc.reason, tc.want, got) + } + }) + } +} diff --git a/internal/plan/output.go b/internal/plan/output.go index f17f67b5..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 @@ -87,7 +89,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 @@ -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/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/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/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/help/exit.md b/internal/simulate/help/exit.md index ecc54afc..ca71a130 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'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. -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/help/output.md b/internal/simulate/help/output.md index e6612f69..f1275ac9 100644 --- a/internal/simulate/help/output.md +++ b/internal/simulate/help/output.md @@ -15,7 +15,24 @@ 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 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 wasn't + 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`. 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 faa79db8..a0b1e89b 100644 --- a/internal/simulate/report.go +++ b/internal/simulate/report.go @@ -29,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" "github.com/crossplane/cli/v2/internal/plan" ) @@ -78,7 +79,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 +145,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 +221,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 +243,54 @@ 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. +// +// 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[string]bool{} + for _, p := range a.plans { + planned[health.Key(p.APIVersion, p.Kind, p.Namespace, p.Name)] = true + } + + entries := make([]health.Entry, len(a.composed)) + for i, composed := range a.composed { + c := a.composites[i] + entries[i] = health.Entry{Key: healthKey(c.Ref), HasHealth: c.Health != nil} + for _, r := range composed { + 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 { + for _, r := range composed { + 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} + if r.Via != nil { + via := resourceRef(*r.Via) + u.Via = &via + } + a.composites[i].Unhealthy = append(a.composites[i].Unhealthy, u) + } + } +} + // 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 +419,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 +448,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..0bc24cec 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,204 @@ 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") + 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 + args args + want want + }{ + "AllHealthy": { + reason: "A healthy run carries health on the composite and plans but emits no unhealthy entries, and counts nothing.", + 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}}, + }, + }, + "CreateHasNoHealth": { + reason: "A created resource has no live object, so its plan carries no health; nor does a composite not in the cluster.", + 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}}, + }, + }, + "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.", + 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{ + {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.", + 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}, + }, + }, + "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}, + }, + }, + "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{ + {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, 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}, + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + 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\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\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 dfc2fa09..1e219010 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,37 @@ 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 and theirs + // included, that are live, not healthy, and have no plan in the run (so + // 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"` +} + +// UnhealthyResource is a composed resource that is not healthy before apply. +type UnhealthyResource struct { + 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. 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. @@ -180,15 +212,72 @@ 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 { + c := r.healthTally().Counts() + return 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 +// 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 { + t.Resource(healthKey(p.ResourceRef()), p.Health) + } + for _, c := range r.Composites { + for _, u := range c.Unhealthy { + t.Resource(healthKey(u.Ref), u.Health) + } + } + 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()) +} + +// 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 -// 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 (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 - Deferred int `json:"deferred"` + Health HealthSummary `json:"health"` + Deferred int `json:"deferred"` } return json.Marshal(struct { FormatVersion string `json:"formatVersion"` @@ -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, Health: r.Health(), Deferred: r.Deferred()}, Composites: r.Composites, Compositions: r.Compositions, Plans: r.Plans, @@ -220,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: @@ -262,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")) } @@ -318,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{} @@ -330,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 @@ -343,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 @@ -378,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" { @@ -391,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 8da19d12..ac7de1dc 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" ) @@ -69,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) } @@ -90,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) } @@ -102,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) } @@ -163,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) } @@ -201,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) } @@ -214,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 { @@ -231,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) } } } @@ -250,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) } @@ -303,3 +309,310 @@ 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 { + composites string + plans string + summary string + } + type args struct { + in Result + } + cases := map[string]struct { + reason string + args args + want want + }{ + "NoLiveObjects": { + reason: "Without live objects no health or unhealthy key is written, and every health count is zero.", + args: args{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,"health":{"unhealthyComposites":0,"failing":0,"notReady":0,"paused":0},"deferred":0}`, + }, + }, + "Unhealthy": { + 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"}}}, + {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"}}},` + + `{"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,"health":{"unhealthyComposites":1,"failing":0,"notReady":1,"paused":2},"deferred":0}`, + }, + }, + } + + for name, tc := range cases { + t.Run(name, func(t *testing.T) { + var buf bytes.Buffer + 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) + } + 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.args.in, back); diff != "" { + t.Errorf("%s\nRender(json) round trip: -want, +got:\n%s", tc.reason, diff) + } + }) + } +} + +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..8210c23b 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" @@ -215,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) } } @@ -235,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") } } @@ -265,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() { @@ -295,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 @@ -421,6 +441,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 @@ -457,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. @@ -468,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 @@ -480,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 { @@ -494,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) { @@ -504,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 @@ -514,7 +536,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 +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.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 +709,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 +721,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..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" ) @@ -312,6 +313,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}}, @@ -574,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 @@ -608,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) { @@ -745,6 +795,7 @@ func TestWarnIgnoredFlags(t *testing.T) { cases := map[string]struct { reason string args []string + output string skipPlan bool project bool warn string @@ -783,6 +834,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 +882,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()) } 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/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) 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-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-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"