Skip to content

Commit

Permalink
Support flag for status policy
Browse files Browse the repository at this point in the history
The default behavior is still to use --status-policy=all. For users that
want to avoid the status behavior for various reasons.

Signed-off-by: Fredrik Sommar <fred.sommar+github@gmail.com>
  • Loading branch information
fsommar committed May 21, 2024
1 parent c0a8d49 commit e464356
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
12 changes: 11 additions & 1 deletion commands/live/apply/cmdapply.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions commands/live/apply/cmdapply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 10 additions & 1 deletion commands/live/destroy/cmddestroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down
10 changes: 10 additions & 0 deletions commands/live/destroy/cmddestroy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit e464356

Please sign in to comment.