Skip to content

Commit 7915dc5

Browse files
committed
change the apply API to InventoryInfo
1 parent 17edc20 commit 7915dc5

20 files changed

+289
-146
lines changed

cmd/apply/cmdapply.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
111111
if err != nil {
112112
return err
113113
}
114-
inv, objs, err := inventory.SplitUnstructureds(objs)
114+
inventoryClient, err := r.provider.InventoryClient()
115+
if err != nil {
116+
return err
117+
}
118+
inv, objs, err := inventory.SplitUnstructureds(inventoryClient.InvInfoFactoryFunc(), objs)
115119
if err != nil {
116120
return err
117121
}

cmd/destroy/cmddestroy.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
6464
if err != nil {
6565
return err
6666
}
67-
inv, _, err := inventory.SplitUnstructureds(objs)
67+
inventoryClient, err := r.provider.InventoryClient()
68+
if err != nil {
69+
return err
70+
}
71+
72+
inv, _, err := inventory.SplitUnstructureds(inventoryClient.InvInfoFactoryFunc(), objs)
6873
if err != nil {
6974
return err
7075
}

cmd/preview/cmdpreview.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
100100
if err != nil {
101101
return err
102102
}
103-
inv, objs, err := inventory.SplitUnstructureds(objs)
103+
inventoryClient, err := r.provider.InventoryClient()
104+
if err != nil {
105+
return err
106+
}
107+
108+
inv, objs, err := inventory.SplitUnstructureds(inventoryClient.InvInfoFactoryFunc(), objs)
104109
if err != nil {
105110
return err
106111
}

cmd/status/cmdstatus.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
8383
return err
8484
}
8585

86-
// Find the inventory template among the manifests.
87-
inv, _, err := inventory.SplitUnstructureds(objs)
86+
invClient, err := r.provider.InventoryClient()
8887
if err != nil {
8988
return err
9089
}
9190

92-
invClient, err := r.provider.InventoryClient()
91+
// Find the inventory template among the manifests.
92+
inv, _, err := inventory.SplitUnstructureds(invClient.InvInfoFactoryFunc(), objs)
9393
if err != nil {
9494
return err
9595
}

