From 1a58d7a52743f806484ae926157c46148bfd533b Mon Sep 17 00:00:00 2001 From: rashmigottipati Date: Mon, 8 Mar 2021 11:47:09 -0500 Subject: [PATCH 1/5] cleanup command takes additional flags to handle optional deletion of crds Signed-off-by: rashmigottipati --- internal/cmd/operator-sdk/cleanup/cmd.go | 5 +++-- internal/olm/operator/uninstall.go | 10 ++++++++++ website/content/en/docs/cli/operator-sdk_cleanup.md | 6 ++++-- .../en/docs/olm-integration/testing-deployment.md | 8 +++++++- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/internal/cmd/operator-sdk/cleanup/cmd.go b/internal/cmd/operator-sdk/cleanup/cmd.go index ae2f42ae2bf..7653a00b22a 100644 --- a/internal/cmd/operator-sdk/cleanup/cmd.go +++ b/internal/cmd/operator-sdk/cleanup/cmd.go @@ -28,6 +28,7 @@ import ( func NewCmd() *cobra.Command { var timeout time.Duration cfg := &operator.Configuration{} + u := operator.NewUninstall(cfg) cmd := &cobra.Command{ Use: "cleanup ", Short: "Clean up an Operator deployed with the 'run' subcommand", @@ -37,9 +38,7 @@ func NewCmd() *cobra.Command { return cfg.Load() }, Run: func(cmd *cobra.Command, args []string) { - u := operator.NewUninstall(cfg) u.Package = args[0] - u.DeleteAll = true u.DeleteOperatorGroupNames = []string{operator.SDKOperatorGroupName} u.Logf = log.Infof @@ -59,7 +58,9 @@ func NewCmd() *cobra.Command { }, } cmd.Flags().DurationVar(&timeout, "timeout", 2*time.Minute, "Time to wait for the command to complete before failing") + cmd.Flags().SortFlags = false cfg.BindFlags(cmd.PersistentFlags()) + u.BindFlags(cmd.Flags()) return cmd } diff --git a/internal/olm/operator/uninstall.go b/internal/olm/operator/uninstall.go index 4020eb25e8d..10910da4682 100644 --- a/internal/olm/operator/uninstall.go +++ b/internal/olm/operator/uninstall.go @@ -22,6 +22,7 @@ import ( v1 "github.com/operator-framework/api/pkg/operators/v1" "github.com/operator-framework/api/pkg/operators/v1alpha1" + "github.com/spf13/pflag" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" @@ -53,6 +54,11 @@ func NewUninstall(cfg *Configuration) *Uninstall { } } +func (u *Uninstall) BindFlags(fs *pflag.FlagSet) { + fs.BoolVar(&u.DeleteCRDs, "delete-crds", true, "If set to false, owned CRDs and CRs will not be deleted") + fs.BoolVar(&u.DeleteAll, "delete-all", true, "If set to true, it will enable all the delete flags") +} + type ErrPackageNotFound struct { PackageName string } @@ -143,9 +149,13 @@ func (u *Uninstall) Run(ctx context.Context) error { return err } var objs []client.Object + if u.DeleteCRDs { objs = append(objs, crds...) + } else { + u.Logf("skipping deletion of CRDs as delete-crds flag is set to %v", u.DeleteCRDs) } + objs = append(objs, csvObj, csObj) // These objects may have owned resources/finalizers, so block on deletion. if err := u.deleteObjects(ctx, true, objs...); err != nil { diff --git a/website/content/en/docs/cli/operator-sdk_cleanup.md b/website/content/en/docs/cli/operator-sdk_cleanup.md index dbd8fed5fce..f828416f8f0 100644 --- a/website/content/en/docs/cli/operator-sdk_cleanup.md +++ b/website/content/en/docs/cli/operator-sdk_cleanup.md @@ -16,10 +16,12 @@ operator-sdk cleanup [flags] ### Options ``` - -h, --help help for cleanup + --timeout duration Time to wait for the command to complete before failing (default 2m0s) + --delete-crds If set to false, owned CRDs and CRs will not be deleted (default true) + --delete-all If set to true, it will enable all the delete flags (default true) --kubeconfig string Path to the kubeconfig file to use for CLI requests. -n, --namespace string If present, namespace scope for this CLI request - --timeout duration Time to wait for the command to complete before failing (default 2m0s) + -h, --help help for cleanup ``` ### Options inherited from parent commands diff --git a/website/content/en/docs/olm-integration/testing-deployment.md b/website/content/en/docs/olm-integration/testing-deployment.md index d281cc2eb16..227b35d7e31 100644 --- a/website/content/en/docs/olm-integration/testing-deployment.md +++ b/website/content/en/docs/olm-integration/testing-deployment.md @@ -123,7 +123,7 @@ Let's look at the anatomy of the `run bundle-upgrade` configuration model: `run packagemanifests`. ``` -operator-sdk cleanup [--kubeconfig=] [--namespace=] [--timeout=] +operator-sdk cleanup [--delete-all=] [--delete-crds=] [--kubeconfig=] [--namespace=] [--timeout=] ``` Let's look at the configuration shared between `run bundle`, `run @@ -141,6 +141,12 @@ Let's look at the anatomy of the `cleanup` configuration model: - **operatorPackageName**: the Operator's package name which you want to remove from the cluster, e.g. memcached-operator. This is a required parameter. +- **delete-all**: a boolean indicating to enable all the delete flags that are present. This is an optional field + which will default to true if not provided. If set to false, it will enable + specific delete flags. +- **delete-crds**: a boolean indicating to delete all owned CRDs and CRs. This is an optional field + which will default to true if not provided. If set to false, owned CRDs and CRs + will stay intact. ### Caveats From 1084f2a52556c94acf6c5a931c408fa34eaf8cc1 Mon Sep 17 00:00:00 2001 From: rashmigottipati Date: Mon, 15 Mar 2021 15:54:23 -0400 Subject: [PATCH 2/5] Address review feedback Signed-off-by: rashmigottipati --- internal/cmd/operator-sdk/cleanup/cmd.go | 6 +++++- internal/olm/operator/uninstall.go | 19 +++++++++++++++++-- .../en/docs/cli/operator-sdk_cleanup.md | 13 +++++++------ .../olm-integration/testing-deployment.md | 4 +++- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/internal/cmd/operator-sdk/cleanup/cmd.go b/internal/cmd/operator-sdk/cleanup/cmd.go index 7653a00b22a..f116d3d8683 100644 --- a/internal/cmd/operator-sdk/cleanup/cmd.go +++ b/internal/cmd/operator-sdk/cleanup/cmd.go @@ -17,6 +17,7 @@ package cleanup import ( "context" "errors" + "fmt" "time" log "github.com/sirupsen/logrus" @@ -42,6 +43,10 @@ func NewCmd() *cobra.Command { u.DeleteOperatorGroupNames = []string{operator.SDKOperatorGroupName} u.Logf = log.Infof + if err := u.Validate(); err != nil { + return fmt.Errorf("invalid command options: %v", err) + } + ctx, cancel := context.WithTimeout(cmd.Context(), timeout) defer cancel() @@ -58,7 +63,6 @@ func NewCmd() *cobra.Command { }, } cmd.Flags().DurationVar(&timeout, "timeout", 2*time.Minute, "Time to wait for the command to complete before failing") - cmd.Flags().SortFlags = false cfg.BindFlags(cmd.PersistentFlags()) u.BindFlags(cmd.Flags()) diff --git a/internal/olm/operator/uninstall.go b/internal/olm/operator/uninstall.go index 10910da4682..7702943259b 100644 --- a/internal/olm/operator/uninstall.go +++ b/internal/olm/operator/uninstall.go @@ -16,12 +16,14 @@ package operator import ( "context" + "errors" "fmt" "strings" "time" v1 "github.com/operator-framework/api/pkg/operators/v1" "github.com/operator-framework/api/pkg/operators/v1alpha1" + log "github.com/sirupsen/logrus" "github.com/spf13/pflag" apierrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -56,7 +58,17 @@ func NewUninstall(cfg *Configuration) *Uninstall { func (u *Uninstall) BindFlags(fs *pflag.FlagSet) { fs.BoolVar(&u.DeleteCRDs, "delete-crds", true, "If set to false, owned CRDs and CRs will not be deleted") - fs.BoolVar(&u.DeleteAll, "delete-all", true, "If set to true, it will enable all the delete flags") + fs.BoolVar(&u.DeleteAll, "delete-all", true, "If set to true, all other delete options will be enabled") + fs.BoolVar(&u.DeleteOperatorGroups, "delete-operator-groups", true, "If set to false, operator groups will not be deleted") +} + +func (u *Uninstall) Validate() error { + if u.DeleteAll { + if !u.DeleteCRDs || !u.DeleteOperatorGroups { + return errors.New("if --delete-all flag is enabled, any other delete flag cannot be set to false") + } + } + return nil } type ErrPackageNotFound struct { @@ -153,7 +165,8 @@ func (u *Uninstall) Run(ctx context.Context) error { if u.DeleteCRDs { objs = append(objs, crds...) } else { - u.Logf("skipping deletion of CRDs as delete-crds flag is set to %v", u.DeleteCRDs) + log.Info("Skipping CRD deletion") + } objs = append(objs, csvObj, csObj) @@ -168,6 +181,8 @@ func (u *Uninstall) Run(ctx context.Context) error { if err := u.deleteOperatorGroup(ctx); err != nil { return err } + } else { + log.Info("Skipping Operator Groups deletion") } // If no objects were cleaned up, the package was not found. diff --git a/website/content/en/docs/cli/operator-sdk_cleanup.md b/website/content/en/docs/cli/operator-sdk_cleanup.md index f828416f8f0..0bda3dc65a8 100644 --- a/website/content/en/docs/cli/operator-sdk_cleanup.md +++ b/website/content/en/docs/cli/operator-sdk_cleanup.md @@ -16,12 +16,13 @@ operator-sdk cleanup [flags] ### Options ``` - --timeout duration Time to wait for the command to complete before failing (default 2m0s) - --delete-crds If set to false, owned CRDs and CRs will not be deleted (default true) - --delete-all If set to true, it will enable all the delete flags (default true) - --kubeconfig string Path to the kubeconfig file to use for CLI requests. - -n, --namespace string If present, namespace scope for this CLI request - -h, --help help for cleanup + --delete-all If set to true, it will enable all the delete flags (default true) + --delete-crds If set to false, owned CRDs and CRs will not be deleted (default true) + --delete-operator-groups If set to false, operator groups will not be deleted (default true) + -h, --help help for cleanup + --kubeconfig string Path to the kubeconfig file to use for CLI requests. + -n, --namespace string If present, namespace scope for this CLI request + --timeout duration Time to wait for the command to complete before failing (default 2m0s) ``` ### Options inherited from parent commands diff --git a/website/content/en/docs/olm-integration/testing-deployment.md b/website/content/en/docs/olm-integration/testing-deployment.md index 227b35d7e31..2be1bcbe0e6 100644 --- a/website/content/en/docs/olm-integration/testing-deployment.md +++ b/website/content/en/docs/olm-integration/testing-deployment.md @@ -123,7 +123,7 @@ Let's look at the anatomy of the `run bundle-upgrade` configuration model: `run packagemanifests`. ``` -operator-sdk cleanup [--delete-all=] [--delete-crds=] [--kubeconfig=] [--namespace=] [--timeout=] +operator-sdk cleanup [--delete-all=] [--delete-crds=] [--delete-operator-groups=] [--kubeconfig=] [--namespace=] [--timeout=] ``` Let's look at the configuration shared between `run bundle`, `run @@ -147,6 +147,8 @@ Let's look at the anatomy of the `cleanup` configuration model: - **delete-crds**: a boolean indicating to delete all owned CRDs and CRs. This is an optional field which will default to true if not provided. If set to false, owned CRDs and CRs will stay intact. +- **delete-operator-groups**: a boolean indicating to delete all operator groups. This is an optional field + which will default to true if not provided. If set to false, operator groups will not be deleted. ### Caveats From 1964a4368829beaa76f40891e9679abf5a4b3882 Mon Sep 17 00:00:00 2001 From: rashmigottipati Date: Tue, 16 Mar 2021 13:35:46 -0400 Subject: [PATCH 3/5] Address review feedback #2 Signed-off-by: rashmigottipati --- internal/cmd/operator-sdk/cleanup/cmd.go | 5 +++-- website/content/en/docs/cli/operator-sdk_cleanup.md | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/cmd/operator-sdk/cleanup/cmd.go b/internal/cmd/operator-sdk/cleanup/cmd.go index f116d3d8683..3445c79379a 100644 --- a/internal/cmd/operator-sdk/cleanup/cmd.go +++ b/internal/cmd/operator-sdk/cleanup/cmd.go @@ -17,7 +17,7 @@ package cleanup import ( "context" "errors" - "fmt" + "os" "time" log "github.com/sirupsen/logrus" @@ -44,7 +44,8 @@ func NewCmd() *cobra.Command { u.Logf = log.Infof if err := u.Validate(); err != nil { - return fmt.Errorf("invalid command options: %v", err) + log.Errorf("Invalid command options: %v", err) + os.Exit(1) } ctx, cancel := context.WithTimeout(cmd.Context(), timeout) diff --git a/website/content/en/docs/cli/operator-sdk_cleanup.md b/website/content/en/docs/cli/operator-sdk_cleanup.md index 0bda3dc65a8..13ccb11b4a6 100644 --- a/website/content/en/docs/cli/operator-sdk_cleanup.md +++ b/website/content/en/docs/cli/operator-sdk_cleanup.md @@ -16,7 +16,7 @@ operator-sdk cleanup [flags] ### Options ``` - --delete-all If set to true, it will enable all the delete flags (default true) + --delete-all If set to true, all other delete options will be enabled (default true) --delete-crds If set to false, owned CRDs and CRs will not be deleted (default true) --delete-operator-groups If set to false, operator groups will not be deleted (default true) -h, --help help for cleanup From 3dc71bcbeff926691f59db6029a4ae8bfa480988 Mon Sep 17 00:00:00 2001 From: rashmigottipati Date: Wed, 24 Mar 2021 10:50:53 -0400 Subject: [PATCH 4/5] Modify cleanup flags defaults Signed-off-by: rashmigottipati --- internal/cmd/operator-sdk/cleanup/cmd.go | 6 ------ internal/olm/operator/uninstall.go | 14 ++------------ .../content/en/docs/cli/operator-sdk_cleanup.md | 4 ++-- .../en/docs/olm-integration/testing-deployment.md | 11 +++++------ 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/internal/cmd/operator-sdk/cleanup/cmd.go b/internal/cmd/operator-sdk/cleanup/cmd.go index 3445c79379a..8570676579f 100644 --- a/internal/cmd/operator-sdk/cleanup/cmd.go +++ b/internal/cmd/operator-sdk/cleanup/cmd.go @@ -17,7 +17,6 @@ package cleanup import ( "context" "errors" - "os" "time" log "github.com/sirupsen/logrus" @@ -43,11 +42,6 @@ func NewCmd() *cobra.Command { u.DeleteOperatorGroupNames = []string{operator.SDKOperatorGroupName} u.Logf = log.Infof - if err := u.Validate(); err != nil { - log.Errorf("Invalid command options: %v", err) - os.Exit(1) - } - ctx, cancel := context.WithTimeout(cmd.Context(), timeout) defer cancel() diff --git a/internal/olm/operator/uninstall.go b/internal/olm/operator/uninstall.go index 7702943259b..6c027265977 100644 --- a/internal/olm/operator/uninstall.go +++ b/internal/olm/operator/uninstall.go @@ -16,7 +16,6 @@ package operator import ( "context" - "errors" "fmt" "strings" "time" @@ -57,18 +56,9 @@ func NewUninstall(cfg *Configuration) *Uninstall { } func (u *Uninstall) BindFlags(fs *pflag.FlagSet) { - fs.BoolVar(&u.DeleteCRDs, "delete-crds", true, "If set to false, owned CRDs and CRs will not be deleted") + fs.BoolVar(&u.DeleteCRDs, "delete-crds", false, "If set to true, owned CRDs and CRs will be deleted") fs.BoolVar(&u.DeleteAll, "delete-all", true, "If set to true, all other delete options will be enabled") - fs.BoolVar(&u.DeleteOperatorGroups, "delete-operator-groups", true, "If set to false, operator groups will not be deleted") -} - -func (u *Uninstall) Validate() error { - if u.DeleteAll { - if !u.DeleteCRDs || !u.DeleteOperatorGroups { - return errors.New("if --delete-all flag is enabled, any other delete flag cannot be set to false") - } - } - return nil + fs.BoolVar(&u.DeleteOperatorGroups, "delete-operator-groups", false, "If set to true, operator groups will be deleted") } type ErrPackageNotFound struct { diff --git a/website/content/en/docs/cli/operator-sdk_cleanup.md b/website/content/en/docs/cli/operator-sdk_cleanup.md index 13ccb11b4a6..5e068656166 100644 --- a/website/content/en/docs/cli/operator-sdk_cleanup.md +++ b/website/content/en/docs/cli/operator-sdk_cleanup.md @@ -17,8 +17,8 @@ operator-sdk cleanup [flags] ``` --delete-all If set to true, all other delete options will be enabled (default true) - --delete-crds If set to false, owned CRDs and CRs will not be deleted (default true) - --delete-operator-groups If set to false, operator groups will not be deleted (default true) + --delete-crds If set to true, owned CRDs and CRs will be deleted + --delete-operator-groups If set to true, operator groups will be deleted -h, --help help for cleanup --kubeconfig string Path to the kubeconfig file to use for CLI requests. -n, --namespace string If present, namespace scope for this CLI request diff --git a/website/content/en/docs/olm-integration/testing-deployment.md b/website/content/en/docs/olm-integration/testing-deployment.md index 2be1bcbe0e6..8021fc7a4de 100644 --- a/website/content/en/docs/olm-integration/testing-deployment.md +++ b/website/content/en/docs/olm-integration/testing-deployment.md @@ -141,14 +141,13 @@ Let's look at the anatomy of the `cleanup` configuration model: - **operatorPackageName**: the Operator's package name which you want to remove from the cluster, e.g. memcached-operator. This is a required parameter. -- **delete-all**: a boolean indicating to enable all the delete flags that are present. This is an optional field - which will default to true if not provided. If set to false, it will enable - specific delete flags. +- **delete-all**: a boolean indicating to enable all the delete flags that are present. This is an optional + field which will default to true if not provided. If set to true, it will enable all the delete flags to be true. If set to false, it will enable specific delete flags. - **delete-crds**: a boolean indicating to delete all owned CRDs and CRs. This is an optional field - which will default to true if not provided. If set to false, owned CRDs and CRs - will stay intact. + which will default to false if not provided. If set to true, owned CRDs and CRs + will be deleted. - **delete-operator-groups**: a boolean indicating to delete all operator groups. This is an optional field - which will default to true if not provided. If set to false, operator groups will not be deleted. + which will default to false if not provided. If set to true, operator groups will be deleted. ### Caveats From f3b73258d0f7045b1811383d6a778033b02ee2c5 Mon Sep 17 00:00:00 2001 From: rashmigottipati Date: Wed, 24 Mar 2021 14:35:08 -0400 Subject: [PATCH 5/5] Add changelog fragment Signed-off-by: rashmigottipati --- changelog/fragments/optional-flags-cleanup.yaml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/fragments/optional-flags-cleanup.yaml diff --git a/changelog/fragments/optional-flags-cleanup.yaml b/changelog/fragments/optional-flags-cleanup.yaml new file mode 100644 index 00000000000..67f03584bbd --- /dev/null +++ b/changelog/fragments/optional-flags-cleanup.yaml @@ -0,0 +1,5 @@ +entries: + - description: > + Added new optional flags `--delete-all`, `--delete-crds` and `--delete-operator-groups` to the cleanup command + kind: "addition" + breaking: false