From bb5341ea30f35ac1a8b208e9454137de78f28f61 Mon Sep 17 00:00:00 2001 From: Maruf Tuhin Date: Thu, 4 Jan 2018 09:35:45 +0600 Subject: [PATCH] Add --all flag for deletes in cli (#145) --- docs/reference/kubedb_delete.md | 9 ++++++++- pkg/cmds/delete.go | 15 ++++++++++++--- pkg/util/flags.go | 1 + 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/docs/reference/kubedb_delete.md b/docs/reference/kubedb_delete.md index cd395f307..fd7e1286b 100644 --- a/docs/reference/kubedb_delete.md +++ b/docs/reference/kubedb_delete.md @@ -19,7 +19,7 @@ Delete resources by filenames, stdin, resources and names, or by resources and l Note that the delete command does NOT do resource version checks ``` -kubedb delete ([-f FILENAME] | TYPE [(NAME | -l label)]) [flags] +kubedb delete ([-f FILENAME] | TYPE [(NAME | -l label | --all)]) [flags] ``` ### Examples @@ -33,11 +33,18 @@ kubedb delete ([-f FILENAME] | TYPE [(NAME | -l label)]) [flags] # Delete elasticsearch with label elasticsearch.kubedb.com/name=elasticsearch-demo. kubedb delete elasticsearch -l elasticsearch.kubedb.com/name=elasticsearch-demo + + # Force delete a mysql object + kubedb delete mysql ms-demo --force + + # Delete all mysql objects + kubedb delete mysql --all ``` ### Options ``` + --all Delete all resources, including uninitialized ones, in the namespace of the specified resource types. -f, --filename stringSlice Filename to use to delete the resource --force Immediate deletion of some resources may result in inconsistency or data loss. -h, --help help for delete diff --git a/pkg/cmds/delete.go b/pkg/cmds/delete.go index 5f26456ff..a9de028cc 100644 --- a/pkg/cmds/delete.go +++ b/pkg/cmds/delete.go @@ -9,6 +9,7 @@ import ( "github.com/kubedb/cli/pkg/util" "github.com/kubedb/cli/pkg/validator" "github.com/spf13/cobra" + kerr "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/types" @@ -34,14 +35,20 @@ var ( cat postgres.json | kubedb delete -f - # Delete elasticsearch with label elasticsearch.kubedb.com/name=elasticsearch-demo. - kubedb delete elasticsearch -l elasticsearch.kubedb.com/name=elasticsearch-demo`) + kubedb delete elasticsearch -l elasticsearch.kubedb.com/name=elasticsearch-demo + + # Force delete a mysql object + kubedb delete mysql ms-demo --force + + # Delete all mysql objects + kubedb delete mysql --all`) ) func NewCmdDelete(out, errOut io.Writer) *cobra.Command { options := &resource.FilenameOptions{} cmd := &cobra.Command{ - Use: "delete ([-f FILENAME] | TYPE [(NAME | -l label)])", + Use: "delete ([-f FILENAME] | TYPE [(NAME | -l label | --all)])", Short: "Delete resources by filenames, stdin, resources and names, or by resources and label selector", Long: deleteLong, Example: deleteExample, @@ -57,6 +64,7 @@ func NewCmdDelete(out, errOut io.Writer) *cobra.Command { func RunDelete(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []string, options *resource.FilenameOptions) error { selector := cmdutil.GetFlagString(cmd, "selector") + deleteAll := cmdutil.GetFlagBool(cmd, "all") cmdNamespace, enforceNamespace := util.GetNamespace(cmd) categoryExpander := f.CategoryExpander() mapper, typer, err := f.UnstructuredObject() @@ -83,6 +91,7 @@ func RunDelete(f cmdutil.Factory, cmd *cobra.Command, out io.Writer, args []stri NamespaceParam(cmdNamespace).DefaultNamespace(). FilenameParam(enforceNamespace, options). SelectorParam(selector). + SelectAllParam(deleteAll). ResourceTypeOrNameArgs(false, args...).RequireObject(true). Flatten(). Do() @@ -153,7 +162,7 @@ func deleteResource(info *resource.Info, out io.Writer, mapper meta.RESTMapper, []byte(forceDeletePatch), ) } - if err := resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name); err != nil { + if err := resource.NewHelper(info.Client, info.Mapping).Delete(info.Namespace, info.Name); err != nil && !kerr.IsNotFound(err) { return cmdutil.AddSourceToErr("deleting", info.Source, err) } cmdutil.PrintSuccess(mapper, shortOutput, out, info.Mapping.Resource, info.Name, false, "deleted") diff --git a/pkg/util/flags.go b/pkg/util/flags.go index 4ef806b8c..f68427c64 100644 --- a/pkg/util/flags.go +++ b/pkg/util/flags.go @@ -25,6 +25,7 @@ func AddCreateFlags(cmd *cobra.Command, options *resource.FilenameOptions) { func AddDeleteFlags(cmd *cobra.Command, options *resource.FilenameOptions) { cmd.Flags().StringP("namespace", "n", core.NamespaceDefault, "Delete object(s) from this namespace.") + cmd.Flags().BoolP("all", "", false, "Delete all resources, including uninitialized ones, in the namespace of the specified resource types.") cmd.Flags().BoolP("force", "", false, "Immediate deletion of some resources may result in inconsistency or data loss.") cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on.") cmd.Flags().StringP("output", "o", "", "Output mode. Use \"-o name\" for shorter output (resource/name).")