Skip to content

Commit 507d11f

Browse files
committed
Use Unstructured instead of Info
1 parent 5748e7c commit 507d11f

File tree

20 files changed

+408
-348
lines changed

20 files changed

+408
-348
lines changed

cmd/apply/cmdapply.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ 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/object"
2021
"sigs.k8s.io/cli-utils/pkg/provider"
2122
"sigs.k8s.io/kustomize/kyaml/setters2"
2223
)
@@ -109,7 +110,7 @@ func (r *ApplyRunner) RunE(cmd *cobra.Command, args []string) error {
109110
if err := r.Applier.Initialize(); err != nil {
110111
return err
111112
}
112-
ch := r.Applier.Run(context.Background(), infos, apply.Options{
113+
ch := r.Applier.Run(context.Background(), object.InfosToUnstructureds(infos), apply.Options{
113114
PollInterval: r.period,
114115
ReconcileTimeout: r.reconcileTimeout,
115116
// If we are not waiting for status, tell the applier to not

cmd/preview/cmdpreview.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
"sigs.k8s.io/cli-utils/pkg/apply/event"
1616
"sigs.k8s.io/cli-utils/pkg/common"
1717
"sigs.k8s.io/cli-utils/pkg/inventory"
18+
"sigs.k8s.io/cli-utils/pkg/object"
1819
"sigs.k8s.io/cli-utils/pkg/provider"
1920
"sigs.k8s.io/kustomize/kyaml/setters2"
2021
)
@@ -112,7 +113,7 @@ func (r *PreviewRunner) RunE(cmd *cobra.Command, args []string) error {
112113

113114
// Run the applier. It will return a channel where we can receive updates
114115
// to keep track of progress and any issues.
115-
ch = r.Applier.Run(ctx, infos, apply.Options{
116+
ch = r.Applier.Run(ctx, object.InfosToUnstructureds(infos), apply.Options{
116117
EmitStatusEvents: false,
117118
NoPrune: noPrune,
118119
DryRunStrategy: drs,

examples/alphaTestExamples/inventoryNamespace.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ test-namespace is created first, so the following resources within the namespace
8686
<!-- @runApply @testE2EAgainstLatestRelease -->
8787
```
8888
kapply apply $BASE --reconcile-timeout=1m > $OUTPUT/status
89-
expectedOutputLine "namespace/test-namespace configured"
89+
expectedOutputLine "namespace/test-namespace unchanged"
9090
expectedOutputLine "configmap/cm-a created"
91-
expectedOutputLine "2 resource(s) applied. 1 created, 0 unchanged, 1 configured"
91+
expectedOutputLine "2 resource(s) applied. 1 created, 1 unchanged, 0 configured"
9292
expectedOutputLine "0 resource(s) pruned, 0 skipped"
9393

9494
# There should be only one inventory object

hack/testExamplesE2EAgainstKapply.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ set -o nounset
77
set -o errexit
88
set -o pipefail
99

10-
mdrip --blockTimeOut 6m0s --mode test \
10+
mdrip -alsologtostderr -v 10 --blockTimeOut 6m0s --mode test \
1111
--label testE2EAgainstLatestRelease examples/alphaTestExamples
1212

1313
echo "Example e2e tests passed against ."

pkg/apply/applier.go

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/go-errors/errors"
1212
"k8s.io/apimachinery/pkg/api/meta"
1313
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14-
"k8s.io/cli-runtime/pkg/resource"
14+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1515
"sigs.k8s.io/cli-utils/pkg/apply/event"
1616
"sigs.k8s.io/cli-utils/pkg/apply/info"
1717
"sigs.k8s.io/cli-utils/pkg/apply/poller"
@@ -36,8 +36,8 @@ func NewApplier(provider provider.Provider) *Applier {
3636
a := &Applier{
3737
PruneOptions: prune.NewPruneOptions(),
3838
provider: provider,
39+
infoHelper: info.NewInfoHelper(provider.Factory()),
3940
}
40-
a.infoHelperFactoryFunc = a.infoHelperFactory
4141
return a
4242
}
4343

@@ -57,10 +57,7 @@ type Applier struct {
5757
PruneOptions *prune.PruneOptions
5858
StatusPoller poller.Poller
5959
invClient inventory.InventoryClient
60-
61-
// infoHelperFactoryFunc is used to create a new instance of the
62-
// InfoHelper. It is defined here so we can override it in unit tests.
63-
infoHelperFactoryFunc func() info.InfoHelper
60+
infoHelper info.InfoHelper
6461
}
6562

6663
// Initialize sets up the Applier for actually doing an apply against
@@ -85,46 +82,39 @@ func (a *Applier) Initialize() error {
8582
return nil
8683
}
8784

88-
// infoHelperFactory returns a new instance of the InfoHelper.
89-
func (a *Applier) infoHelperFactory() info.InfoHelper {
90-
return info.NewInfoHelper(a.provider.Factory())
91-
}
92-
9385
// prepareObjects merges the currently applied objects into the
9486
// set of stored objects in the cluster inventory. In the process, it
9587
// calculates the set of objects to be pruned (pruneIds), and orders the
9688
// resources for the subsequent apply. Returns the sorted resources to
9789
// apply as well as the objects for the prune, or an error if one occurred.
98-
func (a *Applier) prepareObjects(infos []*resource.Info) (*ResourceObjects, error) {
99-
localInv, localInfos, err := inventory.SplitInfos(infos)
90+
func (a *Applier) prepareObjects(objs []*unstructured.Unstructured) (*ResourceObjects, error) {
91+
localInv, localObjs, err := inventory.SplitUnstructureds(objs)
10092
if err != nil {
10193
return nil, err
10294
}
10395

10496
// Ensures the namespace exists before applying the inventory object into it.
105-
if invNamespace := inventoryNamespaceInSet(localInv, localInfos); invNamespace != nil {
106-
if err = a.invClient.ApplyInventoryNamespace(invNamespace); err != nil {
97+
if invNamespace := inventoryNamespaceInSet(localInv, localObjs); invNamespace != nil {
98+
if err = a.invClient.ApplyInventoryNamespace(
99+
object.UnstructuredToInfo(invNamespace)); err != nil {
107100
return nil, err
108101
}
109102
}
110103

111-
currentObjs, err := object.InfosToObjMetas(localInfos)
112-
if err != nil {
113-
return nil, err
114-
}
104+
currentObjs := object.UnstructuredsToObjMetas(localObjs)
115105
// returns the objects (pruneIds) to prune after apply. The prune
116106
// algorithm requires stopping if the merge is not successful. Otherwise,
117107
// the stored objects in inventory could become inconsistent.
118-
pruneIds, err := a.invClient.Merge(localInv, currentObjs)
108+
pruneIds, err := a.invClient.Merge(object.UnstructuredToInfo(localInv), currentObjs)
119109
if err != nil {
120110
return nil, err
121111
}
122112
// Sort order for applied resources.
123-
sort.Sort(ordering.SortableInfos(localInfos))
113+
sort.Sort(ordering.SortableUnstructureds(localObjs))
124114

125115
return &ResourceObjects{
126116
LocalInv: localInv,
127-
Resources: localInfos,
117+
Resources: localObjs,
128118
PruneIds: pruneIds,
129119
}, nil
130120
}
@@ -133,31 +123,28 @@ func (a *Applier) prepareObjects(infos []*resource.Info) (*ResourceObjects, erro
133123
// will be applied and the existing inventories used to determine
134124
// resources that should be pruned.
135125
type ResourceObjects struct {
136-
LocalInv *resource.Info
137-
Resources []*resource.Info
126+
LocalInv *unstructured.Unstructured
127+
Resources []*unstructured.Unstructured
138128
PruneIds []object.ObjMetadata
139129
}
140130

141131
// InfosForApply returns the infos representation for all the resources
142132
// that should be applied, including the inventory object. The
143133
// resources will be in sorted order.
144-
func (r *ResourceObjects) InfosForApply() []*resource.Info {
134+
func (r *ResourceObjects) ObjsForApply() []*unstructured.Unstructured {
145135
return r.Resources
146136
}
147137

148-
func (r *ResourceObjects) InfosForPrune() []*resource.Info {
149-
return append([]*resource.Info{r.LocalInv}, r.Resources...)
138+
func (r *ResourceObjects) ObjsForPrune() []*unstructured.Unstructured {
139+
return append([]*unstructured.Unstructured{r.LocalInv}, r.Resources...)
150140
}
151141

152142
// IdsForApply returns the Ids for all resources that should be applied,
153143
// including the inventory object.
154144
func (r *ResourceObjects) IdsForApply() []object.ObjMetadata {
155145
var ids []object.ObjMetadata
156-
for _, info := range r.InfosForApply() {
157-
id, err := object.InfoToObjMeta(info)
158-
if err == nil {
159-
ids = append(ids, id)
160-
}
146+
for _, obj := range r.ObjsForApply() {
147+
ids = append(ids, object.UnstructuredToObjMeta(obj))
161148
}
162149
return ids
163150
}
@@ -182,7 +169,7 @@ func (r *ResourceObjects) AllIds() []object.ObjMetadata {
182169
// before all the given resources have been applied to the cluster. Any
183170
// cancellation or timeout will only affect how long we Wait for the
184171
// resources to become current.
185-
func (a *Applier) Run(ctx context.Context, objects []*resource.Info, options Options) <-chan event.Event {
172+
func (a *Applier) Run(ctx context.Context, objects []*unstructured.Unstructured, options Options) <-chan event.Event {
186173
eventChannel := make(chan event.Event)
187174
setDefaults(&options)
188175
a.invClient.SetDryRunStrategy(options.DryRunStrategy) // client shared with prune, so sets dry-run for prune too.
@@ -208,7 +195,7 @@ func (a *Applier) Run(ctx context.Context, objects []*resource.Info, options Opt
208195
taskQueue := (&solver.TaskQueueSolver{
209196
PruneOptions: a.PruneOptions,
210197
Factory: a.provider.Factory(),
211-
InfoHelper: a.infoHelperFactoryFunc(),
198+
InfoHelper: a.infoHelper,
212199
Mapper: mapper,
213200
}).BuildTaskQueue(resourceObjects, solver.Options{
214201
ReconcileTimeout: options.ReconcileTimeout,
@@ -306,18 +293,18 @@ func handleError(eventChannel chan event.Event, err error) {
306293
// inventoryNamespaceInSet returns the the namespace the passed inventory
307294
// object will be applied to, or nil if this namespace object does not exist
308295
// in the passed slice "infos" or the inventory object is cluster-scoped.
309-
func inventoryNamespaceInSet(inv *resource.Info, infos []*resource.Info) *resource.Info {
310-
if inv == nil || inv.Object == nil {
296+
func inventoryNamespaceInSet(inv *unstructured.Unstructured, objs []*unstructured.Unstructured) *unstructured.Unstructured {
297+
if inv == nil {
311298
return nil
312299
}
313-
invAcc, _ := meta.Accessor(inv.Object)
300+
invAcc, _ := meta.Accessor(inv)
314301
invNamespace := invAcc.GetNamespace()
315302

316-
for _, info := range infos {
317-
acc, _ := meta.Accessor(info.Object)
318-
gvk := info.Object.GetObjectKind().GroupVersionKind()
303+
for _, obj := range objs {
304+
acc, _ := meta.Accessor(obj)
305+
gvk := obj.GetObjectKind().GroupVersionKind()
319306
if gvk == object.CoreV1Namespace && acc.GetName() == invNamespace {
320-
return info
307+
return obj
321308
}
322309
}
323310
return nil

0 commit comments

Comments
 (0)