Fixes #75706 - Show warning message when namespace is specified for deleting a cluster-scoped resource

This commit is contained in:
Youbing Li
2019-03-28 06:34:59 -04:00
parent e7eb742c19
commit c438d7b495
4 changed files with 28 additions and 0 deletions

View File

@@ -106,6 +106,7 @@ type DeleteOptions struct {
ForceDeletion bool
WaitForDeletion bool
Quiet bool
WarnClusterScope bool
GracePeriod int
Timeout time.Duration
@@ -149,6 +150,8 @@ func (o *DeleteOptions) Complete(f cmdutil.Factory, args []string, cmd *cobra.Co
return err
}
o.WarnClusterScope = enforceNamespace && !o.DeleteAllNamespaces
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
@@ -216,6 +219,7 @@ func (o *DeleteOptions) Validate() error {
case o.ForceDeletion:
fmt.Fprintf(o.ErrOut, "warning: --force is ignored because --grace-period is not 0.\n")
}
return nil
}
@@ -228,6 +232,7 @@ func (o *DeleteOptions) DeleteResult(r *resource.Result) error {
if o.IgnoreNotFound {
r = r.IgnoreErrors(errors.IsNotFound)
}
warnClusterScope := o.WarnClusterScope
deletedInfos := []*resource.Info{}
uidMap := cmdwait.UIDMap{}
err := r.Visit(func(info *resource.Info, err error) error {
@@ -247,6 +252,10 @@ func (o *DeleteOptions) DeleteResult(r *resource.Result) error {
}
options.PropagationPolicy = &policy
if warnClusterScope && info.Mapping.Scope.Name() == meta.RESTScopeNameRoot {
fmt.Fprintf(o.ErrOut, "warning: deleting cluster-scoped resources, not scoped to the provided namespace\n")
warnClusterScope = false
}
response, err := o.deleteResource(info, options)
if err != nil {
return err