diff --git a/commands/live/apply/cmdapply.go b/commands/live/apply/cmdapply.go index 1616012b42..1f08ad4af9 100644 --- a/commands/live/apply/cmdapply.go +++ b/commands/live/apply/cmdapply.go @@ -87,6 +87,9 @@ func NewRunner( "dry-run apply for the resources in the package.") c.Flags().BoolVar(&r.printStatusEvents, "show-status-events", false, "Print status events (always enabled for table output)") + c.Flags().StringVar(&r.statusPolicyString, "status-policy", "all", + "It determines which status information should be saved in the inventory (if compatible). Available options "+ + fmt.Sprintf("%q and %q.", "all", "none")) return r } @@ -113,9 +116,11 @@ type Runner struct { inventoryPolicyString string dryRun bool printStatusEvents bool + statusPolicyString string inventoryPolicy inventory.Policy prunePropPolicy metav1.DeletionPropagation + statusPolicy inventory.StatusPolicy applyRunner func(r *Runner, invInfo inventory.Info, objs []*unstructured.Unstructured, dryRunStrategy common.DryRunStrategy) error @@ -133,6 +138,11 @@ func (r *Runner) preRunE(cmd *cobra.Command, _ []string) error { return err } + r.statusPolicy, err = flagutils.ConvertStatusPolicy(r.statusPolicyString) + if err != nil { + return err + } + if found := printers.ValidatePrinterType(r.output); !found { return fmt.Errorf("unknown output type %q", r.output) } @@ -229,7 +239,7 @@ func runApply(r *Runner, invInfo inventory.Info, objs []*unstructured.Unstructur // Run the applier. It will return a channel where we can receive updates // to keep track of progress and any issues. - invClient, err := inventory.NewClient(r.factory, live.WrapInventoryObj, live.InvToUnstructuredFunc, inventory.StatusPolicyAll, live.ResourceGroupGVK) + invClient, err := inventory.NewClient(r.factory, live.WrapInventoryObj, live.InvToUnstructuredFunc, r.statusPolicy, live.ResourceGroupGVK) if err != nil { return err } diff --git a/commands/live/apply/cmdapply_test.go b/commands/live/apply/cmdapply_test.go index 0f7885a5bf..aa24693869 100644 --- a/commands/live/apply/cmdapply_test.go +++ b/commands/live/apply/cmdapply_test.go @@ -48,6 +48,16 @@ func TestCmd(t *testing.T) { }, expectedErrorMsg: "inventory policy must be one of strict, adopt", }, + "invalid status policy": { + args: []string{ + "--status-policy", "noSuchPolicy", + }, + namespace: "testns", + applyCallbackFunc: func(t *testing.T, _ *Runner, _ inventory.Info) { + t.FailNow() + }, + expectedErrorMsg: "status policy must be one of none, all", + }, "invalid output format": { args: []string{ "--output", "foo", diff --git a/commands/live/destroy/cmddestroy.go b/commands/live/destroy/cmddestroy.go index 596a1f8cfb..651c5216c0 100644 --- a/commands/live/destroy/cmddestroy.go +++ b/commands/live/destroy/cmddestroy.go @@ -64,6 +64,9 @@ func NewRunner( "dry-run apply for the resources in the package.") c.Flags().BoolVar(&r.printStatusEvents, "show-status-events", false, "Print status events (always enabled for table output)") + c.Flags().StringVar(&r.statusPolicyString, "status-policy", "all", + "It determines which status information should be saved in the inventory (if compatible). Available options "+ + fmt.Sprintf("%q and %q.", "all", "none")) return r } @@ -86,8 +89,10 @@ type Runner struct { inventoryPolicyString string dryRun bool printStatusEvents bool + statusPolicyString string inventoryPolicy inventory.Policy + statusPolicy inventory.StatusPolicy // TODO(mortent): This is needed for now since we don't have a good way to // stub out the Destroyer with an interface for testing purposes. @@ -101,6 +106,10 @@ func (r *Runner) preRunE(_ *cobra.Command, _ []string) error { if err != nil { return err } + r.statusPolicy, err = flagutils.ConvertStatusPolicy(r.statusPolicyString) + if err != nil { + return err + } if found := printers.ValidatePrinterType(r.output); !found { return fmt.Errorf("unknown output type %q", r.output) @@ -159,7 +168,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error { func runDestroy(r *Runner, inv inventory.Info, dryRunStrategy common.DryRunStrategy) error { // Run the destroyer. It will return a channel where we can receive updates // to keep track of progress and any issues. - invClient, err := inventory.NewClient(r.factory, live.WrapInventoryObj, live.InvToUnstructuredFunc, inventory.StatusPolicyAll, live.ResourceGroupGVK) + invClient, err := inventory.NewClient(r.factory, live.WrapInventoryObj, live.InvToUnstructuredFunc, r.statusPolicy, live.ResourceGroupGVK) if err != nil { return err } diff --git a/commands/live/destroy/cmddestroy_test.go b/commands/live/destroy/cmddestroy_test.go index db39812004..5855fe1768 100644 --- a/commands/live/destroy/cmddestroy_test.go +++ b/commands/live/destroy/cmddestroy_test.go @@ -47,6 +47,16 @@ func TestCmd(t *testing.T) { }, expectedErrorMsg: "inventory policy must be one of strict, adopt", }, + "invalid status policy": { + args: []string{ + "--status-policy", "noSuchPolicy", + }, + namespace: "testns", + destroyCallbackFunc: func(t *testing.T, _ inventory.Info) { + t.FailNow() + }, + expectedErrorMsg: "status policy must be one of none, all", + }, "invalid output format": { args: []string{ "--output", "foo",