From 99f6dca1a8d1ea64d22a68263c77bcafd6fedc48 Mon Sep 17 00:00:00 2001 From: Brian Pursley Date: Thu, 16 Apr 2020 21:23:36 -0400 Subject: [PATCH] Added kubectl apply check to prevent using --dry-run=server with --force --- .../src/k8s.io/kubectl/pkg/cmd/apply/apply.go | 4 +++ .../kubectl/pkg/cmd/apply/apply_test.go | 28 +++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go index edae087f3f8..489297d9035 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go @@ -261,6 +261,10 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error { return err } + if o.DryRunStrategy == cmdutil.DryRunServer && o.DeleteOptions.ForceDeletion { + return fmt.Errorf("--dry-run=server cannot be used with --force") + } + o.OpenAPISchema, _ = f.OpenAPISchema() o.Validator, err = f.Validator(cmdutil.GetFlagBool(cmd, "validate")) if err != nil { diff --git a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go index 15c34fa7a2a..88b82ee65d5 100644 --- a/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go +++ b/staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go @@ -1388,3 +1388,31 @@ func TestForceApply(t *testing.T) { }) } } + +func TestDontAllowForceApplyWithServerDryRun(t *testing.T) { + expectedError := "error: --dry-run=server cannot be used with --force" + + cmdutil.BehaviorOnFatal(func(str string, code int) { + panic(str) + }) + defer func() { + actualError := recover() + if expectedError != actualError { + t.Fatalf(`expected error "%s", but got "%s"`, expectedError, actualError) + } + }() + + tf := cmdtesting.NewTestFactory().WithNamespace("test") + defer tf.Cleanup() + + tf.ClientConfigVal = cmdtesting.DefaultClientConfig() + + ioStreams, _, _, _ := genericclioptions.NewTestIOStreams() + cmd := NewCmdApply("kubectl", tf, ioStreams) + cmd.Flags().Set("filename", filenameRC) + cmd.Flags().Set("dry-run", "server") + cmd.Flags().Set("force", "true") + cmd.Run(cmd, []string{}) + + t.Fatalf(`expected error "%s"`, expectedError) +}