Skip to content

Commit caea6b7

Browse files
authored
Merge pull request #2870 from daddz/kustomize-flags-part-1
kustomize build args part 1
2 parents 1db0319 + e153e66 commit caea6b7

File tree

2 files changed

+86
-1
lines changed

2 files changed

+86
-1
lines changed

pkg/skaffold/deploy/kustomize.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"os"
2525
"os/exec"
2626
"path/filepath"
27+
"strings"
2728

2829
"github.com/pkg/errors"
2930
yaml "gopkg.in/yaml.v2"
@@ -71,6 +72,7 @@ type KustomizeDeployer struct {
7172
kubectl deploy.CLI
7273
defaultRepo string
7374
insecureRegistries map[string]bool
75+
BuildArgs []string
7476
}
7577

7678
func NewKustomizeDeployer(runCtx *runcontext.RunContext) *KustomizeDeployer {
@@ -248,7 +250,7 @@ func pathExistsLocally(filename string, workingDir string) (bool, os.FileMode) {
248250
}
249251

250252
func (k *KustomizeDeployer) readManifests(ctx context.Context) (deploy.ManifestList, error) {
251-
cmd := exec.CommandContext(ctx, "kustomize", "build", k.KustomizePath)
253+
cmd := exec.CommandContext(ctx, "kustomize", buildCommandArgs(k.BuildArgs, k.KustomizePath)...)
252254
out, err := util.RunCmdOut(cmd)
253255
if err != nil {
254256
return nil, errors.Wrap(err, "kustomize build")
@@ -262,3 +264,21 @@ func (k *KustomizeDeployer) readManifests(ctx context.Context) (deploy.ManifestL
262264
manifests.Append(out)
263265
return manifests, nil
264266
}
267+
268+
func buildCommandArgs(buildArgs []string, kustomizePath string) []string {
269+
var args []string
270+
args = append(args, "build")
271+
272+
if len(buildArgs) > 0 {
273+
for _, v := range buildArgs {
274+
parts := strings.Split(v, " ")
275+
args = append(args, parts...)
276+
}
277+
}
278+
279+
if len(kustomizePath) > 0 {
280+
args = append(args, kustomizePath)
281+
}
282+
283+
return args
284+
}

pkg/skaffold/deploy/kustomize_test.go

+65
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,68 @@ func TestDependenciesForKustomization(t *testing.T) {
328328
})
329329
}
330330
}
331+
332+
func TestKustomizeBuildCommandArgs(t *testing.T) {
333+
tests := []struct {
334+
description string
335+
buildArgs []string
336+
kustomizePath string
337+
expectedArgs []string
338+
}{
339+
{
340+
description: "no BuildArgs, empty KustomizePath ",
341+
buildArgs: []string{},
342+
kustomizePath: "",
343+
expectedArgs: []string{"build"},
344+
},
345+
{
346+
description: "One BuildArg, empty KustomizePath",
347+
buildArgs: []string{"--foo"},
348+
kustomizePath: "",
349+
expectedArgs: []string{"build", "--foo"},
350+
},
351+
{
352+
description: "no BuildArgs, non-empty KustomizePath",
353+
buildArgs: []string{},
354+
kustomizePath: "foo",
355+
expectedArgs: []string{"build", "foo"},
356+
},
357+
{
358+
description: "One BuildArg, non-empty KustomizePath",
359+
buildArgs: []string{"--foo"},
360+
kustomizePath: "bar",
361+
expectedArgs: []string{"build", "--foo", "bar"},
362+
},
363+
{
364+
description: "Multiple BuildArg, empty KustomizePath",
365+
buildArgs: []string{"--foo", "--bar"},
366+
kustomizePath: "",
367+
expectedArgs: []string{"build", "--foo", "--bar"},
368+
},
369+
{
370+
description: "Multiple BuildArg with spaces, empty KustomizePath",
371+
buildArgs: []string{"--foo bar", "--baz"},
372+
kustomizePath: "",
373+
expectedArgs: []string{"build", "--foo", "bar", "--baz"},
374+
},
375+
{
376+
description: "Multiple BuildArg with spaces, non-empty KustomizePath",
377+
buildArgs: []string{"--foo bar", "--baz"},
378+
kustomizePath: "barfoo",
379+
expectedArgs: []string{"build", "--foo", "bar", "--baz", "barfoo"},
380+
},
381+
{
382+
description: "Multiple BuildArg no spaces, non-empty KustomizePath",
383+
buildArgs: []string{"--foo", "bar", "--baz"},
384+
kustomizePath: "barfoo",
385+
expectedArgs: []string{"build", "--foo", "bar", "--baz", "barfoo"},
386+
},
387+
}
388+
389+
for _, test := range tests {
390+
testutil.Run(t, test.description, func(t *testutil.T) {
391+
args := buildCommandArgs(test.buildArgs, test.kustomizePath)
392+
t.CheckDeepEqual(test.expectedArgs, args)
393+
})
394+
}
395+
}

0 commit comments

Comments
 (0)