Skip to content

Commit 99f6dca

Browse files
committed
Added kubectl apply check to prevent using --dry-run=server with --force
1 parent 72fb952 commit 99f6dca

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

staging/src/k8s.io/kubectl/pkg/cmd/apply/apply.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,10 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
261261
return err
262262
}
263263

264+
if o.DryRunStrategy == cmdutil.DryRunServer && o.DeleteOptions.ForceDeletion {
265+
return fmt.Errorf("--dry-run=server cannot be used with --force")
266+
}
267+
264268
o.OpenAPISchema, _ = f.OpenAPISchema()
265269
o.Validator, err = f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
266270
if err != nil {

staging/src/k8s.io/kubectl/pkg/cmd/apply/apply_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,3 +1388,31 @@ func TestForceApply(t *testing.T) {
13881388
})
13891389
}
13901390
}
1391+
1392+
func TestDontAllowForceApplyWithServerDryRun(t *testing.T) {
1393+
expectedError := "error: --dry-run=server cannot be used with --force"
1394+
1395+
cmdutil.BehaviorOnFatal(func(str string, code int) {
1396+
panic(str)
1397+
})
1398+
defer func() {
1399+
actualError := recover()
1400+
if expectedError != actualError {
1401+
t.Fatalf(`expected error "%s", but got "%s"`, expectedError, actualError)
1402+
}
1403+
}()
1404+
1405+
tf := cmdtesting.NewTestFactory().WithNamespace("test")
1406+
defer tf.Cleanup()
1407+
1408+
tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
1409+
1410+
ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
1411+
cmd := NewCmdApply("kubectl", tf, ioStreams)
1412+
cmd.Flags().Set("filename", filenameRC)
1413+
cmd.Flags().Set("dry-run", "server")
1414+
cmd.Flags().Set("force", "true")
1415+
cmd.Run(cmd, []string{})
1416+
1417+
t.Fatalf(`expected error "%s"`, expectedError)
1418+
}

0 commit comments

Comments
 (0)