diff --git a/contrib/completions/bash/kubectl b/contrib/completions/bash/kubectl index 5c22b62c655..65075d60490 100644 --- a/contrib/completions/bash/kubectl +++ b/contrib/completions/bash/kubectl @@ -1179,6 +1179,7 @@ _kubectl_delete() flags+=("--grace-period=") flags+=("--ignore-not-found") flags+=("--include-extended-apis") + flags+=("--now") flags+=("--output=") two_word_flags+=("-o") flags+=("--recursive") diff --git a/docs/man/man1/kubectl-delete.1 b/docs/man/man1/kubectl-delete.1 index 25e178bcf86..18d8f6503fc 100644 --- a/docs/man/man1/kubectl-delete.1 +++ b/docs/man/man1/kubectl-delete.1 @@ -52,6 +52,10 @@ will be lost along with the rest of the resource. \fB\-\-include\-extended\-apis\fP=true If true, include definitions of new APIs via calls to the API server. [default true] +.PP +\fB\-\-now\fP=false + If true, resources are force terminated without graceful deletion (same as \-\-grace\-period=0). + .PP \fB\-o\fP, \fB\-\-output\fP="" Output mode. Use "\-o name" for shorter output (resource/name). @@ -184,6 +188,9 @@ kubectl delete pod,service baz foo # Delete pods and services with label name=myLabel. kubectl delete pods,services \-l name=myLabel +# Delete a pod immediately (no graceful shutdown) +kubectl delete pod foo \-\-now + # Delete a pod with UID 1234\-56\-7890\-234234\-456456. kubectl delete pod 1234\-56\-7890\-234234\-456456 diff --git a/docs/user-guide/kubectl/kubectl_delete.md b/docs/user-guide/kubectl/kubectl_delete.md index 9de55e9f5ff..b1fad60246e 100644 --- a/docs/user-guide/kubectl/kubectl_delete.md +++ b/docs/user-guide/kubectl/kubectl_delete.md @@ -68,6 +68,9 @@ kubectl delete pod,service baz foo # Delete pods and services with label name=myLabel. kubectl delete pods,services -l name=myLabel +# Delete a pod immediately (no graceful shutdown) +kubectl delete pod foo --now + # Delete a pod with UID 1234-56-7890-234234-456456. kubectl delete pod 1234-56-7890-234234-456456 @@ -84,6 +87,7 @@ kubectl delete pods --all --grace-period=-1: Period of time in seconds given to the resource to terminate gracefully. Ignored if negative. --ignore-not-found[=false]: Treat "resource not found" as a successful delete. Defaults to "true" when --all is specified. --include-extended-apis[=true]: If true, include definitions of new APIs via calls to the API server. [default true] + --now[=false]: If true, resources are force terminated without graceful deletion (same as --grace-period=0). -o, --output="": Output mode. Use "-o name" for shorter output (resource/name). -R, --recursive[=false]: If true, process directory recursively. -l, --selector="": Selector (label query) to filter on. @@ -122,7 +126,7 @@ kubectl delete pods --all * [kubectl](kubectl.md) - kubectl controls the Kubernetes cluster manager -###### Auto generated by spf13/cobra on 30-Mar-2016 +###### Auto generated by spf13/cobra on 6-Apr-2016 [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/kubectl/kubectl_delete.md?pixel)]() diff --git a/hack/test-cmd.sh b/hack/test-cmd.sh index 9cae41e8554..1273f152cc6 100755 --- a/hack/test-cmd.sh +++ b/hack/test-cmd.sh @@ -364,6 +364,15 @@ runTests() { # Post-condition: valid-pod POD doesn't exist kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' + ### Delete POD valid-pod by id with --now + # Pre-condition: valid-pod POD exists + kubectl create "${kube_flags[@]}" -f docs/admin/limitrange/valid-pod.yaml + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" 'valid-pod:' + # Command + kubectl delete pod valid-pod "${kube_flags[@]}" --now + # Post-condition: valid-pod POD doesn't exist + kube::test::get_object_assert pods "{{range.items}}{{$id_field}}:{{end}}" '' + ### Create POD valid-pod from dumped YAML # Pre-condition: no POD exists create_and_use_new_namespace @@ -758,7 +767,7 @@ __EOF__ kubectl autoscale -f hack/testdata/frontend-controller.yaml --save-config "${kube_flags[@]}" --max=2 # Post-Condition: hpa "frontend" has configuration annotation [[ "$(kubectl get hpa.v1beta1.extensions frontend -o yaml "${kube_flags[@]}" | grep kubectl.kubernetes.io/last-applied-configuration)" ]] - # Ensure we can interact with HPA objects in lists through both the extensions/v1beta1 and autoscaling/v1 APIs + # Ensure we can interact with HPA objects in lists through both the extensions/v1beta1 and autoscaling/v1 APIs output_message=$(kubectl get hpa -o=jsonpath='{.items[0].apiVersion}' 2>&1 "${kube_flags[@]}") kube::test::if_has_string "${output_message}" 'extensions/v1beta1' output_message=$(kubectl get hpa.extensions -o=jsonpath='{.items[0].apiVersion}' 2>&1 "${kube_flags[@]}") diff --git a/pkg/kubectl/cmd/delete.go b/pkg/kubectl/cmd/delete.go index ca0652b1a55..2982dee1e0d 100644 --- a/pkg/kubectl/cmd/delete.go +++ b/pkg/kubectl/cmd/delete.go @@ -60,6 +60,9 @@ kubectl delete pod,service baz foo # Delete pods and services with label name=myLabel. kubectl delete pods,services -l name=myLabel +# Delete a pod immediately (no graceful shutdown) +kubectl delete pod foo --now + # Delete a pod with UID 1234-56-7890-234234-456456. kubectl delete pod 1234-56-7890-234234-456456 @@ -100,6 +103,7 @@ func NewCmdDelete(f *cmdutil.Factory, out io.Writer) *cobra.Command { cmd.Flags().Bool("ignore-not-found", false, "Treat \"resource not found\" as a successful delete. Defaults to \"true\" when --all is specified.") cmd.Flags().Bool("cascade", true, "If true, cascade the deletion of the resources managed by this resource (e.g. Pods created by a ReplicationController). Default true.") cmd.Flags().Int("grace-period", -1, "Period of time in seconds given to the resource to terminate gracefully. Ignored if negative.") + cmd.Flags().Bool("now", false, "If true, resources are force terminated without graceful deletion (same as --grace-period=0).") cmd.Flags().Duration("timeout", 0, "The length of time to wait before giving up on a delete, zero means determine a timeout from the size of the object") cmdutil.AddOutputFlagsForMutation(cmd) cmdutil.AddInclude3rdPartyFlags(cmd) @@ -140,10 +144,18 @@ func RunDelete(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []str } } + gracePeriod := cmdutil.GetFlagInt(cmd, "grace-period") + if cmdutil.GetFlagBool(cmd, "now") { + if gracePeriod != -1 { + return fmt.Errorf("--now and --grace-period cannot be specified together") + } + gracePeriod = 0 + } + shortOutput := cmdutil.GetFlagString(cmd, "output") == "name" // By default use a reaper to delete all related resources. if cmdutil.GetFlagBool(cmd, "cascade") { - return ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, cmdutil.GetFlagDuration(cmd, "timeout"), cmdutil.GetFlagInt(cmd, "grace-period"), shortOutput, mapper) + return ReapResult(r, f, out, cmdutil.GetFlagBool(cmd, "cascade"), ignoreNotFound, cmdutil.GetFlagDuration(cmd, "timeout"), gracePeriod, shortOutput, mapper) } return DeleteResult(r, out, ignoreNotFound, shortOutput, mapper) }