Skip to content

Commit 902de23

Browse files
authored
Merge pull request #270 from Liujingfang1/inventory
change the apply interface to use inventory.InventoryInfo
2 parents 17edc20 + 3224d5f commit 902de23

24 files changed

+419
-230
lines changed

cmd/apply/cmdapply.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,17 @@ import (
1717
"sigs.k8s.io/cli-utils/cmd/printers"
1818
"sigs.k8s.io/cli-utils/pkg/apply"
1919
"sigs.k8s.io/cli-utils/pkg/common"
20-
"sigs.k8s.io/cli-utils/pkg/inventory"
20+
"sigs.k8s.io/cli-utils/pkg/manifestreader"
2121
"sigs.k8s.io/cli-utils/pkg/provider"
2222
"sigs.k8s.io/kustomize/kyaml/setters2"
2323
)
2424

25-
func GetApplyRunner(provider provider.Provider, ioStreams genericclioptions.IOStreams) *ApplyRunner {
25+
func GetApplyRunner(provider provider.Provider, loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *ApplyRunner {
2626
r := &ApplyRunner{
2727
Applier: apply.NewApplier(provider),
2828
ioStreams: ioStreams,
2929
provider: provider,
30+
loader: loader,
3031
}
3132
cmd := &cobra.Command{
3233
Use: "apply (DIRECTORY | STDIN)",
@@ -61,14 +62,16 @@ func GetApplyRunner(provider provider.Provider, ioStreams genericclioptions.IOSt
6162

6263
func ApplyCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
6364
provider := provider.NewProvider(f)
64-
return GetApplyRunner(provider, ioStreams).Command
65+
loader := manifestreader.NewManifestLoader(f)
66+
return GetApplyRunner(provider, loader, ioStreams).Command
6567
}
6668

6769
type ApplyRunner struct {
6870
Command *cobra.Command
6971
ioStreams genericclioptions.IOStreams
7072
Applier *apply.Applier
7173
provider provider.Provider
74+
loader manifestreader.ManifestLoader
7275

7376
serverSideOptions common.ServerSideOptions
7477
output string
@@ -103,15 +106,16 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
103106
if err != nil {
104107
return err
105108
}
106-
reader, err := r.provider.ManifestReader(cmd.InOrStdin(), args)
109+
reader, err := r.loader.ManifestReader(cmd.InOrStdin(), args)
107110
if err != nil {
108111
return err
109112
}
110113
objs, err := reader.Read()
111114
if err != nil {
112115
return err
113116
}
114-
inv, objs, err := inventory.SplitUnstructureds(objs)
117+
118+
inv, objs, err := r.loader.InventoryInfo(objs)
115119
if err != nil {
116120
return err
117121
}

cmd/destroy/cmddestroy.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ import (
1313
"k8s.io/kubectl/pkg/util/i18n"
1414
"sigs.k8s.io/cli-utils/cmd/printers"
1515
"sigs.k8s.io/cli-utils/pkg/apply"
16-
"sigs.k8s.io/cli-utils/pkg/inventory"
16+
"sigs.k8s.io/cli-utils/pkg/manifestreader"
1717
"sigs.k8s.io/cli-utils/pkg/provider"
1818
)
1919

2020
// GetDestroyRunner creates and returns the DestroyRunner which stores the cobra command.
21-
func GetDestroyRunner(provider provider.Provider, ioStreams genericclioptions.IOStreams) *DestroyRunner {
21+
func GetDestroyRunner(provider provider.Provider, loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *DestroyRunner {
2222
r := &DestroyRunner{
2323
Destroyer: apply.NewDestroyer(provider),
2424
ioStreams: ioStreams,
2525
provider: provider,
26+
loader: loader,
2627
}
2728
cmd := &cobra.Command{
2829
Use: "destroy (DIRECTORY | STDIN)",
@@ -41,7 +42,8 @@ func GetDestroyRunner(provider provider.Provider, ioStreams genericclioptions.IO
4142
// DestroyCommand creates the DestroyRunner, returning the cobra command associated with it.
4243
func DestroyCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
4344
provider := provider.NewProvider(f)
44-
return GetDestroyRunner(provider, ioStreams).Command
45+
loader := manifestreader.NewManifestLoader(f)
46+
return GetDestroyRunner(provider, loader, ioStreams).Command
4547
}
4648

4749
// DestroyRunner encapsulates data necessary to run the destroy command.
@@ -50,21 +52,22 @@ type DestroyRunner struct {
5052
ioStreams genericclioptions.IOStreams
5153
Destroyer *apply.Destroyer
5254
provider provider.Provider
55+
loader manifestreader.ManifestLoader
5356

5457
output string
5558
}
5659

5760
func (r *DestroyRunner) RunE(cmd *cobra.Command, args []string) error {
5861
// Retrieve the inventory object.
59-
reader, err := r.provider.ManifestReader(cmd.InOrStdin(), args)
62+
reader, err := r.loader.ManifestReader(cmd.InOrStdin(), args)
6063
if err != nil {
6164
return err
6265
}
6366
objs, err := reader.Read()
6467
if err != nil {
6568
return err
6669
}
67-
inv, _, err := inventory.SplitUnstructureds(objs)
70+
inv, _, err := r.loader.InventoryInfo(objs)
6871
if err != nil {
6972
return err
7073
}

cmd/preview/cmdpreview.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"sigs.k8s.io/cli-utils/pkg/apply"
1717
"sigs.k8s.io/cli-utils/pkg/apply/event"
1818
"sigs.k8s.io/cli-utils/pkg/common"
19-
"sigs.k8s.io/cli-utils/pkg/inventory"
19+
"sigs.k8s.io/cli-utils/pkg/manifestreader"
2020
"sigs.k8s.io/cli-utils/pkg/provider"
2121
"sigs.k8s.io/kustomize/kyaml/setters2"
2222
)
@@ -27,12 +27,13 @@ var (
2727
)
2828

2929
// GetPreviewRunner creates and returns the PreviewRunner which stores the cobra command.
30-
func GetPreviewRunner(provider provider.Provider, ioStreams genericclioptions.IOStreams) *PreviewRunner {
30+
func GetPreviewRunner(provider provider.Provider, loader manifestreader.ManifestLoader, ioStreams genericclioptions.IOStreams) *PreviewRunner {
3131
r := &PreviewRunner{
3232
Applier: apply.NewApplier(provider),
3333
Destroyer: apply.NewDestroyer(provider),
3434
ioStreams: ioStreams,
3535
provider: provider,
36+
loader: loader,
3637
}
3738
cmd := &cobra.Command{
3839
Use: "preview (DIRECTORY | STDIN)",
@@ -60,7 +61,8 @@ func GetPreviewRunner(provider provider.Provider, ioStreams genericclioptions.IO
6061
// PreviewCommand creates the PreviewRunner, returning the cobra command associated with it.
6162
func PreviewCommand(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
6263
provider := provider.NewProvider(f)
63-
return GetPreviewRunner(provider, ioStreams).Command
64+
loader := manifestreader.NewManifestLoader(f)
65+
return GetPreviewRunner(provider, loader, ioStreams).Command
6466
}
6567

6668
// PreviewRunner encapsulates data necessary to run the preview command.
@@ -70,6 +72,7 @@ type PreviewRunner struct {
7072
Applier *apply.Applier
7173
Destroyer *apply.Destroyer
7274
provider provider.Provider
75+
loader manifestreader.ManifestLoader
7376

7477
serverSideOptions common.ServerSideOptions
7578
output string
@@ -92,15 +95,16 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
9295
r.Destroyer.DryRunStrategy = drs
9396
}
9497

95-
reader, err := r.provider.ManifestReader(cmd.InOrStdin(), args)
98+
reader, err := r.loader.ManifestReader(cmd.InOrStdin(), args)
9699
if err != nil {
97100
return err
98101
}
99102
objs, err := reader.Read()
100103
if err != nil {
101104
return err
102105
}
103-
inv, objs, err := inventory.SplitUnstructureds(objs)
106+
107+
inv, objs, err := r.loader.InventoryInfo(objs)
104108
if err != nil {
105109
return err
106110
}

cmd/status/cmdstatus.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,20 @@ import (
1515
"sigs.k8s.io/cli-utils/cmd/status/printers"
1616
"sigs.k8s.io/cli-utils/pkg/apply/poller"
1717
"sigs.k8s.io/cli-utils/pkg/common"
18-
"sigs.k8s.io/cli-utils/pkg/inventory"
1918
"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
2019
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/aggregator"
2120
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/collector"
2221
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
2322
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
23+
"sigs.k8s.io/cli-utils/pkg/manifestreader"
2424
"sigs.k8s.io/cli-utils/pkg/provider"
2525
"sigs.k8s.io/cli-utils/pkg/util/factory"
2626
)
2727

28-
func GetStatusRunner(provider provider.Provider) *StatusRunner {
28+
func GetStatusRunner(provider provider.Provider, loader manifestreader.ManifestLoader) *StatusRunner {
2929
r := &StatusRunner{
3030
provider: provider,
31+
loader: loader,
3132
pollerFactoryFunc: pollerFactoryFunc,
3233
}
3334
c := &cobra.Command{
@@ -48,14 +49,16 @@ func GetStatusRunner(provider provider.Provider) *StatusRunner {
4849

4950
func StatusCommand(f cmdutil.Factory) *cobra.Command {
5051
provider := provider.NewProvider(f)
51-
return GetStatusRunner(provider).Command
52+
loader := manifestreader.NewManifestLoader(f)
53+
return GetStatusRunner(provider, loader).Command
5254
}
5355

5456
// StatusRunner captures the parameters for the command and contains
5557
// the run function.
5658
type StatusRunner struct {
5759
Command *cobra.Command
5860
provider provider.Provider
61+
loader manifestreader.ManifestLoader
5962

6063
period time.Duration
6164
pollUntil string
@@ -74,7 +77,7 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
7477
return err
7578
}
7679

77-
reader, err := r.provider.ManifestReader(cmd.InOrStdin(), args)
80+
reader, err := r.loader.ManifestReader(cmd.InOrStdin(), args)
7881
if err != nil {
7982
return err
8083
}
@@ -83,8 +86,7 @@ func (r *StatusRunner) runE(cmd *cobra.Command, args []string) error {
8386
return err
8487
}
8588

86-
// Find the inventory template among the manifests.
87-
inv, _, err := inventory.SplitUnstructureds(objs)
89+
inv, _, err := r.loader.InventoryInfo(objs)
8890
if err != nil {
8991
return err
9092
}

cmd/status/cmdstatus_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"sigs.k8s.io/cli-utils/pkg/kstatus/polling"
2020
pollevent "sigs.k8s.io/cli-utils/pkg/kstatus/polling/event"
2121
"sigs.k8s.io/cli-utils/pkg/kstatus/status"
22+
"sigs.k8s.io/cli-utils/pkg/manifestreader"
2223
"sigs.k8s.io/cli-utils/pkg/object"
2324
"sigs.k8s.io/cli-utils/pkg/provider"
2425
)
@@ -222,8 +223,10 @@ deployment.apps/foo is InProgress: inProgress
222223
defer tf.Cleanup()
223224

224225
provider := provider.NewFakeProvider(tf, tc.inventory)
226+
loader := manifestreader.NewFakeLoader(tf, tc.inventory)
225227
runner := &StatusRunner{
226228
provider: provider,
229+
loader: loader,
227230
pollerFactoryFunc: func(c cmdutil.Factory) (poller.Poller, error) {
228231
return &fakePoller{tc.events}, nil
229232
},

pkg/apply/applier.go

Lines changed: 8 additions & 9 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,13 +182,13 @@ 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
189189
}
190190

191-
mapper, err := a.provider.ToRESTMapper()
191+
mapper, err := a.provider.Factory().ToRESTMapper()
192192
if err != nil {
193193
handleError(eventChannel, err)
194194
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)

0 commit comments

Comments
 (0)