Skip to content
This repository was archived by the owner on May 18, 2026. It is now read-only.

Commit 4257bf5

Browse files
author
Wendy Maria Arango Chavarria
committed
feat: Do not wait for workload to be ready when using --output flag (#488)
Signed-off-by: Wendy Arango <warango@vmware.com>
1 parent dcac777 commit 4257bf5

9 files changed

Lines changed: 1037 additions & 402 deletions

File tree

pkg/cli-runtime/printer/resource.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,14 @@ func OutputResource(obj Object, format OutputFormat, scheme *runtime.Scheme) (st
9696
if err != nil {
9797
return "", err
9898
}
99-
return printObject(copy, format)
99+
u, err := runtime.DefaultUnstructuredConverter.ToUnstructured(copy)
100+
if err != nil {
101+
return "", err
102+
}
103+
104+
unstructured.RemoveNestedField(u, "metadata", "managedFields")
105+
106+
return printObject(u, format)
100107
}
101108

102109
func OutputResources(objList []Object, format OutputFormat, scheme *runtime.Scheme) (string, error) {

pkg/cli-runtime/printer/resource_test.go

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -325,8 +325,6 @@ metadata:
325325
generation: 1
326326
labels:
327327
name: value
328-
managedFields:
329-
- manager: tanzu
330328
name: my-workload
331329
namespace: default
332330
ownerReferences:
@@ -395,24 +393,24 @@ status:
395393
},
396394
want: `
397395
{
398-
"kind": "Workload",
399396
"apiVersion": "carto.run/v1alpha1",
397+
"kind": "Workload",
400398
"metadata": {
401-
"name": "my-workload",
402-
"namespace": "default",
403-
"selfLink": "/default/my-workload",
404-
"uid": "uid-xyz",
405-
"resourceVersion": "999",
406-
"generation": 1,
399+
"annotations": {
400+
"name": "value"
401+
},
407402
"creationTimestamp": "2021-09-10T15:00:00Z",
408-
"deletionTimestamp": "2021-09-10T15:00:00Z",
409403
"deletionGracePeriodSeconds": 5,
404+
"deletionTimestamp": "2021-09-10T15:00:00Z",
405+
"finalizers": [
406+
"my.finalizer"
407+
],
408+
"generation": 1,
410409
"labels": {
411410
"name": "value"
412411
},
413-
"annotations": {
414-
"name": "value"
415-
},
412+
"name": "my-workload",
413+
"namespace": "default",
416414
"ownerReferences": [
417415
{
418416
"apiVersion": "v1",
@@ -421,24 +419,19 @@ status:
421419
"uid": ""
422420
}
423421
],
424-
"finalizers": [
425-
"my.finalizer"
426-
],
427-
"managedFields": [
428-
{
429-
"manager": "tanzu"
430-
}
431-
]
422+
"resourceVersion": "999",
423+
"selfLink": "/default/my-workload",
424+
"uid": "uid-xyz"
432425
},
433426
"spec": {},
434427
"status": {
435428
"conditions": [
436429
{
437-
"type": "Ready",
438-
"status": "True",
439430
"lastTransitionTime": "2019-06-29T01:44:05Z",
431+
"message": "a hopefully informative message about what went wrong",
440432
"reason": "No printing status",
441-
"message": "a hopefully informative message about what went wrong"
433+
"status": "True",
434+
"type": "Ready"
442435
}
443436
],
444437
"supplyChainRef": {}

pkg/commands/workload.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -746,27 +746,14 @@ func (opts *WorkloadOptions) WaitToBeReady(c *cli.Config, workload *cartov1alpha
746746
func(ctx context.Context) error {
747747
clientWithWatch, err := watch.GetWatcher(ctx, c)
748748
if err != nil {
749-
panic(err)
749+
return err
750750
}
751751
return wait.UntilCondition(ctx, clientWithWatch, types.NamespacedName{Name: workload.Name, Namespace: workload.Namespace}, &cartov1alpha1.WorkloadList{}, cartov1alpha1.WorkloadReadyConditionFunc)
752752
},
753753
}
754754
return workers
755755
}
756756

757-
func (opts *WorkloadOptions) WaitError(ctx context.Context, c *cli.Config, workload *cartov1alpha1.Workload, workers []wait.Worker) error {
758-
if err := wait.Race(ctx, opts.WaitTimeout, workers); err != nil {
759-
if err == context.DeadlineExceeded {
760-
c.Printf("%s timeout after %s waiting for %q to become ready\n", printer.Serrorf("Error:"), opts.WaitTimeout, workload.Name)
761-
return cli.SilenceError(err)
762-
}
763-
c.Eprintf("%s %s\n", printer.Serrorf("Error:"), err)
764-
return cli.SilenceError(err)
765-
}
766-
767-
return nil
768-
}
769-
770757
func (opts *WorkloadOptions) DefineFlags(ctx context.Context, c *cli.Config, cmd *cobra.Command) {
771758
cli.NamespaceFlag(ctx, cmd, c, &opts.Namespace)
772759
cmd.Flags().StringVarP(&opts.FilePath, cli.StripDash(flags.FilePathFlagName), "f", "", "`file path` containing the description of a single workload, other flags are layered on top of this resource. Use value \"-\" to read from stdin")

pkg/commands/workload_apply.go

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"github.com/vmware-tanzu/apps-cli-plugin/pkg/cli-runtime/wait"
3535
"github.com/vmware-tanzu/apps-cli-plugin/pkg/completion"
3636
"github.com/vmware-tanzu/apps-cli-plugin/pkg/flags"
37+
"github.com/vmware-tanzu/apps-cli-plugin/pkg/printer"
3738
)
3839

3940
type WorkloadApplyOptions struct {
@@ -206,45 +207,55 @@ func (opts *WorkloadApplyOptions) Exec(ctx context.Context, c *cli.Config) error
206207
}
207208
}
208209

209-
var workers []wait.Worker
210-
if opts.Output != "" && okToApply {
211-
workers = opts.WaitToBeReady(c, workload)
212-
if err := opts.WaitError(ctx, c, workload, workers); err != nil {
213-
return err
214-
}
215-
// once the workload is ready, get it as is in the cluster
216-
if err := c.Get(ctx, client.ObjectKey{Namespace: opts.Namespace, Name: opts.Name}, workload); err != nil {
217-
return err
218-
}
219-
if err := opts.OutputWorkload(c, workload); err != nil {
220-
return err
221-
}
222-
223-
return nil
224-
}
225-
226-
anyTail := opts.Tail || opts.TailTimestamps
227-
if okToApply && (opts.Wait || anyTail) {
228-
c.Infof("Waiting for workload %q to become ready...\n", opts.Name)
229-
230-
workers = opts.WaitToBeReady(c, workload)
210+
if okToApply {
211+
anyTail := opts.Tail || opts.TailTimestamps
212+
if opts.Wait || anyTail {
213+
cli.PrintPrompt(shouldPrint, c.Infof, "Waiting for workload %q to become ready...\n", opts.Name)
214+
215+
workers := opts.WaitToBeReady(c, workload)
216+
217+
if anyTail {
218+
workers = append(workers, func(ctx context.Context) error {
219+
selector, err := labels.Parse(fmt.Sprintf("%s=%s", cartov1alpha1.WorkloadLabelName, workload.Name))
220+
if err != nil {
221+
return err
222+
}
223+
containers := []string{}
224+
return logs.Tail(ctx, c, opts.Namespace, selector, containers, time.Minute, opts.TailTimestamps)
225+
})
226+
}
231227

232-
if anyTail {
233-
workers = append(workers, func(ctx context.Context) error {
234-
selector, err := labels.Parse(fmt.Sprintf("%s=%s", cartov1alpha1.WorkloadLabelName, workload.Name))
235-
if err != nil {
236-
panic(err)
228+
err := wait.Race(ctx, opts.WaitTimeout, workers)
229+
// print wait error only if output is not set or it was not used with --yes
230+
if err != nil {
231+
if err == context.DeadlineExceeded {
232+
cli.PrintPrompt(shouldPrint, c.Printf, "%s timeout after %s waiting for %q to become ready\n", printer.Serrorf("Error:"), opts.WaitTimeout, workload.Name)
233+
} else {
234+
cli.PrintPrompt(shouldPrint, c.Eprintf, "%s %s\n", printer.Serrorf("Error:"), err)
237235
}
238-
containers := []string{}
239-
return logs.Tail(ctx, c, opts.Namespace, selector, containers, time.Minute, opts.TailTimestamps)
240-
})
241-
}
236+
}
237+
// do not return if --output is set
238+
// because workload has to be printed despite it's in a failing state
239+
if err != nil && opts.Output == "" {
240+
return cli.SilenceError(err)
241+
}
242242

243-
if err := opts.WaitError(ctx, c, workload, workers); err != nil {
244-
return err
243+
// since there is a possibility that wait failed but did not return
244+
// make sure this prompt is printed only if there is no error
245+
if err == nil {
246+
cli.PrintPrompt(shouldPrint, c.Infof, "Workload %q is ready\n\n", workload.Name)
247+
}
245248
}
246249

247-
c.Infof("Workload %q is ready\n\n", workload.Name)
250+
if opts.Output != "" {
251+
// once the workload is applied, get it as is in the cluster
252+
if err := c.Get(ctx, client.ObjectKey{Namespace: opts.Namespace, Name: opts.Name}, workload); err != nil {
253+
return err
254+
}
255+
if err := opts.OutputWorkload(c, workload); err != nil {
256+
return err
257+
}
258+
}
248259
}
249260

250261
return nil

0 commit comments

Comments
 (0)