Skip to content

Commit 4e45421

Browse files
committed
Moves visitedUids and visitedNamespaces (used for pruning) into ApplyOptions
1 parent 670369f commit 4e45421

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed

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

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,19 @@ type ApplyOptions struct {
9292
// not call the resource builder; only return the set objects.
9393
objects []*resource.Info
9494
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
95108
}
96109

97110
var (
@@ -140,6 +153,11 @@ func NewApplyOptions(ioStreams genericclioptions.IOStreams) *ApplyOptions {
140153

141154
objects: []*resource.Info{},
142155
objectsCached: false,
156+
157+
VisitedUids: sets.NewString(),
158+
VisitedNamespaces: sets.NewString(),
159+
160+
postProcessorFn: PrintAndPrune,
143161
}
144162
}
145163

@@ -340,9 +358,6 @@ func (o *ApplyOptions) Run() error {
340358
OpenAPIGetter: o.DiscoveryClient,
341359
}
342360

343-
visitedUids := sets.NewString()
344-
visitedNamespaces := sets.NewString()
345-
346361
infos, err := o.GetObjects()
347362
if err != nil {
348363
return err
@@ -360,9 +375,7 @@ func (o *ApplyOptions) Run() error {
360375
}
361376
}
362377

363-
if info.Namespaced() {
364-
visitedNamespaces.Insert(info.Namespace)
365-
}
378+
o.MarkNamespaceVisited(info)
366379

367380
if err := o.Recorder.Record(info.Object); err != nil {
368381
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)
414427
}
415428

416429
info.Refresh(obj, true)
417-
metadata, err := meta.Accessor(info.Object)
418-
if err != nil {
430+
431+
if err := o.MarkObjectVisited(info); err != nil {
419432
return err
420433
}
421434

422-
visitedUids.Insert(string(metadata.GetUID()))
423435
if o.shouldPrintObject() {
424436
continue
425437
}
@@ -467,11 +479,9 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
467479
info.Refresh(obj, true)
468480
}
469481

470-
metadata, err := meta.Accessor(info.Object)
471-
if err != nil {
482+
if err := o.MarkObjectVisited(info); err != nil {
472483
return err
473484
}
474-
visitedUids.Insert(string(metadata.GetUID()))
475485

476486
if o.shouldPrintObject() {
477487
continue
@@ -487,13 +497,12 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
487497
continue
488498
}
489499

490-
metadata, err := meta.Accessor(info.Object)
491-
if err != nil {
500+
if err := o.MarkObjectVisited(info); err != nil {
492501
return err
493502
}
494-
visitedUids.Insert(string(metadata.GetUID()))
495503

496504
if !o.DryRun {
505+
metadata, _ := meta.Accessor(info.Object)
497506
annotationMap := metadata.GetAnnotations()
498507
if _, ok := annotationMap[corev1.LastAppliedConfigAnnotation]; !ok {
499508
fmt.Fprintf(o.ErrOut, warningNoLastAppliedConfigAnnotation, o.cmdBaseName)
@@ -537,7 +546,7 @@ See http://k8s.io/docs/reference/using-api/api-concepts/#conflicts`, err)
537546
}
538547

539548
if o.Prune {
540-
p := newPruner(o, visitedUids, visitedNamespaces)
549+
p := newPruner(o)
541550
return p.pruneAll(o)
542551
}
543552

@@ -599,6 +608,25 @@ func (o *ApplyOptions) printObjects() error {
599608
return nil
600609
}
601610

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+
602630
// DryRunVerifier verifies if a given group-version-kind supports DryRun
603631
// against the current server. Sending dryRun requests to apiserver that
604632
// don't support it will result in objects being unwillingly persisted.

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@ type pruner struct {
4949
out io.Writer
5050
}
5151

52-
func newPruner(o *ApplyOptions, visitedUids sets.String, visitedNamespaces sets.String) pruner {
52+
func newPruner(o *ApplyOptions) pruner {
5353
return pruner{
5454
mapper: o.Mapper,
5555
dynamicClient: o.DynamicClient,
5656

5757
labelSelector: o.Selector,
58-
visitedUids: visitedUids,
59-
visitedNamespaces: visitedNamespaces,
58+
visitedUids: o.VisitedUids,
59+
visitedNamespaces: o.VisitedNamespaces,
6060

6161
cascade: o.DeleteOptions.Cascade,
6262
dryRun: o.DryRun,

0 commit comments

Comments
 (0)