Skip to content

Commit 7813995

Browse files
committed
Rename Application to KustTarget
1 parent ffffbed commit 7813995

File tree

3 files changed

+73
-69
lines changed

3 files changed

+73
-69
lines changed

pkg/commands/build.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ package commands
1919
import (
2020
"errors"
2121
"io"
22+
"log"
2223
"strings"
2324

2425
"github.com/spf13/cobra"
25-
26-
"log"
27-
"sigs.k8s.io/kustomize/pkg/app"
2826
"sigs.k8s.io/kustomize/pkg/constants"
2927
"sigs.k8s.io/kustomize/pkg/fs"
3028
"sigs.k8s.io/kustomize/pkg/loader"
29+
"sigs.k8s.io/kustomize/pkg/target"
3130
"sigs.k8s.io/kustomize/pkg/transformerconfig"
3231
)
3332

@@ -121,13 +120,12 @@ func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error {
121120
return err
122121
}
123122
defer rootLoader.Cleanup()
124-
125-
application, err := app.NewApplication(
123+
target, err := target.NewKustTarget(
126124
rootLoader, fSys, makeTransformerconfig(fSys, o.transformerconfigPaths))
127125
if err != nil {
128126
return err
129127
}
130-
allResources, err := application.MakeCustomizedResMap()
128+
allResources, err := target.MakeCustomizedResMap()
131129
if err != nil {
132130
return err
133131
}

pkg/app/application.go renamed to pkg/target/kusttarget.go

Lines changed: 62 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,8 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
// Package app implements state for the set of all resources being customized.
18-
// Should rename this - there's nothing "app"y about it.
19-
package app
17+
// Package target implements state for the set of all resources being customized.
18+
package target
2019

2120
import (
2221
"bytes"
@@ -42,19 +41,18 @@ import (
4241
"sigs.k8s.io/kustomize/pkg/types"
4342
)
4443

45-
// Application implements the guts of the kustomize 'build' command.
46-
// TODO: Change name, as "application" is overloaded and somewhat
47-
// misleading (one can customize an RBAC policy). Perhaps "Target"
48-
// https://github.com/kubernetes-sigs/kustomize/blob/master/docs/glossary.md#target
49-
type Application struct {
44+
// KustTarget encapsulates the entirety of a kustomization build.
45+
type KustTarget struct {
5046
kustomization *types.Kustomization
5147
ldr loader.Loader
5248
fSys fs.FileSystem
5349
tcfg *transformerconfig.TransformerConfig
5450
}
5551

56-
// NewApplication returns a new instance of Application primed with a Loader.
57-
func NewApplication(ldr loader.Loader, fSys fs.FileSystem, tcfg *transformerconfig.TransformerConfig) (*Application, error) {
52+
// NewKustTarget returns a new instance of KustTarget primed with a Loader.
53+
func NewKustTarget(
54+
ldr loader.Loader, fSys fs.FileSystem,
55+
tcfg *transformerconfig.TransformerConfig) (*KustTarget, error) {
5856
content, err := ldr.Load(constants.KustomizationFileName)
5957
if err != nil {
6058
return nil, err
@@ -65,7 +63,7 @@ func NewApplication(ldr loader.Loader, fSys fs.FileSystem, tcfg *transformerconf
6563
if err != nil {
6664
return nil, err
6765
}
68-
return &Application{
66+
return &KustTarget{
6967
kustomization: &m,
7068
ldr: ldr,
7169
fSys: fSys,
@@ -86,33 +84,33 @@ func unmarshal(y []byte, o interface{}) error {
8684

8785
// MakeCustomizedResMap creates a ResMap per kustomization instructions.
8886
// The Resources in the returned ResMap are fully customized.
89-
func (a *Application) MakeCustomizedResMap() (resmap.ResMap, error) {
90-
m, err := a.loadCustomizedResMap()
87+
func (kt *KustTarget) MakeCustomizedResMap() (resmap.ResMap, error) {
88+
m, err := kt.loadCustomizedResMap()
9189
if err != nil {
9290
return nil, err
9391
}
94-
return a.resolveRefsToGeneratedResources(m)
92+
return kt.resolveRefsToGeneratedResources(m)
9593
}
9694

9795
// resolveRefsToGeneratedResources fixes all name references.
98-
func (a *Application) resolveRefsToGeneratedResources(m resmap.ResMap) (resmap.ResMap, error) {
96+
func (kt *KustTarget) resolveRefsToGeneratedResources(m resmap.ResMap) (resmap.ResMap, error) {
9997
err := transformers.NewNameHashTransformer().Transform(m)
10098
if err != nil {
10199
return nil, err
102100
}
103101

104102
var r []transformers.Transformer
105-
t, err := transformers.NewNameReferenceTransformer(a.tcfg.NameReference)
103+
t, err := transformers.NewNameReferenceTransformer(kt.tcfg.NameReference)
106104
if err != nil {
107105
return nil, err
108106
}
109107
r = append(r, t)
110108

111-
refVars, err := a.resolveRefVars(m)
109+
refVars, err := kt.resolveRefVars(m)
112110
if err != nil {
113111
return nil, err
114112
}
115-
t = transformers.NewRefVarTransformer(refVars, a.tcfg.VarReference)
113+
t = transformers.NewRefVarTransformer(refVars, kt.tcfg.VarReference)
116114
r = append(r, t)
117115

118116
err = transformers.NewMultiTransformer(r).Transform(m)
@@ -123,26 +121,26 @@ func (a *Application) resolveRefsToGeneratedResources(m resmap.ResMap) (resmap.R
123121
}
124122

125123
// loadCustomizedResMap loads and customizes resources to build a ResMap.
126-
func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) {
124+
func (kt *KustTarget) loadCustomizedResMap() (resmap.ResMap, error) {
127125
errs := &interror.KustomizationErrors{}
128-
result, err := a.loadResMapFromBasesAndResources()
126+
result, err := kt.loadResMapFromBasesAndResources()
129127
if err != nil {
130128
errs.Append(errors.Wrap(err, "loadResMapFromBasesAndResources"))
131129
}
132-
crdPathConfigs, err := crds.RegisterCRDs(a.ldr, a.kustomization.Crds)
133-
a.tcfg = a.tcfg.Merge(crdPathConfigs)
130+
crdPathConfigs, err := crds.RegisterCRDs(kt.ldr, kt.kustomization.Crds)
131+
kt.tcfg = kt.tcfg.Merge(crdPathConfigs)
134132
if err != nil {
135133
errs.Append(errors.Wrap(err, "RegisterCRDs"))
136134
}
137135
cms, err := resmap.NewResMapFromConfigMapArgs(
138-
configmapandsecret.NewConfigMapFactory(a.fSys, a.ldr),
139-
a.kustomization.ConfigMapGenerator)
136+
configmapandsecret.NewConfigMapFactory(kt.fSys, kt.ldr),
137+
kt.kustomization.ConfigMapGenerator)
140138
if err != nil {
141139
errs.Append(errors.Wrap(err, "NewResMapFromConfigMapArgs"))
142140
}
143141
secrets, err := resmap.NewResMapFromSecretArgs(
144-
configmapandsecret.NewSecretFactory(a.fSys, a.ldr.Root()),
145-
a.kustomization.SecretGenerator)
142+
configmapandsecret.NewSecretFactory(kt.fSys, kt.ldr.Root()),
143+
kt.kustomization.SecretGenerator)
146144
if err != nil {
147145
errs.Append(errors.Wrap(err, "NewResMapFromSecretArgs"))
148146
}
@@ -156,8 +154,11 @@ func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) {
156154
return nil, err
157155
}
158156

159-
a.kustomization.PatchesStrategicMerge = patch.Append(a.kustomization.PatchesStrategicMerge, a.kustomization.Patches...)
160-
patches, err := resmap.NewResourceSliceFromPatches(a.ldr, a.kustomization.PatchesStrategicMerge)
157+
kt.kustomization.PatchesStrategicMerge = patch.Append(
158+
kt.kustomization.PatchesStrategicMerge,
159+
kt.kustomization.Patches...)
160+
patches, err := resmap.NewResourceSliceFromPatches(
161+
kt.ldr, kt.kustomization.PatchesStrategicMerge)
161162
if err != nil {
162163
errs.Append(errors.Wrap(err, "NewResourceSliceFromPatches"))
163164
}
@@ -167,17 +168,18 @@ func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) {
167168
}
168169

169170
var r []transformers.Transformer
170-
t, err := a.newTransformer(patches)
171+
t, err := kt.newTransformer(patches)
171172
if err != nil {
172173
return nil, err
173174
}
174175
r = append(r, t)
175-
t, err = patchtransformer.NewPatchJson6902Factory(a.ldr).MakePatchJson6902Transformer(a.kustomization.PatchesJson6902)
176+
t, err = patchtransformer.NewPatchJson6902Factory(kt.ldr).
177+
MakePatchJson6902Transformer(kt.kustomization.PatchesJson6902)
176178
if err != nil {
177179
return nil, err
178180
}
179181
r = append(r, t)
180-
t, err = transformers.NewImageTagTransformer(a.kustomization.ImageTags)
182+
t, err = transformers.NewImageTagTransformer(kt.kustomization.ImageTags)
181183
if err != nil {
182184
return nil, err
183185
}
@@ -191,9 +193,9 @@ func (a *Application) loadCustomizedResMap() (resmap.ResMap, error) {
191193
}
192194

193195
// Gets Bases and Resources as advertised.
194-
func (a *Application) loadResMapFromBasesAndResources() (resmap.ResMap, error) {
195-
bases, errs := a.loadCustomizedBases()
196-
resources, err := resmap.NewResMapFromFiles(a.ldr, a.kustomization.Resources)
196+
func (kt *KustTarget) loadResMapFromBasesAndResources() (resmap.ResMap, error) {
197+
bases, errs := kt.loadCustomizedBases()
198+
resources, err := resmap.NewResMapFromFiles(kt.ldr, kt.kustomization.Resources)
197199
if err != nil {
198200
errs.Append(errors.Wrap(err, "rawResources failed to read Resources"))
199201
}
@@ -205,21 +207,21 @@ func (a *Application) loadResMapFromBasesAndResources() (resmap.ResMap, error) {
205207

206208
// Loop through the Bases of this kustomization recursively loading resources.
207209
// Combine into one ResMap, demanding unique Ids for each resource.
208-
func (a *Application) loadCustomizedBases() (resmap.ResMap, *interror.KustomizationErrors) {
210+
func (kt *KustTarget) loadCustomizedBases() (resmap.ResMap, *interror.KustomizationErrors) {
209211
var list []resmap.ResMap
210212
errs := &interror.KustomizationErrors{}
211-
for _, path := range a.kustomization.Bases {
212-
ldr, err := a.ldr.New(path)
213+
for _, path := range kt.kustomization.Bases {
214+
ldr, err := kt.ldr.New(path)
213215
if err != nil {
214216
errs.Append(errors.Wrap(err, "couldn't make ldr for "+path))
215217
continue
216218
}
217-
app, err := NewApplication(ldr, a.fSys, a.tcfg)
219+
target, err := NewKustTarget(ldr, kt.fSys, kt.tcfg)
218220
if err != nil {
219-
errs.Append(errors.Wrap(err, "couldn't make app for "+path))
221+
errs.Append(errors.Wrap(err, "couldn't make target for "+path))
220222
continue
221223
}
222-
resMap, err := app.loadCustomizedResMap()
224+
resMap, err := target.loadCustomizedResMap()
223225
if err != nil {
224226
errs.Append(errors.Wrap(err, "SemiResources"))
225227
continue
@@ -234,21 +236,21 @@ func (a *Application) loadCustomizedBases() (resmap.ResMap, *interror.Kustomizat
234236
return result, errs
235237
}
236238

237-
func (a *Application) loadBasesAsFlatList() ([]*Application, error) {
238-
var result []*Application
239+
func (kt *KustTarget) loadBasesAsFlatList() ([]*KustTarget, error) {
240+
var result []*KustTarget
239241
errs := &interror.KustomizationErrors{}
240-
for _, path := range a.kustomization.Bases {
241-
ldr, err := a.ldr.New(path)
242+
for _, path := range kt.kustomization.Bases {
243+
ldr, err := kt.ldr.New(path)
242244
if err != nil {
243245
errs.Append(err)
244246
continue
245247
}
246-
a, err := NewApplication(ldr, a.fSys, a.tcfg)
248+
target, err := NewKustTarget(ldr, kt.fSys, kt.tcfg)
247249
if err != nil {
248250
errs.Append(err)
249251
continue
250252
}
251-
result = append(result, a)
253+
result = append(result, target)
252254
}
253255
if len(errs.Get()) > 0 {
254256
return nil, errs
@@ -257,35 +259,39 @@ func (a *Application) loadBasesAsFlatList() ([]*Application, error) {
257259
}
258260

259261
// newTransformer makes a Transformer that does everything except resolve generated names.
260-
func (a *Application) newTransformer(patches []*resource.Resource) (transformers.Transformer, error) {
262+
func (kt *KustTarget) newTransformer(patches []*resource.Resource) (transformers.Transformer, error) {
261263
var r []transformers.Transformer
262264
t, err := transformers.NewPatchTransformer(patches)
263265
if err != nil {
264266
return nil, err
265267
}
266268
r = append(r, t)
267-
r = append(r, transformers.NewNamespaceTransformer(string(a.kustomization.Namespace), a.tcfg.NameSpace))
268-
t, err = transformers.NewNamePrefixTransformer(string(a.kustomization.NamePrefix), a.tcfg.NamePrefix)
269+
r = append(r, transformers.NewNamespaceTransformer(
270+
string(kt.kustomization.Namespace), kt.tcfg.NameSpace))
271+
t, err = transformers.NewNamePrefixTransformer(
272+
string(kt.kustomization.NamePrefix), kt.tcfg.NamePrefix)
269273
if err != nil {
270274
return nil, err
271275
}
272276
r = append(r, t)
273-
t, err = transformers.NewLabelsMapTransformer(a.kustomization.CommonLabels, a.tcfg.CommonLabels)
277+
t, err = transformers.NewLabelsMapTransformer(
278+
kt.kustomization.CommonLabels, kt.tcfg.CommonLabels)
274279
if err != nil {
275280
return nil, err
276281
}
277282
r = append(r, t)
278-
t, err = transformers.NewAnnotationsMapTransformer(a.kustomization.CommonAnnotations, a.tcfg.CommonAnnotations)
283+
t, err = transformers.NewAnnotationsMapTransformer(
284+
kt.kustomization.CommonAnnotations, kt.tcfg.CommonAnnotations)
279285
if err != nil {
280286
return nil, err
281287
}
282288
r = append(r, t)
283289
return transformers.NewMultiTransformer(r), nil
284290
}
285291

286-
func (a *Application) resolveRefVars(m resmap.ResMap) (map[string]string, error) {
292+
func (kt *KustTarget) resolveRefVars(m resmap.ResMap) (map[string]string, error) {
287293
result := map[string]string{}
288-
vars, err := a.getAllVars()
294+
vars, err := kt.getAllVars()
289295
if err != nil {
290296
return result, err
291297
}
@@ -306,11 +312,11 @@ func (a *Application) resolveRefVars(m resmap.ResMap) (map[string]string, error)
306312
}
307313

308314
// getAllVars returns all the "environment" style Var instances defined in the app.
309-
func (a *Application) getAllVars() ([]types.Var, error) {
315+
func (kt *KustTarget) getAllVars() ([]types.Var, error) {
310316
var result []types.Var
311317
errs := &interror.KustomizationErrors{}
312318

313-
bases, err := a.loadBasesAsFlatList()
319+
bases, err := kt.loadBasesAsFlatList()
314320
if err != nil {
315321
return nil, err
316322
}
@@ -325,7 +331,7 @@ func (a *Application) getAllVars() ([]types.Var, error) {
325331
b.ldr.Cleanup()
326332
result = append(result, vars...)
327333
}
328-
for _, v := range a.kustomization.Vars {
334+
for _, v := range kt.kustomization.Vars {
329335
v.Defaulting()
330336
result = append(result, v)
331337
}

pkg/app/application_test.go renamed to pkg/target/kusttarget_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package app
17+
package target
1818

1919
import (
2020
"encoding/base64"
@@ -204,11 +204,11 @@ func TestResources1(t *testing.T) {
204204
l := makeLoader1(t)
205205
fakeFs := fs.MakeFakeFS()
206206
fakeFs.Mkdir("/")
207-
app, err := NewApplication(l, fakeFs, transformerconfig.MakeDefaultTransformerConfig())
207+
target, err := NewKustTarget(l, fakeFs, transformerconfig.MakeDefaultTransformerConfig())
208208
if err != nil {
209209
t.Fatalf("Unexpected construction error %v", err)
210210
}
211-
actual, err := app.MakeCustomizedResMap()
211+
actual, err := target.MakeCustomizedResMap()
212212
if err != nil {
213213
t.Fatalf("Unexpected Resources error %v", err)
214214
}
@@ -227,11 +227,11 @@ func TestResourceNotFound(t *testing.T) {
227227
}
228228
fakeFs := fs.MakeFakeFS()
229229
fakeFs.Mkdir("/")
230-
app, err := NewApplication(l, fakeFs, transformerconfig.MakeDefaultTransformerConfig())
230+
target, err := NewKustTarget(l, fakeFs, transformerconfig.MakeDefaultTransformerConfig())
231231
if err != nil {
232232
t.Fatalf("Unexpected construction error %v", err)
233233
}
234-
_, err = app.MakeCustomizedResMap()
234+
_, err = target.MakeCustomizedResMap()
235235
if err == nil {
236236
t.Fatalf("Didn't get the expected error for an unknown resource")
237237
}
@@ -248,11 +248,11 @@ func TestSecretTimeout(t *testing.T) {
248248
}
249249
fakeFs := fs.MakeFakeFS()
250250
fakeFs.Mkdir("/")
251-
app, err := NewApplication(l, fakeFs, transformerconfig.MakeDefaultTransformerConfig())
251+
target, err := NewKustTarget(l, fakeFs, transformerconfig.MakeDefaultTransformerConfig())
252252
if err != nil {
253253
t.Fatalf("Unexpected construction error %v", err)
254254
}
255-
_, err = app.MakeCustomizedResMap()
255+
_, err = target.MakeCustomizedResMap()
256256
if err == nil {
257257
t.Fatalf("Didn't get the expected error for an unknown resource")
258258
}

0 commit comments

Comments
 (0)