@@ -157,89 +157,78 @@ func (a *Applier) infoHelperFactory() info.InfoHelper {
157
157
return info .NewInfoHelper (a .factory , a .ApplyOptions .Namespace )
158
158
}
159
159
160
- // prepareObjects handles ordering of resources and sets up the inventory object
161
- // based on the provided inventory object template.
160
+ // prepareObjects merges the currently applied objects into the
161
+ // set of stored objects in the cluster inventory. In the process, it
162
+ // calculates the set of objects to be pruned (pruneIds), and orders the
163
+ // resources for the subsequent apply. Returns the sorted resources to
164
+ // apply as well as the objects for the prune, or an error if one occurred.
162
165
func (a * Applier ) prepareObjects (infos []* resource.Info ) (* ResourceObjects , error ) {
163
- resources , invs := splitInfos (infos )
164
-
165
- if len (invs ) == 0 {
166
- return nil , inventory.NoInventoryObjError {}
167
- }
168
- if len (invs ) > 1 {
169
- return nil , inventory.MultipleInventoryObjError {
170
- InventoryObjectTemplates : invs ,
171
- }
166
+ localInv , localInfos , err := inventory .SplitInfos (infos )
167
+ if err != nil {
168
+ return nil , err
172
169
}
173
-
174
- inv := a .InventoryFactoryFunc (invs [0 ])
175
- inventoryObject , err := inventory .CreateInventoryObj (inv , resources )
170
+ currentObjs , err := object .InfosToObjMetas (localInfos )
176
171
if err != nil {
177
172
return nil , err
178
173
}
179
-
180
- // Fetch all previous inventories.
181
- previousInventories , err := a .invClient .GetPreviousInventoryObjects (inventoryObject )
174
+ // returns the objects (pruneIds) to prune after apply. The prune
175
+ // algorithm requires stopping if the merge is not successful. Otherwise,
176
+ // the stored objects in inventory could become inconsistent.
177
+ pruneIds , err := a .invClient .Merge (localInv , currentObjs )
182
178
if err != nil {
183
179
return nil , err
184
180
}
185
-
186
- sort .Sort (ordering .SortableInfos (resources ))
187
-
188
- if ! validateNamespace (resources ) {
181
+ // Sort order for applied resources.
182
+ sort .Sort (ordering .SortableInfos (localInfos ))
183
+ // TODO(seans3): Remove this single namespace requirement once we've
184
+ // implemented multi-namespace apply.
185
+ if ! validateNamespace (localInfos ) {
189
186
return nil , fmt .Errorf ("objects have differing namespaces" )
190
187
}
191
-
192
188
return & ResourceObjects {
193
- CurrentInventory : inventoryObject ,
194
- PreviousInventories : previousInventories ,
195
- Resources : resources ,
189
+ LocalInv : localInv ,
190
+ Resources : localInfos ,
191
+ PruneIds : pruneIds ,
196
192
}, nil
197
193
}
198
194
199
195
// ResourceObjects contains information about the resources that
200
196
// will be applied and the existing inventories used to determine
201
197
// resources that should be pruned.
202
198
type ResourceObjects struct {
203
- CurrentInventory * resource.Info
204
- PreviousInventories []* resource.Info
205
- Resources [] * resource. Info
199
+ LocalInv * resource.Info
200
+ Resources []* resource.Info
201
+ PruneIds []object. ObjMetadata
206
202
}
207
203
208
204
// InfosForApply returns the infos representation for all the resources
209
205
// that should be applied, including the inventory object. The
210
206
// resources will be in sorted order.
211
207
func (r * ResourceObjects ) InfosForApply () []* resource.Info {
212
- return append ([]* resource.Info {r .CurrentInventory }, r .Resources ... )
208
+ return r .Resources
209
+ }
210
+
211
+ func (r * ResourceObjects ) InfosForPrune () []* resource.Info {
212
+ return append ([]* resource.Info {r .LocalInv }, r .Resources ... )
213
213
}
214
214
215
215
// IdsForApply returns the Ids for all resources that should be applied,
216
216
// including the inventory object.
217
217
func (r * ResourceObjects ) IdsForApply () []object.ObjMetadata {
218
218
var ids []object.ObjMetadata
219
219
for _ , info := range r .InfosForApply () {
220
- ids = append (ids , object .InfoToObjMeta (info ))
220
+ id , err := object .InfoToObjMeta (info )
221
+ if err == nil {
222
+ ids = append (ids , id )
223
+ }
221
224
}
222
225
return ids
223
226
}
224
227
225
228
// IdsForPrune returns the Ids for all resources that should
226
229
// be pruned.
227
230
func (r * ResourceObjects ) IdsForPrune () []object.ObjMetadata {
228
- inventory , _ := inventory .UnionPastObjs (r .PreviousInventories )
229
-
230
- applyIds := make (map [object.ObjMetadata ]bool )
231
- for _ , id := range r .IdsForApply () {
232
- applyIds [id ] = true
233
- }
234
-
235
- var ids []object.ObjMetadata
236
- for _ , id := range inventory {
237
- if _ , found := applyIds [id ]; found {
238
- continue
239
- }
240
- ids = append (ids , id )
241
- }
242
- return ids
231
+ return r .PruneIds
243
232
}
244
233
245
234
// AllIds returns the Ids for all resources that are relevant. This
@@ -248,23 +237,6 @@ func (r *ResourceObjects) AllIds() []object.ObjMetadata {
248
237
return append (r .IdsForApply (), r .IdsForPrune ()... )
249
238
}
250
239
251
- // splitInfos takes a slice of resource.Info objects and splits it
252
- // into one slice that contains the inventory object templates and
253
- // another one that contains the remaining resources.
254
- func splitInfos (infos []* resource.Info ) ([]* resource.Info , []* resource.Info ) {
255
- inventoryObjectTemplates := make ([]* resource.Info , 0 )
256
- resources := make ([]* resource.Info , 0 )
257
-
258
- for _ , info := range infos {
259
- if inventory .IsInventoryObject (info .Object ) {
260
- inventoryObjectTemplates = append (inventoryObjectTemplates , info )
261
- } else {
262
- resources = append (resources , info )
263
- }
264
- }
265
- return resources , inventoryObjectTemplates
266
- }
267
-
268
240
// Run performs the Apply step. This happens asynchronously with updates
269
241
// on progress and any errors are reported back on the event channel.
270
242
// Cancelling the operation or setting timeout on how long to Wait
@@ -276,6 +248,7 @@ func splitInfos(infos []*resource.Info) ([]*resource.Info, []*resource.Info) {
276
248
func (a * Applier ) Run (ctx context.Context , objects []* resource.Info , options Options ) <- chan event.Event {
277
249
eventChannel := make (chan event.Event )
278
250
setDefaults (& options )
251
+ a .invClient .SetDryRun (options .DryRun ) // client shared with prune, so sets dry-run for prune too.
279
252
280
253
go func () {
281
254
defer close (eventChannel )
0 commit comments