Skip to content

Commit 875f31e

Browse files
authored
Merge pull request kubernetes#90225 from brianpursley/kubernetes-90017
Added kubectl apply validation to prevent using --dry-run=server with --force
2 parents a4ceb18 + 99f6dca commit 875f31e

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
@@ -250,6 +250,10 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
250250
return err
251251
}
252252

253+
if o.DryRunStrategy == cmdutil.DryRunServer && o.DeleteOptions.ForceDeletion {
254+
return fmt.Errorf("--dry-run=server cannot be used with --force")
255+
}
256+
253257
o.OpenAPISchema, _ = f.OpenAPISchema()
254258
o.Validator, err = f.Validator(cmdutil.GetFlagBool(cmd, "validate"))
255259
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
@@ -1382,3 +1382,31 @@ func TestForceApply(t *testing.T) {
13821382
})
13831383
}
13841384
}
1385+
1386+
func TestDontAllowForceApplyWithServerDryRun(t *testing.T) {
1387+
expectedError := "error: --dry-run=server cannot be used with --force"
1388+
1389+
cmdutil.BehaviorOnFatal(func(str string, code int) {
1390+
panic(str)
1391+
})
1392+
defer func() {
1393+
actualError := recover()
1394+
if expectedError != actualError {
1395+
t.Fatalf(`expected error "%s", but got "%s"`, expectedError, actualError)
1396+
}
1397+
}()
1398+
1399+
tf := cmdtesting.NewTestFactory().WithNamespace("test")
1400+
defer tf.Cleanup()
1401+
1402+
tf.ClientConfigVal = cmdtesting.DefaultClientConfig()
1403+
1404+
ioStreams, _, _, _ := genericclioptions.NewTestIOStreams()
1405+
cmd := NewCmdApply("kubectl", tf, ioStreams)
1406+
cmd.Flags().Set("filename", filenameRC)
1407+
cmd.Flags().Set("dry-run", "server")
1408+
cmd.Flags().Set("force", "true")
1409+
cmd.Run(cmd, []string{})
1410+
1411+
t.Fatalf(`expected error "%s"`, expectedError)
1412+
}

0 commit comments

Comments
 (0)