From 66b1f5ba67aa8da83366f6d00149d9d9bc1eeef4 Mon Sep 17 00:00:00 2001 From: David Eads Date: Mon, 4 Feb 2019 15:25:16 -0500 Subject: [PATCH] add --all-namespaces to --- hack/.shellcheck_failures | 1 + pkg/kubectl/cmd/delete/delete.go | 18 ++++++----- pkg/kubectl/cmd/delete/delete_flags.go | 9 ++++++ test/cmd/delete.sh | 45 ++++++++++++++++++++++++++ test/cmd/legacy-script.sh | 8 +++++ 5 files changed, 73 insertions(+), 8 deletions(-) create mode 100755 test/cmd/delete.sh diff --git a/hack/.shellcheck_failures b/hack/.shellcheck_failures index e2a3cccf735..ca7b6cfa293 100644 --- a/hack/.shellcheck_failures +++ b/hack/.shellcheck_failures @@ -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 diff --git a/pkg/kubectl/cmd/delete/delete.go b/pkg/kubectl/cmd/delete/delete.go index bd3aa793492..eabc6c6df0f 100644 --- a/pkg/kubectl/cmd/delete/delete.go +++ b/pkg/kubectl/cmd/delete/delete.go @@ -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 @@ -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() diff --git a/pkg/kubectl/cmd/delete/delete_flags.go b/pkg/kubectl/cmd/delete/delete_flags.go index 22751c697d0..4a65a9c1a81 100644 --- a/pkg/kubectl/cmd/delete/delete_flags.go +++ b/pkg/kubectl/cmd/delete/delete_flags.go @@ -33,6 +33,7 @@ type DeleteFlags struct { FieldSelector *string All *bool + AllNamespaces *bool Cascade *bool Force *bool GracePeriod *int @@ -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 } @@ -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.") } @@ -137,6 +144,7 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags { // setup command defaults all := false + allNamespaces := false force := false ignoreNotFound := false now := false @@ -158,6 +166,7 @@ func NewDeleteCommandFlags(usage string) *DeleteFlags { GracePeriod: &gracePeriod, All: &all, + AllNamespaces: &allNamespaces, Force: &force, IgnoreNotFound: &ignoreNotFound, Now: &now, diff --git a/test/cmd/delete.sh b/test/cmd/delete.sh new file mode 100755 index 00000000000..18b9ba172ee --- /dev/null +++ b/test/cmd/delete.sh @@ -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 +} diff --git a/test/cmd/legacy-script.sh b/test/cmd/legacy-script.sh index 73eef56e297..3eea6394656 100755 --- a/test/cmd/legacy-script.sh +++ b/test/cmd/legacy-script.sh @@ -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" @@ -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 # ##################