Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation when removing cluster-scope resources and providing namespace #62167

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion hack/make-rules/test-cmd-util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5195,7 +5195,8 @@ runTests() {
kube::test::get_object_assert 'clusterrolebindings -l test-cmd=auth' "{{range.items}}{{$id_field}}:{{end}}" 'testing-CRB:'
kube::test::get_object_assert 'clusterroles -l test-cmd=auth' "{{range.items}}{{$id_field}}:{{end}}" 'testing-CR:'

kubectl delete "${kube_flags[@]}" rolebindings,role,clusterroles,clusterrolebindings -n some-other-random -l test-cmd=auth
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think uses like this are exactly the reason we shouldn't make this breaking change

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel too strong in any direction, to be honest.

kubectl delete "${kube_flags[@]}" clusterroles,clusterrolebindings -l test-cmd=auth
kubectl delete "${kube_flags[@]}" rolebindings,role -n some-other-random -l test-cmd=auth
fi

#####################
Expand Down
30 changes: 23 additions & 7 deletions pkg/kubectl/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,14 @@ var (
type DeleteOptions struct {
resource.FilenameOptions

Selector string
DeleteAll bool
IgnoreNotFound bool
Cascade bool
DeleteNow bool
ForceDeletion bool
WaitForDeletion bool
Selector string
DeleteAll bool
IgnoreNotFound bool
Cascade bool
DeleteNow bool
ForceDeletion bool
WaitForDeletion bool
EnforceNamespace bool

GracePeriod int
Timeout time.Duration
Expand Down Expand Up @@ -167,6 +168,7 @@ func (o *DeleteOptions) Complete(f cmdutil.Factory, out, errOut io.Writer, args
if err != nil {
return err
}
o.EnforceNamespace = enforceNamespace

includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false)
r := f.NewBuilder().
Expand Down Expand Up @@ -229,6 +231,20 @@ func (o *DeleteOptions) Validate(cmd *cobra.Command) error {
} else if o.ForceDeletion {
fmt.Fprintf(o.ErrOut, "warning: --force is ignored because --grace-period is not 0.\n")
}
if o.EnforceNamespace {
if err := o.Result.Visit(func(info *resource.Info, err error) error {
if err != nil {
return err
}
if info.Mapping.Scope.Name() == meta.RESTScopeNameRoot {
kind := info.Mapping.GroupVersionKind.GroupKind()
return fmt.Errorf("%s/%s", strings.ToLower(kind.String()), info.Name)
}
return nil
}); err != nil {
return fmt.Errorf("You cannot specify namespace for non-namespaced resources: %v", err)
}
}
return nil
}

Expand Down