@@ -92,6 +92,19 @@ type ApplyOptions struct {
92
92
// not call the resource builder; only return the set objects.
93
93
objects []* resource.Info
94
94
objectsCached bool
95
+
96
+ // Stores visited objects/namespaces for later use
97
+ // calculating the set of objects to prune.
98
+ VisitedUids sets.String
99
+ VisitedNamespaces sets.String
100
+
101
+ // Function run after the objects are generated and
102
+ // stored in the "objects" field, but before the
103
+ // apply is run on these objects.
104
+ preProcessorFn func (* ApplyOptions ) error
105
+ // Function run after all objects have been applied.
106
+ // The standard postprocessorFn is "PrintAndPrune()".
107
+ postProcessorFn func (* ApplyOptions ) error
95
108
}
96
109
97
110
var (
@@ -140,6 +153,11 @@ func NewApplyOptions(ioStreams genericclioptions.IOStreams) *ApplyOptions {
140
153
141
154
objects : []* resource.Info {},
142
155
objectsCached : false ,
156
+
157
+ VisitedUids : sets .NewString (),
158
+ VisitedNamespaces : sets .NewString (),
159
+
160
+ postProcessorFn : PrintAndPrune ,
143
161
}
144
162
}
145
163
@@ -340,9 +358,6 @@ func (o *ApplyOptions) Run() error {
340
358
OpenAPIGetter : o .DiscoveryClient ,
341
359
}
342
360
343
- visitedUids := sets .NewString ()
344
- visitedNamespaces := sets .NewString ()
345
-
346
361
infos , err := o .GetObjects ()
347
362
if err != nil {
348
363
return err
@@ -360,9 +375,7 @@ func (o *ApplyOptions) Run() error {
360
375
}
361
376
}
362
377
363
- if info .Namespaced () {
364
- visitedNamespaces .Insert (info .Namespace )
365
- }
378
+ o .MarkNamespaceVisited (info )
366
379
367
380
if err := o .Recorder .Record (info .Object ); err != nil {
368
381
klog .V (4 ).Infof ("error recording current command: %v" , err )
@@ -414,12 +427,11 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
414
427
}
415
428
416
429
info .Refresh (obj , true )
417
- metadata , err := meta . Accessor ( info . Object )
418
- if err != nil {
430
+
431
+ if err := o . MarkObjectVisited ( info ); err != nil {
419
432
return err
420
433
}
421
434
422
- visitedUids .Insert (string (metadata .GetUID ()))
423
435
if o .shouldPrintObject () {
424
436
continue
425
437
}
@@ -467,11 +479,9 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
467
479
info .Refresh (obj , true )
468
480
}
469
481
470
- metadata , err := meta .Accessor (info .Object )
471
- if err != nil {
482
+ if err := o .MarkObjectVisited (info ); err != nil {
472
483
return err
473
484
}
474
- visitedUids .Insert (string (metadata .GetUID ()))
475
485
476
486
if o .shouldPrintObject () {
477
487
continue
@@ -487,13 +497,12 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
487
497
continue
488
498
}
489
499
490
- metadata , err := meta .Accessor (info .Object )
491
- if err != nil {
500
+ if err := o .MarkObjectVisited (info ); err != nil {
492
501
return err
493
502
}
494
- visitedUids .Insert (string (metadata .GetUID ()))
495
503
496
504
if ! o .DryRun {
505
+ metadata , _ := meta .Accessor (info .Object )
497
506
annotationMap := metadata .GetAnnotations ()
498
507
if _ , ok := annotationMap [corev1 .LastAppliedConfigAnnotation ]; ! ok {
499
508
fmt .Fprintf (o .ErrOut , warningNoLastAppliedConfigAnnotation , o .cmdBaseName )
@@ -537,7 +546,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
537
546
}
538
547
539
548
if o .Prune {
540
- p := newPruner (o , visitedUids , visitedNamespaces )
549
+ p := newPruner (o )
541
550
return p .pruneAll (o )
542
551
}
543
552
@@ -599,6 +608,25 @@ func (o *ApplyOptions) printObjects() error {
599
608
return nil
600
609
}
601
610
611
+ // MarkNamespaceVisited keeps track of which namespaces the applied
612
+ // objects belong to. Used for pruning.
613
+ func (o * ApplyOptions ) MarkNamespaceVisited (info * resource.Info ) {
614
+ if info .Namespaced () {
615
+ o .VisitedNamespaces .Insert (info .Namespace )
616
+ }
617
+ }
618
+
619
+ // MarkNamespaceVisited keeps track of UIDs of the applied
620
+ // objects. Used for pruning.
621
+ func (o * ApplyOptions ) MarkObjectVisited (info * resource.Info ) error {
622
+ metadata , err := meta .Accessor (info .Object )
623
+ if err != nil {
624
+ return err
625
+ }
626
+ o .VisitedUids .Insert (string (metadata .GetUID ()))
627
+ return nil
628
+ }
629
+
602
630
// DryRunVerifier verifies if a given group-version-kind supports DryRun
603
631
// against the current server. Sending dryRun requests to apiserver that
604
632
// don't support it will result in objects being unwillingly persisted.
0 commit comments