Skip to content

Commit

Permalink
add --all-namespaces to
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed Feb 5, 2019
1 parent 9d6ebf6 commit 66b1f5b
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 8 deletions.
1 change: 1 addition & 0 deletions hack/.shellcheck_failures
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
./test/cmd/core.sh
./test/cmd/crd.sh
./test/cmd/create.sh
./test/cmd/delete.sh
./test/cmd/diff.sh
./test/cmd/discovery.sh
./test/cmd/generic-resources.sh
Expand Down
18 changes: 10 additions & 8 deletions pkg/kubectl/cmd/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,15 @@ var (
type DeleteOptions struct {
resource.FilenameOptions

LabelSelector string
FieldSelector string
DeleteAll bool
IgnoreNotFound bool
Cascade bool
DeleteNow bool
ForceDeletion bool
WaitForDeletion bool
LabelSelector string
FieldSelector string
DeleteAll bool
DeleteAllNamespaces bool
IgnoreNotFound bool
Cascade bool
DeleteNow bool
ForceDeletion bool
WaitForDeletion bool

GracePeriod int
Timeout time.Duration
Expand Down Expand Up @@ -170,6 +171,7 @@ func (o *DeleteOptions) Complete(f cmdutil.Factory, args []string, cmd *cobra.Co
LabelSelectorParam(o.LabelSelector).
FieldSelectorParam(o.FieldSelector).
SelectAllParam(o.DeleteAll).
AllNamespaces(o.DeleteAllNamespaces).
ResourceTypeOrNameArgs(false, args...).RequireObject(false).
Flatten().
Do()
Expand Down
9 changes: 9 additions & 0 deletions pkg/kubectl/cmd/delete/delete_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type DeleteFlags struct {
FieldSelector *string

All *bool
AllNamespaces *bool
Cascade *bool
Force *bool
GracePeriod *int
Expand Down Expand Up @@ -68,6 +69,9 @@ func (f *DeleteFlags) ToOptions(dynamicClient dynamic.Interface, streams generic
if f.All != nil {
options.DeleteAll = *f.All
}
if f.AllNamespaces != nil {
options.DeleteAllNamespaces = *f.AllNamespaces
}
if f.Cascade != nil {
options.Cascade = *f.Cascade
}
Expand Down Expand Up @@ -104,6 +108,9 @@ func (f *DeleteFlags) AddFlags(cmd *cobra.Command) {
if f.All != nil {
cmd.Flags().BoolVar(f.All, "all", *f.All, "Delete all resources, including uninitialized ones, in the namespace of the specified resource types.")
}
if f.AllNamespaces != nil {
cmd.Flags().BoolVarP(f.AllNamespaces, "all-namespaces", "A", *f.AllNamespaces, "If present, list the requested object(s) across all namespaces. Namespace in current context is ignored even if specified with --namespace.")
}
if f.Force != nil {
cmd.Flags().BoolVar(f.Force, "force", *f.Force, "Only used when grace-period=0. If true, immediately remove resources from API and bypass graceful deletion. Note that immediate deletion of some resources may result in inconsistency or data loss and requires confirmation.")
}
Expand Down Expand Up @@ -137,6 +144,7 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags {

// setup command defaults
all := false
allNamespaces := false
force := false
ignoreNotFound := false
now := false
Expand All @@ -158,6 +166,7 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags {
GracePeriod: &gracePeriod,

All: &all,
AllNamespaces: &allNamespaces,
Force: &force,
IgnoreNotFound: &ignoreNotFound,
Now: &now,
Expand Down
45 changes: 45 additions & 0 deletions test/cmd/delete.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#!/usr/bin/env bash

# Copyright 2018 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -o errexit
set -o nounset
set -o pipefail

# Runs tests related to kubectl delete --all-namespaces.
run_kubectl_delete_allnamespaces_tests() {
set -o nounset
set -o errexit

ns_one="namespace-$(date +%s)-${RANDOM}"
ns_two="namespace-$(date +%s)-${RANDOM}"
kubectl create namespace "${ns_one}"
kubectl create namespace "${ns_two}"

kubectl create configmap "one" --namespace="${ns_one}"
kubectl create configmap "two" --namespace="${ns_two}"
kubectl label configmap "one" --namespace="${ns_one}" deletetest=true
kubectl label configmap "two" --namespace="${ns_two}" deletetest=true
kubectl delete configmap -l deletetest=true --all-namespaces

# no configmaps should be in either of those namespaces
kubectl config set-context "${CONTEXT}" --namespace="${ns_one}"
kube::test::get_object_assert configmap "{{range.items}}{{$id_field}}:{{end}}" ''
kubectl config set-context "${CONTEXT}" --namespace="${ns_two}"
kube::test::get_object_assert configmap "{{range.items}}{{$id_field}}:{{end}}" ''

set +o nounset
set +o errexit
}
8 changes: 8 additions & 0 deletions test/cmd/legacy-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ source "${KUBE_ROOT}/test/cmd/certificate.sh"
source "${KUBE_ROOT}/test/cmd/core.sh"
source "${KUBE_ROOT}/test/cmd/crd.sh"
source "${KUBE_ROOT}/test/cmd/create.sh"
source "${KUBE_ROOT}/test/cmd/delete.sh"
source "${KUBE_ROOT}/test/cmd/diff.sh"
source "${KUBE_ROOT}/test/cmd/discovery.sh"
source "${KUBE_ROOT}/test/cmd/generic-resources.sh"
Expand Down Expand Up @@ -492,6 +493,13 @@ runTests() {
record_command run_create_secret_tests
fi

######################
# Delete #
######################
if kube::test::if_supports_resource "${configmaps}" ; then
record_command run_kubectl_delete_allnamespaces_tests
fi

##################
# Global timeout #
##################
Expand Down

0 comments on commit 66b1f5b

Please sign in to comment.