Skip to content

Commit

Permalink
Merge pull request #4129 from fsommar/status-policy-conversion
Browse files Browse the repository at this point in the history
Support flag for status policy
  • Loading branch information
kptdev-robot[bot] authored May 21, 2024
2 parents c25345f + e464356 commit 72a151f
Show file tree
Hide file tree
Showing 16 changed files with 244 additions and 635 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
88 changes: 44 additions & 44 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@ require (
github.com/otiai10/copy v1.7.0
github.com/philopon/go-toposort v0.0.0-20170620085441-9be86dbd762f
github.com/prep/wasmexec v0.0.0-20220807105708-6554945c1dec
github.com/spf13/cobra v1.6.1
github.com/spf13/cobra v1.7.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
github.com/xlab/treeprint v1.1.0
golang.org/x/mod v0.9.0
github.com/stretchr/testify v1.8.4
github.com/xlab/treeprint v1.2.0
golang.org/x/mod v0.10.0
golang.org/x/text v0.14.0
gopkg.in/yaml.v3 v3.0.1
gotest.tools v2.2.0+incompatible
k8s.io/api v0.26.9
k8s.io/apiextensions-apiserver v0.26.9
k8s.io/apimachinery v0.26.9
k8s.io/cli-runtime v0.26.9
k8s.io/client-go v0.26.9
k8s.io/component-base v0.26.9
k8s.io/klog/v2 v2.90.1
k8s.io/kubectl v0.26.9
sigs.k8s.io/cli-utils v0.35.0
sigs.k8s.io/controller-runtime v0.14.1
sigs.k8s.io/kustomize/api v0.12.1
sigs.k8s.io/kustomize/kyaml v0.13.9
sigs.k8s.io/yaml v1.3.0
k8s.io/api v0.28.4
k8s.io/apiextensions-apiserver v0.28.4
k8s.io/apimachinery v0.28.4
k8s.io/cli-runtime v0.28.4
k8s.io/client-go v0.28.4
k8s.io/component-base v0.28.4
k8s.io/klog/v2 v2.100.1
k8s.io/kubectl v0.28.4
sigs.k8s.io/cli-utils v0.36.0
sigs.k8s.io/controller-runtime v0.16.3
sigs.k8s.io/kustomize/api v0.15.0
sigs.k8s.io/kustomize/kyaml v0.15.0
sigs.k8s.io/yaml v1.4.0
)

require (
cloud.google.com/go/compute v1.23.0 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/MakeNowJust/heredoc v1.0.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20190424111038-f61b66f89f4a // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/blang/semver/v4 v4.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand All @@ -55,24 +55,24 @@ require (
github.com/docker/distribution v2.8.2+incompatible // indirect
github.com/docker/docker v24.0.9+incompatible // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
github.com/exponent-io/jsonpath v0.0.0-20151013193312-d6023ce2651d // indirect
github.com/fatih/camelcase v1.0.0 // indirect
github.com/fvbommel/sortorder v1.0.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/fvbommel/sortorder v1.1.0 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-openapi/jsonpointer v0.19.6 // indirect
github.com/go-openapi/jsonreference v0.20.1 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/btree v1.1.2 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/gregjones/httpcache v0.0.0-20190611155906-901d90724c79 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jonboulle/clockwork v0.2.2 // indirect
github.com/josharian/intern v1.0.0 // indirect
Expand All @@ -81,45 +81,45 @@ require (
github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.0.0-20220808134915-39b0c02b01ae // indirect
github.com/moby/term v0.0.0-20221205130635-1aeaba878587 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/onsi/gomega v1.24.2 // indirect
github.com/onsi/gomega v1.27.10 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc2 // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/client_model v0.4.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.10.1 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.2.0 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spyzhov/ajson v0.7.2 // indirect
github.com/spyzhov/ajson v0.9.0 // indirect
github.com/vbatts/tar-split v0.11.2 // indirect
go.starlark.net v0.0.0-20210901212718-87f333178d59 // indirect
golang.org/x/net v0.23.0 // indirect
go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.8.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/sync v0.2.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.33.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/evanphx/json-patch.v5 v5.6.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
k8s.io/kube-openapi v0.0.0-20230109183929-3758b55a6596 // indirect
k8s.io/utils v0.0.0-20230115233650-391b47cb4029 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
k8s.io/kube-openapi v0.0.0-20230717233707-2695361300d9 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
)
Loading

0 comments on commit 72a151f

Please sign in to comment.