diff --git a/cmd/crossplane/composition/integration_test.go b/cmd/crossplane/composition/integration_test.go index 6b7368fa..53d1f76d 100644 --- a/cmd/crossplane/composition/integration_test.go +++ b/cmd/crossplane/composition/integration_test.go @@ -2511,6 +2511,26 @@ Summary: 2 modified`, WithCompositionModified(). WithXRImpact("XNopResource", "selector-match-resource", "default", "changed"), }, + // A Manual XR that names no revision yet is not pinned: Crossplane gives it the latest revision, + // the one this composition produces, so it is evaluated like an Automatic one, not deferred. + "ManualXRWithoutRevisionRefIsEvaluated": { + Reason: "--resource matching a Manual-policy composite with no compositionRevisionRef evaluates it against the new composition, ignoring a compositionRevisionSelector that rejects it, and counts it as re-pointing instead of marking it filtered", + 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", + "testdata/comp/resources/existing-xr-manual-without-revision-ref.yaml", + }, + InputFiles: []string{"testdata/comp/updated-composition-labeled.yaml"}, + Resources: []string{"default/manual-unpinned-resource"}, + OutputFormat: "json", + ExpectedExitCode: dp.ExitCodeDiffDetected, + ExpectedStructured: simtest.Expect(). + WithComposition("xnopresources.diff.example.org"). + WithCompositionModified(). + WithRevisionImpact("spec", true, 1). + WithXRImpact("XNopResource", "manual-unpinned-resource", "default", "changed"), + }, } for name, tt := range tests { diff --git a/cmd/crossplane/composition/testdata/comp/resources/existing-xr-manual-without-revision-ref.yaml b/cmd/crossplane/composition/testdata/comp/resources/existing-xr-manual-without-revision-ref.yaml new file mode 100644 index 00000000..ea8f3949 --- /dev/null +++ b/cmd/crossplane/composition/testdata/comp/resources/existing-xr-manual-without-revision-ref.yaml @@ -0,0 +1,21 @@ +# A Manual XR that names no compositionRevisionRef yet, as one has before Crossplane first reconciles +# it. Crossplane pins a Manual XR only once it names a revision; until then it gives the XR the latest +# revision and writes the ref (crossplane v2.4.3 internal/controller/apiextensions/composite/ +# api.go:163-189). The selector is there only so the fixture loader leaves the ref unset (it derives a +# ref for an XR naming neither). It deliberately rejects the diffed composition's labels +# (version: 0.0.2): Crossplane ignores a selector under Manual (api.go:203), so the XR is still +# evaluated and re-points. +apiVersion: ns.diff.example.org/v1alpha1 +kind: XNopResource +metadata: + name: manual-unpinned-resource + namespace: default +spec: + coolField: manual-unpinned-existing-value + crossplane: + compositionRef: + name: xnopresources.diff.example.org + compositionUpdatePolicy: Manual + compositionRevisionSelector: + matchLabels: + version: 0.0.1 diff --git a/internal/diff/diffprocessor/comp_processor.go b/internal/diff/diffprocessor/comp_processor.go index 2cd08c07..bb1463a7 100644 --- a/internal/diff/diffprocessor/comp_processor.go +++ b/internal/diff/diffprocessor/comp_processor.go @@ -460,8 +460,8 @@ func (p *DefaultCompDiffProcessor) processSingleComposition(ctx context.Context, // revision are exactly the ones that would re-point at it, and the ones that would not are a // consequence of applying this composition in their own right. The same classification decides, in // cliCompositionRevision, which composites are rendered with the new revision's name: a Manual - // composite kept by --include-pinned stays pinned, so it never is. - keptXRs, droppedXRs, repointingXRs, err := p.partitionXRsByUpdatePolicy(affectedXRs, newComp, pred) + // composite pinned to a revision and kept by --include-pinned stays pinned, so it never is. + keptXRs, droppedXRs, repointingXRs, unresolvedXRs, err := p.partitionXRsByUpdatePolicy(ctx, affectedXRs, newComp, pred) if err != nil { return nil, nil, err } @@ -479,6 +479,14 @@ func (p *DefaultCompDiffProcessor) processSingleComposition(ctx context.Context, // This holds even when the diffed composition itself calls no changed function and matches the // cluster: an XR pinned to an older revision that does is analysed as a metadata-scope change. pinnedXRs, pinnedKept, pinnedDropped := p.pinnedFunctionChangeXRs(ctx, keptXRs, droppedXRs) + // A claim not known to be pinned or not carries that error the same way, as its own result. + if len(unresolvedXRs) > 0 { + if pinnedXRs == nil { + pinnedXRs = make(map[string]pinnedResolution, len(unresolvedXRs)) + } + + maps.Copy(pinnedXRs, unresolvedXRs) + } // A failed lookup does not raise the scope: it says nothing about whether the revision calls a // changed function, and raising it would report unrelated drift as this composition's impact. The // XR still carries its error when the analysis runs for another reason. @@ -565,6 +573,13 @@ func (p *DefaultCompDiffProcessor) processSingleComposition(ctx context.Context, result.ImpactAnalysisSkipped = true + // A kept claim whose pin could not be read is an error the run already earned, not a verdict + // the skip withholds, so it is reported as one: without it the claim would vanish from the + // report, the error from output.Errors, and RepointedComposites would undercount silently. + impacts, summary := p.unresolvedImpacts(keptXRs, unresolvedXRs) + result.ImpactAnalysis = append(result.ImpactAnalysis, impacts...) + result.AffectedResources.WithErrors = summary.WithErrors + return result, nil, nil } @@ -611,10 +626,12 @@ func (p *DefaultCompDiffProcessor) processSingleComposition(ctx context.Context, // already-appended filtered entries. keptImpacts, keptSummary := p.buildImpactAnalysis(keptXRs, xrResults) // buildImpactAnalysis keeps keptXRs' order. A kept composite that does not re-point is one - // --include-pinned kept although its Manual policy pins it. + // --include-pinned kept although its Manual policy pins it, or a claim whose pin could not be read, + // which is not marked Pinned because whether it is pinned is unknown. for i, xr := range keptXRs { key := dt.MakeDiffKeyFromResource(xr) - keptImpacts[i].Pinned = !repointingXRs[key] + _, unknown := unresolvedXRs[key] + keptImpacts[i].Pinned = !repointingXRs[key] && !unknown if pinned, ok := pinnedXRs[key]; ok && pinned.err == nil { keptImpacts[i].RenderedRevision = pinned.resolved.RevisionName @@ -715,6 +732,11 @@ func (p *DefaultCompDiffProcessor) collectXRDiffs(ctx context.Context, xrs []*un cliCompTargetAPIVersion := cliComp.Spec.CompositeTypeRef.APIVersion cliCompTargetKind := cliComp.Spec.CompositeTypeRef.Kind + // Only the group is compared: an XR may use a served version that is not the referenceable one the + // compositeTypeRef names, and Crossplane still composes it with this composition. A malformed + // apiVersion matches nothing, as in pinnedRevisionCallsChangedFunction. + cliCompTargetGV, cliCompTargetGVErr := schema.ParseGroupVersion(cliCompTargetAPIVersion) + p.config.Logger.Debug("CLI composition targets", "apiVersion", cliCompTargetAPIVersion, "kind", cliCompTargetKind) @@ -770,17 +792,18 @@ func (p *DefaultCompDiffProcessor) collectXRDiffs(ctx context.Context, xrs []*un "resource", resourceID, "composition", cliComp.GetName()) - return p.cliCompositionRevision(res, cliComp, revisionName, revisionLabels) + return p.cliCompositionRevision(ctx, res, cliComp, revisionName, revisionLabels) } // Check 2: Does this resource's type match the CLI composition's target type? - // This handles XRs encountered during rendering that match the composition's type. - if resAPIVersion == cliCompTargetAPIVersion && resKind == cliCompTargetKind { + // This handles XRs encountered during rendering that match the composition's type, whichever + // served version they use. + if cliCompTargetGVErr == nil && resGVK.Group == cliCompTargetGV.Group && resKind == cliCompTargetKind { p.config.Logger.Debug("Resource matches CLI composition type, using CLI composition", "resource", resourceID, "composition", cliComp.GetName()) - return p.cliCompositionRevision(res, cliComp, revisionName, revisionLabels) + return p.cliCompositionRevision(ctx, res, cliComp, revisionName, revisionLabels) } // This is a nested XR with a different type - look up its composition from the cluster @@ -935,21 +958,21 @@ func pinnedRevisionCallsChangedFunction(composite *un.Unstructured, resolved dty // Its revision is the one applying that composition leaves it on, named revisionName, for a composite // that would re-point at it as classifyXR decides; revisionLabels is the label set that revision would // carry, which a compositionRevisionSelector is matched against. For any other composite the revision -// is left unknown, so it keeps the ref it has: one that is Manual (#479), being deleted, or whose +// is left unknown, so it keeps the ref it has: one a Manual policy pins to a revision (#479), being deleted, or whose // compositionRevisionSelector rejects that revision. So does every composite when revisionName is // empty, i.e. not predictable (#474). -func (p *DefaultCompDiffProcessor) cliCompositionRevision(res *un.Unstructured, cliComp *apiextensionsv1.Composition, revisionName string, revisionLabels map[string]string) (dtypes.ResolvedComposition, error) { +func (p *DefaultCompDiffProcessor) cliCompositionRevision(ctx context.Context, res *un.Unstructured, cliComp *apiextensionsv1.Composition, revisionName string, revisionLabels map[string]string) (dtypes.ResolvedComposition, error) { resolved := dtypes.ResolvedComposition{Composition: cliComp} if revisionName == "" { return resolved, nil } - disposition, err := p.classifyXR(res, revisionLabels, cliComp.GetLabels()) + disposition, err := p.classifyXR(ctx, res, cliComp.Spec.CompositeTypeRef.Kind, revisionLabels, cliComp.GetLabels()) if err != nil { return dtypes.ResolvedComposition{}, err } - if disposition.filtered == nil && !disposition.pinnedToRevision { + if disposition.filtered == nil && !disposition.pinnedToRevision && disposition.unresolved == nil { resolved.RevisionName = revisionName } @@ -1294,7 +1317,11 @@ type filteredXR struct { // `repointing` is the set of kept XRs (keyed by dt.MakeDiffKeyFromResource) that would genuinely // re-point at the resulting revision, which is not the same as the kept set: --include-pinned keeps a Manual-policy XR for analysis even though it // stays pinned to whatever compositionRevisionRef already names. See classifyXR and issue #479. -func (p *DefaultCompDiffProcessor) partitionXRsByUpdatePolicy(xrs []*un.Unstructured, newComp *un.Unstructured, pred predictedRevision) (kept []*un.Unstructured, dropped []filteredXR, repointing map[string]bool, err error) { +// +// `unresolved`, keyed the same way, holds the kept Manual claims whose backing XR could not be resolved +// to tell whether they are pinned (see xrDisposition.unresolved), each with its error: that claim's own +// result, not the run's. +func (p *DefaultCompDiffProcessor) partitionXRsByUpdatePolicy(ctx context.Context, xrs []*un.Unstructured, newComp *un.Unstructured, pred predictedRevision) (kept []*un.Unstructured, dropped []filteredXR, repointing map[string]bool, unresolved map[string]pinnedResolution, err error) { // The selector is matched against the label set the new revision would carry (composition labels // plus the stamped crossplane.io/composition-name and crossplane.io/composition-hash), while // mismatch messages display the user's own composition labels; see predictedRevisionLabels and @@ -1302,11 +1329,12 @@ func (p *DefaultCompDiffProcessor) partitionXRsByUpdatePolicy(xrs []*un.Unstruct targetLabels := predictedRevisionLabels(newComp, pred) repointing = make(map[string]bool, len(xrs)) compLabels := newComp.GetLabels() + xrKind, _, _ := un.NestedString(newComp.Object, "spec", "compositeTypeRef", "kind") for _, xr := range xrs { - disposition, classifyErr := p.classifyXR(xr, targetLabels, compLabels) + disposition, classifyErr := p.classifyXR(ctx, xr, xrKind, targetLabels, compLabels) if classifyErr != nil { - return nil, nil, nil, classifyErr + return nil, nil, nil, nil, classifyErr } if disposition.filtered != nil { @@ -1315,13 +1343,21 @@ func (p *DefaultCompDiffProcessor) partitionXRsByUpdatePolicy(xrs []*un.Unstruct } kept = append(kept, xr) + key := dt.MakeDiffKeyFromResource(xr) + + switch { + case disposition.unresolved != nil: + if unresolved == nil { + unresolved = make(map[string]pinnedResolution) + } - if !disposition.pinnedToRevision { - repointing[dt.MakeDiffKeyFromResource(xr)] = true + unresolved[key] = pinnedResolution{err: disposition.unresolved} + case !disposition.pinnedToRevision: + repointing[key] = true } } - return kept, dropped, repointing, nil + return kept, dropped, repointing, unresolved, nil } // xrDisposition is what classifyXR decided about one XR. @@ -1335,24 +1371,36 @@ type xrDisposition struct { // XR so its would-be rendered output can be inspected — without changing the fact that it does not // re-point. Counting it as re-pointing was issue #479. pinnedToRevision bool + + // unresolved is set, with nothing else, for a Manual claim naming no revision itself whose backing + // XR could not be resolved to read the revision it is pinned to. Whether it is pinned is unknown, so + // the claim is kept, not re-pointing, and reported as this error. + unresolved error } // classifyXR decides whether a single XR should be kept for impact analysis or dropped (and why), // based on whether it would adopt the CompositionRevision resulting from the diffed composition. +// xrKind is the diffed composition's compositeTypeRef kind: a composite of another kind is a claim. // targetLabels is the predicted revision label set (used for selector matching); compLabels is the // composition's own metadata.labels (used for the user-facing mismatch detail). // // Rules: // - Being deleted (metadata.deletionTimestamp set): dropped (reason deleting). Checked first, // and NOT overridden by IncludeManual, because deletion supersedes the policy rules entirely. -// - Manual compositionUpdatePolicy: dropped (reason manual_policy) — pinned via -// compositionRevisionRef — unless IncludeManual is set, in which case it is kept but marked -// pinnedToRevision: opting into analysing it does not make it adopt the new revision. +// - Manual compositionUpdatePolicy with a compositionRevisionRef: dropped (reason manual_policy), +// pinned to that revision, unless IncludeManual is set, in which case it is kept but marked +// pinnedToRevision: opting into analysing it does not make it adopt the new revision. A claim +// that names no revision itself is pinned to the one its backing XR names (see pinnedRevision). +// - Manual policy with no compositionRevisionRef: kept, and re-points. Crossplane pins only a +// composite that already names a revision; one that names none gets the latest revision, the +// one the diffed composition produces, and is pinned to it from then on (crossplane v2.4.3 +// internal/controller/apiextensions/composite/api.go:163-189). Its compositionRevisionSelector +// is ignored, as Crossplane ignores it under Manual (api.go:203). // - Automatic policy with a compositionRevisionSelector that does not match: dropped (reason // revision_selector_mismatch). NOT overridden by IncludeManual, since the XR genuinely would // not select the resulting revision. // - Automatic policy with no selector, or a matching selector: kept, and re-points. -func (p *DefaultCompDiffProcessor) classifyXR(xr *un.Unstructured, targetLabels, compLabels map[string]string) (xrDisposition, error) { +func (p *DefaultCompDiffProcessor) classifyXR(ctx context.Context, xr *un.Unstructured, xrKind string, targetLabels, compLabels map[string]string) (xrDisposition, error) { // An XR with a deletionTimestamp is on Crossplane's teardown path: the composite reconciler // deletes its composed resources instead of composing them. It will never adopt the revision // this composition would produce, and rendering it yields no composed resources to diff — so @@ -1386,28 +1434,38 @@ func (p *DefaultCompDiffProcessor) classifyXR(xr *un.Unstructured, targetLabels, "kind", xr.GetKind(), "policy", policy) - // Manual policy: pinned to a specific revision; excluded unless the user opts in. Opting in keeps - // it for analysis but does not unpin it, so it is marked as not re-pointing. + // Manual policy with a revision: pinned to it; excluded unless the user opts in. Opting in keeps + // it for analysis but does not unpin it, so it is marked as not re-pointing. With no revision it + // adopts the resulting one, as an Automatic composite does, so it falls through. if policy == compositionUpdatePolicyManual { - if p.config.IncludeManual { - return xrDisposition{pinnedToRevision: true}, nil - } - - // Name the revision it stays pinned to, when it names one. revision, err := xp.XRCompositionRevisionRef(xr.Object, xr.GetAPIVersion()) if err != nil { return xrDisposition{}, errors.Wrapf(err, "cannot read compositionRevisionRef for XR %q", xr.GetName()) } - detail := "" - if revision != "" { - detail = "compositionRevisionRef: " + revision + if revision == "" && xr.GetKind() != xrKind { + // The backing XR read is cluster I/O for this claim alone, so it runs under the claim's own + // XRTimeout budget: a slow read fails this claim, not the run. + xrCtx, cancel := p.config.xrContext(ctx) + revision, err = p.pinnedRevision(xrCtx, xr) + cancel() + + if err != nil { + return xrDisposition{unresolved: err}, nil //nolint:nilerr // The claim's own error, not the run's. + } } - return xrDisposition{filtered: &filteredXR{xr: xr, reason: dt.FilterReasonManualPolicy, detail: detail}}, nil + if revision != "" { + if p.config.IncludeManual { + return xrDisposition{pinnedToRevision: true}, nil + } + + return xrDisposition{filtered: &filteredXR{xr: xr, reason: dt.FilterReasonManualPolicy, detail: "compositionRevisionRef: " + revision}}, nil + } } - // Automatic (or default) policy: honor compositionRevisionSelector. An XR whose selector does not + // Automatic (or default) policy, or Manual with no revision: honor compositionRevisionSelector, + // which only an Automatic policy has (see xp.XRRevisionSelectorMatch). An XR whose selector does not // match the predicted revision labels would not select the resulting revision, so it is excluded — // regardless of IncludeManual. matches, detail, err := xp.XRRevisionSelectorMatch(xr, targetLabels, compLabels) @@ -1422,6 +1480,22 @@ func (p *DefaultCompDiffProcessor) classifyXR(xr *un.Unstructured, targetLabels, return xrDisposition{}, nil } +// pinnedRevision returns the compositionRevisionRef of claim's effective composite (see +// DiffProcessor.ResolveComposite): its backing XR with the claim synced in. A Manual claim usually names +// no revision itself, because Crossplane writes the pin onto the backing XR alone and copies a claim's +// ref onto it only when the claim sets one (crossplane v2.4.3 internal/controller/apiextensions/claim/ +// syncer_ssa.go:147-152 and :211). Empty when the backing XR names none either. +func (p *DefaultCompDiffProcessor) pinnedRevision(ctx context.Context, claim *un.Unstructured) (string, error) { + composite, err := p.xrProc.ResolveComposite(ctx, claim) + if err != nil { + return "", errors.Wrapf(err, "cannot resolve the backing XR of claim %q to read the revision it is pinned to", claim.GetName()) + } + + revision, err := xp.XRCompositionRevisionRef(composite.Object, composite.GetAPIVersion()) + + return revision, errors.Wrapf(err, "cannot read compositionRevisionRef of the backing XR of claim %q", claim.GetName()) +} + // filterCounts tallies dropped XRs by filter reason. A struct rather than a growing list of // positional return values, so adding a reason cannot silently transpose a caller's arguments. type filterCounts struct { @@ -1456,6 +1530,24 @@ func (c filterCounts) applyTo(summary *dt.AffectedResourcesSummary) { summary.FilteredByDeletion = c.byDeletion } +// unresolvedImpacts builds the XRStatusError impacts, in xrs' order, of the composites in xrs that +// unresolved holds (see partitionXRsByUpdatePolicy), without rendering anything. +func (p *DefaultCompDiffProcessor) unresolvedImpacts(xrs []*un.Unstructured, unresolved map[string]pinnedResolution) ([]dt.XRImpact, dt.AffectedResourcesSummary) { + var errored []*un.Unstructured + + results := make(map[string]*XRDiffResult, len(unresolved)) + + for _, xr := range xrs { + key := dt.MakeDiffKeyFromResource(xr) + if u, ok := unresolved[key]; ok { + errored = append(errored, xr) + results[key] = &XRDiffResult{Error: u.err} + } + } + + return p.buildImpactAnalysis(errored, results) +} + // buildImpactAnalysis builds the impact analysis and summary from XR results. func (p *DefaultCompDiffProcessor) buildImpactAnalysis(xrs []*un.Unstructured, results map[string]*XRDiffResult) ([]dt.XRImpact, dt.AffectedResourcesSummary) { impacts := make([]dt.XRImpact, 0, len(xrs)) diff --git a/internal/diff/diffprocessor/comp_processor_test.go b/internal/diff/diffprocessor/comp_processor_test.go index ef87f1b5..cd607253 100644 --- a/internal/diff/diffprocessor/comp_processor_test.go +++ b/internal/diff/diffprocessor/comp_processor_test.go @@ -813,9 +813,13 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { reason dt.FilterReason } + errBackingXRGone := errors.New("backing XR gone") + tests := map[string]struct { + reason string includeManual bool - compName string // defaults to "test-comp" when empty + xrTimeout time.Duration // the per-XR budget; zero adds none + compName string // defaults to "test-comp" when empty compLabels map[string]string xrs []*un.Unstructured wantKept []string @@ -825,15 +829,155 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { // except where --include-pinned keeps a Manual-policy XR, which stays pinned by its // compositionRevisionRef (issue #479). wantRepointing []string + // resolve stands in for DiffProcessor.ResolveComposite, which a Manual claim naming no revision + // itself is resolved with to read its backing XR's; unset, a composite is its own. + resolve func(context.Context, *un.Unstructured) (*un.Unstructured, error) + // wantUnresolved maps the name of each kept composite whose pin could not be read to the error it + // carries. + wantUnresolved map[string]error wantErr bool }{ + // Crossplane pins a Manual composite only once it names a revision (crossplane v2.4.3 + // internal/controller/apiextensions/composite/api.go:163); one that names none gets the latest + // revision, so it re-points like an Automatic one. Both the v2 and the legacy field paths count. + "ManualWithRevisionRefV2PathIsDropped": { + reason: "A Manual composite naming a revision at the v2 path is pinned to it, so applying the change does not reach it.", + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "XResource", "manual-xr").WithNamespace("default"). + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy"). + WithNestedField(map[string]any{"name": "test-comp-0ld0001"}, "spec", "crossplane", "compositionRevisionRef").Build(), + }, + wantDropped: []droppedWant{{name: "manual-xr", reason: dt.FilterReasonManualPolicy}}, + }, + "ManualWithRevisionRefV1PathIsDropped": { + reason: "A Manual composite naming a revision at the legacy path is pinned to it too.", + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "XResource", "manual-xr"). + WithNestedField("Manual", "spec", "compositionUpdatePolicy"). + WithNestedField(map[string]any{"name": "test-comp-0ld0001"}, "spec", "compositionRevisionRef").Build(), + }, + wantDropped: []droppedWant{{name: "manual-xr", reason: dt.FilterReasonManualPolicy}}, + }, + "ManualWithoutRevisionRefV2PathIsKeptAndRepoints": { + reason: "A Manual composite naming no revision at the v2 path gets the latest one, so it is kept and re-points.", + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "XResource", "manual-xr").WithNamespace("default"). + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy").Build(), + }, + wantKept: []string{"manual-xr"}, + wantRepointing: []string{"manual-xr"}, + }, + "ManualWithoutRevisionRefV1PathIsKeptAndRepoints": { + reason: "A Manual composite naming no revision at the legacy path gets the latest one, so it is kept and re-points.", + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "XResource", "manual-xr"). + WithNestedField("Manual", "spec", "compositionUpdatePolicy").Build(), + }, + wantKept: []string{"manual-xr"}, + wantRepointing: []string{"manual-xr"}, + }, + // --include-pinned has nothing to unpin: the composite already re-points. + "ManualWithoutRevisionRefUnderIncludeManualRepoints": { + reason: "--include-pinned has nothing to unpin: the composite already re-points.", + includeManual: true, + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "XResource", "manual-xr").WithNamespace("default"). + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy").Build(), + }, + wantKept: []string{"manual-xr"}, + wantRepointing: []string{"manual-xr"}, + }, + // Crossplane ignores a compositionRevisionSelector under Manual (api.go:203), so one that + // rejects the new revision does not keep a Manual composite with no revision off it. + "ManualWithoutRevisionRefIgnoresRejectingSelector": { + reason: "Crossplane ignores a compositionRevisionSelector under Manual, so a rejecting one does not keep a Manual composite with no revision off the new revision.", + compLabels: map[string]string{"version": "0.0.2"}, + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "XResource", "manual-xr").WithNamespace("default"). + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy"). + WithCompositionRevisionSelector(xp.CrossplaneAPIExtGroupV2, map[string]string{"version": "0.0.1"}, nil).Build(), + }, + wantKept: []string{"manual-xr"}, + wantRepointing: []string{"manual-xr"}, + }, + // A Manual claim usually names no revision itself: Crossplane writes the pin onto its backing XR + // alone (claim/syncer_ssa.go:147-152, :211). So it is pinned by its backing XR's ref. + "ManualClaimWithoutRevisionRefBackingXRPinnedIsDropped": { + reason: "A Manual claim usually names no revision itself, because Crossplane writes the pin onto its backing XR alone, so the backing XR's ref pins it.", + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "Resource", "my-claim").WithNamespace("default"). + WithNestedField("Manual", "spec", "compositionUpdatePolicy").Build(), + }, + resolve: func(context.Context, *un.Unstructured) (*un.Unstructured, error) { + return tu.NewResource("example.org/v1", "XResource", "my-claim-x7k2p"). + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy"). + WithNestedField(map[string]any{"name": "test-comp-0ld0001"}, "spec", "crossplane", "compositionRevisionRef").Build(), nil + }, + wantDropped: []droppedWant{{name: "my-claim", reason: dt.FilterReasonManualPolicy}}, + }, + "ManualClaimWithoutRevisionRefBackingXRUnpinnedIsKeptAndRepoints": { + reason: "A Manual claim whose backing XR names no revision either is not pinned, so it is kept and re-points.", + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "Resource", "my-claim").WithNamespace("default"). + WithNestedField("Manual", "spec", "compositionUpdatePolicy").Build(), + }, + resolve: func(context.Context, *un.Unstructured) (*un.Unstructured, error) { + return tu.NewResource("example.org/v1", "XResource", "my-claim"). + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy").Build(), nil + }, + wantKept: []string{"my-claim"}, + wantRepointing: []string{"my-claim"}, + }, + // Whether the claim is pinned is unknown, so it is neither filtered nor counted as re-pointing: + // it is that claim's own error, not the run's. + "ManualClaimWithoutRevisionRefResolveFailureIsKeptAsError": { + reason: "Whether the claim is pinned is unknown when its backing XR cannot be resolved, so it is neither filtered nor counted as re-pointing: it is that claim's own error, not the run's.", + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "Resource", "my-claim").WithNamespace("default"). + WithNestedField("Manual", "spec", "compositionUpdatePolicy").Build(), + }, + resolve: func(context.Context, *un.Unstructured) (*un.Unstructured, error) { + return nil, errBackingXRGone + }, + wantKept: []string{"my-claim"}, + wantUnresolved: map[string]error{"my-claim": errBackingXRGone}, + }, + "ManualClaimWithoutRevisionRefSlowLookupTimesOutAlone": { + reason: "The backing XR read runs under the claim's own XRTimeout, so a slow one fails that claim alone, as a failed read does.", + xrTimeout: 10 * time.Millisecond, + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "Resource", "my-claim").WithNamespace("default"). + WithNestedField("Manual", "spec", "compositionUpdatePolicy").Build(), + }, + resolve: func(ctx context.Context, _ *un.Unstructured) (*un.Unstructured, error) { + <-ctx.Done() + + return nil, context.Cause(ctx) + }, + wantKept: []string{"my-claim"}, + wantUnresolved: map[string]error{"my-claim": ErrXRTimedOut}, + }, + // A claim naming its own revision is pinned by it, without resolving its backing XR. + "ManualClaimWithRevisionRefIsDroppedWithoutResolving": { + reason: "A claim naming its own revision is pinned by it, without resolving its backing XR.", + xrs: []*un.Unstructured{ + tu.NewResource("example.org/v1", "Resource", "my-claim").WithNamespace("default"). + WithNestedField("Manual", "spec", "compositionUpdatePolicy"). + WithNestedField(map[string]any{"name": "test-comp-0ld0001"}, "spec", "compositionRevisionRef").Build(), + }, + resolve: func(context.Context, *un.Unstructured) (*un.Unstructured, error) { + return nil, errors.New("resolved a claim that names its revision") + }, + wantDropped: []droppedWant{{name: "my-claim", reason: dt.FilterReasonManualPolicy}}, + }, // Manual side: --include-pinned keeps Manual XRs... "IncludeManualTrue_KeepsManualXRs": { includeManual: true, compLabels: map[string]string{"version": "0.0.2"}, xrs: []*un.Unstructured{ tu.NewResource("example.org/v1", "XResource", "manual-xr").WithNamespace("default"). - WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy").Build(), + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy"). + WithNestedField(map[string]any{"name": "test-comp-0ld0001"}, "spec", "crossplane", "compositionRevisionRef").Build(), tu.NewResource("example.org/v1", "XResource", "auto-xr").WithNamespace("default"). WithNestedField("Automatic", "spec", "crossplane", "compositionUpdatePolicy").Build(), }, @@ -849,7 +993,8 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { compLabels: map[string]string{"version": "0.0.2"}, xrs: []*un.Unstructured{ tu.NewResource("example.org/v1", "XResource", "manual-only").WithNamespace("default"). - WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy").Build(), + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy"). + WithNestedField(map[string]any{"name": "test-comp-0ld0001"}, "spec", "crossplane", "compositionRevisionRef").Build(), }, wantKept: []string{"manual-only"}, wantRepointing: nil, @@ -862,7 +1007,8 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { compLabels: map[string]string{"version": "0.0.2"}, xrs: []*un.Unstructured{ tu.NewResource("example.org/v1", "XResource", "manual-xr").WithNamespace("default"). - WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy").Build(), + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy"). + WithNestedField(map[string]any{"name": "test-comp-0ld0001"}, "spec", "crossplane", "compositionRevisionRef").Build(), tu.NewResource("example.org/v1", "XResource", "auto-mismatch").WithNamespace("default"). WithNestedField("Automatic", "spec", "crossplane", "compositionUpdatePolicy"). WithCompositionRevisionSelector(xp.CrossplaneAPIExtGroupV2, map[string]string{"version": "0.0.1"}, nil).Build(), @@ -877,7 +1023,8 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { compLabels: map[string]string{"version": "0.0.2"}, xrs: []*un.Unstructured{ tu.NewResource("example.org/v1", "XResource", "manual-xr").WithNamespace("default"). - WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy").Build(), + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy"). + WithNestedField(map[string]any{"name": "test-comp-0ld0001"}, "spec", "crossplane", "compositionRevisionRef").Build(), tu.NewResource("example.org/v1", "XResource", "auto-xr").WithNamespace("default"). WithNestedField("Automatic", "spec", "crossplane", "compositionUpdatePolicy").Build(), }, @@ -969,6 +1116,7 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { xrs: []*un.Unstructured{ tu.NewResource("example.org/v1", "XResource", "manual-match").WithNamespace("default"). WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy"). + WithNestedField(map[string]any{"name": "test-comp-0ld0001"}, "spec", "crossplane", "compositionRevisionRef"). WithCompositionRevisionSelector(xp.CrossplaneAPIExtGroupV2, map[string]string{"version": "0.0.2"}, nil).Build(), }, wantKept: nil, @@ -1062,8 +1210,10 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { processor := &DefaultCompDiffProcessor{ config: ProcessorConfig{ IncludeManual: tt.includeManual, + XRTimeout: tt.xrTimeout, Logger: tu.TestLogger(t, false), }, + xrProc: &tu.MockDiffProcessor{ResolveCompositeFn: tt.resolve}, } compName := tt.compName @@ -1078,7 +1228,7 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { pred := predictRevision(typedComposition(t, newComp)) - kept, dropped, repointing, err := processor.partitionXRsByUpdatePolicy(tt.xrs, newComp, pred) + kept, dropped, repointing, unresolved, err := processor.partitionXRsByUpdatePolicy(t.Context(), tt.xrs, newComp, pred) if tt.wantErr { if err == nil { @@ -1098,7 +1248,7 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { } if diff := gcmp.Diff(tt.wantKept, gotKept, cmpopts.EquateEmpty()); diff != "" { - t.Errorf("kept XR names mismatch (-want +got):\n%s", diff) + t.Errorf("%s\nkept XR names mismatch (-want +got):\n%s", tt.reason, diff) } gotDropped := make([]droppedWant, len(dropped)) @@ -1107,7 +1257,7 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { } if diff := gcmp.Diff(tt.wantDropped, gotDropped, cmpopts.EquateEmpty(), gcmp.AllowUnexported(droppedWant{})); diff != "" { - t.Errorf("dropped XRs mismatch (-want +got):\n%s", diff) + t.Errorf("%s\ndropped XRs mismatch (-want +got):\n%s", tt.reason, diff) } var gotRepointing []string @@ -1119,7 +1269,19 @@ func TestDefaultCompDiffProcessor_partitionXRsByUpdatePolicy(t *testing.T) { } if diff := gcmp.Diff(tt.wantRepointing, gotRepointing, cmpopts.EquateEmpty()); diff != "" { - t.Errorf("repointing XRs mismatch (-want +got):\n%s", diff) + t.Errorf("%s\nrepointing XRs mismatch (-want +got):\n%s", tt.reason, diff) + } + + gotUnresolved := make(map[string]error) + + for _, xr := range kept { + if u, ok := unresolved[dt.MakeDiffKeyFromResource(xr)]; ok && u.err != nil { + gotUnresolved[xr.GetName()] = u.err + } + } + + if diff := gcmp.Diff(tt.wantUnresolved, gotUnresolved, cmpopts.EquateEmpty(), cmpopts.EquateErrors()); diff != "" { + t.Errorf("%s\nunresolved XRs mismatch (-want +got):\n%s", tt.reason, diff) } }) } @@ -1235,10 +1397,14 @@ func TestDefaultCompDiffProcessor_collectXRDiffs_RevisionName(t *testing.T) { manual := tu.NewResource("example.org/v1", "XResource", "manual-xr").WithNamespace("default"). WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy"). WithNestedField(map[string]any{"name": "comp-0ld0001"}, "spec", "crossplane", "compositionRevisionRef").Build() + manualWithoutRef := tu.NewResource("example.org/v1", "XResource", "manual-unpinned-xr").WithNamespace("default"). + WithNestedField("Manual", "spec", "crossplane", "compositionUpdatePolicy").Build() selective := tu.NewResource("example.org/v1", "XResource", "selective-xr").WithNamespace("default"). WithCompositionRevisionSelector(xp.CrossplaneAPIExtGroupV2, map[string]string{"channel": "stable"}, nil).Build() claim := tu.NewResource("example.org/v1", "Resource", "my-claim").WithNamespace("default").Build() backing := tu.NewResource("example.org/v1", "XResource", "my-claim-x7k2p").Build() + // backingOlderVersion is a backing XR on a served version that is not the referenceable one. + backingOlderVersion := tu.NewResource("example.org/v1alpha1", "XResource", "my-claim-x7k2p").Build() // The labels the predicted revision would carry; the selective composite's selector rejects them. revisionLabels := map[string]string{xp.LabelCompositionName: "comp"} @@ -1267,11 +1433,24 @@ func TestDefaultCompDiffProcessor_collectXRDiffs_RevisionName(t *testing.T) { revisionName: predicted, wantRevision: predicted, }, + "ClaimsBackingXROnOlderServedVersionGetsThePredictedName": { + reason: "A backing XR on a served version other than the compositeTypeRef's is matched by group and kind, so it renders with the diffed composition and gets the name, as the report says.", + xr: claim, + rendered: backingOlderVersion, + revisionName: predicted, + wantRevision: predicted, + }, "ManualCompositeKeepsItsRef": { - reason: "A Manual composite stays pinned, so no revision is resolved and it keeps the ref it has.", + reason: "A Manual composite that names a revision stays pinned, so no revision is resolved and it keeps the ref it has.", xr: manual, revisionName: predicted, }, + "ManualCompositeWithoutRefGetsThePredictedName": { + reason: "A Manual composite naming no revision gets the latest one, so it is rendered with the new revision's name.", + xr: manualWithoutRef, + revisionName: predicted, + wantRevision: predicted, + }, "SelectorRejectingTheRevisionKeepsItsRef": { reason: "An Automatic composite whose compositionRevisionSelector rejects the new revision does not adopt it.", xr: selective, @@ -1913,11 +2092,14 @@ func TestDefaultCompDiffProcessor_DiffComposition_ResourceMode(t *testing.T) { WithSpecField("compositionRef", map[string]any{"name": "test-comp"}). Build() - // Manual-policy XR (v2 path). + // Manual-policy XR (v2 path), pinned: Crossplane pins only a Manual XR that names a revision. manualXR := tu.NewResource("example.org/v1", "XR", "manual-xr"). InNamespace("ns"). WithSpecField("compositionRef", map[string]any{"name": "test-comp"}). - WithSpecField("crossplane", map[string]any{"compositionUpdatePolicy": "Manual"}). + WithSpecField("crossplane", map[string]any{ + "compositionUpdatePolicy": "Manual", + "compositionRevisionRef": map[string]any{"name": "test-comp-0ld0001"}, + }). Build() // DispatchesToCorrectFindMode: DiffComposition routes to ref-lookup vs default-discovery @@ -1983,7 +2165,7 @@ func TestDefaultCompDiffProcessor_DiffComposition_ResourceMode(t *testing.T) { // RecordsFilteredInBothModes: Manual-policy XRs are recorded as XRStatusFiltered impacts (reason // manual_policy) whether the set came from --resource or from discovery; Scoped records which, so - // the renderer can decide whether to name them. A Manual XR that names its revision carries it as + // the renderer can decide whether to name them. Each carries the revision it is pinned to as // FilterDetail. t.Run("RecordsFilteredInBothModes", func(t *testing.T) { pinnedManualXR := tu.NewResource("example.org/v1", "XR", "pinned-manual-xr"). @@ -2011,7 +2193,7 @@ func TestDefaultCompDiffProcessor_DiffComposition_ResourceMode(t *testing.T) { } wantImpacts := []dt.XRImpact{ - filtered("manual-xr", ""), + filtered("manual-xr", "compositionRevisionRef: test-comp-0ld0001"), filtered("pinned-manual-xr", "compositionRevisionRef: test-comp-abc1234"), } @@ -2082,12 +2264,42 @@ func TestDefaultCompDiffProcessor_DiffComposition_ResourceMode(t *testing.T) { Impacts []string } + // A Manual claim naming no revision itself, whose pin is read from its backing XR. + claimWithoutRef := tu.NewResource("example.org/v1", "Claim", "unresolved-claim"). + InNamespace("ns"). + WithSpecField("crossplane", map[string]any{"compositionUpdatePolicy": "Manual"}). + Build() + cases := map[string]struct { + reason string analyzeOn AnalyzeOn includeManual bool scoped bool - want want + // unresolvedClaim adds claimWithoutRef, whose backing XR cannot be resolved. + unresolvedClaim bool + want want }{ + "SkippedUnresolvedClaimKeepsItsError": { + reason: "Whether the claim is pinned is unknown, and that error is a verdict the run did earn, so a skip must not drop it: it stays in the report, the error counts and the exit status. It is not counted as re-pointing, and not marked Pinned.", + analyzeOn: AnalyzeOnSpecChange, + unresolvedClaim: true, + want: want{ + Skipped: true, + Summary: dt.AffectedResourcesSummary{Total: 3, WithErrors: 1, FilteredByPolicy: 1}, + RevisionImpact: dt.RevisionImpact{ChangeScope: "metadata", CreatesRevision: true, RepointedComposites: 1}, + Impacts: []string{"filtered manual-xr", "error unresolved-claim"}, + }, + }, + "AnalysedUnresolvedClaimKeepsItsError": { + reason: "The same unresolved claim is reported as an error when the analysis does run, next to the verdicts of the composites that were evaluated.", + analyzeOn: AnalyzeOnAnyChange, + unresolvedClaim: true, + want: want{ + Summary: dt.AffectedResourcesSummary{Total: 3, Unchanged: 1, WithErrors: 1, FilteredByPolicy: 1}, + RevisionImpact: dt.RevisionImpact{ChangeScope: "metadata", CreatesRevision: true, RepointedComposites: 1}, + Impacts: []string{"filtered manual-xr", "unchanged xr-1", "error unresolved-claim"}, + }, + }, // Discovery records the filtered composite too; only the renderer leaves it unnamed. "Skipped_DefaultDiscovery_RecordsFilteredComposites": { analyzeOn: AnalyzeOnSpecChange, @@ -2144,7 +2356,19 @@ func TestDefaultCompDiffProcessor_DiffComposition_ResourceMode(t *testing.T) { proc := newCompProcessorForTest(t, client, tt.includeManual) proc.config.AnalyzeOn = tt.analyzeOn - got, _, err := proc.processSingleComposition(t.Context(), metaOnlyComp, []*un.Unstructured{xr1, manualXR}, tt.scoped) + xrs := []*un.Unstructured{xr1, manualXR} + if tt.unresolvedClaim { + xrs = append(xrs, claimWithoutRef) + proc.xrProc.(*tu.MockDiffProcessor).ResolveCompositeFn = func(_ context.Context, res *un.Unstructured) (*un.Unstructured, error) { + if res.GetKind() == "Claim" { + return nil, errors.New("backing XR gone") + } + + return res, nil + } + } + + got, _, err := proc.processSingleComposition(t.Context(), metaOnlyComp, xrs, tt.scoped) if err != nil { t.Fatalf("processSingleComposition: %v", err) } @@ -2174,7 +2398,7 @@ func TestDefaultCompDiffProcessor_DiffComposition_ResourceMode(t *testing.T) { } if diff := gcmp.Diff(tt.want, gotWant, cmpopts.EquateEmpty()); diff != "" { - t.Errorf("processSingleComposition() mismatch (-want +got):\n%s", diff) + t.Errorf("%s\nprocessSingleComposition() mismatch (-want +got):\n%s", tt.reason, diff) } }) } @@ -2363,13 +2587,16 @@ func TestDefaultCompDiffProcessor_processSingleComposition_PinnedXRWithChangedFu "compositionRevisionRef": map[string]any{"name": "test-comp-rev1"}, }). Build() - resolveComposite := func(_ context.Context, res *un.Unstructured) (*un.Unstructured, error) { - if res.GetKind() == "Claim" { - return pinnedXR("pinned-xyz"), nil - } - - return res, nil - } + // A Manual claim naming no revision itself, as Crossplane leaves one: the pin is on its backing XR. + claimWithoutRef := tu.NewResource("example.org/v1", "Claim", "pinned").InNamespace("ns"). + WithSpecField("crossplane", map[string]any{"compositionUpdatePolicy": "Manual"}). + Build() + // The backing XR of a claim not bound to a revision yet, which Crossplane gives the latest one. + unpinnedXR := tu.NewResource("example.org/v1", "XR", "pinned-xyz"). + InNamespace("ns"). + WithSpecField("compositionRef", map[string]any{"name": "test-comp"}). + WithSpecField("crossplane", map[string]any{"compositionUpdatePolicy": "Manual"}). + Build() // The pinned revision's pipeline has a step the CLI composition lacks, so a render that saw the // CLI composition instead is detectable. @@ -2392,12 +2619,18 @@ func TestDefaultCompDiffProcessor_processSingleComposition_PinnedXRWithChangedFu } cases := map[string]struct { + reason string revision *apiextensionsv1.Composition lookupErr error includeManual bool unrelated bool - claim bool - want want + // claim is the composite diffed instead of a pinned XR, and backing its effective composite + // (pinnedXR("pinned-xyz") when nil). + claim *un.Unstructured + backing *un.Unstructured + // resolveErr fails resolving the claim's effective composite. + resolveErr error + want want }{ "PinnedXRWithChangedFunctionRendersPinnedRevision": { revision: revisionWith("function-a"), @@ -2411,14 +2644,34 @@ func TestDefaultCompDiffProcessor_processSingleComposition_PinnedXRWithChangedFu // backing XR, finds the pin under its key. "PinnedClaimWithChangedFunctionRendersPinnedRevision": { revision: revisionWith("function-a"), - claim: true, + claim: claim, want: want{Status: dt.XRStatusUnchanged, Pinned: true, RenderedRevision: "test-comp-rev1", Pipeline: []string{"function-a"}}, }, "PinnedClaimWithUnrelatedFunctionChangeStaysDeferred": { revision: revisionWith("function-other"), - claim: true, + claim: claim, want: want{Status: dt.XRStatusFiltered, FilterReason: dt.FilterReasonManualPolicy}, }, + "ClaimPinnedByBackingXRWithChangedFunctionRendersPinnedRevision": { + reason: "A Manual claim naming no revision is pinned by its backing XR's, so it is treated the same.", + revision: revisionWith("function-a"), + claim: claimWithoutRef, + want: want{Status: dt.XRStatusUnchanged, Pinned: true, RenderedRevision: "test-comp-rev1", Pipeline: []string{"function-a"}}, + }, + "ClaimWithoutRevisionRenderedAgainstDiffedComposition": { + reason: "With no revision on the backing XR either, the claim is not pinned: Crossplane gives it the latest revision, so it renders against the diffed composition and re-points, like an Automatic one, whatever the revision it is not pinned to calls.", + revision: revisionWith("function-other"), + claim: claimWithoutRef, + backing: unpinnedXR, + want: want{Status: dt.XRStatusUnchanged, Pipeline: []string{"function-a"}}, + }, + "ClaimWithoutRevisionUnresolvableBackingXRIsPerClaimError": { + reason: "Whether that claim is pinned is unknown when its backing XR cannot be resolved: the claim's own error, with no render, and not counted as re-pointing.", + revision: revisionWith("function-a"), + claim: claimWithoutRef, + resolveErr: errors.New("backing XR gone"), + want: want{Status: dt.XRStatusError, Error: true}, + }, "PinnedXRWithChangedFunctionRendersWhenCompositionCallsNone": { revision: revisionWith("function-a"), unrelated: true, @@ -2472,6 +2725,19 @@ func TestDefaultCompDiffProcessor_processSingleComposition_PinnedXRWithChangedFu gotRevision string ) + backing := tt.backing + if backing == nil { + backing = pinnedXR("pinned-xyz") + } + + resolveComposite := func(_ context.Context, res *un.Unstructured) (*un.Unstructured, error) { + if res.GetKind() == "Claim" { + return backing, tt.resolveErr + } + + return res, nil + } + proc.xrProc = &tu.MockDiffProcessor{ ResolveCompositeFn: resolveComposite, // Like the real one, the provider is handed the effective composite. @@ -2496,8 +2762,8 @@ func TestDefaultCompDiffProcessor_processSingleComposition_PinnedXRWithChangedFu } input := pinnedXR("pinned") - if tt.claim { - input = claim + if tt.claim != nil { + input = tt.claim } got, _, err := proc.processSingleComposition(t.Context(), cliComp.BuildAsUnstructured(), []*un.Unstructured{input}, false) @@ -2521,7 +2787,7 @@ func TestDefaultCompDiffProcessor_processSingleComposition_PinnedXRWithChangedFu // A lookup failure is reported on the XR, and no render is attempted for it. if diff := gcmp.Diff(tt.want, gotWant); diff != "" { - t.Errorf("processSingleComposition() mismatch (-want +got):\n%s", diff) + t.Errorf("%s\nprocessSingleComposition() mismatch (-want +got):\n%s", tt.reason, diff) } if tt.want.RenderedRevision != "" && gotRevision != tt.want.RenderedRevision { diff --git a/internal/diff/result/comp.go b/internal/diff/result/comp.go index f40a4144..2ab1eb45 100644 --- a/internal/diff/result/comp.go +++ b/internal/diff/result/comp.go @@ -44,7 +44,9 @@ type FilterReason string const ( // FilterReasonManualPolicy indicates the XR was excluded because it has a Manual - // compositionUpdatePolicy and --include-pinned was not set. Such XRs are pinned to a specific + // compositionUpdatePolicy, is pinned by a compositionRevisionRef, and --include-pinned was not set. + // The ref is the XR's own or, for a claim naming none, its backing XR's: Crossplane writes the pin + // onto the backing XR alone unless the claim sets one. Such XRs are pinned to a specific // revision and would not adopt the composition change automatically. FilterReasonManualPolicy FilterReason = "manual_policy" // FilterReasonRevisionSelectorMismatch indicates the XR was excluded because it has an Automatic @@ -131,8 +133,12 @@ type RevisionImpact struct { // RepointedComposites counts the composites that would adopt that revision and reconcile as a // result: those with an Automatic (or defaulted) compositionUpdatePolicy whose // compositionRevisionSelector, if any, selects the resulting revision, and which are not being - // deleted. A Manual-policy composite is never counted — it is pinned by compositionRevisionRef, so - // --include-pinned keeping it for analysis does not make it re-point (issue #479). + // deleted, plus those with a Manual policy that name no compositionRevisionRef yet, which Crossplane + // gives the latest revision. A Manual-policy composite that names a revision (for a claim, whose + // backing XR does) is never counted — it is pinned to it, so --include-pinned keeping it for + // analysis does not make it re-point (issue #479). Nor is a Manual claim naming no revision whose + // backing XR could not be resolved: it is reported as an XRStatusError instead, even when the + // analysis is skipped. // // Note this counts composites that re-point, which is not the same as composites whose rendered // output changes; re-pointing alone usually renders identically. @@ -205,8 +211,8 @@ type AffectedResourcesSummary struct { WithChanges int `json:"withChanges"` Unchanged int `json:"unchanged"` WithErrors int `json:"withErrors"` - // FilteredByPolicy counts XRs excluded because of a Manual compositionUpdatePolicy - // (FilterReasonManualPolicy). + // FilteredByPolicy counts XRs excluded because a Manual compositionUpdatePolicy pins them to a + // compositionRevisionRef (FilterReasonManualPolicy). FilteredByPolicy int `json:"filteredByPolicy,omitempty"` // FilteredBySelector counts XRs excluded because their compositionRevisionSelector does not match // the diffed composition's labels (FilterReasonRevisionSelectorMismatch). Kept separate from @@ -230,12 +236,16 @@ type XRImpact struct { FilterReason FilterReason // FilterDetail is an optional human-readable explanation for a filtered outcome (e.g. which // selector failed to match which labels), surfaced to help users self-diagnose the exclusion. A - // Manual-policy composite names the revision it stays pinned to ("compositionRevisionRef: ") - // when it has a ref. + // Manual-policy composite names the revision it stays pinned to ("compositionRevisionRef: "). FilterDetail string // Pinned marks an evaluated composite that would not adopt the composition's resulting revision, - // because its Manual compositionUpdatePolicy pins it to its compositionRevisionRef. Only - // --include-pinned evaluates such a composite; without it, the composite is XRStatusFiltered. + // because its Manual compositionUpdatePolicy pins it to its compositionRevisionRef. It is an + // attribute of the composite, not a status: only one filtered for it (FilterReasonManualPolicy) is + // deferred. Such a composite is evaluated under --include-pinned, or without it when its pinned + // revision calls a changed function (see RenderedRevision); otherwise it is XRStatusFiltered. A Manual + // composite that names no revision is not pinned: Crossplane gives it the latest revision. A claim + // whose backing XR could not be resolved is an XRStatusError and not Pinned, since whether it is + // pinned is unknown. Pinned bool // RenderedRevision names the CompositionRevision a Pinned composite was rendered against, when that // is its own pinned revision rather than the diffed composition: the revision calls a function that diff --git a/internal/simulate/help/selection.md b/internal/simulate/help/selection.md index d2e8c2e5..c9ee1888 100644 --- a/internal/simulate/help/selection.md +++ b/internal/simulate/help/selection.md @@ -7,10 +7,14 @@ how the change prints: `--namespace`, or is cluster scoped without it. - `--include-pinned` renders every pinned composite resource against the new Composition. By default `simulate` skips composite resources with a `Manual` - composition update policy, because they don't adopt a new Composition - revision on their own. A pinned composite whose revision calls a function - the run changes is still simulated, rendered against the revision it's - pinned to. + composition update policy that are pinned to a Composition revision by + `compositionRevisionRef`, because they don't adopt a new revision on their + own. A `Manual` composite resource with no revision yet gets the new one, + so it's simulated like any other. A pinned composite whose revision calls + a function the run changes is simulated even without the flag, rendered + against the revision it's pinned to, so the preview shows what applying + the change does to it today. With the flag it's rendered against the new + Composition instead, which previews the composite advancing to it. - `--analyze-on` sets the smallest Composition change that triggers impact analysis: `spec-change` only when the Composition spec changes; `any-change`, the default, also when only its metadata changes, which still diff --git a/internal/simulate/report.go b/internal/simulate/report.go index 6a6bed0e..68869db6 100644 --- a/internal/simulate/report.go +++ b/internal/simulate/report.go @@ -156,10 +156,12 @@ func FromCompReport(o *dt.CompDiffOutput, plans *ProviderPlans, keep func(*unstr return res } -// compositeResult maps an affected composite's impact onto its status. A -// Manual-policy composite is deferred: applying changes nothing for it until -// its revision advances, unless it was rendered against its pinned revision -// because that calls a changed function. Other filtered composites are skipped. +// compositeResult maps an affected composite's impact onto its status. Pinned +// is an attribute, not a status: a composite filtered for its Manual policy is +// deferred, since applying changes nothing for it until its revision advances, +// while a pinned one that was simulated anyway (under --include-pinned, or +// rendered against its pinned revision because that calls a changed function) +// keeps the status of its simulation. Other filtered composites are skipped. func compositeResult(impact dt.XRImpact) CompositeResult { c := CompositeResult{Ref: resourceRef(impact.ObjectReference), Pinned: impact.Pinned || impact.FilterReason == dt.FilterReasonManualPolicy} diff --git a/internal/simulate/result.go b/internal/simulate/result.go index 1e25976d..91c03bdb 100644 --- a/internal/simulate/result.go +++ b/internal/simulate/result.go @@ -54,7 +54,9 @@ type CompositeReason string const ( // CompositeReasonManualPolicy means the composite has a Manual - // compositionUpdatePolicy, so it is pinned to its current revision. + // compositionUpdatePolicy and is pinned to a revision by a + // compositionRevisionRef: its own or, for a claim naming none, its + // composite resource's. CompositeReasonManualPolicy CompositeReason = "manualPolicy" // CompositeReasonRevisionSelectorMismatch means the composite's // compositionRevisionSelector does not match the changed revision.