Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions internal/async/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ package async
// is used to pass information back to callers that are interested in the
// operation's progress.
type Event struct {
// Name identifies the operation when its Text changes over time (e.g. a
// progress line). Consumers key the operation's output line by Name when
// set, by Text otherwise.
Name string

// Text is a description of the event. Events with the same text represent
// updates to the status of a single sub-operation.
Text string
Expand All @@ -40,6 +45,9 @@ const (
EventStatusSuccess EventStatus = "success"
// EventStatusFailure indicates that an operation has failed.
EventStatusFailure EventStatus = "failure"
// EventStatusUpdated indicates that a started operation has new status
// text; it updates the operation's line in place.
EventStatusUpdated EventStatus = "updated"
)

// EventChannel is a channel for sending events. We define our own type for it
Expand All @@ -58,3 +66,25 @@ func (ch EventChannel) SendEvent(text string, status EventStatus) {
Status: status,
}
}

// SendNamedEvent sends an event for the named operation, whose text may
// change over time. It is a no-op if the channel is nil.
func (ch EventChannel) SendNamedEvent(name, text string, status EventStatus) {
if ch == nil {
return
}
ch <- Event{
Name: name,
Text: text,
Status: status,
}
}

// Key identifies the operation an event belongs to: its Name when set, its
// Text otherwise.
func (e Event) Key() string {
if e.Name != "" {
return e.Name
}
return e.Text
}
3 changes: 2 additions & 1 deletion internal/diff/diffprocessor/comp_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,9 @@ func (p *DefaultCompDiffProcessor) collectXRDiffs(ctx context.Context, xrs []*un

results := make(map[string]*XRDiffResult)

for _, xr := range xrs {
for i, xr := range xrs {
resourceID := dt.MakeDiffKeyFromResource(xr)
p.config.Progress.Report(i, len(xrs), resourceID)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Is it worth prefixing this with the composition being processed?


if pin, ok := pinned[resourceID]; ok && pin.err != nil {
results[resourceID] = &XRDiffResult{
Expand Down
1 change: 1 addition & 0 deletions internal/diff/diffprocessor/diff_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (p *DefaultDiffProcessor) PerformDiff(ctx context.Context, resources []*un.

for i, in := range toRender {
res := in.Resource
p.config.Progress.Report(i, len(toRender), dt.MakeDiffKeyFromResource(res))

// Attribute the group to the name the XR is rendered under. A synthesized one (generateName
// only) is shown the way the diff formatter shows it, "<generateName>(generated)", so the
Expand Down
11 changes: 11 additions & 0 deletions internal/diff/diffprocessor/processor_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ type ProcessorConfig struct {
// IncludeManual determines whether to include XRs with Manual update policy in composition diffs
IncludeManual bool

// Progress receives coarse processing progress; see Progress. Nil drops
// reports.
Progress *Progress

// Advisories is the channel user-facing advisories are raised on, and drained from into the report.
// Pass the same collector to every processor of one run (the CLI passes one set of options to
// both), so the composition processor's report includes what its XR processor raised. Unset, each
Expand Down Expand Up @@ -254,6 +258,13 @@ func WithIncludeManual(includeManual bool) ProcessorOption {
}

// WithAdvisories sets the collector advisories are raised on and drained from for structured output.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WithProgress insertion happened between this doc comment and its function below. Please fix

// WithProgress sets the sink coarse processing progress is reported to.
func WithProgress(progress *Progress) ProcessorOption {
return func(config *ProcessorConfig) {
config.Progress = progress
}
}

func WithAdvisories(advisories *AdvisoryCollector) ProcessorOption {
return func(config *ProcessorConfig) {
config.Advisories = advisories
Expand Down
63 changes: 63 additions & 0 deletions internal/diff/diffprocessor/progress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
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 diffprocessor

import "sync"

// Progress receives coarse processing progress from the diff processors:
// which composite is being worked on, and how many of how many are done.
// Rendering and planning a composite can take a while, and without this the
// CLI looks hung between its last setup step and the plan output.
//
// The reporter is settable after processor construction, so a caller can
// install it when its output machinery (e.g. a spinner session) is running
// and remove it afterwards. Safe for concurrent use; a nil *Progress or an
// unset reporter drops reports.
type Progress struct {
mu sync.Mutex
fn func(done, total int, detail string)
}

// NewProgress returns a Progress with no reporter installed.
func NewProgress() *Progress {
return &Progress{}
}

// SetReporter installs fn as the reporter, replacing any previous one. A nil
// fn removes the reporter.
func (p *Progress) SetReporter(fn func(done, total int, detail string)) {
if p == nil {
return
}
p.mu.Lock()
defer p.mu.Unlock()
p.fn = fn
}

// Report reports that done of total composites are processed and detail is
// being worked on.
func (p *Progress) Report(done, total int, detail string) {
if p == nil {
return
}
p.mu.Lock()
fn := p.fn
p.mu.Unlock()
if fn != nil {
fn(done, total, detail)
}
}
50 changes: 43 additions & 7 deletions internal/simulate/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (
"github.com/crossplane/cli/v2/internal/diff/client"
xpclient "github.com/crossplane/cli/v2/internal/diff/client/crossplane"
k8client "github.com/crossplane/cli/v2/internal/diff/client/kubernetes"
"github.com/crossplane/cli/v2/internal/async"
dp "github.com/crossplane/cli/v2/internal/diff/diffprocessor"
"github.com/crossplane/cli/v2/internal/diff/ref"
"github.com/crossplane/cli/v2/internal/diff/renderer"
Expand Down Expand Up @@ -215,9 +216,13 @@ 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) {
func (c *Runner) diffCompositions(k *kong.Context, logger logging.Logger, sp terminal.SpinnerPrinter, progress *dp.Progress, proc dp.CompDiffProcessor, comps []*un.Unstructured, msg string) func(context.Context) (bool, error) {
return func(ctx context.Context) (bool, error) {
output, err := proc.DiffComposition(ctx, comps, c.sel.ns, c.sel.refs)
var output *dt.CompDiffOutput
var err error
simulateWithProgress(sp, progress, func() {
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
Expand All @@ -235,12 +240,16 @@ 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) {
func (c *Runner) diffComposites(k *kong.Context, logger logging.Logger, sp terminal.SpinnerPrinter, progress *dp.Progress, proc dp.DiffProcessor, xp *xpclient.Clients, ns string, xrs []*un.Unstructured) func(context.Context) (bool, error) {
return func(ctx context.Context) (bool, error) {
if err := checkComposites(ctx, xp.Definition, ns, xrs); err != nil {
return false, err
}
report, err := proc.PerformDiff(ctx, xrs, xp.Composition.FindMatchingComposition)
var report *dt.XRReport
var err error
simulateWithProgress(sp, progress, func() {
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
Expand Down Expand Up @@ -276,6 +285,32 @@ type processor interface {
// exits 3; 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.
// simulatingMsg is the progress line shown while composites render and their

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this was added between the simulate fucntion's comment and its implementation :-).

// managed resources are planned against the diff servers - the long stretch
// between the last setup step and the plan output.
const simulatingMsg = "Simulating changes"

// simulateWithProgress runs fn inside a spinner session showing a
// "Simulating changes" line, updated with which composite of how many is
// being processed. The processing outcome is reported by the caller's own
// error handling; the spinner line itself always resolves, so the step style
// matches the setup steps above it.
func simulateWithProgress(sp terminal.SpinnerPrinter, progress *dp.Progress, fn func()) {
//nolint:errcheck // The wrapped function carries its error out via the closure.
_ = sp.WrapAsyncWithSuccessSpinners(func(ch async.EventChannel) error {
ch.SendNamedEvent(simulatingMsg, simulatingMsg, async.EventStatusStarted)
progress.SetReporter(func(done, total int, detail string) {
ch.SendNamedEvent(simulatingMsg, fmt.Sprintf("%s (%d/%d composites: %s)", simulatingMsg, done+1, total, detail), async.EventStatusUpdated)
})
defer progress.SetReporter(nil)

fn()

ch.SendNamedEvent(simulatingMsg, simulatingMsg, async.EventStatusSuccess)
return nil
})
}

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) {
// An interrupted run reports the interruption, not the cancelled calls it
// caused. main exits 130 for it.
Expand Down Expand Up @@ -630,7 +665,8 @@ func (c *Runner) Run(sigCtx context.Context, k *kong.Context, logger logging.Log
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))
progress := dp.NewProgress()
opts := append(c.processorOptions(advisories, logger, creds, fns, digests, pkgs.ICs), dp.WithChangedFunctions(changed...), dp.WithXRTimeout(c.Timeout), dp.WithProgress(progress))

// xrPlanner and servers are for rendering the plans; they stay nil with
// --skip-plan, and servers also when hooks.Router supplies the router.
Expand All @@ -647,10 +683,10 @@ func (c *Runner) Run(sigCtx context.Context, k *kong.Context, logger logging.Log
}
xrProc := dp.NewDiffProcessor(clients.K8sClients, clients.XpClients, opts...)
if in.Composites != nil {
return c.simulate(sigCtx, logger, clients.Initialize, xrProc, c.diffComposites(k, logger, xrProc, &clients.XpClients, ns, in.Composites))
return c.simulate(sigCtx, logger, clients.Initialize, xrProc, c.diffComposites(k, logger, sp, progress, xrProc, &clients.XpClients, ns, in.Composites))
}
proc := dp.NewCompDiffProcessor(xrProc, clients.XpClients.Composition, opts...)
return c.simulate(sigCtx, logger, clients.Initialize, proc, c.diffCompositions(k, logger, proc, comps, msg))
return c.simulate(sigCtx, logger, clients.Initialize, proc, c.diffCompositions(k, logger, sp, progress, proc, comps, msg))
}

// build builds in's project, or for file inputs (Compositions or composite
Expand Down
35 changes: 33 additions & 2 deletions internal/simulate/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import (

xpclient "github.com/crossplane/cli/v2/internal/diff/client/crossplane"
dp "github.com/crossplane/cli/v2/internal/diff/diffprocessor"
"github.com/crossplane/cli/v2/internal/terminal"
"github.com/crossplane/cli/v2/internal/diff/renderer"
dt "github.com/crossplane/cli/v2/internal/diff/result"
"github.com/crossplane/cli/v2/internal/diff/testutils"
Expand Down Expand Up @@ -709,7 +710,7 @@ func TestSimulateRun(t *testing.T) {
if tc.args.files {
msg = "cannot simulate Compositions"
}
diff := tc.args.cmd.diffCompositions(k, logging.NewNopLogger(), tc.args.proc, []*un.Unstructured{{}}, msg)
diff := tc.args.cmd.diffCompositions(k, logging.NewNopLogger(), terminal.NewNopSpinnerPrinter(), dp.NewProgress(), tc.args.proc, []*un.Unstructured{{}}, msg)
err := tc.args.cmd.simulate(ctx, logging.NewNopLogger(), tc.args.initClients, tc.args.proc, diff)

if diff := cmp.Diff(tc.want.code, exitCode(err)); diff != "" {
Expand Down Expand Up @@ -1047,7 +1048,7 @@ func TestDiffComposites(t *testing.T) {
k := &kong.Context{Kong: &kong.Kong{Stdout: stdout, Stderr: &bytes.Buffer{}}}
xp := &xpclient.Clients{Definition: defs, Composition: &testutils.MockCompositionClient{}}

_, err := c.diffComposites(k, logging.NewNopLogger(), proc, xp, "", tc.xrs)(context.Background())
_, err := c.diffComposites(k, logging.NewNopLogger(), terminal.NewNopSpinnerPrinter(), dp.NewProgress(), proc, xp, "", tc.xrs)(context.Background())
if err == nil || !strings.Contains(err.Error(), tc.wantErr) {
t.Errorf("\n%s\ndiffComposites(...): want error containing %q, got %v", tc.reason, tc.wantErr, err)
}
Expand All @@ -1060,3 +1061,33 @@ func TestDiffComposites(t *testing.T) {
})
}
}

func TestSimulateWithProgress(t *testing.T) {
t.Parallel()

var buf bytes.Buffer
sp := terminal.NewSpinnerPrinter(&buf, false)
progress := dp.NewProgress()

ran := false
simulateWithProgress(sp, progress, func() {
ran = true
// Reports flow to the reporter the session installed; plain output
// drops the updates but must not break the session.
progress.Report(0, 14, "example.org/v1, Kind=XNetwork/prod")
})

if !ran {
t.Fatal("simulateWithProgress did not run the wrapped function")
}
if progressFn := progress; progressFn == nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you lose this if progress is a local?

t.Fatal("progress sink lost")
}
out := buf.String()
if !strings.Contains(out, simulatingMsg+"...") {
t.Errorf("expected a started line for %q, got:\n%s", simulatingMsg, out)
}
if !strings.Contains(out, "✓ "+simulatingMsg) {
t.Errorf("expected a success line for %q, got:\n%s", simulatingMsg, out)
}
}
Loading
Loading