Skip to content

Commit 13b80b4

Browse files
Use --dry-run=client,server in kubectl.
- Support --dry-run=server for subcommands apply, run, create, annotate, expose, patch, label, autoscale, apply set-last-applied, drain, rollout undo - Support --dry-run=server for set subcommands - image - resources - serviceaccount - selector - env - subject - Support --dry-run=server for create subcommands. - clusterrole - clusterrolebinding - configmap - cronjob - job - deployment - namespace - poddisruptionbudget - priorityclass - quota - role - rolebinding - service - secret - serviceaccount - Remove GetClientSideDryRun
1 parent f7eafa1 commit 13b80b4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+936
-328
lines changed

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

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ type AnnotateOptions struct {
5353
// Common user flags
5454
overwrite bool
5555
local bool
56-
dryrun bool
56+
dryRunStrategy cmdutil.DryRunStrategy
57+
dryRunVerifier *resource.DryRunVerifier
5758
all bool
5859
resourceVersion string
5960
selector string
@@ -164,11 +165,21 @@ func (o *AnnotateOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, args [
164165
}
165166

166167
o.outputFormat = cmdutil.GetFlagString(cmd, "output")
167-
o.dryrun = cmdutil.GetClientSideDryRun(cmd)
168-
169-
if o.dryrun {
170-
o.PrintFlags.Complete("%s (dry run)")
168+
o.dryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
169+
if err != nil {
170+
return err
171+
}
172+
dynamicClient, err := f.DynamicClient()
173+
if err != nil {
174+
return err
171175
}
176+
discoveryClient, err := f.ToDiscoveryClient()
177+
if err != nil {
178+
return err
179+
}
180+
o.dryRunVerifier = resource.NewDryRunVerifier(dynamicClient, discoveryClient)
181+
182+
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.dryRunStrategy)
172183
printer, err := o.PrintFlags.ToPrinter()
173184
if err != nil {
174185
return err
@@ -266,12 +277,18 @@ func (o AnnotateOptions) RunAnnotate() error {
266277
var outputObj runtime.Object
267278
obj := info.Object
268279

269-
if o.dryrun || o.local {
280+
if o.dryRunStrategy == cmdutil.DryRunClient || o.local {
270281
if err := o.updateAnnotations(obj); err != nil {
271282
return err
272283
}
273284
outputObj = obj
274285
} else {
286+
mapping := info.ResourceMapping()
287+
if o.dryRunStrategy == cmdutil.DryRunServer {
288+
if err := o.dryRunVerifier.HasSupport(mapping.GroupVersionKind); err != nil {
289+
return err
290+
}
291+
}
275292
name, namespace := info.Name, info.Namespace
276293

277294
if len(o.resourceVersion) != 0 {
@@ -303,12 +320,13 @@ func (o AnnotateOptions) RunAnnotate() error {
303320
klog.V(2).Infof("couldn't compute patch: %v", err)
304321
}
305322

306-
mapping := info.ResourceMapping()
307323
client, err := o.unstructuredClientForMapping(mapping)
308324
if err != nil {
309325
return err
310326
}
311-
helper := resource.NewHelper(client, mapping)
327+
helper := resource.
328+
NewHelper(client, mapping).
329+
DryRun(o.dryRunStrategy == cmdutil.DryRunServer)
312330

313331
if createdPatch {
314332
outputObj, err = helper.Patch(namespace, name, types.MergePatchType, patchBytes, nil)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ go_library(
3030
"//staging/src/k8s.io/cli-runtime/pkg/genericclioptions:go_default_library",
3131
"//staging/src/k8s.io/cli-runtime/pkg/printers:go_default_library",
3232
"//staging/src/k8s.io/cli-runtime/pkg/resource:go_default_library",
33-
"//staging/src/k8s.io/client-go/discovery:go_default_library",
3433
"//staging/src/k8s.io/client-go/dynamic:go_default_library",
3534
"//staging/src/k8s.io/kubectl/pkg/cmd/delete:go_default_library",
3635
"//staging/src/k8s.io/kubectl/pkg/cmd/util:go_default_library",

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

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"k8s.io/cli-runtime/pkg/genericclioptions"
3333
"k8s.io/cli-runtime/pkg/printers"
3434
"k8s.io/cli-runtime/pkg/resource"
35-
"k8s.io/client-go/discovery"
3635
"k8s.io/client-go/dynamic"
3736
"k8s.io/klog"
3837
"k8s.io/kubectl/pkg/cmd/delete"
@@ -60,8 +59,8 @@ type ApplyOptions struct {
6059
ForceConflicts bool
6160
FieldManager string
6261
Selector string
63-
DryRun bool
64-
ServerDryRun bool
62+
DryRunStrategy cmdutil.DryRunStrategy
63+
DryRunVerifier *resource.DryRunVerifier
6564
Prune bool
6665
PruneResources []pruneResource
6766
cmdBaseName string
@@ -70,12 +69,11 @@ type ApplyOptions struct {
7069
OpenAPIPatch bool
7170
PruneWhitelist []string
7271

73-
Validator validation.Schema
74-
Builder *resource.Builder
75-
Mapper meta.RESTMapper
76-
DynamicClient dynamic.Interface
77-
DiscoveryClient discovery.DiscoveryInterface
78-
OpenAPISchema openapi.Resources
72+
Validator validation.Schema
73+
Builder *resource.Builder
74+
Mapper meta.RESTMapper
75+
DynamicClient dynamic.Interface
76+
OpenAPISchema openapi.Resources
7977

8078
Namespace string
8179
EnforceNamespace bool
@@ -192,7 +190,7 @@ func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions
192190
cmd.Flags().BoolVar(&o.All, "all", o.All, "Select all resources in the namespace of the specified resource types.")
193191
cmd.Flags().StringArrayVar(&o.PruneWhitelist, "prune-whitelist", o.PruneWhitelist, "Overwrite the default whitelist with <group/version/kind> for --prune")
194192
cmd.Flags().BoolVar(&o.OpenAPIPatch, "openapi-patch", o.OpenAPIPatch, "If true, use openapi to calculate diff when the openapi presents and the resource can be found in the openapi spec. Otherwise, fall back to use baked-in types.")
195-
cmd.Flags().BoolVar(&o.ServerDryRun, "server-dry-run", o.ServerDryRun, "If true, request will be sent to server with dry-run flag, which means the modifications won't be persisted.")
193+
cmd.Flags().Bool("server-dry-run", false, "If true, request will be sent to server with dry-run flag, which means the modifications won't be persisted.")
196194
cmd.Flags().MarkDeprecated("server-dry-run", "--server-dry-run is deprecated and can be replaced with --dry-run=server.")
197195
cmdutil.AddDryRunFlag(cmd)
198196
cmdutil.AddServerSideApplyFlags(cmd)
@@ -210,39 +208,42 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
210208
var err error
211209
o.ServerSideApply = cmdutil.GetServerSideApplyFlag(cmd)
212210
o.ForceConflicts = cmdutil.GetForceConflictsFlag(cmd)
213-
o.FieldManager = cmdutil.GetFieldManagerFlag(cmd)
214-
o.DryRun = cmdutil.GetClientSideDryRun(cmd)
211+
o.DryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
212+
if err != nil {
213+
return err
214+
}
215215
o.DynamicClient, err = f.DynamicClient()
216216
if err != nil {
217217
return err
218218
}
219-
220-
o.DiscoveryClient, err = f.ToDiscoveryClient()
219+
discoveryClient, err := f.ToDiscoveryClient()
221220
if err != nil {
222221
return err
223222
}
223+
o.DryRunVerifier = resource.NewDryRunVerifier(o.DynamicClient, discoveryClient)
224+
o.FieldManager = cmdutil.GetFieldManagerFlag(cmd)
224225

225226
if o.ForceConflicts && !o.ServerSideApply {
226227
return fmt.Errorf("--force-conflicts only works with --server-side")
227228
}
228229

229-
if o.DryRun && o.ServerSideApply {
230-
return fmt.Errorf("--dry-run doesn't work with --server-side (did you mean --server-dry-run instead?)")
230+
if o.DryRunStrategy == cmdutil.DryRunClient && o.ServerSideApply {
231+
return fmt.Errorf("--dry-run=client doesn't work with --server-side (did you mean --dry-run=server instead?)")
232+
}
233+
234+
var deprecatedServerDryRunFlag = cmdutil.GetFlagBool(cmd, "server-dry-run")
235+
if o.DryRunStrategy == cmdutil.DryRunClient && deprecatedServerDryRunFlag {
236+
return fmt.Errorf("--dry-run=client and --server-dry-run can't be used together (did you mean --dry-run=server instead?)")
231237
}
232238

233-
if o.DryRun && o.ServerDryRun {
234-
return fmt.Errorf("--dry-run and --server-dry-run can't be used together")
239+
if o.DryRunStrategy == cmdutil.DryRunNone && deprecatedServerDryRunFlag {
240+
o.DryRunStrategy = cmdutil.DryRunServer
235241
}
236242

237243
// allow for a success message operation to be specified at print time
238244
o.ToPrinter = func(operation string) (printers.ResourcePrinter, error) {
239245
o.PrintFlags.NamePrintFlags.Operation = operation
240-
if o.DryRun {
241-
o.PrintFlags.Complete("%s (dry run)")
242-
}
243-
if o.ServerDryRun {
244-
o.PrintFlags.Complete("%s (server dry run)")
245-
}
246+
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.DryRunStrategy)
246247
return o.PrintFlags.ToPrinter()
247248
}
248249

@@ -397,11 +398,11 @@ func (o *ApplyOptions) Run() error {
397398
}
398399

399400
helper := resource.NewHelper(info.Client, info.Mapping)
400-
if o.ServerDryRun {
401-
if err := resource.VerifyDryRun(info.Mapping.GroupVersionKind, o.DynamicClient, o.DiscoveryClient); err != nil {
401+
if o.DryRunStrategy == cmdutil.DryRunServer {
402+
if err := o.DryRunVerifier.HasSupport(info.Mapping.GroupVersionKind); err != nil {
402403
return err
403404
}
404-
helper.DryRun(o.ServerDryRun)
405+
helper.DryRun(true)
405406
}
406407
obj, err := helper.Patch(
407408
info.Namespace,
@@ -471,14 +472,14 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
471472
return cmdutil.AddSourceToErr("creating", info.Source, err)
472473
}
473474

474-
if !o.DryRun {
475+
if o.DryRunStrategy != cmdutil.DryRunClient {
475476
// Then create the resource and skip the three-way merge
476477
helper := resource.NewHelper(info.Client, info.Mapping)
477-
if o.ServerDryRun {
478-
if err := resource.VerifyDryRun(info.Mapping.GroupVersionKind, o.DynamicClient, o.DiscoveryClient); err != nil {
478+
if o.DryRunStrategy == cmdutil.DryRunServer {
479+
if err := o.DryRunVerifier.HasSupport(info.Mapping.GroupVersionKind); err != nil {
479480
return cmdutil.AddSourceToErr("creating", info.Source, err)
480481
}
481-
helper.DryRun(o.ServerDryRun)
482+
helper.DryRun(true)
482483
}
483484
obj, err := helper.Create(info.Namespace, true, info.Object)
484485
if err != nil {
@@ -509,7 +510,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
509510
return err
510511
}
511512

512-
if !o.DryRun {
513+
if o.DryRunStrategy != cmdutil.DryRunClient {
513514
metadata, _ := meta.Accessor(info.Object)
514515
annotationMap := metadata.GetAnnotations()
515516
if _, ok := annotationMap[corev1.LastAppliedConfigAnnotation]; !ok {

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

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ type SetLastAppliedOptions struct {
4949
infoList []*resource.Info
5050
namespace string
5151
enforceNamespace bool
52-
dryRun bool
52+
dryRunStrategy cmdutil.DryRunStrategy
53+
dryRunVerifier *resource.DryRunVerifier
5354
shortOutput bool
5455
output string
5556
patchBufferList []PatchBuffer
@@ -118,25 +119,31 @@ func NewCmdApplySetLastApplied(f cmdutil.Factory, ioStreams genericclioptions.IO
118119

119120
// Complete populates dry-run and output flag options.
120121
func (o *SetLastAppliedOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
121-
o.dryRun = cmdutil.GetClientSideDryRun(cmd)
122+
var err error
123+
o.dryRunStrategy, err = cmdutil.GetDryRunStrategy(cmd)
124+
if err != nil {
125+
return err
126+
}
127+
dynamicClient, err := f.DynamicClient()
128+
if err != nil {
129+
return err
130+
}
131+
discoveryClient, err := f.ToDiscoveryClient()
132+
if err != nil {
133+
return err
134+
}
135+
o.dryRunVerifier = resource.NewDryRunVerifier(dynamicClient, discoveryClient)
122136
o.output = cmdutil.GetFlagString(cmd, "output")
123137
o.shortOutput = o.output == "name"
124138

125-
var err error
126139
o.namespace, o.enforceNamespace, err = f.ToRawKubeConfigLoader().Namespace()
127140
if err != nil {
128141
return err
129142
}
130143
o.builder = f.NewBuilder()
131144
o.unstructuredClientForMapping = f.UnstructuredClientForMapping
132145

133-
if o.dryRun {
134-
// TODO(juanvallejo): This can be cleaned up even further by creating
135-
// a PrintFlags struct that binds the --dry-run flag, and whose
136-
// ToPrinter method returns a printer that understands how to print
137-
// this success message.
138-
o.PrintFlags.Complete("%s (dry run)")
139-
}
146+
cmdutil.PrintFlagsWithDryRunStrategy(o.PrintFlags, o.dryRunStrategy)
140147
printer, err := o.PrintFlags.ToPrinter()
141148
if err != nil {
142149
return err
@@ -199,13 +206,20 @@ func (o *SetLastAppliedOptions) RunSetLastApplied() error {
199206
info := o.infoList[i]
200207
finalObj := info.Object
201208

202-
if !o.dryRun {
209+
if o.dryRunStrategy != cmdutil.DryRunClient {
203210
mapping := info.ResourceMapping()
204211
client, err := o.unstructuredClientForMapping(mapping)
205212
if err != nil {
206213
return err
207214
}
208-
helper := resource.NewHelper(client, mapping)
215+
if o.dryRunStrategy == cmdutil.DryRunServer {
216+
if err := o.dryRunVerifier.HasSupport(mapping.GroupVersionKind); err != nil {
217+
return err
218+
}
219+
}
220+
helper := resource.
221+
NewHelper(client, mapping).
222+
DryRun(o.dryRunStrategy == cmdutil.DryRunServer)
209223
finalObj, err = helper.Patch(info.Namespace, info.Name, patch.PatchType, patch.Patch, nil)
210224
if err != nil {
211225
return err

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,11 @@ func newPatcher(o *ApplyOptions, info *resource.Info) (*Patcher, error) {
8181
}
8282

8383
helper := resource.NewHelper(info.Client, info.Mapping)
84-
if o.ServerDryRun {
85-
if err := resource.VerifyDryRun(info.Mapping.GroupVersionKind, o.DynamicClient, o.DiscoveryClient); err != nil {
84+
if o.DryRunStrategy == cmdutil.DryRunServer {
85+
if err := o.DryRunVerifier.HasSupport(info.Mapping.GroupVersionKind); err != nil {
8686
return nil, err
8787
}
88-
helper.DryRun(o.ServerDryRun)
88+
helper.DryRun(true)
8989
}
9090
return &Patcher{
9191
Mapping: info.Mapping,
@@ -97,7 +97,7 @@ func newPatcher(o *ApplyOptions, info *resource.Info) (*Patcher, error) {
9797
Cascade: o.DeleteOptions.Cascade,
9898
Timeout: o.DeleteOptions.Timeout,
9999
GracePeriod: o.DeleteOptions.GracePeriod,
100-
ServerDryRun: o.ServerDryRun,
100+
ServerDryRun: o.DryRunStrategy == cmdutil.DryRunServer,
101101
OpenapiSchema: openapiSchema,
102102
Retries: maxPatchRetry,
103103
}, nil

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/apimachinery/pkg/util/sets"
2929
"k8s.io/cli-runtime/pkg/printers"
3030
"k8s.io/client-go/dynamic"
31+
cmdutil "k8s.io/kubectl/pkg/cmd/util"
3132
)
3233

3334
type pruner struct {
@@ -39,10 +40,9 @@ type pruner struct {
3940
labelSelector string
4041
fieldSelector string
4142

42-
cascade bool
43-
serverDryRun bool
44-
dryRun bool
45-
gracePeriod int
43+
cascade bool
44+
dryRunStrategy cmdutil.DryRunStrategy
45+
gracePeriod int
4646

4747
toPrinter func(string) (printers.ResourcePrinter, error)
4848

@@ -58,10 +58,9 @@ func newPruner(o *ApplyOptions) pruner {
5858
visitedUids: o.VisitedUids,
5959
visitedNamespaces: o.VisitedNamespaces,
6060

61-
cascade: o.DeleteOptions.Cascade,
62-
dryRun: o.DryRun,
63-
serverDryRun: o.ServerDryRun,
64-
gracePeriod: o.DeleteOptions.GracePeriod,
61+
cascade: o.DeleteOptions.Cascade,
62+
dryRunStrategy: o.DryRunStrategy,
63+
gracePeriod: o.DeleteOptions.GracePeriod,
6564

6665
toPrinter: o.ToPrinter,
6766

@@ -126,7 +125,7 @@ func (p *pruner) prune(namespace string, mapping *meta.RESTMapping) error {
126125
continue
127126
}
128127
name := metadata.GetName()
129-
if !p.dryRun {
128+
if p.dryRunStrategy != cmdutil.DryRunClient {
130129
if err := p.delete(namespace, name, mapping); err != nil {
131130
return err
132131
}
@@ -142,7 +141,7 @@ func (p *pruner) prune(namespace string, mapping *meta.RESTMapping) error {
142141
}
143142

144143
func (p *pruner) delete(namespace, name string, mapping *meta.RESTMapping) error {
145-
return runDelete(namespace, name, mapping, p.dynamicClient, p.cascade, p.gracePeriod, p.serverDryRun)
144+
return runDelete(namespace, name, mapping, p.dynamicClient, p.cascade, p.gracePeriod, p.dryRunStrategy == cmdutil.DryRunServer)
146145
}
147146

148147
func runDelete(namespace, name string, mapping *meta.RESTMapping, c dynamic.Interface, cascade bool, gracePeriod int, serverDryRun bool) error {

0 commit comments

Comments
 (0)