@@ -11,7 +11,7 @@ import (
11
11
"github.com/go-errors/errors"
12
12
"k8s.io/apimachinery/pkg/api/meta"
13
13
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
14
- "k8s.io/cli-runtime /pkg/resource "
14
+ "k8s.io/apimachinery /pkg/apis/meta/v1/unstructured "
15
15
"sigs.k8s.io/cli-utils/pkg/apply/event"
16
16
"sigs.k8s.io/cli-utils/pkg/apply/info"
17
17
"sigs.k8s.io/cli-utils/pkg/apply/poller"
@@ -36,8 +36,8 @@ func NewApplier(provider provider.Provider) *Applier {
36
36
a := & Applier {
37
37
PruneOptions : prune .NewPruneOptions (),
38
38
provider : provider ,
39
+ infoHelper : info .NewInfoHelper (provider .Factory ()),
39
40
}
40
- a .infoHelperFactoryFunc = a .infoHelperFactory
41
41
return a
42
42
}
43
43
@@ -57,10 +57,7 @@ type Applier struct {
57
57
PruneOptions * prune.PruneOptions
58
58
StatusPoller poller.Poller
59
59
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
64
61
}
65
62
66
63
// Initialize sets up the Applier for actually doing an apply against
@@ -85,46 +82,39 @@ func (a *Applier) Initialize() error {
85
82
return nil
86
83
}
87
84
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
-
93
85
// prepareObjects merges the currently applied objects into the
94
86
// set of stored objects in the cluster inventory. In the process, it
95
87
// calculates the set of objects to be pruned (pruneIds), and orders the
96
88
// resources for the subsequent apply. Returns the sorted resources to
97
89
// 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 )
100
92
if err != nil {
101
93
return nil , err
102
94
}
103
95
104
96
// 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 {
107
100
return nil , err
108
101
}
109
102
}
110
103
111
- currentObjs , err := object .InfosToObjMetas (localInfos )
112
- if err != nil {
113
- return nil , err
114
- }
104
+ currentObjs := object .UnstructuredsToObjMetas (localObjs )
115
105
// returns the objects (pruneIds) to prune after apply. The prune
116
106
// algorithm requires stopping if the merge is not successful. Otherwise,
117
107
// 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 )
119
109
if err != nil {
120
110
return nil , err
121
111
}
122
112
// Sort order for applied resources.
123
- sort .Sort (ordering .SortableInfos ( localInfos ))
113
+ sort .Sort (ordering .SortableUnstructureds ( localObjs ))
124
114
125
115
return & ResourceObjects {
126
116
LocalInv : localInv ,
127
- Resources : localInfos ,
117
+ Resources : localObjs ,
128
118
PruneIds : pruneIds ,
129
119
}, nil
130
120
}
@@ -133,31 +123,28 @@ func (a *Applier) prepareObjects(infos []*resource.Info) (*ResourceObjects, erro
133
123
// will be applied and the existing inventories used to determine
134
124
// resources that should be pruned.
135
125
type ResourceObjects struct {
136
- LocalInv * resource. Info
137
- Resources []* resource. Info
126
+ LocalInv * unstructured. Unstructured
127
+ Resources []* unstructured. Unstructured
138
128
PruneIds []object.ObjMetadata
139
129
}
140
130
141
131
// InfosForApply returns the infos representation for all the resources
142
132
// that should be applied, including the inventory object. The
143
133
// resources will be in sorted order.
144
- func (r * ResourceObjects ) InfosForApply () []* resource. Info {
134
+ func (r * ResourceObjects ) ObjsForApply () []* unstructured. Unstructured {
145
135
return r .Resources
146
136
}
147
137
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 ... )
150
140
}
151
141
152
142
// IdsForApply returns the Ids for all resources that should be applied,
153
143
// including the inventory object.
154
144
func (r * ResourceObjects ) IdsForApply () []object.ObjMetadata {
155
145
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 ))
161
148
}
162
149
return ids
163
150
}
@@ -182,7 +169,7 @@ func (r *ResourceObjects) AllIds() []object.ObjMetadata {
182
169
// before all the given resources have been applied to the cluster. Any
183
170
// cancellation or timeout will only affect how long we Wait for the
184
171
// 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 {
186
173
eventChannel := make (chan event.Event )
187
174
setDefaults (& options )
188
175
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
208
195
taskQueue := (& solver.TaskQueueSolver {
209
196
PruneOptions : a .PruneOptions ,
210
197
Factory : a .provider .Factory (),
211
- InfoHelper : a .infoHelperFactoryFunc () ,
198
+ InfoHelper : a .infoHelper ,
212
199
Mapper : mapper ,
213
200
}).BuildTaskQueue (resourceObjects , solver.Options {
214
201
ReconcileTimeout : options .ReconcileTimeout ,
@@ -306,18 +293,18 @@ func handleError(eventChannel chan event.Event, err error) {
306
293
// inventoryNamespaceInSet returns the the namespace the passed inventory
307
294
// object will be applied to, or nil if this namespace object does not exist
308
295
// 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 {
311
298
return nil
312
299
}
313
- invAcc , _ := meta .Accessor (inv . Object )
300
+ invAcc , _ := meta .Accessor (inv )
314
301
invNamespace := invAcc .GetNamespace ()
315
302
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 ()
319
306
if gvk == object .CoreV1Namespace && acc .GetName () == invNamespace {
320
- return info
307
+ return obj
321
308
}
322
309
}
323
310
return nil
0 commit comments