@@ -46,6 +46,8 @@ type InventoryClient interface {
46
46
GetClusterInventoryInfo (inv InventoryInfo ) (* unstructured.Unstructured , error )
47
47
// UpdateLabels updates the labels of the cluster inventory object if it exists.
48
48
UpdateLabels (InventoryInfo , map [string ]string ) error
49
+ // GetInventoryObjs looks up the inventory objects from the cluster.
50
+ GetClusterInventoryObjs (inv InventoryInfo ) ([]* unstructured.Unstructured , error )
49
51
}
50
52
51
53
// ClusterInventoryClient is a concrete implementation of the
@@ -199,7 +201,30 @@ func (cic *ClusterInventoryClient) replaceInventory(inv *unstructured.Unstructur
199
201
200
202
// DeleteInventoryObj deletes the inventory object from the cluster.
201
203
func (cic * ClusterInventoryClient ) DeleteInventoryObj (localInv InventoryInfo ) error {
202
- return cic .deleteInventoryObj (cic .invToUnstructuredFunc (localInv ))
204
+ if localInv == nil {
205
+ return fmt .Errorf ("retrieving cluster inventory object with nil local inventory" )
206
+ }
207
+ switch localInv .Strategy () {
208
+ case NameStrategy :
209
+ return cic .deleteInventoryObjByName (cic .invToUnstructuredFunc (localInv ))
210
+ case LabelStrategy :
211
+ return cic .deleteInventoryObjsByLabel (localInv )
212
+ default :
213
+ panic (fmt .Errorf ("unknown inventory strategy: %s" , localInv .Strategy ()))
214
+ }
215
+ }
216
+
217
+ func (cic * ClusterInventoryClient ) deleteInventoryObjsByLabel (inv InventoryInfo ) error {
218
+ clusterInvObjs , err := cic .getClusterInventoryObjsByLabel (inv )
219
+ if err != nil {
220
+ return err
221
+ }
222
+ for _ , invObj := range clusterInvObjs {
223
+ if err := cic .deleteInventoryObjByName (invObj ); err != nil {
224
+ return err
225
+ }
226
+ }
227
+ return nil
203
228
}
204
229
205
230
// GetClusterObjs returns the objects stored in the cluster inventory object, or
@@ -228,6 +253,24 @@ func (cic *ClusterInventoryClient) GetClusterObjs(localInv InventoryInfo) ([]obj
228
253
// TODO(seans3): Remove the special case code to merge multiple cluster inventory
229
254
// objects once we've determined that this case is no longer possible.
230
255
func (cic * ClusterInventoryClient ) GetClusterInventoryInfo (inv InventoryInfo ) (* unstructured.Unstructured , error ) {
256
+ clusterInvObjects , err := cic .GetClusterInventoryObjs (inv )
257
+ if err != nil {
258
+ return nil , err
259
+ }
260
+
261
+ var clusterInv * unstructured.Unstructured
262
+ if len (clusterInvObjects ) == 1 {
263
+ clusterInv = clusterInvObjects [0 ]
264
+ } else if len (clusterInvObjects ) > 1 {
265
+ clusterInv , err = cic .mergeClusterInventory (clusterInvObjects )
266
+ if err != nil {
267
+ return nil , err
268
+ }
269
+ }
270
+ return clusterInv , nil
271
+ }
272
+
273
+ func (cic * ClusterInventoryClient ) getClusterInventoryObjsByLabel (inv InventoryInfo ) ([]* unstructured.Unstructured , error ) {
231
274
localInv := cic .invToUnstructuredFunc (inv )
232
275
if localInv == nil {
233
276
return nil , fmt .Errorf ("retrieving cluster inventory object with nil local inventory" )
@@ -260,16 +303,37 @@ func (cic *ClusterInventoryClient) GetClusterInventoryInfo(inv InventoryInfo) (*
260
303
if err != nil {
261
304
return nil , err
262
305
}
263
- var clusterInv * unstructured.Unstructured
264
- if len (retrievedInventoryInfos ) == 1 {
265
- clusterInv = object .InfoToUnstructured (retrievedInventoryInfos [0 ])
266
- } else if len (retrievedInventoryInfos ) > 1 {
267
- clusterInv , err = cic .mergeClusterInventory (object .InfosToUnstructureds (retrievedInventoryInfos ))
268
- if err != nil {
269
- return nil , err
270
- }
306
+ return object .InfosToUnstructureds (retrievedInventoryInfos ), nil
307
+ }
308
+
309
+ func (cic * ClusterInventoryClient ) getClusterInventoryObjsByName (inv InventoryInfo ) ([]* unstructured.Unstructured , error ) {
310
+ localInv := cic .invToUnstructuredFunc (inv )
311
+ if localInv == nil {
312
+ return nil , fmt .Errorf ("retrieving cluster inventory object with nil local inventory" )
271
313
}
272
- return clusterInv , nil
314
+
315
+ invInfo , err := cic .toInfo (localInv )
316
+ if err != nil {
317
+ return nil , err
318
+ }
319
+
320
+ helper , err := cic .helperFromInfo (invInfo )
321
+ if err != nil {
322
+ return nil , err
323
+ }
324
+
325
+ res , err := helper .Get (inv .Namespace (), inv .Name ())
326
+ if err != nil && ! apierrors .IsNotFound (err ) {
327
+ return nil , err
328
+ }
329
+ if apierrors .IsNotFound (err ) {
330
+ return []* unstructured.Unstructured {}, nil
331
+ }
332
+ clusterInv , ok := res .(* unstructured.Unstructured )
333
+ if ! ok {
334
+ return nil , fmt .Errorf ("retrieved cluster inventory object is not of type *Unstructured" )
335
+ }
336
+ return []* unstructured.Unstructured {clusterInv }, nil
273
337
}
274
338
275
339
func (cic * ClusterInventoryClient ) UpdateLabels (inv InventoryInfo , labels map [string ]string ) error {
@@ -284,6 +348,24 @@ func (cic *ClusterInventoryClient) UpdateLabels(inv InventoryInfo, labels map[st
284
348
return cic .applyInventoryObj (obj )
285
349
}
286
350
351
+ func (cic * ClusterInventoryClient ) GetClusterInventoryObjs (inv InventoryInfo ) ([]* unstructured.Unstructured , error ) {
352
+ if inv == nil {
353
+ return nil , fmt .Errorf ("inventoryInfo must be specified" )
354
+ }
355
+
356
+ var clusterInvObjects []* unstructured.Unstructured
357
+ var err error
358
+ switch inv .Strategy () {
359
+ case NameStrategy :
360
+ clusterInvObjects , err = cic .getClusterInventoryObjsByName (inv )
361
+ case LabelStrategy :
362
+ clusterInvObjects , err = cic .getClusterInventoryObjsByLabel (inv )
363
+ default :
364
+ panic (fmt .Errorf ("unknown inventory strategy: %s" , inv .Strategy ()))
365
+ }
366
+ return clusterInvObjects , err
367
+ }
368
+
287
369
// mergeClusterInventory merges the inventory of multiple inventory objects
288
370
// into one inventory object, and applies it. Deletes the remaining unnecessary
289
371
// inventory objects. There should be only one inventory object stored in the
@@ -335,7 +417,7 @@ func (cic *ClusterInventoryClient) mergeClusterInventory(invObjs []*unstructured
335
417
// Finally, delete the other inventory objects.
336
418
for i := 1 ; i < len (invObjs ); i ++ {
337
419
merge := invObjs [i ]
338
- if err := cic .deleteInventoryObj (merge ); err != nil {
420
+ if err := cic .deleteInventoryObjByName (merge ); err != nil {
339
421
return nil , err
340
422
}
341
423
}
@@ -399,9 +481,9 @@ func (cic *ClusterInventoryClient) createInventoryObj(obj *unstructured.Unstruct
399
481
return invInfo .Refresh (createdObj , ignoreError )
400
482
}
401
483
402
- // deleteInventoryObj deletes the passed inventory object from the APIServer, or
484
+ // deleteInventoryObjByName deletes the passed inventory object from the APIServer, or
403
485
// an error if one occurs.
404
- func (cic * ClusterInventoryClient ) deleteInventoryObj (obj * unstructured.Unstructured ) error {
486
+ func (cic * ClusterInventoryClient ) deleteInventoryObjByName (obj * unstructured.Unstructured ) error {
405
487
if cic .dryRunStrategy .ClientOrServerDryRun () {
406
488
klog .V (4 ).Infof ("dry-run delete inventory object: not deleted" )
407
489
return nil
0 commit comments