Skip to content

Commit 1fbc2d7

Browse files
Asuforcechansuke
authored andcommitted
fix(repo-server): When using custom kustomize versions, obtain the correct path (argoproj#21449) (argoproj#21537)
Signed-off-by: asuforce <[email protected]>
1 parent 199183c commit 1fbc2d7

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

util/kustomize/kustomize.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (k *kustomize) getBinaryPath() string {
107107
// https://github.com/kubernetes-sigs/kustomize/commit/b214fa7d5aa51d7c2ae306ec15115bf1c044fed8#diff-0328c59bcd29799e365ff0647653b886f17c8853df008cd54e7981db882c1b36
108108
func mapToEditAddArgs(val map[string]string) []string {
109109
var args []string
110-
if getSemverSafe().LessThan(semver.MustParse("v3.8.5")) {
110+
if getSemverSafe(&kustomize{}).LessThan(semver.MustParse("v3.8.5")) {
111111
arg := ""
112112
for labelName, labelValue := range val {
113113
if arg != "" {
@@ -338,7 +338,7 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
338338
if len(opts.Components) > 0 {
339339
// components only supported in kustomize >= v3.7.0
340340
// https://github.com/kubernetes-sigs/kustomize/blob/master/examples/components.md
341-
if getSemverSafe().LessThan(semver.MustParse("v3.7.0")) {
341+
if getSemverSafe(k).LessThan(semver.MustParse("v3.7.0")) {
342342
return nil, nil, nil, errors.New("kustomize components require kustomize v3.7.0 and above")
343343
}
344344

@@ -374,7 +374,7 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
374374

375375
var cmd *exec.Cmd
376376
if kustomizeOptions != nil && kustomizeOptions.BuildOptions != "" {
377-
params := parseKustomizeBuildOptions(k.path, kustomizeOptions.BuildOptions, buildOpts)
377+
params := parseKustomizeBuildOptions(k, kustomizeOptions.BuildOptions, buildOpts)
378378
cmd = exec.Command(k.getBinaryPath(), params...)
379379
} else {
380380
cmd = exec.Command(k.getBinaryPath(), "build", k.path)
@@ -401,10 +401,10 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
401401
return objs, getImageParameters(objs), redactedCommands, nil
402402
}
403403

404-
func parseKustomizeBuildOptions(path string, buildOptions string, buildOpts *BuildOpts) []string {
405-
buildOptsParams := append([]string{"build", path}, strings.Fields(buildOptions)...)
404+
func parseKustomizeBuildOptions(k *kustomize, buildOptions string, buildOpts *BuildOpts) []string {
405+
buildOptsParams := append([]string{"build", k.path}, strings.Fields(buildOptions)...)
406406

407-
if buildOpts != nil && !getSemverSafe().LessThan(semver.MustParse("v5.3.0")) && isHelmEnabled(buildOptions) {
407+
if buildOpts != nil && !getSemverSafe(k).LessThan(semver.MustParse("v5.3.0")) && isHelmEnabled(buildOptions) {
408408
if buildOpts.KubeVersion != "" {
409409
buildOptsParams = append(buildOptsParams, "--helm-kube-version", buildOpts.KubeVersion)
410410
}
@@ -435,8 +435,8 @@ var (
435435
)
436436

437437
// getSemver returns parsed kustomize version
438-
func getSemver() (*semver.Version, error) {
439-
verStr, err := Version()
438+
func getSemver(k *kustomize) (*semver.Version, error) {
439+
verStr, err := versionWithBinaryPath(k)
440440
if err != nil {
441441
return nil, err
442442
}
@@ -452,12 +452,12 @@ func getSemver() (*semver.Version, error) {
452452
// getSemverSafe returns parsed kustomize version;
453453
// if version cannot be parsed assumes that "kustomize version" output format changed again
454454
// and fallback to latest ( v99.99.99 )
455-
func getSemverSafe() *semver.Version {
455+
func getSemverSafe(k *kustomize) *semver.Version {
456456
if semVer == nil {
457457
semVerLock.Lock()
458458
defer semVerLock.Unlock()
459459

460-
if ver, err := getSemver(); err != nil {
460+
if ver, err := getSemver(k); err != nil {
461461
semVer = unknownVersion
462462
log.Warnf("Failed to parse kustomize version: %v", err)
463463
} else {
@@ -468,7 +468,12 @@ func getSemverSafe() *semver.Version {
468468
}
469469

470470
func Version() (string, error) {
471-
cmd := exec.Command("kustomize", "version", "--short")
471+
return versionWithBinaryPath(&kustomize{})
472+
}
473+
474+
func versionWithBinaryPath(k *kustomize) (string, error) {
475+
executable := k.getBinaryPath()
476+
cmd := exec.Command(executable, "version", "--short")
472477
// example version output:
473478
// short: "{kustomize/v3.8.1 2020-07-16T00:58:46Z }"
474479
version, err := executil.Run(cmd)

util/kustomize/kustomize_test.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,15 @@ func TestIsKustomization(t *testing.T) {
148148
}
149149

150150
func TestParseKustomizeBuildOptions(t *testing.T) {
151-
built := parseKustomizeBuildOptions("guestbook", "-v 6 --logtostderr", &BuildOpts{
151+
built := parseKustomizeBuildOptions(&kustomize{path: "guestbook"}, "-v 6 --logtostderr", &BuildOpts{
152152
KubeVersion: "1.27", APIVersions: []string{"foo", "bar"},
153153
})
154154
// Helm is not enabled so helm options are not in the params
155155
assert.Equal(t, []string{"build", "guestbook", "-v", "6", "--logtostderr"}, built)
156156
}
157157

158158
func TestParseKustomizeBuildHelmOptions(t *testing.T) {
159-
built := parseKustomizeBuildOptions("guestbook", "-v 6 --logtostderr --enable-helm", &BuildOpts{
159+
built := parseKustomizeBuildOptions(&kustomize{path: "guestbook"}, "-v 6 --logtostderr --enable-helm", &BuildOpts{
160160
KubeVersion: "1.27",
161161
APIVersions: []string{"foo", "bar"},
162162
})
@@ -174,8 +174,14 @@ func TestVersion(t *testing.T) {
174174
assert.NotEmpty(t, ver)
175175
}
176176

177+
func TestVersionWithBinaryPath(t *testing.T) {
178+
ver, err := versionWithBinaryPath(&kustomize{binaryPath: "kustomize"})
179+
require.NoError(t, err)
180+
assert.NotEmpty(t, ver)
181+
}
182+
177183
func TestGetSemver(t *testing.T) {
178-
ver, err := getSemver()
184+
ver, err := getSemver(&kustomize{})
179185
require.NoError(t, err)
180186
assert.NotEmpty(t, ver)
181187
}

0 commit comments

Comments
 (0)