diff --git a/internal/async/event.go b/internal/async/event.go index 2bf0851a..9b3bb1a3 100644 --- a/internal/async/event.go +++ b/internal/async/event.go @@ -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 @@ -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 @@ -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 +} diff --git a/internal/diff/diffprocessor/comp_processor.go b/internal/diff/diffprocessor/comp_processor.go index 55cc47e2..75764662 100644 --- a/internal/diff/diffprocessor/comp_processor.go +++ b/internal/diff/diffprocessor/comp_processor.go @@ -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) if pin, ok := pinned[resourceID]; ok && pin.err != nil { results[resourceID] = &XRDiffResult{ diff --git a/internal/diff/diffprocessor/diff_processor.go b/internal/diff/diffprocessor/diff_processor.go index f52482bc..b2b6c634 100644 --- a/internal/diff/diffprocessor/diff_processor.go +++ b/internal/diff/diffprocessor/diff_processor.go @@ -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, "(generated)", so the diff --git a/internal/diff/diffprocessor/processor_config.go b/internal/diff/diffprocessor/processor_config.go index ed32ecf7..adf8b637 100644 --- a/internal/diff/diffprocessor/processor_config.go +++ b/internal/diff/diffprocessor/processor_config.go @@ -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 @@ -254,6 +258,13 @@ func WithIncludeManual(includeManual bool) ProcessorOption { } // WithAdvisories sets the collector advisories are raised on and drained from for structured output. +// 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 diff --git a/internal/diff/diffprocessor/progress.go b/internal/diff/diffprocessor/progress.go new file mode 100644 index 00000000..50548cc3 --- /dev/null +++ b/internal/diff/diffprocessor/progress.go @@ -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) + } +} diff --git a/internal/simulate/run.go b/internal/simulate/run.go index b8a01424..f360a794 100644 --- a/internal/simulate/run.go +++ b/internal/simulate/run.go @@ -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" @@ -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 @@ -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 @@ -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 +// 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. @@ -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. @@ -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 diff --git a/internal/simulate/run_test.go b/internal/simulate/run_test.go index ad267985..edf5a8d7 100644 --- a/internal/simulate/run_test.go +++ b/internal/simulate/run_test.go @@ -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" @@ -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 != "" { @@ -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) } @@ -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 { + 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) + } +} diff --git a/internal/terminal/spinner.go b/internal/terminal/spinner.go index a62c513d..ff472534 100644 --- a/internal/terminal/spinner.go +++ b/internal/terminal/spinner.go @@ -142,11 +142,13 @@ func (p *defaultSpinnerPrinter) asyncPretty(fn func(ch async.EventChannel) error for update := range updateChan { switch update.Status { case async.EventStatusStarted: - multi.Add(update.Text) + multi.AddKeyed(update.Key(), update.Text) + case async.EventStatusUpdated: + multi.UpdateKeyed(update.Key(), update.Text) case async.EventStatusSuccess: - multi.Success(update.Text) + multi.SuccessKeyed(update.Key(), update.Text) case async.EventStatusFailure: - multi.Fail(update.Text) + multi.FailKeyed(update.Key(), update.Text) } } err := <-doneChan @@ -171,23 +173,26 @@ func (p *defaultSpinnerPrinter) asyncPlain(fn func(ch async.EventChannel) error) printed := make(map[string]bool) for update := range updateChan { - prevStatus := statusMap[update.Text] + key := update.Key() + prevStatus := statusMap[key] switch update.Status { case async.EventStatusStarted: - if !printed[update.Text] { + if !printed[key] { _, _ = fmt.Fprintln(p.out, update.Text+"...") - printed[update.Text] = true - statusMap[update.Text] = "started" + printed[key] = true + statusMap[key] = "started" } + case async.EventStatusUpdated: + // Plain output has no line to update; progress is dropped. case async.EventStatusSuccess: if prevStatus != "success" { _, _ = fmt.Fprintln(p.out, "✓ "+update.Text) - statusMap[update.Text] = "success" + statusMap[key] = "success" } case async.EventStatusFailure: if prevStatus != "failure" { _, _ = fmt.Fprintln(p.out, "✗ "+update.Text) - statusMap[update.Text] = "failure" + statusMap[key] = "failure" } } } @@ -198,6 +203,9 @@ func (p *defaultSpinnerPrinter) asyncPlain(fn func(ch async.EventChannel) error) // MultiSpinner is a collection of independent spinners that get displayed // together. Spinners can be dynamically added. type MultiSpinner struct { + // keys identifies spinners[i] independently of its (mutable) title, so + // an operation whose text changes over time keeps one line. + keys []string spinners []*SuccessSpinner mu sync.Mutex program *tea.Program @@ -246,43 +254,79 @@ func (m *MultiSpinner) View() tea.View { // Add adds a spinner to the multi-spinner. func (m *MultiSpinner) Add(title string) { + m.AddKeyed(title, title) +} + +// AddKeyed adds a new spinner under the given key, which identifies it even +// when later updates change its title. +func (m *MultiSpinner) AddKeyed(key, title string) { m.mu.Lock() defer m.mu.Unlock() - for _, sp := range m.spinners { - if sp.title == title { - return - } + if m.find(key) != nil { + return } + m.keys = append(m.keys, key) m.spinners = append(m.spinners, newSuccessSpinner(m.out, title)) } +// UpdateKeyed replaces the title of the spinner under the given key, e.g. +// with progress counts. +func (m *MultiSpinner) UpdateKeyed(key, title string) { + m.mu.Lock() + defer m.mu.Unlock() + + if sp := m.find(key); sp != nil { + sp.UpdateText(title) + } +} + +// find returns the spinner under key, or nil. Callers hold m.mu. +func (m *MultiSpinner) find(key string) *SuccessSpinner { + for i, k := range m.keys { + if k == key { + return m.spinners[i] + } + } + return nil +} + // Success marks an existing spinner in the multi-spinner as having succeeded. func (m *MultiSpinner) Success(title string) { + m.SuccessKeyed(title, title) +} + +// SuccessKeyed marks the spinner under the given key as having succeeded, +// updating its title when one is given. +func (m *MultiSpinner) SuccessKeyed(key, title string) { m.mu.Lock() defer m.mu.Unlock() - for _, sp := range m.spinners { - if sp.title != title { - continue + if sp := m.find(key); sp != nil { + if title != "" { + sp.UpdateText(title) } sp.setSuccess(true) - return } } // Fail marks an existing spinner in the multi-spinner as having failed. func (m *MultiSpinner) Fail(title string) { + m.FailKeyed(title, title) +} + +// FailKeyed marks the spinner under the given key as having failed, updating +// its title when one is given. +func (m *MultiSpinner) FailKeyed(key, title string) { m.mu.Lock() defer m.mu.Unlock() - for _, sp := range m.spinners { - if sp.title != title { - continue + if sp := m.find(key); sp != nil { + if title != "" { + sp.UpdateText(title) } sp.setSuccess(false) - return } }