Delete fails on notfound errors.
If deleting multiple resources, command will continue on error and report not found resources at the end.
This commit is contained in:
@@ -69,10 +69,11 @@ func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command {
|
||||
cmdutil.CheckErr(err)
|
||||
},
|
||||
}
|
||||
usage := "Filename, directory, or URL to a file containing the resource to delete"
|
||||
usage := "Filename, directory, or URL to a file containing the resource to delete."
|
||||
kubectl.AddJsonFilenameFlag(cmd, &filenames, usage)
|
||||
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on")
|
||||
cmd.Flags().Bool("all", false, "[-all] to select all the specified resources")
|
||||
cmd.Flags().StringP("selector", "l", "", "Selector (label query) to filter on.")
|
||||
cmd.Flags().Bool("all", false, "[-all] to select all the specified resources.")
|
||||
cmd.Flags().Bool("ignore-not-found", false, "Treat \"resource not found\" as a successful delete.")
|
||||
cmd.Flags().Bool("cascade", true, "If true, cascade the delete resources managed by this resource (e.g. Pods created by a ReplicationController). Default true.")
|
||||
cmd.Flags().Int("grace-period", -1, "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.")
|
||||
return cmd
|
||||
@@ -98,20 +99,24 @@ func RunDelete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str
|
||||
return err
|
||||
}
|
||||
|
||||
ignoreNotFound := cmdutil.GetFlagBool(cmd, "ignore-not-found")
|
||||
// By default use a reaper to delete all related resources.
|
||||
if cmdutil.GetFlagBool(cmd, "cascade") {
|
||||
return ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), cmdutil.GetFlagInt(cmd, "grace-period"))
|
||||
return ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, cmdutil.GetFlagInt(cmd, "grace-period"))
|
||||
}
|
||||
return DeleteResult(r, out)
|
||||
return DeleteResult(r, out, ignoreNotFound)
|
||||
}
|
||||
|
||||
func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefaultDelete bool, gracePeriod int) error {
|
||||
func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefaultDelete, ignoreNotFound bool, gracePeriod int) error {
|
||||
found := 0
|
||||
err := r.IgnoreErrors(errors.IsNotFound).Visit(func(info *resource.Info) error {
|
||||
if ignoreNotFound {
|
||||
r = r.IgnoreErrors(errors.IsNotFound)
|
||||
}
|
||||
err := r.Visit(func(info *resource.Info) error {
|
||||
found++
|
||||
reaper, err := f.Reaper(info.Mapping)
|
||||
if err != nil {
|
||||
// If the error is "not found" and the user didn't explicitly ask for stop.
|
||||
// If there is no reaper for this resources and the user didn't explicitly ask for stop.
|
||||
if kubectl.IsNoSuchReaperError(err) && isDefaultDelete {
|
||||
return deleteResource(info, out)
|
||||
}
|
||||
@@ -136,9 +141,12 @@ func ReapResult(r *resource.Result, f *cmdutil.Factory, out io.Writer, isDefault
|
||||
return nil
|
||||
}
|
||||
|
||||
func DeleteResult(r *resource.Result, out io.Writer) error {
|
||||
func DeleteResult(r *resource.Result, out io.Writer, ignoreNotFound bool) error {
|
||||
found := 0
|
||||
err := r.IgnoreErrors(errors.IsNotFound).Visit(func(info *resource.Info) error {
|
||||
if ignoreNotFound {
|
||||
r = r.IgnoreErrors(errors.IsNotFound)
|
||||
}
|
||||
err := r.Visit(func(info *resource.Info) error {
|
||||
found++
|
||||
return deleteResource(info, out)
|
||||
})
|
||||
|
Reference in New Issue
Block a user