4
4
package task
5
5
6
6
import (
7
+ "context"
8
+ "fmt"
9
+
7
10
apierrors "k8s.io/apimachinery/pkg/api/errors"
8
11
"k8s.io/klog/v2"
12
+ "sigs.k8s.io/cli-utils/pkg/apis/actuation"
9
13
"sigs.k8s.io/cli-utils/pkg/apply/event"
10
14
"sigs.k8s.io/cli-utils/pkg/apply/taskrunner"
11
15
"sigs.k8s.io/cli-utils/pkg/common"
12
16
"sigs.k8s.io/cli-utils/pkg/inventory"
17
+ "sigs.k8s.io/cli-utils/pkg/inventory2"
13
18
"sigs.k8s.io/cli-utils/pkg/object"
14
19
)
15
20
16
21
// DeleteOrUpdateInvTask encapsulates structures necessary to set the
17
22
// inventory references at the end of the apply/prune.
18
23
type DeleteOrUpdateInvTask struct {
19
24
TaskName string
20
- InvClient inventory .Client
25
+ InvClient inventory2 .Client
21
26
InvInfo inventory.Info
22
27
PrevInventory object.ObjMetadataSet
23
28
DryRun common.DryRunStrategy
29
+ StatusPolicy inventory.StatusPolicy
24
30
// if Destroy is set, the inventory will be deleted if all objects were successfully pruned
25
31
Destroy bool
26
32
}
@@ -47,12 +53,14 @@ func (i *DeleteOrUpdateInvTask) Identifiers() object.ObjMetadataSet {
47
53
// If Destroy is false, the inventory will be updated.
48
54
func (i * DeleteOrUpdateInvTask ) Start (taskContext * taskrunner.TaskContext ) {
49
55
go func () {
56
+ klog .V (2 ).Infof ("inventory set task starting (name: %q)" , i .TaskName )
50
57
var err error
51
58
if i .Destroy && i .destroySuccessful (taskContext ) {
52
59
err = i .deleteInventory ()
53
60
} else {
54
61
err = i .updateInventory (taskContext )
55
62
}
63
+ klog .V (2 ).Infof ("inventory set task completing (name: %q)" , i .TaskName )
56
64
taskContext .TaskChannel () <- taskrunner.TaskResult {Err : err }
57
65
}()
58
66
}
@@ -84,7 +92,12 @@ func (i *DeleteOrUpdateInvTask) StatusUpdate(_ *taskrunner.TaskContext, _ object
84
92
// - Deleted resources (successful)
85
93
// - Abandoned resources (successful)
86
94
func (i * DeleteOrUpdateInvTask ) updateInventory (taskContext * taskrunner.TaskContext ) error {
87
- klog .V (2 ).Infof ("inventory set task starting (name: %q)" , i .TaskName )
95
+ inv , err := i .InvClient .Get (context .TODO ())
96
+ if err != nil {
97
+ return fmt .Errorf ("getting inventory: %w" )
98
+ }
99
+ prevObjs := inventory .ObjMetadataSetFromObjectReferenceList (inv .Spec .Objects ).Unique ()
100
+
88
101
invObjs := object.ObjMetadataSet {}
89
102
90
103
// TODO: Just use InventoryManager.Store()
@@ -100,7 +113,7 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
100
113
// This will remove new resources that failed to apply from the inventory,
101
114
// because even tho they were added by InvAddTask, the PrevInventory
102
115
// represents the inventory before the pipeline has run.
103
- applyFailures := i . PrevInventory .Intersection (im .FailedApplies ())
116
+ applyFailures := prevObjs .Intersection (im .FailedApplies ())
104
117
klog .V (4 ).Infof ("keep in inventory %d failed applies" , len (applyFailures ))
105
118
invObjs = invObjs .Union (applyFailures )
106
119
@@ -109,7 +122,7 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
109
122
// It's likely that all the skipped applies are already in the inventory,
110
123
// because the apply filters all currently depend on cluster state,
111
124
// but we're doing the intersection anyway just to be sure.
112
- applySkips := i . PrevInventory .Intersection (im .SkippedApplies ())
125
+ applySkips := prevObjs .Intersection (im .SkippedApplies ())
113
126
klog .V (4 ).Infof ("keep in inventory %d skipped applies" , len (applySkips ))
114
127
invObjs = invObjs .Union (applySkips )
115
128
@@ -118,7 +131,7 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
118
131
// It's likely that all the delete failures are already in the inventory,
119
132
// because the set of resources to prune comes from the inventory,
120
133
// but we're doing the intersection anyway just to be sure.
121
- pruneFailures := i . PrevInventory .Intersection (im .FailedDeletes ())
134
+ pruneFailures := prevObjs .Intersection (im .FailedDeletes ())
122
135
klog .V (4 ).Infof ("set inventory %d failed prunes" , len (pruneFailures ))
123
136
invObjs = invObjs .Union (pruneFailures )
124
137
@@ -127,19 +140,19 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
127
140
// It's likely that all the skipped deletes are already in the inventory,
128
141
// because the set of resources to prune comes from the inventory,
129
142
// but we're doing the intersection anyway just to be sure.
130
- pruneSkips := i . PrevInventory .Intersection (im .SkippedDeletes ())
143
+ pruneSkips := prevObjs .Intersection (im .SkippedDeletes ())
131
144
klog .V (4 ).Infof ("keep in inventory %d skipped prunes" , len (pruneSkips ))
132
145
invObjs = invObjs .Union (pruneSkips )
133
146
134
147
// If an object failed to reconcile and was previously stored in the inventory,
135
148
// then keep it in the inventory so it can be waited on next time.
136
- reconcileFailures := i . PrevInventory .Intersection (im .FailedReconciles ())
149
+ reconcileFailures := prevObjs .Intersection (im .FailedReconciles ())
137
150
klog .V (4 ).Infof ("set inventory %d failed reconciles" , len (reconcileFailures ))
138
151
invObjs = invObjs .Union (reconcileFailures )
139
152
140
153
// If an object timed out reconciling and was previously stored in the inventory,
141
154
// then keep it in the inventory so it can be waited on next time.
142
- reconcileTimeouts := i . PrevInventory .Intersection (im .TimeoutReconciles ())
155
+ reconcileTimeouts := prevObjs .Intersection (im .TimeoutReconciles ())
143
156
klog .V (4 ).Infof ("keep in inventory %d timeout reconciles" , len (reconcileTimeouts ))
144
157
invObjs = invObjs .Union (reconcileTimeouts )
145
158
@@ -150,24 +163,44 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
150
163
151
164
// If an object is invalid and was previously stored in the inventory,
152
165
// then keep it in the inventory so it can be applied/pruned next time.
153
- invalidObjects := i . PrevInventory .Intersection (taskContext .InvalidObjects ())
166
+ invalidObjects := prevObjs .Intersection (taskContext .InvalidObjects ())
154
167
klog .V (4 ).Infof ("keep in inventory %d invalid objects" , len (invalidObjects ))
155
168
invObjs = invObjs .Union (invalidObjects )
156
169
170
+ // Update inventory spec in memory
171
+ inv .Spec .Objects = inventory .ObjectReferenceListFromObjMetadataSet (invObjs )
172
+
157
173
klog .V (4 ).Infof ("get the apply status for %d objects" , len (invObjs ))
158
- objStatus : = taskContext .InventoryManager ().Inventory ().Status .Objects
174
+ inv . Status . Objects = taskContext .InventoryManager ().Inventory ().Status .Objects
159
175
160
176
klog .V (4 ).Infof ("set inventory %d total objects" , len (invObjs ))
161
- err := i .InvClient .Replace (i .InvInfo , invObjs , objStatus , i .DryRun )
177
+ if err = i .InvClient .Update (context .TODO (), inv , i .updateOptionList ()... ); err != nil {
178
+ return fmt .Errorf ("updating inventory: %w" )
179
+ }
180
+ return nil
181
+ }
162
182
163
- klog .V (2 ).Infof ("inventory set task completing (name: %q)" , i .TaskName )
164
- return err
183
+ func (i * DeleteOrUpdateInvTask ) updateOptionList () []inventory2.UpdateOption {
184
+ return []inventory2.UpdateOption {
185
+ inventory2 .WithDryRun (i .DryRun ),
186
+ inventory2 .WithStatus (i .StatusPolicy ),
187
+ }
188
+ }
189
+
190
+ func (i * DeleteOrUpdateInvTask ) deleteOptionList () []inventory2.DeleteOption {
191
+ return []inventory2.DeleteOption {
192
+ inventory2 .WithDryRun (i .DryRun ),
193
+ inventory2 .WithStatus (i .StatusPolicy ),
194
+ }
165
195
}
166
196
167
197
// deleteInventory deletes the inventory object from the cluster.
168
198
func (i * DeleteOrUpdateInvTask ) deleteInventory () error {
169
199
klog .V (2 ).Infof ("delete inventory task starting (name: %q)" , i .Name ())
170
- err := i .InvClient .DeleteInventoryObj (i .InvInfo , i .DryRun )
200
+ inv := & actuation.Inventory {}
201
+ inv .SetName (i .InvInfo .Name ())
202
+ inv .SetName (i .InvInfo .Namespace ())
203
+ err := i .InvClient .Delete (context .TODO (), inv , i .deleteOptionList ()... )
171
204
// Not found is not error, since this means it was already deleted.
172
205
if apierrors .IsNotFound (err ) {
173
206
err = nil
0 commit comments