From 6f1b178ed762202a83200c9523f095794b1c5bca Mon Sep 17 00:00:00 2001 From: Nail Islamov Date: Sun, 27 May 2018 23:40:52 +1000 Subject: [PATCH] Declare wait flag in way consistent with other deletion flags --- pkg/kubectl/cmd/delete.go | 14 ++++++-------- pkg/kubectl/cmd/delete_flags.go | 13 +++++++++++-- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/kubectl/cmd/delete.go b/pkg/kubectl/cmd/delete.go index 6446e2a354f..10c93998f05 100644 --- a/pkg/kubectl/cmd/delete.go +++ b/pkg/kubectl/cmd/delete.go @@ -139,8 +139,6 @@ func NewCmdDelete(f cmdutil.Factory, streams genericclioptions.IOStreams) *cobra deleteFlags.AddFlags(cmd) - cmd.Flags().Bool("wait", true, `If true, wait for resources to be gone before returning. This waits for finalizers.`) - cmdutil.AddIncludeUninitializedFlag(cmd) return cmd } @@ -165,14 +163,9 @@ func (o *DeleteOptions) Complete(f cmdutil.Factory, args []string, cmd *cobra.Co } 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 + // into --grace-period=1. Users may provide --force to bypass this conversion. o.GracePeriod = 1 } - if b, err := cmd.Flags().GetBool("wait"); err == nil { - o.WaitForDeletion = b - } includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false) r := f.NewBuilder(). @@ -218,6 +211,11 @@ func (o *DeleteOptions) Validate(cmd *cobra.Command) error { return fmt.Errorf("cannot set --all and --field-selector at the same time") } + if o.GracePeriod == 0 && !o.ForceDeletion && !o.WaitForDeletion { + // With the explicit --wait flag we need extra validation for backward compatibility + return fmt.Errorf("--grace-period=0 must have either --force specified, or --wait to be set to true") + } + 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") diff --git a/pkg/kubectl/cmd/delete_flags.go b/pkg/kubectl/cmd/delete_flags.go index fc0580c688d..c98d1fce0f5 100644 --- a/pkg/kubectl/cmd/delete_flags.go +++ b/pkg/kubectl/cmd/delete_flags.go @@ -39,6 +39,7 @@ type DeleteFlags struct { IgnoreNotFound *bool Now *bool Timeout *time.Duration + Wait *bool Output *string } @@ -85,6 +86,9 @@ func (f *DeleteFlags) ToOptions(dynamicClient dynamic.Interface, streams generic if f.Timeout != nil { options.Timeout = *f.Timeout } + if f.Wait != nil { + options.WaitForDeletion = *f.Wait + } return options } @@ -118,11 +122,12 @@ func (f *DeleteFlags) AddFlags(cmd *cobra.Command) { if f.IgnoreNotFound != nil { cmd.Flags().BoolVar(f.IgnoreNotFound, "ignore-not-found", *f.IgnoreNotFound, "Treat \"resource not found\" as a successful delete. Defaults to \"true\" when --all is specified.") } - + if f.Wait != nil { + cmd.Flags().BoolVar(f.Wait, "wait", *f.Wait, "If true, wait for resources to be gone before returning. This waits for finalizers.") + } if f.Output != nil { cmd.Flags().StringVarP(f.Output, "output", "o", *f.Output, "Output mode. Use \"-o name\" for shorter output (resource/name).") } - } // NewDeleteCommandFlags provides default flags and values for use with the "delete" command @@ -139,6 +144,7 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags { labelSelector := "" fieldSelector := "" timeout := time.Duration(0) + wait := true filenames := []string{} recursive := false @@ -156,6 +162,7 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags { IgnoreNotFound: &ignoreNotFound, Now: &now, Timeout: &timeout, + Wait: &wait, Output: &output, } } @@ -167,6 +174,7 @@ func NewDeleteFlags(usage string) *DeleteFlags { force := false timeout := time.Duration(0) + wait := false filenames := []string{} recursive := false @@ -180,5 +188,6 @@ func NewDeleteFlags(usage string) *DeleteFlags { // add non-defaults Force: &force, Timeout: &timeout, + Wait: &wait, } }