From 88e17d74780c27fd90c94cc2f0942789637b17cf Mon Sep 17 00:00:00 2001 From: Michael Fraenkel Date: Wed, 31 Aug 2016 14:48:47 -0400 Subject: [PATCH] Require force when using grace period or timeout --- hack/make-rules/test-cmd.sh | 9 +++++++++ pkg/kubectl/cmd/replace.go | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/hack/make-rules/test-cmd.sh b/hack/make-rules/test-cmd.sh index e77b06dfe9e..3fdbdbb6116 100755 --- a/hack/make-rules/test-cmd.sh +++ b/hack/make-rules/test-cmd.sh @@ -750,6 +750,15 @@ runTests() { kubectl replace "${kube_flags[@]}" --force -f /tmp/tmp-valid-pod.json # Post-condition: spec.container.name = "replaced-k8s-serve-hostname" kube::test::get_object_assert 'pod valid-pod' "{{(index .spec.containers 0).name}}" 'replaced-k8s-serve-hostname' + + ## check replace --grace-period requires --force + output_message=$(! kubectl replace "${kube_flags[@]}" --grace-period=1 -f /tmp/tmp-valid-pod.json 2>&1) + kube::test::if_has_string "${output_message}" '\-\-grace-period must have \-\-force specified' + + ## check replace --timeout requires --force + output_message=$(! kubectl replace "${kube_flags[@]}" --timeout=1s -f /tmp/tmp-valid-pod.json 2>&1) + kube::test::if_has_string "${output_message}" '\-\-timeout must have \-\-force specified' + #cleaning rm /tmp/tmp-valid-pod.json diff --git a/pkg/kubectl/cmd/replace.go b/pkg/kubectl/cmd/replace.go index a16d4591673..f75e94cf3f7 100644 --- a/pkg/kubectl/cmd/replace.go +++ b/pkg/kubectl/cmd/replace.go @@ -120,6 +120,14 @@ func RunReplace(f *cmdutil.Factory, out io.Writer, cmd *cobra.Command, args []st return forceReplace(f, out, cmd, args, shortOutput, options) } + if cmdutil.GetFlagInt(cmd, "grace-period") >= 0 { + return fmt.Errorf("--grace-period must have --force specified") + } + + if cmdutil.GetFlagDuration(cmd, "timeout") != 0 { + return fmt.Errorf("--timeout must have --force specified") + } + mapper, typer := f.Object(cmdutil.GetIncludeThirdPartyAPIs(cmd)) r := resource.NewBuilder(mapper, typer, resource.ClientMapperFunc(f.ClientForMapping), f.Decoder(true)). Schema(schema).