Skip to content

Commit 0aeb284

Browse files
MarlonGameztejal29
andauthored
feat: allow specifying args to be passed to kustomize build (#7414)
* feat: allow specifying args to be passed to kustomize build * chore: fix references * fix: make upgrade work properly * feat: allow specifying args to be passed to kustomize build * chore: fix references * fix: make upgrade work properly * fix: integration test examples Co-authored-by: tejal29 <[email protected]>
1 parent d161b4d commit 0aeb284

File tree

8 files changed

+109
-41
lines changed

8 files changed

+109
-41
lines changed

integration/testdata/debug/skaffold.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ manifests:
2929
profiles:
3030
- name: kustomize
3131
patches:
32+
- op: remove
33+
path: /manifests/rawYaml
3234
manifests:
3335
kustomize:
36+
paths:
3437
- "."
3538
# use GCP Buildpacks to build the individual projects
3639
- name: buildpacks
@@ -62,6 +65,8 @@ profiles:
6265
# builder: "gcr.io/buildpacks/builder:v1"
6366
- name: docker
6467
patches:
68+
- op: remove
69+
path: /manifests/rawYaml
6570
build:
6671
artifacts:
6772
- image: skaffold-debug-nodejs

pkg/skaffold/render/generate/generate.go

+64-27
Original file line numberDiff line numberDiff line change
@@ -91,34 +91,18 @@ func (g Generator) Generate(ctx context.Context, out io.Writer) (manifest.Manife
9191
var manifests manifest.ManifestList
9292

9393
// Generate kustomize Manifests
94-
_, endTrace := instrumentation.StartTrace(ctx, "Render_expandGlobKustomizeManifests")
95-
kustomizePaths, err := resolveRemoteAndLocal(g.config.Kustomize, g.workingDir)
96-
if err != nil {
97-
event.DeployInfoEvent(fmt.Errorf("could not expand the glob kustomize manifests: %w", err))
98-
return nil, err
99-
}
100-
endTrace()
101-
kustomizePathMap := make(map[string]bool)
102-
for _, path := range kustomizePaths {
103-
if dir, ok := isKustomizeDir(path); ok {
104-
kustomizePathMap[dir] = true
105-
}
106-
}
107-
for kPath := range kustomizePathMap {
108-
// TODO: kustomize kpt-fn not available yet. See https://github.com/GoogleContainerTools/kpt/issues/1447
109-
cmd := exec.CommandContext(ctx, "kustomize", "build", kPath)
110-
out, err := util.RunCmdOut(ctx, cmd)
94+
if g.config.Kustomize != nil && len(g.config.Kustomize.Paths) != 0 {
95+
kustomizeManifests, err := g.generateKustomizeManifests(ctx)
11196
if err != nil {
11297
return nil, err
11398
}
114-
if len(out) == 0 {
115-
continue
99+
for _, m := range kustomizeManifests {
100+
manifests.Append(m)
116101
}
117-
manifests.Append(out)
118102
}
119103

120104
// Generate in-place hydrated kpt Manifests
121-
_, endTrace = instrumentation.StartTrace(ctx, "Render_expandGlobKptManifests")
105+
_, endTrace := instrumentation.StartTrace(ctx, "Render_expandGlobKptManifests")
122106
kptPaths, err := resolveRemoteAndLocal(g.config.Kpt, g.workingDir)
123107
if err != nil {
124108
event.DeployInfoEvent(fmt.Errorf("could not expand the glob kpt manifests: %w", err))
@@ -174,6 +158,38 @@ func (g Generator) Generate(ctx context.Context, out io.Writer) (manifest.Manife
174158
return manifests, nil
175159
}
176160

161+
func (g Generator) generateKustomizeManifests(ctx context.Context) ([][]byte, error) {
162+
var manifests [][]byte
163+
164+
_, endTrace := instrumentation.StartTrace(ctx, "Render_expandGlobKustomizeManifests")
165+
kustomizePaths, err := resolveRemoteAndLocal(g.config.Kustomize.Paths, g.workingDir)
166+
if err != nil {
167+
event.DeployInfoEvent(fmt.Errorf("could not expand the glob kustomize manifests: %w", err))
168+
return nil, err
169+
}
170+
endTrace()
171+
kustomizePathMap := make(map[string]bool)
172+
for _, path := range kustomizePaths {
173+
if dir, ok := isKustomizeDir(path); ok {
174+
kustomizePathMap[dir] = true
175+
}
176+
}
177+
for kPath := range kustomizePathMap {
178+
// TODO: kustomize kpt-fn not available yet. See https://github.com/GoogleContainerTools/kpt/issues/1447
179+
cmd := exec.CommandContext(ctx, "kustomize", append([]string{"build"}, kustomizeBuildArgs(g.config.Kustomize.BuildArgs, kPath)...)...)
180+
out, err := util.RunCmdOut(ctx, cmd)
181+
if err != nil {
182+
return nil, err
183+
}
184+
if len(out) == 0 {
185+
continue
186+
}
187+
manifests = append(manifests, out)
188+
}
189+
190+
return manifests, nil
191+
}
192+
177193
// isKustomizeDir checks if the path is managed by kustomize. A more reliable approach is parsing the kustomize content
178194
// resources, bases, overlays. However, this switches the manifests parsing from kustomize/kpt to skaffold. To avoid
179195
// skaffold render.generate mis-use, we expect the users do not place non-kustomize manifests under the kustomization.yaml directory, so as the kpt manifests.
@@ -200,6 +216,24 @@ func isKustomizeDir(path string) (string, bool) {
200216
return "", false
201217
}
202218

219+
// kustomizeBuildArgs returns a list of build args to be passed to kustomize build.
220+
func kustomizeBuildArgs(buildArgs []string, kustomizePath string) []string {
221+
var args []string
222+
223+
if len(buildArgs) > 0 {
224+
for _, v := range buildArgs {
225+
parts := strings.Split(v, " ")
226+
args = append(args, parts...)
227+
}
228+
}
229+
230+
if len(kustomizePath) > 0 {
231+
args = append(args, kustomizePath)
232+
}
233+
234+
return args
235+
}
236+
203237
func isKptDir(path string) (string, bool) {
204238
fileInfo, err := os.Stat(path)
205239
if err != nil {
@@ -220,16 +254,19 @@ func isKptDir(path string) (string, bool) {
220254

221255
// walkManifests finds out all the manifests from the `.manifests.generate`, so they can be registered in the file watcher.
222256
// Note: the logic about manifest dependencies shall separate from the "Generate" function, which requires "context" and
223-
// only be called when a renderig action is needed (normally happens after the file watcher registration).
257+
// only be called when a rendering action is needed (normally happens after the file watcher registration).
224258
func (g Generator) walkManifests() ([]string, error) {
225259
var dependencyPaths []string
260+
226261
// Generate kustomize Manifests
227-
kustomizePaths, err := resolveRemoteAndLocal(g.config.Kustomize, g.workingDir)
228-
if err != nil {
229-
event.DeployInfoEvent(fmt.Errorf("could not expand the glob kustomize manifests: %w", err))
230-
return nil, err
262+
if g.config.Kustomize != nil {
263+
kustomizePaths, err := resolveRemoteAndLocal(g.config.Kustomize.Paths, g.workingDir)
264+
if err != nil {
265+
event.DeployInfoEvent(fmt.Errorf("could not expand the glob kustomize manifests: %w", err))
266+
return nil, err
267+
}
268+
dependencyPaths = append(dependencyPaths, kustomizePaths...)
231269
}
232-
dependencyPaths = append(dependencyPaths, kustomizePaths...)
233270

234271
// Generate in-place hydrated kpt Manifests
235272
kptPaths, err := resolveRemoteAndLocal(g.config.Kpt, g.workingDir)

pkg/skaffold/render/generate/generate_test.go

+8-4
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ func TestManifestDeps(t *testing.T) {
201201
{
202202
description: "kustomize dir",
203203
generateConfig: latest.Generate{
204-
Kustomize: []string{"kustomize-sample"},
204+
Kustomize: &latest.Kustomize{
205+
Paths: []string{"kustomize-sample"},
206+
},
205207
},
206208
expected: []string{"kustomize-sample/kustomization.yaml", "kustomize-sample/patch.yaml"},
207209
},
@@ -215,9 +217,11 @@ func TestManifestDeps(t *testing.T) {
215217
{
216218
description: "multi manifest, mixed dir and file",
217219
generateConfig: latest.Generate{
218-
RawK8s: []string{"rawYaml-sample"},
219-
Kustomize: []string{"kustomize-sample"},
220-
Kpt: []string{"kpt-sample"},
220+
RawK8s: []string{"rawYaml-sample"},
221+
Kustomize: &latest.Kustomize{
222+
Paths: []string{"kustomize-sample"},
223+
},
224+
Kpt: []string{"kpt-sample"},
221225
},
222226
expected: []string{
223227
"kpt-sample/Kptfile",

pkg/skaffold/schema/defaults/defaults.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ func SetDefaultRenderer(c *latest.SkaffoldConfig) {
121121
if len(c.Render.Generate.RawK8s) > 0 {
122122
return
123123
}
124-
if len(c.Render.Generate.Kustomize) > 0 {
124+
if c.Render.Generate.Kustomize != nil {
125125
return
126126
}
127127
if c.Render.Generate.Helm != nil {

pkg/skaffold/schema/defaults/defaults_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,12 @@ func TestSetDefaultRenderer(t *testing.T) {
450450
description: "kustomize manifests",
451451
input: latest.RenderConfig{
452452
Generate: latest.Generate{
453-
Kustomize: []string{"/kmanifests"},
453+
Kustomize: &latest.Kustomize{Paths: []string{"/kmanifests"}},
454454
},
455455
},
456456
expected: latest.RenderConfig{
457457
Generate: latest.Generate{
458-
Kustomize: []string{"/kmanifests"},
458+
Kustomize: &latest.Kustomize{Paths: []string{"/kmanifests"}},
459459
},
460460
},
461461
},
@@ -490,14 +490,14 @@ func TestSetDefaultRenderer(t *testing.T) {
490490
input: latest.RenderConfig{
491491
Generate: latest.Generate{
492492
Kpt: []string{"/kmanifests1"},
493-
Kustomize: []string{"/kmanifests2"},
493+
Kustomize: &latest.Kustomize{Paths: []string{"/kmanifests2"}},
494494
Helm: &latest.Helm{Releases: []latest.HelmRelease{{Name: "test"}}},
495495
},
496496
},
497497
expected: latest.RenderConfig{
498498
Generate: latest.Generate{
499499
Kpt: []string{"/kmanifests1"},
500-
Kustomize: []string{"/kmanifests2"},
500+
Kustomize: &latest.Kustomize{Paths: []string{"/kmanifests2"}},
501501
Helm: &latest.Helm{Releases: []latest.HelmRelease{{Name: "test"}}},
502502
},
503503
},

pkg/skaffold/schema/latest/config.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -569,8 +569,9 @@ type Generate struct {
569569
// RawK8s TODO: add description.
570570
RawK8s []string `yaml:"rawYaml,omitempty" skaffold:"filepath"`
571571

572-
// Kustomize TODO: add description.
573-
Kustomize []string `yaml:"kustomize,omitempty" skaffold:"filepath"`
572+
// Kustomize defines the paths to be modified with kustomize, along with extra
573+
// flags to be passed to kustomize
574+
Kustomize *Kustomize `yaml:"kustomize,omitempty"`
574575

575576
// Helm TODO: add description.
576577
Helm *Helm `yaml:"helm,omitempty"`
@@ -579,6 +580,17 @@ type Generate struct {
579580
Kpt []string `yaml:"kpt,omitempty" skaffold:"filepath"`
580581
}
581582

583+
// Kustomize defines the paths to be modified with kustomize, along with
584+
// extra flags to be passed to kustomize
585+
type Kustomize struct {
586+
// Paths is the path to Kustomization files.
587+
// Defaults to `["."]`.
588+
Paths []string `yaml:"paths,omitempty" skaffold:"filepath"`
589+
590+
// BuildArgs are additional args passed to `kustomize build`.
591+
BuildArgs []string `yaml:"buildArgs,omitempty"`
592+
}
593+
582594
// Helm defines the manifests from helm releases.
583595
type Helm struct {
584596
// Flags are additional option flags that are passed on the command

pkg/skaffold/schema/v2beta28/upgrade.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,15 @@ func upgradeOnePipeline(oldPipeline, newPipeline interface{}) error {
4848
newPL.Deploy.KubectlDeploy.Manifests = nil
4949
}
5050

51-
// TODO(marlongamez): port over buildArgs config and copy that
5251
// Copy kustomize deploy config to render config
5352
if oldPL.Deploy.KustomizeDeploy != nil {
54-
newPL.Render.Kustomize = oldPL.Deploy.KustomizeDeploy.KustomizePaths
53+
newPL.Render.Kustomize = &next.Kustomize{
54+
Paths: oldPL.Deploy.KustomizeDeploy.KustomizePaths,
55+
BuildArgs: oldPL.Deploy.KustomizeDeploy.BuildArgs,
56+
}
57+
if len(newPL.Render.Kustomize.Paths) == 0 {
58+
newPL.Render.Kustomize.Paths = append(newPL.Render.Kustomize.Paths, ".")
59+
}
5560
// nil out kustomize deployer as it shouldn't be a thing anymore
5661
newPL.Deploy.KustomizeDeploy = nil
5762

pkg/skaffold/schema/v2beta28/upgrade_test.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ manifests:
149149
rawYaml:
150150
- k8s-*
151151
kustomize:
152+
paths:
152153
- kustomization-main
153154
helm:
154155
releases:
@@ -181,7 +182,8 @@ profiles:
181182
rawYaml:
182183
- k8s-*
183184
kustomize:
184-
- kustomization-test
185+
paths:
186+
- kustomization-test
185187
deploy:
186188
kubectl: {}
187189
- name: test local
@@ -195,6 +197,9 @@ profiles:
195197
manifests:
196198
rawYaml:
197199
- k8s-*
200+
kustomize:
201+
paths:
202+
- "."
198203
deploy:
199204
kubectl: {}
200205
`

0 commit comments

Comments
 (0)