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,16 @@ 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
+ id := inventory2.ID {
96
+ Name : i .InvInfo .Name (),
97
+ Namespace : i .InvInfo .Namespace (),
98
+ }
99
+ inv , err := i .InvClient .Get (context .TODO (), id )
100
+ if err != nil {
101
+ return fmt .Errorf ("getting inventory: %w" )
102
+ }
103
+ prevObjs := inventory .ObjMetadataSetFromObjectReferenceList (inv .Spec .Objects ).Unique ()
104
+
88
105
invObjs := object.ObjMetadataSet {}
89
106
90
107
// TODO: Just use InventoryManager.Store()
@@ -100,7 +117,7 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
100
117
// This will remove new resources that failed to apply from the inventory,
101
118
// because even tho they were added by InvAddTask, the PrevInventory
102
119
// represents the inventory before the pipeline has run.
103
- applyFailures := i . PrevInventory .Intersection (im .FailedApplies ())
120
+ applyFailures := prevObjs .Intersection (im .FailedApplies ())
104
121
klog .V (4 ).Infof ("keep in inventory %d failed applies" , len (applyFailures ))
105
122
invObjs = invObjs .Union (applyFailures )
106
123
@@ -109,7 +126,7 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
109
126
// It's likely that all the skipped applies are already in the inventory,
110
127
// because the apply filters all currently depend on cluster state,
111
128
// but we're doing the intersection anyway just to be sure.
112
- applySkips := i . PrevInventory .Intersection (im .SkippedApplies ())
129
+ applySkips := prevObjs .Intersection (im .SkippedApplies ())
113
130
klog .V (4 ).Infof ("keep in inventory %d skipped applies" , len (applySkips ))
114
131
invObjs = invObjs .Union (applySkips )
115
132
@@ -118,7 +135,7 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
118
135
// It's likely that all the delete failures are already in the inventory,
119
136
// because the set of resources to prune comes from the inventory,
120
137
// but we're doing the intersection anyway just to be sure.
121
- pruneFailures := i . PrevInventory .Intersection (im .FailedDeletes ())
138
+ pruneFailures := prevObjs .Intersection (im .FailedDeletes ())
122
139
klog .V (4 ).Infof ("set inventory %d failed prunes" , len (pruneFailures ))
123
140
invObjs = invObjs .Union (pruneFailures )
124
141
@@ -127,19 +144,19 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
127
144
// It's likely that all the skipped deletes are already in the inventory,
128
145
// because the set of resources to prune comes from the inventory,
129
146
// but we're doing the intersection anyway just to be sure.
130
- pruneSkips := i . PrevInventory .Intersection (im .SkippedDeletes ())
147
+ pruneSkips := prevObjs .Intersection (im .SkippedDeletes ())
131
148
klog .V (4 ).Infof ("keep in inventory %d skipped prunes" , len (pruneSkips ))
132
149
invObjs = invObjs .Union (pruneSkips )
133
150
134
151
// If an object failed to reconcile and was previously stored in the inventory,
135
152
// then keep it in the inventory so it can be waited on next time.
136
- reconcileFailures := i . PrevInventory .Intersection (im .FailedReconciles ())
153
+ reconcileFailures := prevObjs .Intersection (im .FailedReconciles ())
137
154
klog .V (4 ).Infof ("set inventory %d failed reconciles" , len (reconcileFailures ))
138
155
invObjs = invObjs .Union (reconcileFailures )
139
156
140
157
// If an object timed out reconciling and was previously stored in the inventory,
141
158
// then keep it in the inventory so it can be waited on next time.
142
- reconcileTimeouts := i . PrevInventory .Intersection (im .TimeoutReconciles ())
159
+ reconcileTimeouts := prevObjs .Intersection (im .TimeoutReconciles ())
143
160
klog .V (4 ).Infof ("keep in inventory %d timeout reconciles" , len (reconcileTimeouts ))
144
161
invObjs = invObjs .Union (reconcileTimeouts )
145
162
@@ -150,24 +167,44 @@ func (i *DeleteOrUpdateInvTask) updateInventory(taskContext *taskrunner.TaskCont
150
167
151
168
// If an object is invalid and was previously stored in the inventory,
152
169
// then keep it in the inventory so it can be applied/pruned next time.
153
- invalidObjects := i . PrevInventory .Intersection (taskContext .InvalidObjects ())
170
+ invalidObjects := prevObjs .Intersection (taskContext .InvalidObjects ())
154
171
klog .V (4 ).Infof ("keep in inventory %d invalid objects" , len (invalidObjects ))
155
172
invObjs = invObjs .Union (invalidObjects )
156
173
174
+ // Update inventory spec in memory
175
+ inv .Spec .Objects = inventory .ObjectReferenceListFromObjMetadataSet (invObjs )
176
+
157
177
klog .V (4 ).Infof ("get the apply status for %d objects" , len (invObjs ))
158
- objStatus : = taskContext .InventoryManager ().Inventory ().Status .Objects
178
+ inv . Status . Objects = taskContext .InventoryManager ().Inventory ().Status .Objects
159
179
160
180
klog .V (4 ).Infof ("set inventory %d total objects" , len (invObjs ))
161
- err := i .InvClient .Replace (i .InvInfo , invObjs , objStatus , i .DryRun )
181
+ if err = i .InvClient .Update (context .TODO (), inv , i .updateOptionList ()... ); err != nil {
182
+ return fmt .Errorf ("updating inventory: %w" )
183
+ }
184
+ return nil
185
+ }
162
186
163
- klog .V (2 ).Infof ("inventory set task completing (name: %q)" , i .TaskName )
164
- return err
187
+ func (i * DeleteOrUpdateInvTask ) updateOptionList () []inventory2.UpdateOption {
188
+ return []inventory2.UpdateOption {
189
+ inventory2 .WithDryRun (i .DryRun ),
190
+ inventory2 .WithStatus (i .StatusPolicy ),
191
+ }
192
+ }
193
+
194
+ func (i * DeleteOrUpdateInvTask ) deleteOptionList () []inventory2.DeleteOption {
195
+ return []inventory2.DeleteOption {
196
+ inventory2 .WithDryRun (i .DryRun ),
197
+ inventory2 .WithStatus (i .StatusPolicy ),
198
+ }
165
199
}
166
200
167
201
// deleteInventory deletes the inventory object from the cluster.
168
202
func (i * DeleteOrUpdateInvTask ) deleteInventory () error {
169
203
klog .V (2 ).Infof ("delete inventory task starting (name: %q)" , i .Name ())
170
- err := i .InvClient .DeleteInventoryObj (i .InvInfo , i .DryRun )
204
+ inv := & actuation.Inventory {}
205
+ inv .SetName (i .InvInfo .Name ())
206
+ inv .SetName (i .InvInfo .Namespace ())
207
+ err := i .InvClient .Delete (context .TODO (), inv , i .deleteOptionList ()... )
171
208
// Not found is not error, since this means it was already deleted.
172
209
if apierrors .IsNotFound (err ) {
173
210
err = nil
0 commit comments