diff --git a/internal/diff/diffprocessor/comp_processor.go b/internal/diff/diffprocessor/comp_processor.go index 2cd08c07..408c21a6 100644 --- a/internal/diff/diffprocessor/comp_processor.go +++ b/internal/diff/diffprocessor/comp_processor.go @@ -527,6 +527,7 @@ func (p *DefaultCompDiffProcessor) processSingleComposition(ctx context.Context, // list them or only count them is the renderer's decision (see CompositionDiff.Scoped). for _, d := range droppedXRs { result.ImpactAnalysis = append(result.ImpactAnalysis, dt.XRImpact{ + ComposedBy: parentCompositeRef(d.xr), ObjectReference: corev1.ObjectReference{ APIVersion: d.xr.GetAPIVersion(), Kind: d.xr.GetKind(), @@ -1467,7 +1468,7 @@ func (p *DefaultCompDiffProcessor) buildImpactAnalysis(xrs []*un.Unstructured, r resourceID := dt.MakeDiffKeyFromResource(xr) result := results[resourceID] - impact := dt.XRImpact{ObjectReference: objectRef(xr)} + impact := dt.XRImpact{ObjectReference: objectRef(xr), ComposedBy: parentCompositeRef(xr)} switch { case result != nil && result.HasError(): @@ -1495,3 +1496,23 @@ func (p *DefaultCompDiffProcessor) buildImpactAnalysis(xrs []*un.Unstructured, r return impacts, summary } + +// parentCompositeRef returns a reference to the parent composite of a nested XR, +// derived from its controller owner reference; nil for a top-level XR. Only +// an XR that carries the composite label is considered nested. +func parentCompositeRef(xr *un.Unstructured) *corev1.ObjectReference { + if _, ok := xr.GetLabels()[LabelComposite]; !ok { + return nil + } + for _, ref := range xr.GetOwnerReferences() { + if ref.Controller != nil && *ref.Controller { + return &corev1.ObjectReference{ + APIVersion: ref.APIVersion, + Kind: ref.Kind, + Name: ref.Name, + Namespace: xr.GetNamespace(), + } + } + } + return nil +} diff --git a/internal/diff/diffprocessor/resource_manager.go b/internal/diff/diffprocessor/resource_manager.go index cabde6f0..d8cd7e01 100644 --- a/internal/diff/diffprocessor/resource_manager.go +++ b/internal/diff/diffprocessor/resource_manager.go @@ -173,6 +173,14 @@ func (m *DefaultResourceManager) checkCompositeOwnership(ctx context.Context, cu return } + // A new owner that is itself composed by the resource's current owner is + // not taking ownership away from it: the resource is moving within one + // composition tree, from a parent XR to a nested XR it composes. That is + // the normal shape of a nested composition, not something to warn about. + if composite.GetLabels()[LabelComposite] == owner { + return + } + m.advisories.Raise(dt.OutputWarning{ // TODO: should we fail by default here? maybe require a --force flag to proceed? Message: "Resource already belongs to another composite. Applying this diff will assume ownership!", @@ -182,6 +190,8 @@ func (m *DefaultResourceManager) checkCompositeOwnership(ctx context.Context, cu "currentOwner": owner, "newOwner": newOwner, }, + // One line per owner pair is enough for a human; structured output keeps every resource. + GroupBy: []string{"currentOwner", "newOwner"}, }) } diff --git a/internal/diff/diffprocessor/warnings.go b/internal/diff/diffprocessor/warnings.go index e852eb48..b5eac403 100644 --- a/internal/diff/diffprocessor/warnings.go +++ b/internal/diff/diffprocessor/warnings.go @@ -67,12 +67,23 @@ type AdvisoryCollector struct { // seen holds the identity of every advisory already recorded, so an identical repeat is dropped // from both channels rather than re-emitted. Keyed by warningKey. seen map[string]struct{} + // groups tracks advisory families (see dt.OutputWarning.GroupBy): the first member of a family + // is streamed to stderr as usual, later members are only collected, and Summarize reports how + // many were withheld per family. Keyed by message plus the GroupBy context pairs. + groups map[string]*advisoryGroup +} + +// advisoryGroup is the stderr-side state of one advisory family. +type advisoryGroup struct { + message string + context map[string]string + suppressed int } // NewAdvisoryCollector returns a collector that writes each advisory to stderr and collects it for // structured output. A nil stderr only collects. func NewAdvisoryCollector(stderr io.Writer) *AdvisoryCollector { - return &AdvisoryCollector{stderr: stderr, seen: map[string]struct{}{}} + return &AdvisoryCollector{stderr: stderr, seen: map[string]struct{}{}, groups: map[string]*advisoryGroup{}} } // Raise records w: written to stderr now, and collected for structured output. An advisory identical @@ -90,11 +101,65 @@ func (c *AdvisoryCollector) Raise(w dt.OutputWarning) { c.seen[key] = struct{}{} c.advisories = append(c.advisories, w) - if c.stderr != nil { - // A failed write to stderr is not worth failing a diff over, and there is nowhere useful to - // report it: the reporting channel is the thing that just failed. - _, _ = fmt.Fprintln(c.stderr, w.FormatWarning()) + if c.stderr == nil { + return + } + + // A grouped advisory streams only its family's first member; Summarize reports the rest. + if len(w.GroupBy) > 0 { + gk := groupKeyOf(w) + if g, ok := c.groups[gk]; ok { + g.suppressed++ + return + } + gctx := make(map[string]string, len(w.GroupBy)) + for _, k := range w.GroupBy { + gctx[k] = w.Context[k] + } + c.groups[gk] = &advisoryGroup{message: w.Message, context: gctx} + } + + // A failed write to stderr is not worth failing a diff over, and there is nowhere useful to + // report it: the reporting channel is the thing that just failed. + _, _ = fmt.Fprintln(c.stderr, w.FormatWarning()) +} + +// Summarize writes one stderr line per advisory family with withheld members, reporting how many +// similar advisories were not streamed. Call it once the run is done raising advisories; structured +// output needs no equivalent, since it kept every occurrence. +func (c *AdvisoryCollector) Summarize() { + c.mu.Lock() + defer c.mu.Unlock() + + if c.stderr == nil { + return } + + keys := slices.Sorted(maps.Keys(c.groups)) + for _, gk := range keys { + g := c.groups[gk] + if g.suppressed == 0 { + continue + } + _, _ = fmt.Fprintln(c.stderr, dt.OutputWarning{ + Message: fmt.Sprintf("...and %d more like the above.", g.suppressed), + Context: g.context, + }.FormatWarning()) + } +} + +// groupKeyOf builds the family identity of a grouped warning: its message plus the GroupBy context +// pairs, quoted the same way warningKey quotes, so values containing separators cannot collide. +func groupKeyOf(w dt.OutputWarning) string { + var b strings.Builder + + fmt.Fprintf(&b, "%q", w.Message) + + for _, k := range slices.Sorted(slices.Values(w.GroupBy)) { + fmt.Fprintf(&b, " %q=%q", k, w.Context[k]) + } + + return b.String() } // Advisories returns the advisories raised so far, sorted by message and then context (by warningKey), diff --git a/internal/diff/result/comp.go b/internal/diff/result/comp.go index f40a4144..c6a1a6d4 100644 --- a/internal/diff/result/comp.go +++ b/internal/diff/result/comp.go @@ -225,6 +225,10 @@ type AffectedResourcesSummary struct { type XRImpact struct { corev1.ObjectReference + // ComposedBy references the parent composite of a nested XR, derived + // from its controller owner reference; nil for a top-level XR. + ComposedBy *corev1.ObjectReference + Status XRStatus // FilterReason explains a Status == XRStatusFiltered outcome; empty otherwise. FilterReason FilterReason diff --git a/internal/diff/result/types.go b/internal/diff/result/types.go index c0ee37fa..f00022ea 100644 --- a/internal/diff/result/types.go +++ b/internal/diff/result/types.go @@ -459,6 +459,14 @@ type FieldValidationError struct { type OutputWarning struct { Message string `json:"message"` Context map[string]string `json:"context,omitempty"` + + // GroupBy names the context keys that identify a family of related + // occurrences, as a rendering hint: a human reader told once that + // composite A's resources will be assumed by composite B does not need a + // line per resource, so a renderer may stream only the first occurrence + // per group and summarize the rest. Structured output is unaffected: + // every occurrence is kept. Not serialized. + GroupBy []string `json:"-"` } // Advisories is the channel for user-facing advisories. A component that discovers something the diff --git a/internal/simulate/report.go b/internal/simulate/report.go index 6a6bed0e..faa79db8 100644 --- a/internal/simulate/report.go +++ b/internal/simulate/report.go @@ -162,6 +162,10 @@ func FromCompReport(o *dt.CompDiffOutput, plans *ProviderPlans, keep func(*unstr // because that calls a changed function. Other filtered composites are skipped. func compositeResult(impact dt.XRImpact) CompositeResult { c := CompositeResult{Ref: resourceRef(impact.ObjectReference), Pinned: impact.Pinned || impact.FilterReason == dt.FilterReasonManualPolicy} + if impact.ComposedBy != nil { + ref := resourceRef(*impact.ComposedBy) + c.ComposedBy = &ref + } if impact.Pinned && impact.RenderedRevision != "" { c.Detail = "rendered against pinned revision " + impact.RenderedRevision + " with the changed functions" diff --git a/internal/simulate/result.go b/internal/simulate/result.go index 1e25976d..dfc2fa09 100644 --- a/internal/simulate/result.go +++ b/internal/simulate/result.go @@ -83,6 +83,9 @@ type CompositeResult struct { // Composition is the Composition whose change reaches this composite, // set only for a composition simulate run. Composition *plan.ResourceRef `json:"composition,omitempty"` + // ComposedBy references the parent composite of a nested XR; nil for a + // top-level XR. + ComposedBy *plan.ResourceRef `json:"composedBy,omitempty"` } // Error is an error from a simulate run, optionally anchored to a resource. @@ -348,6 +351,14 @@ func renderGrouped(w io.Writer, r Result, impacts map[plan.ResourceRef]string) { // and its plans indented beneath. func renderComposite(w io.Writer, c CompositeResult, ref plan.ResourceRef, plans []*plan.ResourcePlan) { _, _ = fmt.Fprintln(w, compositeHeader(c, ref)) + if c.ComposedBy != nil { + gvk := schema.FromAPIVersionAndKind(c.ComposedBy.APIVersion, c.ComposedBy.Kind) + name := c.ComposedBy.Name + if c.ComposedBy.Namespace != "" { + name = c.ComposedBy.Namespace + "/" + name + } + _, _ = fmt.Fprintf(w, " ↳ composed by %s.%s %s\n", gvk.Kind, gvk.Group, name) + } switch c.Status { case CompositeDeferred: pinned := "Pinned to a composition revision" diff --git a/internal/simulate/run.go b/internal/simulate/run.go index 0160578e..b8a01424 100644 --- a/internal/simulate/run.go +++ b/internal/simulate/run.go @@ -343,12 +343,12 @@ func (c *CompositionFlags) scope() (string, []k8stypes.NamespacedName, error) { // XR and composition processors get the same options, so they share one advisory collector. fns are the project's // functions, of which embedded are the locally built ones, and ics rewrite // the other functions' images. -func (c *Runner) processorOptions(k *kong.Context, logger logging.Logger, creds []corev1.Secret, fns []pkgv1.Function, embedded map[string]name.Digest, ics []pkgv1beta1.ImageConfig) []dp.ProcessorOption { +func (c *Runner) processorOptions(advisories *dp.AdvisoryCollector, logger logging.Logger, creds []corev1.Secret, fns []pkgv1.Function, embedded map[string]name.Digest, ics []pkgv1beta1.ImageConfig) []dp.ProcessorOption { c.SetDefaultCrossplaneDockerNetwork(fns) opts := []dp.ProcessorOption{ dp.WithLogger(logger), - dp.WithAdvisories(dp.NewAdvisoryCollector(k.Stderr)), + dp.WithAdvisories(advisories), dp.WithMaxNestedDepth(c.MaxNestedDepth), dp.WithEventualState(c.EventualState), dp.WithAnalyzeOn(dp.AnalyzeOn(c.sel.AnalyzeOn)), @@ -625,7 +625,12 @@ func (c *Runner) Run(sigCtx context.Context, k *kong.Context, logger logging.Log return err } - opts := append(c.processorOptions(k, logger, creds, fns, digests, pkgs.ICs), dp.WithChangedFunctions(changed...), dp.WithXRTimeout(c.Timeout)) + // Both processors share one advisory collector; summarize the advisory + // families it withheld from streaming once the run is done. + advisories := dp.NewAdvisoryCollector(k.Stderr) + defer advisories.Summarize() + + opts := append(c.processorOptions(advisories, logger, creds, fns, digests, pkgs.ICs), dp.WithChangedFunctions(changed...), dp.WithXRTimeout(c.Timeout)) // xrPlanner and servers are for rendering the plans; they stay nil with // --skip-plan, and servers also when hooks.Router supplies the router. diff --git a/internal/simulate/run_test.go b/internal/simulate/run_test.go index f3479d71..7d6e75dc 100644 --- a/internal/simulate/run_test.go +++ b/internal/simulate/run_test.go @@ -136,8 +136,6 @@ func TestSimulateScope(t *testing.T) { } func TestSimulateProcessorOptions(t *testing.T) { - stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} - k := &kong.Context{Kong: &kong.Kong{Stdout: stdout, Stderr: stderr}} c := &Runner{sel: CompositionFlags{IncludePinned: true, AnalyzeOn: "spec-change"}, Flags: &Flags{ MaxIterations: 7, EngineFlags: xprender.EngineFlags{CrossplaneVersion: "v2.3.4", CrossplaneDockerNetwork: "ci"}, @@ -151,7 +149,7 @@ func TestSimulateProcessorOptions(t *testing.T) { creds := []corev1.Secret{{ObjectMeta: metav1.ObjectMeta{Name: "creds"}}} cfg := dp.ProcessorConfig{} - for _, o := range c.processorOptions(k, logging.NewNopLogger(), creds, []pkgv1.Function{{ObjectMeta: metav1.ObjectMeta{Name: "fn"}}}, nil, nil) { + for _, o := range c.processorOptions(dp.NewAdvisoryCollector(nil), logging.NewNopLogger(), creds, []pkgv1.Function{{ObjectMeta: metav1.ObjectMeta{Name: "fn"}}}, nil, nil) { o(&cfg) } @@ -209,7 +207,7 @@ func TestSimulateProcessorOptions(t *testing.T) { // Without project functions or ImageConfigs every function comes from the // cluster as is, so there is nothing to wrap. cfg = dp.ProcessorConfig{} - for _, o := range c.processorOptions(k, logging.NewNopLogger(), nil, nil, nil, nil) { + for _, o := range c.processorOptions(dp.NewAdvisoryCollector(nil), logging.NewNopLogger(), nil, nil, nil, nil) { o(&cfg) } if cfg.Factories.FunctionProvider != nil { @@ -218,7 +216,7 @@ func TestSimulateProcessorOptions(t *testing.T) { // ImageConfigs alone still wrap the cluster's functions. cfg = dp.ProcessorConfig{} - for _, o := range c.processorOptions(k, logging.NewNopLogger(), nil, nil, nil, []pkgv1beta1.ImageConfig{{}}) { + for _, o := range c.processorOptions(dp.NewAdvisoryCollector(nil), logging.NewNopLogger(), nil, nil, nil, []pkgv1beta1.ImageConfig{{}}) { o(&cfg) } if cfg.Factories.FunctionProvider == nil { @@ -228,7 +226,7 @@ func TestSimulateProcessorOptions(t *testing.T) { // --crossplane-binary renders with a local binary. c.EngineFlags = xprender.EngineFlags{CrossplaneBinary: "/bin/crossplane"} cfg = dp.ProcessorConfig{} - for _, o := range c.processorOptions(k, logging.NewNopLogger(), nil, nil, nil, nil) { + for _, o := range c.processorOptions(dp.NewAdvisoryCollector(nil), logging.NewNopLogger(), nil, nil, nil, nil) { o(&cfg) } if cfg.CrossplaneRenderBinary != "/bin/crossplane" { @@ -937,9 +935,8 @@ func TestSimulateProcessorOptionsDockerNetwork(t *testing.T) { if err != nil { t.Fatalf("parseFlags(%v): %v", tc.args, err) } - k := &kong.Context{Kong: &kong.Kong{Stdout: &bytes.Buffer{}, Stderr: &bytes.Buffer{}}} cfg := dp.ProcessorConfig{} - for _, o := range (&Runner{Flags: c}).processorOptions(k, logging.NewNopLogger(), nil, fns, nil, nil) { + for _, o := range (&Runner{Flags: c}).processorOptions(dp.NewAdvisoryCollector(nil), logging.NewNopLogger(), nil, fns, nil, nil) { o(&cfg) } if cfg.CrossplaneDockerNetwork != tc.want {