Skip to content

Commit 034d5c3

Browse files
committed
Use the TaskContext for passing UIDs of applied resources to the pruner
1 parent 8e98b5a commit 034d5c3

File tree

9 files changed

+56
-26
lines changed

9 files changed

+56
-26
lines changed

pkg/apply/applier.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func NewApplier(provider provider.Provider, ioStreams genericclioptions.IOStream
4141
ApplyOptions: applyOptions,
4242
// VisitedUids keeps track of the unique identifiers for all
4343
// currently applied objects. Used to calculate prune set.
44-
PruneOptions: prune.NewPruneOptions(applyOptions.VisitedUids),
44+
PruneOptions: prune.NewPruneOptions(),
4545
provider: provider,
4646
ioStreams: ioStreams,
4747
}

pkg/apply/destroyer.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ import (
2929
func NewDestroyer(provider provider.Provider, ioStreams genericclioptions.IOStreams) *Destroyer {
3030
return &Destroyer{
3131
ApplyOptions: apply.NewApplyOptions(ioStreams),
32-
// Create and maintain an empty set of UID's. This empty UID set
33-
// is used during prune calculation to prune every object.
34-
PruneOptions: prune.NewPruneOptions(sets.NewString()),
32+
PruneOptions: prune.NewPruneOptions(),
3533
provider: provider,
3634
ioStreams: ioStreams,
3735
}
@@ -98,7 +96,7 @@ func (d *Destroyer) Run(inv *resource.Info) <-chan event.Event {
9896
// Events. That we use Prune to implement destroy is an
9997
// implementation detail and the events should not be Prune events.
10098
tempChannel, completedChannel := runPruneEventTransformer(ch)
101-
err := d.PruneOptions.Prune(infos, tempChannel, prune.Options{
99+
err := d.PruneOptions.Prune(infos, sets.NewString(), tempChannel, prune.Options{
102100
DryRunStrategy: d.DryRunStrategy,
103101
PropagationPolicy: metav1.DeletePropagationBackground,
104102
})

pkg/apply/prune/prune.go

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ type PruneOptions struct {
3636
InvClient inventory.InventoryClient
3737
client dynamic.Interface
3838
mapper meta.RESTMapper
39-
// Stores the UID for each of the currently applied objects.
40-
// These UID's are written during the apply, and this data
41-
// structure is shared. IMPORTANT: the apply task must
42-
// always complete before this prune is run.
43-
currentUids sets.String
4439
// True if we are destroying, which deletes the inventory object
4540
// as well (possibly) the inventory namespace.
4641
Destroy bool
@@ -49,10 +44,9 @@ type PruneOptions struct {
4944
// NewPruneOptions returns a struct (PruneOptions) encapsulating the necessary
5045
// information to run the prune. Returns an error if an error occurs
5146
// gathering this information.
52-
func NewPruneOptions(currentUids sets.String) *PruneOptions {
47+
func NewPruneOptions() *PruneOptions {
5348
po := &PruneOptions{
54-
currentUids: currentUids,
55-
Destroy: false,
49+
Destroy: false,
5650
}
5751
return po
5852
}
@@ -86,7 +80,8 @@ type Options struct {
8680
// (retrieved from previous inventory objects) but omitted in
8781
// the current apply. Prune also delete all previous inventory
8882
// objects. Returns an error if there was a problem.
89-
func (po *PruneOptions) Prune(localInfos []*resource.Info, eventChannel chan<- event.Event, o Options) error {
83+
func (po *PruneOptions) Prune(localInfos []*resource.Info, currentUIDs sets.String,
84+
eventChannel chan<- event.Event, o Options) error {
9085
localInv, localInfos, err := inventory.SplitInfos(localInfos)
9186
if err != nil {
9287
return err
@@ -97,7 +92,7 @@ func (po *PruneOptions) Prune(localInfos []*resource.Info, eventChannel chan<- e
9792
if err != nil {
9893
return err
9994
}
100-
klog.V(4).Infof("prune %d currently applied objects", len(po.currentUids))
95+
klog.V(4).Infof("prune %d currently applied objects", len(currentUIDs))
10196
klog.V(4).Infof("prune %d previously applied objects", len(clusterObjs))
10297
// Sort the resources in reverse order using the same rules as is
10398
// used for apply.
@@ -125,7 +120,7 @@ func (po *PruneOptions) Prune(localInfos []*resource.Info, eventChannel chan<- e
125120
// object is part of the local apply set, skip it.
126121
uid := string(metadata.GetUID())
127122
klog.V(7).Infof("prune previously applied object UID: %s", uid)
128-
if po.currentUids.Has(uid) {
123+
if currentUIDs.Has(uid) {
129124
klog.V(7).Infof("prune object in current apply; do not prune: %s", uid)
130125
continue
131126
}

pkg/apply/prune/prune_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func TestPrune(t *testing.T) {
192192
for i := range common.Strategies {
193193
drs := common.Strategies[i]
194194
t.Run(name, func(t *testing.T) {
195-
po := NewPruneOptions(populateObjectIds(tc.currentInfos, t))
195+
po := NewPruneOptions()
196196
// Set up the previously applied objects.
197197
clusterObjs, _ := object.InfosToObjMetas(tc.pastInfos)
198198
po.InvClient = inventory.NewFakeInventoryClient(clusterObjs)
@@ -210,7 +210,7 @@ func TestPrune(t *testing.T) {
210210
err := func() error {
211211
defer close(eventChannel)
212212
// Run the prune and validate.
213-
return po.Prune(currentInfos, eventChannel, Options{
213+
return po.Prune(currentInfos, populateObjectIds(tc.currentInfos, t), eventChannel, Options{
214214
DryRunStrategy: drs,
215215
})
216216
}()

pkg/apply/task/apply_task.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ func (a *ApplyTask) Start(taskContext *taskrunner.TaskContext) {
119119
if err != nil {
120120
continue
121121
}
122+
uid := acc.GetUID()
122123
gen := acc.GetGeneration()
123-
taskContext.ResourceApplied(id, gen)
124+
taskContext.ResourceApplied(id, uid, gen)
124125
}
125126
}
126127
a.sendTaskResult(taskContext, nil)

pkg/apply/task/apply_task_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"gotest.tools/assert"
1111
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1212
"k8s.io/apimachinery/pkg/runtime/schema"
13+
"k8s.io/apimachinery/pkg/types"
1314
"k8s.io/cli-runtime/pkg/resource"
1415
"sigs.k8s.io/cli-utils/pkg/apply/event"
1516
"sigs.k8s.io/cli-utils/pkg/apply/taskrunner"
@@ -24,6 +25,7 @@ type resourceInfo struct {
2425
kind string
2526
name string
2627
namespace string
28+
uid types.UID
2729
generation int64
2830
}
2931

@@ -39,6 +41,7 @@ func TestApplyTask_FetchGeneration(t *testing.T) {
3941
kind: "Deployment",
4042
name: "foo",
4143
namespace: "default",
44+
uid: types.UID("my-uid"),
4245
generation: int64(42),
4346
},
4447
},
@@ -50,13 +53,15 @@ func TestApplyTask_FetchGeneration(t *testing.T) {
5053
apiVersion: "custom.io/v1beta1",
5154
kind: "Custom",
5255
name: "bar",
56+
uid: types.UID("uid-1"),
5357
generation: int64(32),
5458
},
5559
{
5660
group: "custom2.io",
5761
apiVersion: "custom2.io/v1",
5862
kind: "Custom2",
5963
name: "foo",
64+
uid: types.UID("uid-2"),
6065
generation: int64(1),
6166
},
6267
},
@@ -92,7 +97,10 @@ func TestApplyTask_FetchGeneration(t *testing.T) {
9297
Name: info.name,
9398
Namespace: info.namespace,
9499
}
95-
gen := taskContext.ResourceGeneration(id)
100+
uid, _ := taskContext.ResourceUID(id)
101+
assert.Equal(t, info.uid, uid)
102+
103+
gen, _ := taskContext.ResourceGeneration(id)
96104
assert.Equal(t, info.generation, gen)
97105
}
98106
})
@@ -294,6 +302,7 @@ func toInfos(rss []resourceInfo) []*resource.Info {
294302
"metadata": map[string]interface{}{
295303
"name": rs.name,
296304
"namespace": rs.namespace,
305+
"uid": string(rs.uid),
297306
"generation": rs.generation,
298307
},
299308
},

pkg/apply/task/prune_task.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ type PruneTask struct {
2727
// to signal to the taskrunner that the task has completed (or failed).
2828
func (p *PruneTask) Start(taskContext *taskrunner.TaskContext) {
2929
go func() {
30-
err := p.PruneOptions.Prune(p.Objects, taskContext.EventChannel(),
30+
currentUIDs := taskContext.AllResourceUIDs()
31+
err := p.PruneOptions.Prune(p.Objects, currentUIDs, taskContext.EventChannel(),
3132
prune.Options{
3233
DryRunStrategy: p.DryRunStrategy,
3334
PropagationPolicy: p.PropagationPolicy,

pkg/apply/taskrunner/context.go

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
package taskrunner
55

66
import (
7+
"k8s.io/apimachinery/pkg/types"
8+
"k8s.io/apimachinery/pkg/util/sets"
79
"sigs.k8s.io/cli-utils/pkg/apply/event"
810
"sigs.k8s.io/cli-utils/pkg/object"
911
)
@@ -38,20 +40,40 @@ func (tc *TaskContext) EventChannel() chan event.Event {
3840
// ResourceApplied updates the context with information about the
3941
// resource identified by the provided id. Currently, we keep information
4042
// about the generation of the resource after the apply operation completed.
41-
func (tc *TaskContext) ResourceApplied(id object.ObjMetadata, gen int64) {
43+
func (tc *TaskContext) ResourceApplied(id object.ObjMetadata, uid types.UID, gen int64) {
4244
tc.appliedResources[id] = applyInfo{
4345
generation: gen,
46+
uid: uid,
4447
}
4548
}
4649

50+
// ResourceUID looks up the UID of the given resource
51+
func (tc *TaskContext) ResourceUID(id object.ObjMetadata) (types.UID, bool) {
52+
ai, found := tc.appliedResources[id]
53+
if !found {
54+
return "", false
55+
}
56+
return ai.uid, true
57+
}
58+
59+
// AllResourceUIDs returns a set with the UIDs of all the resources in the
60+
// context.
61+
func (tc *TaskContext) AllResourceUIDs() sets.String {
62+
uids := sets.NewString()
63+
for _, ai := range tc.appliedResources {
64+
uids.Insert(string(ai.uid))
65+
}
66+
return uids
67+
}
68+
4769
// ResourceGeneration looks up the generation of the given resource
4870
// after it was applied.
49-
func (tc *TaskContext) ResourceGeneration(id object.ObjMetadata) int64 {
71+
func (tc *TaskContext) ResourceGeneration(id object.ObjMetadata) (int64, bool) {
5072
ai, found := tc.appliedResources[id]
5173
if !found {
52-
return 0
74+
return 0, false
5375
}
54-
return ai.generation
76+
return ai.generation, true
5577
}
5678

5779
// applyInfo captures information about resources that have been
@@ -63,4 +85,7 @@ type applyInfo struct {
6385
// that the APIServer increases every time the desired state of a
6486
// resource changes.
6587
generation int64
88+
89+
// uid captures the uid of the resource that has been applied.
90+
uid types.UID
6691
}

pkg/apply/taskrunner/task.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,10 @@ func (w *WaitTask) checkCondition(taskContext *TaskContext, coll *resourceStatus
112112
func (w *WaitTask) computeResourceWaitData(taskContext *TaskContext) []resourceWaitData {
113113
var rwd []resourceWaitData
114114
for _, id := range w.Identifiers {
115+
gen, _ := taskContext.ResourceGeneration(id)
115116
rwd = append(rwd, resourceWaitData{
116117
identifier: id,
117-
generation: taskContext.ResourceGeneration(id),
118+
generation: gen,
118119
})
119120
}
120121
return rwd

0 commit comments

Comments
 (0)