...
1
2
3
4 package task
5
6 import (
7 "k8s.io/klog/v2"
8 "sigs.k8s.io/cli-utils/pkg/apply/event"
9 "sigs.k8s.io/cli-utils/pkg/apply/taskrunner"
10 "sigs.k8s.io/cli-utils/pkg/common"
11 "sigs.k8s.io/cli-utils/pkg/inventory"
12 "sigs.k8s.io/cli-utils/pkg/object"
13 )
14
15
16
17 type InvSetTask struct {
18 TaskName string
19 InvClient inventory.Client
20 InvInfo inventory.Info
21 PrevInventory object.ObjMetadataSet
22 DryRun common.DryRunStrategy
23 }
24
25 func (i *InvSetTask) Name() string {
26 return i.TaskName
27 }
28
29 func (i *InvSetTask) Action() event.ResourceAction {
30 return event.InventoryAction
31 }
32
33 func (i *InvSetTask) Identifiers() object.ObjMetadataSet {
34 return object.ObjMetadataSet{}
35 }
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 func (i *InvSetTask) Start(taskContext *taskrunner.TaskContext) {
58 go func() {
59 klog.V(2).Infof("inventory set task starting (name: %q)", i.Name())
60 invObjs := object.ObjMetadataSet{}
61
62
63 im := taskContext.InventoryManager()
64
65
66 appliedObjs := im.SuccessfulApplies()
67 klog.V(4).Infof("set inventory %d successful applies", len(appliedObjs))
68 invObjs = invObjs.Union(appliedObjs)
69
70
71
72
73
74
75 applyFailures := i.PrevInventory.Intersection(im.FailedApplies())
76 klog.V(4).Infof("keep in inventory %d failed applies", len(applyFailures))
77 invObjs = invObjs.Union(applyFailures)
78
79
80
81
82
83
84 applySkips := i.PrevInventory.Intersection(im.SkippedApplies())
85 klog.V(4).Infof("keep in inventory %d skipped applies", len(applySkips))
86 invObjs = invObjs.Union(applySkips)
87
88
89
90
91
92
93 pruneFailures := i.PrevInventory.Intersection(im.FailedDeletes())
94 klog.V(4).Infof("set inventory %d failed prunes", len(pruneFailures))
95 invObjs = invObjs.Union(pruneFailures)
96
97
98
99
100
101
102 pruneSkips := i.PrevInventory.Intersection(im.SkippedDeletes())
103 klog.V(4).Infof("keep in inventory %d skipped prunes", len(pruneSkips))
104 invObjs = invObjs.Union(pruneSkips)
105
106
107 abandonedObjects := taskContext.AbandonedObjects()
108 klog.V(4).Infof("remove from inventory %d abandoned objects", len(abandonedObjects))
109 invObjs = invObjs.Diff(abandonedObjects)
110
111
112
113 invalidObjects := i.PrevInventory.Intersection(taskContext.InvalidObjects())
114 klog.V(4).Infof("keep in inventory %d invalid objects", len(invalidObjects))
115 invObjs = invObjs.Union(invalidObjects)
116
117 klog.V(4).Infof("get the apply status for %d objects", len(invObjs))
118 objStatus := taskContext.InventoryManager().Inventory().Status.Objects
119
120 klog.V(4).Infof("set inventory %d total objects", len(invObjs))
121 err := i.InvClient.Replace(i.InvInfo, invObjs, objStatus, i.DryRun)
122
123 klog.V(2).Infof("inventory set task completing (name: %q)", i.Name())
124 taskContext.TaskChannel() <- taskrunner.TaskResult{Err: err}
125 }()
126 }
127
128
129 func (i *InvSetTask) Cancel(_ *taskrunner.TaskContext) {}
130
131
132 func (i *InvSetTask) StatusUpdate(_ *taskrunner.TaskContext, _ object.ObjMetadata) {}
133
View as plain text