Added kubectl apply check to prevent using --dry-run=server with --force

This commit is contained in:
Brian Pursley 2020-04-16 21:23:36 -04:00
parent 72fb952dd6
commit 99f6dca1a8
2 changed files with 32 additions and 0 deletions

View File

@ -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 {

View File

@ -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)
}