Skip to content

Commit 24b7916

Browse files
committed
Change inventory package to use Unstructured instead of Info
1 parent 91bebf1 commit 24b7916

File tree

13 files changed

+196
-308
lines changed

13 files changed

+196
-308
lines changed

cmd/destroy/cmddestroy.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,14 @@ func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
6262
if err != nil {
6363
return err
6464
}
65-
inv, _, err := inventory.SplitInfos(infos)
65+
inv, _, err := inventory.SplitUnstructureds(object.InfosToUnstructureds(infos))
6666
if err != nil {
6767
return err
6868
}
6969

7070
// Run the destroyer. It will return a channel where we can receive updates
7171
// to keep track of progress and any issues.
72-
ch := r.Destroyer.Run(object.InfoToUnstructured(inv))
72+
ch := r.Destroyer.Run(inv)
7373

7474
// The printer will print updates from the channel. It will block
7575
// until the channel is closed.

cmd/preview/cmdpreview.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
119119
DryRunStrategy: drs,
120120
})
121121
} else {
122-
inv, _, err := inventory.SplitInfos(infos)
122+
inv, _, err := inventory.SplitUnstructureds(object.InfosToUnstructureds(infos))
123123
if err != nil {
124124
return err
125125
}
126-
ch = r.Destroyer.Run(object.InfoToUnstructured(inv))
126+
ch = r.Destroyer.Run(inv)
127127
}
128128

129129
// The printer will print updates from the channel. It will block

cmd/status/cmdstatus.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector"
2222
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
2323
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
24+
"sigs.k8s.io/cli-utils/pkg/object"
2425
"sigs.k8s.io/cli-utils/pkg/provider"
2526
"sigs.k8s.io/cli-utils/pkg/util/factory"
2627
)
@@ -84,7 +85,7 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
8485
}
8586

8687
// Find the inventory template among the manifests.
87-
inv, _, err := inventory.SplitInfos(infos)
88+
inv, _, err := inventory.SplitUnstructureds(object.InfosToUnstructureds(infos))
8889
if err != nil {
8990
return err
9091
}

