@@ -16,7 +16,6 @@ import (
16
16
"k8s.io/client-go/dynamic"
17
17
"k8s.io/klog/v2"
18
18
cmdutil "k8s.io/kubectl/pkg/cmd/util"
19
- "k8s.io/kubectl/pkg/util"
20
19
"sigs.k8s.io/cli-utils/pkg/apis/actuation"
21
20
"sigs.k8s.io/cli-utils/pkg/common"
22
21
"sigs.k8s.io/cli-utils/pkg/object"
@@ -39,12 +38,6 @@ type Client interface {
39
38
Replace (inv Info , objs object.ObjMetadataSet , status []actuation.ObjectStatus , dryRun common.DryRunStrategy ) error
40
39
// DeleteInventoryObj deletes the passed inventory object from the APIServer.
41
40
DeleteInventoryObj (inv Info , dryRun common.DryRunStrategy ) error
42
- // ApplyInventoryNamespace applies the Namespace that the inventory object should be in.
43
- ApplyInventoryNamespace (invNamespace * unstructured.Unstructured , dryRun common.DryRunStrategy ) error
44
- // GetClusterInventoryInfo returns the cluster inventory object.
45
- GetClusterInventoryInfo (inv Info ) (* unstructured.Unstructured , error )
46
- // GetClusterInventoryObjs looks up the inventory objects from the cluster.
47
- GetClusterInventoryObjs (inv Info ) (object.UnstructuredSet , error )
48
41
// ListClusterInventoryObjs returns a map mapping from inventory name to a list of cluster inventory objects
49
42
ListClusterInventoryObjs (ctx context.Context ) (map [string ]object.ObjMetadataSet , error )
50
43
}
@@ -105,7 +98,7 @@ func NewClient(factory cmdutil.Factory,
105
98
func (cic * ClusterClient ) Merge (localInv Info , objs object.ObjMetadataSet , dryRun common.DryRunStrategy ) (object.ObjMetadataSet , error ) {
106
99
pruneIds := object.ObjMetadataSet {}
107
100
invObj := cic .invToUnstructuredFunc (localInv )
108
- clusterInv , err := cic .GetClusterInventoryInfo (localInv )
101
+ clusterInv , err := cic .getClusterInventoryInfo (localInv )
109
102
if err != nil {
110
103
return pruneIds , err
111
104
}
@@ -174,7 +167,7 @@ func (cic *ClusterClient) Replace(localInv Info, objs object.ObjMetadataSet, sta
174
167
klog .V (4 ).Infoln ("dry-run replace inventory object: not applied" )
175
168
return nil
176
169
}
177
- clusterInv , err := cic .GetClusterInventoryInfo (localInv )
170
+ clusterInv , err := cic .getClusterInventoryInfo (localInv )
178
171
if err != nil {
179
172
return fmt .Errorf ("failed to read inventory from cluster: %w" , err )
180
173
}
@@ -256,7 +249,7 @@ func (cic *ClusterClient) deleteInventoryObjsByLabel(inv Info, dryRun common.Dry
256
249
// an error if one occurred.
257
250
func (cic * ClusterClient ) GetClusterObjs (localInv Info ) (object.ObjMetadataSet , error ) {
258
251
var objs object.ObjMetadataSet
259
- clusterInv , err := cic .GetClusterInventoryInfo (localInv )
252
+ clusterInv , err := cic .getClusterInventoryInfo (localInv )
260
253
if err != nil {
261
254
return objs , fmt .Errorf ("failed to read inventory from cluster: %w" , err )
262
255
}
@@ -277,8 +270,8 @@ func (cic *ClusterClient) GetClusterObjs(localInv Info) (object.ObjMetadataSet,
277
270
//
278
271
// TODO(seans3): Remove the special case code to merge multiple cluster inventory
279
272
// objects once we've determined that this case is no longer possible.
280
- func (cic * ClusterClient ) GetClusterInventoryInfo (inv Info ) (* unstructured.Unstructured , error ) {
281
- clusterInvObjects , err := cic .GetClusterInventoryObjs (inv )
273
+ func (cic * ClusterClient ) getClusterInventoryInfo (inv Info ) (* unstructured.Unstructured , error ) {
274
+ clusterInvObjects , err := cic .getClusterInventoryObjs (inv )
282
275
if err != nil {
283
276
return nil , fmt .Errorf ("failed to read inventory objects from cluster: %w" , err )
284
277
}
@@ -344,10 +337,18 @@ func (cic *ClusterClient) getClusterInventoryObjsByName(inv Info) (object.Unstru
344
337
if apierrors .IsNotFound (err ) {
345
338
return object.UnstructuredSet {}, nil
346
339
}
340
+ if inv .ID () != "" {
341
+ if inventoryID , err := retrieveInventoryLabel (clusterInv ); err != nil {
342
+ return nil , err
343
+ } else if inv .ID () != inventoryID {
344
+ return nil , fmt .Errorf ("inventory-id of inventory object %s/%s in cluster doesn't match provided id %q" ,
345
+ inv .Namespace (), inv .Name (), inv .ID ())
346
+ }
347
+ }
347
348
return object.UnstructuredSet {clusterInv }, nil
348
349
}
349
350
350
- func (cic * ClusterClient ) GetClusterInventoryObjs (inv Info ) (object.UnstructuredSet , error ) {
351
+ func (cic * ClusterClient ) getClusterInventoryObjs (inv Info ) (object.UnstructuredSet , error ) {
351
352
if inv == nil {
352
353
return nil , fmt .Errorf ("inventoryInfo must be specified" )
353
354
}
@@ -443,33 +444,6 @@ func (cic *ClusterClient) deleteInventoryObjByName(obj *unstructured.Unstructure
443
444
Delete (context .TODO (), obj .GetName (), metav1.DeleteOptions {})
444
445
}
445
446
446
- // ApplyInventoryNamespace creates the passed namespace if it does not already
447
- // exist, or returns an error if one happened. NOTE: No error if already exists.
448
- func (cic * ClusterClient ) ApplyInventoryNamespace (obj * unstructured.Unstructured , dryRun common.DryRunStrategy ) error {
449
- if dryRun .ClientOrServerDryRun () {
450
- klog .V (4 ).Infof ("dry-run apply inventory namespace (%s): not applied" , obj .GetName ())
451
- return nil
452
- }
453
-
454
- invNamespace := obj .DeepCopy ()
455
- klog .V (4 ).Infof ("applying inventory namespace: %s" , obj .GetName ())
456
- object .StripKyamlAnnotations (invNamespace )
457
- if err := util .CreateApplyAnnotation (invNamespace , unstructured .UnstructuredJSONScheme ); err != nil {
458
- return err
459
- }
460
-
461
- mapping , err := cic .getMapping (obj )
462
- if err != nil {
463
- return err
464
- }
465
-
466
- _ , err = cic .dc .Resource (mapping .Resource ).Create (context .TODO (), invNamespace , metav1.CreateOptions {})
467
- if apierrors .IsAlreadyExists (err ) {
468
- return nil
469
- }
470
- return err
471
- }
472
-
473
447
// getMapping returns the RESTMapping for the provided resource.
474
448
func (cic * ClusterClient ) getMapping (obj * unstructured.Unstructured ) (* meta.RESTMapping , error ) {
475
449
return cic .mapper .RESTMapping (obj .GroupVersionKind ().GroupKind (), obj .GroupVersionKind ().Version )
0 commit comments