Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #63490 from deads2k/cli-48-delete
Automatic merge from submit-queue (batch tested with PRs 63291, 63490, 60445, 63507, 63524). If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

default the ignorenotfound for delete when selecting objects

When selecting resources and then deleting them (as opposed to specifically indicating a resource to delete) we should default to not failing if something disappeared partway through.  This is quite common when label selecting deletes and GC runs before you get to a dependent object.

I also took the liberty of doing complete work in complete instead of validate.

```release-note
`kubectl delete` with selection criteria defaults to ignoring not found errors
```

@kubernetes/sig-cli-maintainers
  • Loading branch information
Kubernetes Submit Queue committed May 8, 2018
2 parents 9091c26 + 4ae3f88 commit f9fd3c8
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions pkg/kubectl/cmd/delete.go
Expand Up @@ -149,6 +149,26 @@ func (o *DeleteOptions) Complete(f cmdutil.Factory, out, errOut io.Writer, args
return err
}

if o.DeleteAll || len(o.LabelSelector) > 0 || len(o.FieldSelector) > 0 {
if f := cmd.Flags().Lookup("ignore-not-found"); f != nil && !f.Changed {
// If the user didn't explicitly set the option, default to ignoring NotFound errors when used with --all, -l, or --field-selector
o.IgnoreNotFound = true
}
}
if o.DeleteNow {
if o.GracePeriod != -1 {
return fmt.Errorf("--now and --grace-period cannot be specified together")
}
o.GracePeriod = 1
}
if o.GracePeriod == 0 && !o.ForceDeletion {
// To preserve backwards compatibility, but prevent accidental data loss, we convert --grace-period=0
// into --grace-period=1 and wait until the object is successfully deleted. Users may provide --force
// to bypass this wait.
o.WaitForDeletion = true
o.GracePeriod = 1
}

o.Reaper = f.Reaper

includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false)
Expand Down Expand Up @@ -193,34 +213,11 @@ func (o *DeleteOptions) Validate(cmd *cobra.Command) error {
if o.DeleteAll && len(o.FieldSelector) > 0 {
return fmt.Errorf("cannot set --all and --field-selector at the same time")
}
if o.DeleteAll {
f := cmd.Flags().Lookup("ignore-not-found")
// The flag should never be missing
if f == nil {
return fmt.Errorf("missing --ignore-not-found flag")
}
// If the user didn't explicitly set the option, default to ignoring NotFound errors when used with --all
if !f.Changed {
o.IgnoreNotFound = true
}
}
if o.DeleteNow {
if o.GracePeriod != -1 {
return fmt.Errorf("--now and --grace-period cannot be specified together")
}
o.GracePeriod = 1
}
if o.GracePeriod == 0 {
if o.ForceDeletion {
fmt.Fprintf(o.ErrOut, "warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.\n")
} else {
// To preserve backwards compatibility, but prevent accidental data loss, we convert --grace-period=0
// into --grace-period=1 and wait until the object is successfully deleted. Users may provide --force
// to bypass this wait.
o.WaitForDeletion = true
o.GracePeriod = 1
}
} else if o.ForceDeletion {

switch {
case o.GracePeriod == 0 && o.ForceDeletion:
fmt.Fprintf(o.ErrOut, "warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.\n")
case o.ForceDeletion:
fmt.Fprintf(o.ErrOut, "warning: --force is ignored because --grace-period is not 0.\n")
}
return nil
Expand Down

0 comments on commit f9fd3c8

Please sign in to comment.