Skip to content

Commit e464356

Browse files
committed
Support flag for status policy
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 <[email protected]>
1 parent c0a8d49 commit e464356

File tree

4 files changed

+41
-2
lines changed

4 files changed

+41
-2
lines changed

commands/live/apply/cmdapply.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ func NewRunner(
8787
"dry-run apply for the resources in the package.")
8888
c.Flags().BoolVar(&r.printStatusEvents, "show-status-events", false,
8989
"Print status events (always enabled for table output)")
90+
c.Flags().StringVar(&r.statusPolicyString, "status-policy", "all",
91+
"It determines which status information should be saved in the inventory (if compatible). Available options "+
92+
fmt.Sprintf("%q and %q.", "all", "none"))
9093
return r
9194
}
9295

@@ -113,9 +116,11 @@ type Runner struct {
113116
inventoryPolicyString string
114117
dryRun bool
115118
printStatusEvents bool
119+
statusPolicyString string
116120

117121
inventoryPolicy inventory.Policy
118122
prunePropPolicy metav1.DeletionPropagation
123+
statusPolicy inventory.StatusPolicy
119124

120125
applyRunner func(r *Runner, invInfo inventory.Info, objs []*unstructured.Unstructured,
121126
dryRunStrategy common.DryRunStrategy) error
@@ -133,6 +138,11 @@ func (r *Runner) preRunE(cmd *cobra.Command, _ []string) error {
133138
return err
134139
}
135140

141+
r.statusPolicy, err = flagutils.ConvertStatusPolicy(r.statusPolicyString)
142+
if err != nil {
143+
return err
144+
}
145+
136146
if found := printers.ValidatePrinterType(r.output); !found {
137147
return fmt.Errorf("unknown output type %q", r.output)
138148
}
@@ -229,7 +239,7 @@ func runApply(r *Runner, invInfo inventory.Info, objs []*unstructured.Unstructur
229239

230240
// Run the applier. It will return a channel where we can receive updates
231241
// to keep track of progress and any issues.
232-
invClient, err := inventory.NewClient(r.factory, live.WrapInventoryObj, live.InvToUnstructuredFunc, inventory.StatusPolicyAll, live.ResourceGroupGVK)
242+
invClient, err := inventory.NewClient(r.factory, live.WrapInventoryObj, live.InvToUnstructuredFunc, r.statusPolicy, live.ResourceGroupGVK)
233243
if err != nil {
234244
return err
235245
}

commands/live/apply/cmdapply_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ func TestCmd(t *testing.T) {
4848
},
4949
expectedErrorMsg: "inventory policy must be one of strict, adopt",
5050
},
51+
"invalid status policy": {
52+
args: []string{
53+
"--status-policy", "noSuchPolicy",
54+
},
55+
namespace: "testns",
56+
applyCallbackFunc: func(t *testing.T, _ *Runner, _ inventory.Info) {
57+
t.FailNow()
58+
},
59+
expectedErrorMsg: "status policy must be one of none, all",
60+
},
5161
"invalid output format": {
5262
args: []string{
5363
"--output", "foo",

commands/live/destroy/cmddestroy.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ func NewRunner(
6464
"dry-run apply for the resources in the package.")
6565
c.Flags().BoolVar(&r.printStatusEvents, "show-status-events", false,
6666
"Print status events (always enabled for table output)")
67+
c.Flags().StringVar(&r.statusPolicyString, "status-policy", "all",
68+
"It determines which status information should be saved in the inventory (if compatible). Available options "+
69+
fmt.Sprintf("%q and %q.", "all", "none"))
6770
return r
6871
}
6972

@@ -86,8 +89,10 @@ type Runner struct {
8689
inventoryPolicyString string
8790
dryRun bool
8891
printStatusEvents bool
92+
statusPolicyString string
8993

9094
inventoryPolicy inventory.Policy
95+
statusPolicy inventory.StatusPolicy
9196

9297
// TODO(mortent): This is needed for now since we don't have a good way to
9398
// stub out the Destroyer with an interface for testing purposes.
@@ -101,6 +106,10 @@ func (r *Runner) preRunE(_ *cobra.Command, _ []string) error {
101106
if err != nil {
102107
return err
103108
}
109+
r.statusPolicy, err = flagutils.ConvertStatusPolicy(r.statusPolicyString)
110+
if err != nil {
111+
return err
112+
}
104113

105114
if found := printers.ValidatePrinterType(r.output); !found {
106115
return fmt.Errorf("unknown output type %q", r.output)
@@ -159,7 +168,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error {
159168
func runDestroy(r *Runner, inv inventory.Info, dryRunStrategy common.DryRunStrategy) error {
160169
// Run the destroyer. It will return a channel where we can receive updates
161170
// to keep track of progress and any issues.
162-
invClient, err := inventory.NewClient(r.factory, live.WrapInventoryObj, live.InvToUnstructuredFunc, inventory.StatusPolicyAll, live.ResourceGroupGVK)
171+
invClient, err := inventory.NewClient(r.factory, live.WrapInventoryObj, live.InvToUnstructuredFunc, r.statusPolicy, live.ResourceGroupGVK)
163172
if err != nil {
164173
return err
165174
}

commands/live/destroy/cmddestroy_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ func TestCmd(t *testing.T) {
4747
},
4848
expectedErrorMsg: "inventory policy must be one of strict, adopt",
4949
},
50+
"invalid status policy": {
51+
args: []string{
52+
"--status-policy", "noSuchPolicy",
53+
},
54+
namespace: "testns",
55+
destroyCallbackFunc: func(t *testing.T, _ inventory.Info) {
56+
t.FailNow()
57+
},
58+
expectedErrorMsg: "status policy must be one of none, all",
59+
},
5060
"invalid output format": {
5161
args: []string{
5262
"--output", "foo",

0 commit comments

Comments
 (0)