@@ -32,7 +32,6 @@ import (
32
32
"k8s.io/cli-runtime/pkg/genericclioptions"
33
33
"k8s.io/cli-runtime/pkg/printers"
34
34
"k8s.io/cli-runtime/pkg/resource"
35
- "k8s.io/client-go/discovery"
36
35
"k8s.io/client-go/dynamic"
37
36
"k8s.io/klog"
38
37
"k8s.io/kubectl/pkg/cmd/delete"
@@ -60,8 +59,8 @@ type ApplyOptions struct {
60
59
ForceConflicts bool
61
60
FieldManager string
62
61
Selector string
63
- DryRun bool
64
- ServerDryRun bool
62
+ DryRunStrategy cmdutil. DryRunStrategy
63
+ DryRunVerifier * resource. DryRunVerifier
65
64
Prune bool
66
65
PruneResources []pruneResource
67
66
cmdBaseName string
@@ -70,12 +69,11 @@ type ApplyOptions struct {
70
69
OpenAPIPatch bool
71
70
PruneWhitelist []string
72
71
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
79
77
80
78
Namespace string
81
79
EnforceNamespace bool
@@ -192,7 +190,7 @@ func NewCmdApply(baseName string, f cmdutil.Factory, ioStreams genericclioptions
192
190
cmd .Flags ().BoolVar (& o .All , "all" , o .All , "Select all resources in the namespace of the specified resource types." )
193
191
cmd .Flags ().StringArrayVar (& o .PruneWhitelist , "prune-whitelist" , o .PruneWhitelist , "Overwrite the default whitelist with <group/version/kind> for --prune" )
194
192
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." )
196
194
cmd .Flags ().MarkDeprecated ("server-dry-run" , "--server-dry-run is deprecated and can be replaced with --dry-run=server." )
197
195
cmdutil .AddDryRunFlag (cmd )
198
196
cmdutil .AddServerSideApplyFlags (cmd )
@@ -210,39 +208,42 @@ func (o *ApplyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
210
208
var err error
211
209
o .ServerSideApply = cmdutil .GetServerSideApplyFlag (cmd )
212
210
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
+ }
215
215
o .DynamicClient , err = f .DynamicClient ()
216
216
if err != nil {
217
217
return err
218
218
}
219
-
220
- o .DiscoveryClient , err = f .ToDiscoveryClient ()
219
+ discoveryClient , err := f .ToDiscoveryClient ()
221
220
if err != nil {
222
221
return err
223
222
}
223
+ o .DryRunVerifier = resource .NewDryRunVerifier (o .DynamicClient , discoveryClient )
224
+ o .FieldManager = cmdutil .GetFieldManagerFlag (cmd )
224
225
225
226
if o .ForceConflicts && ! o .ServerSideApply {
226
227
return fmt .Errorf ("--force-conflicts only works with --server-side" )
227
228
}
228
229
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?)" )
231
237
}
232
238
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
235
241
}
236
242
237
243
// allow for a success message operation to be specified at print time
238
244
o .ToPrinter = func (operation string ) (printers.ResourcePrinter , error ) {
239
245
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 )
246
247
return o .PrintFlags .ToPrinter ()
247
248
}
248
249
@@ -397,11 +398,11 @@ func (o *ApplyOptions) Run() error {
397
398
}
398
399
399
400
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 {
402
403
return err
403
404
}
404
- helper .DryRun (o . ServerDryRun )
405
+ helper .DryRun (true )
405
406
}
406
407
obj , err := helper .Patch (
407
408
info .Namespace ,
@@ -471,14 +472,14 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
471
472
return cmdutil .AddSourceToErr ("creating" , info .Source , err )
472
473
}
473
474
474
- if ! o . DryRun {
475
+ if o . DryRunStrategy != cmdutil . DryRunClient {
475
476
// Then create the resource and skip the three-way merge
476
477
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 {
479
480
return cmdutil .AddSourceToErr ("creating" , info .Source , err )
480
481
}
481
- helper .DryRun (o . ServerDryRun )
482
+ helper .DryRun (true )
482
483
}
483
484
obj , err := helper .Create (info .Namespace , true , info .Object )
484
485
if err != nil {
@@ -509,7 +510,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
509
510
return err
510
511
}
511
512
512
- if ! o . DryRun {
513
+ if o . DryRunStrategy != cmdutil . DryRunClient {
513
514
metadata , _ := meta .Accessor (info .Object )
514
515
annotationMap := metadata .GetAnnotations ()
515
516
if _ , ok := annotationMap [corev1 .LastAppliedConfigAnnotation ]; ! ok {
0 commit comments