pkg/apply/applier.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,7 @@ func (a *Applier) prepareObjects(objs []*unstructured.Unstructured) (*ResourceOb
9595

9696
// Ensures the namespace exists before applying the inventory object into it.
9797
if invNamespace := inventoryNamespaceInSet(localInv, localObjs); invNamespace != nil {
98-
if err = a.invClient.ApplyInventoryNamespace(
99-
object.UnstructuredToInfo(invNamespace)); err != nil {
98+
if err = a.invClient.ApplyInventoryNamespace(invNamespace); err != nil {
10099
return nil, err
101100
}
102101
}
@@ -105,7 +104,7 @@ func (a *Applier) prepareObjects(objs []*unstructured.Unstructured) (*ResourceOb
105104
// returns the objects (pruneIds) to prune after apply. The prune
106105
// algorithm requires stopping if the merge is not successful. Otherwise,
107106
// the stored objects in inventory could become inconsistent.
108-
pruneIds, err := a.invClient.Merge(object.UnstructuredToInfo(localInv), currentObjs)
107+
pruneIds, err := a.invClient.Merge(localInv, currentObjs)
109108
if err != nil {
110109
return nil, err
111110
}

pkg/apply/destroyer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"sigs.k8s.io/cli-utils/pkg/apply/prune"
1515
"sigs.k8s.io/cli-utils/pkg/common"
1616
"sigs.k8s.io/cli-utils/pkg/inventory"
17-
"sigs.k8s.io/cli-utils/pkg/object"
1817
"sigs.k8s.io/cli-utils/pkg/provider"
1918
)
2019

@@ -92,7 +91,7 @@ func (d *Destroyer) Run(inv *unstructured.Unstructured) <-chan event.Event {
9291
}
9392

9493
// Now delete the inventory object as well.
95-
err = d.invClient.DeleteInventoryObj(object.UnstructuredToInfo(inv))
94+
err = d.invClient.DeleteInventoryObj(inv)
9695
if err != nil {
9796
ch <- event.Event{
9897
Type: event.ErrorType,

pkg/apply/prune/prune.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (po *PruneOptions) Prune(localObjs []*unstructured.Unstructured, currentUID
8888
}
8989
invNamespace := localInv.GetNamespace()
9090
klog.V(4).Infof("prune local inventory object: %s/%s", invNamespace, localInv.GetName())
91-
clusterObjs, err := po.InvClient.GetClusterObjs(object.UnstructuredToInfo(localInv))
91+
clusterObjs, err := po.InvClient.GetClusterObjs(localInv)
9292
if err != nil {
9393
return err
9494
}
@@ -149,7 +149,7 @@ func (po *PruneOptions) Prune(localObjs []*unstructured.Unstructured, currentUID
149149
eventChannel <- createPruneEvent(obj, event.Pruned)
150150
}
151151
localIds := object.UnstructuredsToObjMetas(localObjs)
152-
return po.InvClient.Replace(object.UnstructuredToInfo(localInv), localIds)
152+
return po.InvClient.Replace(localInv, localIds)
153153
}
154154

155155
// preventDeleteAnnotation returns true if the "onRemove:keep"

pkg/apply/prune/prune_test.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"k8s.io/apimachinery/pkg/api/meta/testrestmapper"
1111
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1212
"k8s.io/apimachinery/pkg/util/sets"
13-
"k8s.io/cli-runtime/pkg/resource"
1413
"k8s.io/client-go/dynamic/fake"
1514
"k8s.io/kubectl/pkg/scheme"
1615
"sigs.k8s.io/cli-utils/pkg/apply/event"
@@ -79,24 +78,15 @@ var role = &unstructured.Unstructured{
7978

8079
// Returns a inventory object with the inventory set from
8180
// the passed "children".
82-
func createInventoryInfo(name string, children ...*unstructured.Unstructured) *unstructured.Unstructured {
83-
inventoryName := inventoryObjName
84-
if len(name) > 0 {
85-
inventoryName = name
86-
}
81+
func createInventoryInfo(children ...*unstructured.Unstructured) *unstructured.Unstructured {
8782
inventoryObjCopy := inventoryObj.DeepCopy()
88-
var inventoryInfo = &resource.Info{
89-
Namespace: testNamespace,
90-
Name: inventoryName,
91-
Object: inventoryObjCopy,
92-
}
93-
wrappedInv := inventory.WrapInventoryObj(inventoryInfo)
83+
wrappedInv := inventory.WrapInventoryObj(inventoryObjCopy)
9484
objs := object.UnstructuredsToObjMetas(children)
9585
if err := wrappedInv.Store(objs); err != nil {
9686
return nil
9787
}
98-
inventoryInfo, _ = wrappedInv.GetObject()
99-
return object.InfoToUnstructured(inventoryInfo)
88+
inventoryInfo, _ := wrappedInv.GetObject()
89+
return inventoryInfo
10090
}
10191

10292
// preventDelete object contains the "on-remove:keep" lifecycle directive.
@@ -170,7 +160,7 @@ func TestPrune(t *testing.T) {
170160
clusterObjs := object.UnstructuredsToObjMetas(tc.pastObjs)
171161
po.InvClient = inventory.NewFakeInventoryClient(clusterObjs)
172162
// Set up the currently applied objects.
173-
currentInventory := createInventoryInfo("current-group", tc.currentObjs...)
163+
currentInventory := createInventoryInfo(tc.currentObjs...)
174164
currentObjs := append(tc.currentObjs, currentInventory)
175165
// Set up the fake dynamic client to recognize all objects, and the RESTMapper.
176166
po.client = fake.NewSimpleDynamicClient(scheme.Scheme,

pkg/inventory/fake-inventory-client.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
package inventory
55

66
import (
7-
"k8s.io/cli-runtime/pkg/resource"
7+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
88
"sigs.k8s.io/cli-utils/pkg/common"
99
"sigs.k8s.io/cli-utils/pkg/object"
1010
)
@@ -26,7 +26,7 @@ func NewFakeInventoryClient(initObjs []object.ObjMetadata) *FakeInventoryClient
2626
}
2727

2828
// GetClusterObjs returns currently stored set of objects.
29-
func (fic *FakeInventoryClient) GetClusterObjs(inv *resource.Info) ([]object.ObjMetadata, error) {
29+
func (fic *FakeInventoryClient) GetClusterObjs(inv *unstructured.Unstructured) ([]object.ObjMetadata, error) {
3030
if fic.Err != nil {
3131
return []object.ObjMetadata{}, fic.Err
3232
}
@@ -36,7 +36,7 @@ func (fic *FakeInventoryClient) GetClusterObjs(inv *resource.Info) ([]object.Obj
3636
// Merge stores the passed objects with the current stored cluster inventory
3737
// objects. Returns the set difference of the current set of objects minus
3838
// the passed set of objects, or an error if one is set up.
39-
func (fic *FakeInventoryClient) Merge(inv *resource.Info, objs []object.ObjMetadata) ([]object.ObjMetadata, error) {
39+
func (fic *FakeInventoryClient) Merge(inv *unstructured.Unstructured, objs []object.ObjMetadata) ([]object.ObjMetadata, error) {
4040
if fic.Err != nil {
4141
return []object.ObjMetadata{}, fic.Err
4242
}
@@ -47,7 +47,7 @@ func (fic *FakeInventoryClient) Merge(inv *resource.Info, objs []object.ObjMetad
4747

4848
// Replace the stored cluster inventory objs with the passed obj, or an
4949
// error if one is set up.
50-
func (fic *FakeInventoryClient) Replace(inv *resource.Info, objs []object.ObjMetadata) error {
50+
func (fic *FakeInventoryClient) Replace(inv *unstructured.Unstructured, objs []object.ObjMetadata) error {
5151
if fic.Err != nil {
5252
return fic.Err
5353
}
@@ -56,7 +56,7 @@ func (fic *FakeInventoryClient) Replace(inv *resource.Info, objs []object.ObjMet
5656
}
5757

5858
// DeleteInventoryObj returns an error if one is forced; does nothing otherwise.
59-
func (fic *FakeInventoryClient) DeleteInventoryObj(inv *resource.Info) error {
59+
func (fic *FakeInventoryClient) DeleteInventoryObj(inv *unstructured.Unstructured) error {
6060
if fic.Err != nil {
6161
return fic.Err
6262
}
@@ -65,7 +65,7 @@ func (fic *FakeInventoryClient) DeleteInventoryObj(inv *resource.Info) error {
6565

6666
func (fic *FakeInventoryClient) SetDryRunStrategy(drs common.DryRunStrategy) {}
6767

68-
func (fic *FakeInventoryClient) ApplyInventoryNamespace(info *resource.Info) error {
68+
func (fic *FakeInventoryClient) ApplyInventoryNamespace(inv *unstructured.Unstructured) error {
6969
if fic.Err != nil {
7070
return fic.Err
7171
}

0 commit comments

Comments
 (0)