Repository navigation
Show progress while simulate renders and plans composites #565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| // 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 | ||
|
|
||
| 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) | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this was added between the |
||
| // 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 | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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?