pkg/apply/applier.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (a *Applier) Initialize() error {
8888
// calculates the set of objects to be pruned (pruneIds), and orders the
8989
// resources for the subsequent apply. Returns the sorted resources to
9090
// apply as well as the objects for the prune, or an error if one occurred.
91-
func (a *Applier) prepareObjects(localInv *unstructured.Unstructured, localObjs []*unstructured.Unstructured) (*ResourceObjects, error) {
91+
func (a *Applier) prepareObjects(localInv inventory.InventoryInfo, localObjs []*unstructured.Unstructured) (*ResourceObjects, error) {
9292
if localInv == nil {
9393
return nil, fmt.Errorf("the local inventory can't be nil")
9494
}
@@ -125,7 +125,7 @@ func (a *Applier) prepareObjects(localInv *unstructured.Unstructured, localObjs
125125
// will be applied and the existing inventories used to determine
126126
// resources that should be pruned.
127127
type ResourceObjects struct {
128-
LocalInv *unstructured.Unstructured
128+
LocalInv inventory.InventoryInfo
129129
Resources []*unstructured.Unstructured
130130
PruneIds []object.ObjMetadata
131131
}
@@ -138,7 +138,7 @@ func (r *ResourceObjects) ObjsForApply() []*unstructured.Unstructured {
138138
}
139139

140140
// Inventory returns the unstructured representation of the inventory object.
141-
func (r *ResourceObjects) Inventory() *unstructured.Unstructured {
141+
func (r *ResourceObjects) Inventory() inventory.InventoryInfo {
142142
return r.LocalInv
143143
}
144144

@@ -172,7 +172,7 @@ func (r *ResourceObjects) AllIds() []object.ObjMetadata {
172172
// before all the given resources have been applied to the cluster. Any
173173
// cancellation or timeout will only affect how long we Wait for the
174174
// resources to become current.
175-
func (a *Applier) Run(ctx context.Context, inventory *unstructured.Unstructured, objects []*unstructured.Unstructured, options Options) <-chan event.Event {
175+
func (a *Applier) Run(ctx context.Context, invInfo inventory.InventoryInfo, objects []*unstructured.Unstructured, options Options) <-chan event.Event {
176176
eventChannel := make(chan event.Event)
177177
setDefaults(&options)
178178
a.invClient.SetDryRunStrategy(options.DryRunStrategy) // client shared with prune, so sets dry-run for prune too.
@@ -182,7 +182,7 @@ func (a *Applier) Run(ctx context.Context, inventory *unstructured.Unstructured,
182182
// This provides us with a slice of all the objects that will be
183183
// applied to the cluster. This takes care of ordering resources
184184
// and handling the inventory object.
185-
resourceObjects, err := a.prepareObjects(inventory, objects)
185+
resourceObjects, err := a.prepareObjects(invInfo, objects)
186186
if err != nil {
187187
handleError(eventChannel, err)
188188
return
@@ -300,12 +300,11 @@ func handleError(eventChannel chan event.Event, err error) {
300300
// inventoryNamespaceInSet returns the the namespace the passed inventory
301301
// object will be applied to, or nil if this namespace object does not exist
302302
// in the passed slice "infos" or the inventory object is cluster-scoped.
303-
func inventoryNamespaceInSet(inv *unstructured.Unstructured, objs []*unstructured.Unstructured) *unstructured.Unstructured {
303+
func inventoryNamespaceInSet(inv inventory.InventoryInfo, objs []*unstructured.Unstructured) *unstructured.Unstructured {
304304
if inv == nil {
305305
return nil
306306
}
307-
invAcc, _ := meta.Accessor(inv)
308-
invNamespace := invAcc.GetNamespace()
307+
invNamespace := inv.Namespace()
309308

310309
for _, obj := range objs {
311310
acc, _ := meta.Accessor(obj)

pkg/apply/applier_test.go

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,8 @@ var inventoryObj = &unstructured.Unstructured{
285285
},
286286
}
287287

288+
var localInv = inventory.WrapInventoryInfoObj(inventoryObj)
289+
288290
var obj1 = &unstructured.Unstructured{
289291
Object: map[string]interface{}{
290292
"apiVersion": "v1",
@@ -344,7 +346,7 @@ func TestInventoryNamespaceInSet(t *testing.T) {
344346
inventoryNamespace := createNamespace(namespace)
345347

346348
tests := map[string]struct {
347-
inv *unstructured.Unstructured
349+
inv inventory.InventoryInfo
348350
objects []*unstructured.Unstructured
349351
namespace *unstructured.Unstructured
350352
}{
@@ -354,22 +356,22 @@ func TestInventoryNamespaceInSet(t *testing.T) {
354356
namespace: nil,
355357
},
356358
"Inventory object, but no resources returns nil namespace": {
357-
inv: inventoryObj,
359+
inv: localInv,
358360
objects: []*unstructured.Unstructured{},
359361
namespace: nil,
360362
},
361363
"Inventory object, resources with no namespace returns nil namespace": {
362-
inv: inventoryObj,
364+
inv: localInv,
363365
objects: []*unstructured.Unstructured{obj1, obj2},
364366
namespace: nil,
365367
},
366368
"Inventory object, different namespace returns nil namespace": {
367-
inv: inventoryObj,
369+
inv: localInv,
368370
objects: []*unstructured.Unstructured{createNamespace("foo")},
369371
namespace: nil,
370372
},
371373
"Inventory object, inventory namespace returns inventory namespace": {
372-
inv: inventoryObj,
374+
inv: localInv,
373375
objects: []*unstructured.Unstructured{obj1, inventoryNamespace, obj3},
374376
namespace: inventoryNamespace,
375377
},
@@ -388,13 +390,13 @@ func TestInventoryNamespaceInSet(t *testing.T) {
388390
func TestReadAndPrepareObjects(t *testing.T) {
389391
testCases := map[string]struct {
390392
// local inventory input into applier.prepareObjects
391-
inventory *unstructured.Unstructured
393+
inventory inventory.InventoryInfo
392394
// locally read resources input into applier.prepareObjects
393395
resources []*unstructured.Unstructured
394396
// objects already stored in the cluster inventory
395397
clusterObjs []*unstructured.Unstructured
396398
// expected returned local inventory object
397-
localInv *unstructured.Unstructured
399+
localInv inventory.InventoryInfo
398400
// expected returned local objects to apply (in order)
399401
localObjs []*unstructured.Unstructured
400402
// expected calculated prune objects
@@ -407,43 +409,43 @@ func TestReadAndPrepareObjects(t *testing.T) {
407409
isError: true,
408410
},
409411
"multiple inventory objects": {
410-
inventory: inventoryObj,
412+
inventory: localInv,
411413
resources: []*unstructured.Unstructured{inventoryObj},
412414
isError: true,
413415
},
414416
"only inventory object": {
415-
inventory: inventoryObj,
416-
localInv: inventoryObj,
417+
inventory: localInv,
418+
localInv: localInv,
417419
isError: false,
418420
},
419421
"only inventory object, prune one object": {
420-
inventory: inventoryObj,
422+
inventory: localInv,
421423
clusterObjs: []*unstructured.Unstructured{obj1},
422-
localInv: inventoryObj,
424+
localInv: localInv,
423425
pruneObjs: []*unstructured.Unstructured{obj1},
424426
isError: false,
425427
},
426428
"inventory object already at the beginning": {
427-
inventory: inventoryObj,
429+
inventory: localInv,
428430
resources: []*unstructured.Unstructured{obj1, clusterScopedObj},
429-
localInv: inventoryObj,
431+
localInv: localInv,
430432
localObjs: []*unstructured.Unstructured{obj1, clusterScopedObj},
431433
isError: false,
432434
},
433435
"inventory object already at the beginning, prune one": {
434-
inventory: inventoryObj,
436+
inventory: localInv,
435437
resources: []*unstructured.Unstructured{obj1, clusterScopedObj},
436438
clusterObjs: []*unstructured.Unstructured{obj2},
437-
localInv: inventoryObj,
439+
localInv: localInv,
438440
localObjs: []*unstructured.Unstructured{obj1, clusterScopedObj},
439441
pruneObjs: []*unstructured.Unstructured{obj2},
440442
isError: false,
441443
},
442444
"inventory object not at the beginning": {
443-
inventory: inventoryObj,
445+
inventory: localInv,
444446
resources: []*unstructured.Unstructured{obj1, obj2, clusterScopedObj},
445447
clusterObjs: []*unstructured.Unstructured{obj2},
446-
localInv: inventoryObj,
448+
localInv: localInv,
447449
localObjs: []*unstructured.Unstructured{obj1, obj2, clusterScopedObj},
448450
pruneObjs: []*unstructured.Unstructured{},
449451
isError: false,
@@ -468,8 +470,11 @@ func TestReadAndPrepareObjects(t *testing.T) {
468470
return
469471
}
470472
// Validate the returned ResourceObjs
471-
if tc.localInv.GetNamespace() != resourceObjs.LocalInv.GetNamespace() ||
472-
tc.localInv.GetName() != resourceObjs.LocalInv.GetName() {
473+
expected := tc.localInv
474+
actual := resourceObjs.LocalInv
475+
if expected.Namespace() != actual.Namespace() ||
476+
expected.Name() != actual.Name() ||
477+
expected.ID() != actual.ID() {
473478
t.Errorf("expected local inventory (%v), got (%v)",
474479
tc.localInv, resourceObjs.LocalInv)
475480
}
@@ -536,7 +541,7 @@ func toIdentifier(t *testing.T, resourceInfo resourceInfo, namespace string) obj
536541
}
537542
}
538543

539-
func createObjs(resources []resourceInfo) (*unstructured.Unstructured, []*unstructured.Unstructured, error) {
544+
func createObjs(resources []resourceInfo) (inventory.InventoryInfo, []*unstructured.Unstructured, error) {
540545
var objs []*unstructured.Unstructured
541546
for _, ri := range resources {
542547
u := &unstructured.Unstructured{}
@@ -546,7 +551,7 @@ func createObjs(resources []resourceInfo) (*unstructured.Unstructured, []*unstru
546551
}
547552
objs = append(objs, u)
548553
}
549-
return inventory.SplitUnstructureds(objs)
554+
return inventory.SplitUnstructureds(inventory.WrapInventoryInfoObj, objs)
550555
}
551556

552557
// The handler interface allows different testcases to provide

pkg/apply/destroyer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88

99
"github.com/go-errors/errors"
1010
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1211
"k8s.io/apimachinery/pkg/util/sets"
1312
"sigs.k8s.io/cli-utils/pkg/apply/event"
1413
"sigs.k8s.io/cli-utils/pkg/apply/prune"
@@ -59,7 +58,7 @@ func (d *Destroyer) Initialize() error {
5958
// Run performs the destroy step. Passes the inventory object. This
6059
// happens asynchronously on progress and any errors are reported
6160
// back on the event channel.
62-
func (d *Destroyer) Run(inv *unstructured.Unstructured) <-chan event.Event {
61+
func (d *Destroyer) Run(inv inventory.InventoryInfo) <-chan event.Event {
6362
ch := make(chan event.Event)
6463

6564
go func() {

pkg/apply/prune/prune.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package prune
1313

1414
import (
1515
"context"
16+
"fmt"
1617
"sort"
1718
"strings"
1819

@@ -82,10 +83,13 @@ type Options struct {
8283
// (retrieved from previous inventory objects) but omitted in
8384
// the current apply. Prune also delete all previous inventory
8485
// objects. Returns an error if there was a problem.
85-
func (po *PruneOptions) Prune(localInv *unstructured.Unstructured, localObjs []*unstructured.Unstructured, currentUIDs sets.String,
86+
func (po *PruneOptions) Prune(localInv inventory.InventoryInfo, localObjs []*unstructured.Unstructured, currentUIDs sets.String,
8687
eventChannel chan<- event.Event, o Options) error {
87-
invNamespace := strings.TrimSpace(strings.ToLower(localInv.GetNamespace()))
88-
klog.V(4).Infof("prune local inventory object: %s/%s", invNamespace, localInv.GetName())
88+
if localInv == nil {
89+
return fmt.Errorf("the local inventory object can't be nil")
90+
}
91+
invNamespace := localInv.Namespace()
92+
klog.V(4).Infof("prune local inventory object: %s/%s", invNamespace, localInv.Name())
8993
// Create the set of namespaces for currently (locally) applied objects, including
9094
// the namespace the inventory object lives in (if it's not cluster-scoped). When
9195
// pruning, check this set of namespaces to ensure these namespaces are not deleted.
@@ -139,8 +143,8 @@ func (po *PruneOptions) Prune(localInv *unstructured.Unstructured, localObjs []*
139143
// currently applied objects.
140144
if !po.Destroy {
141145
if clusterObj.GroupKind == object.CoreV1Namespace.GroupKind() &&
142-
localNamespaces.Has(strings.ToLower(clusterObj.Name)) {
143-
klog.V(7).Infof("skip pruning namespace: %s", clusterObj.Name)
146+
clusterObj.Name == localInv.Namespace() {
147+
klog.V(7).Infof("skip pruning inventory namespace: %s", obj)
144148
eventChannel <- createPruneEvent(obj, event.PruneSkipped)
145149
continue
146150
}

pkg/apply/prune/prune_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,18 @@ var role = &unstructured.Unstructured{
8989

9090
// Returns a inventory object with the inventory set from
9191
// the passed "children".
92-
func createInventoryInfo(children ...*unstructured.Unstructured) *unstructured.Unstructured {
92+
func createInventoryInfo(children ...*unstructured.Unstructured) inventory.InventoryInfo {
9393
inventoryObjCopy := inventoryObj.DeepCopy()
9494
wrappedInv := inventory.WrapInventoryObj(inventoryObjCopy)
9595
objs := object.UnstructuredsToObjMetas(children)
9696
if err := wrappedInv.Store(objs); err != nil {
9797
return nil
9898
}
99-
inventoryInfo, _ := wrappedInv.GetObject()
100-
return inventoryInfo
99+
obj, err := wrappedInv.GetObject()
100+
if err != nil {
101+
return nil
102+
}
103+
return inventory.WrapInventoryInfoObj(obj)
101104
}
102105

103106
// preventDelete object contains the "on-remove:keep" lifecycle directive.

pkg/apply/solver/solver.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"sigs.k8s.io/cli-utils/pkg/apply/task"
3131
"sigs.k8s.io/cli-utils/pkg/apply/taskrunner"
3232
"sigs.k8s.io/cli-utils/pkg/common"
33+
"sigs.k8s.io/cli-utils/pkg/inventory"
3334
"sigs.k8s.io/cli-utils/pkg/object"
3435
)
3536

@@ -51,7 +52,7 @@ type Options struct {
5152

5253
type resourceObjects interface {
5354
ObjsForApply() []*unstructured.Unstructured
54-
Inventory() *unstructured.Unstructured
55+
Inventory() inventory.InventoryInfo
5556
IdsForApply() []object.ObjMetadata
5657
IdsForPrune() []object.ObjMetadata
5758
}

0 commit comments

Comments
 (0)