Skip to content

Commit

Permalink
Add --all flag for deletes in cli (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
the-redback authored and tamalsaha committed Jan 4, 2018
1 parent c82bd7d commit bb5341e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
9 changes: 8 additions & 1 deletion docs/reference/kubedb_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
15 changes: 12 additions & 3 deletions pkg/cmds/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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")
Expand Down
1 change: 1 addition & 0 deletions pkg/util/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).")
Expand Down

0 comments on commit bb5341e

Please sign in to comment.