diff --git a/util/kustomize/kustomize.go b/util/kustomize/kustomize.go index 1eb255d758442..c7ca7695d9c0f 100644 --- a/util/kustomize/kustomize.go +++ b/util/kustomize/kustomize.go @@ -83,7 +83,7 @@ func (k *kustomize) getBinaryPath() string { // https://github.com/kubernetes-sigs/kustomize/commit/b214fa7d5aa51d7c2ae306ec15115bf1c044fed8#diff-0328c59bcd29799e365ff0647653b886f17c8853df008cd54e7981db882c1b36 func mapToEditAddArgs(val map[string]string) []string { var args []string - if getSemverSafe().LessThan(semver.MustParse("v3.8.5")) { + if getSemverSafe(&kustomize{}).LessThan(semver.MustParse("v3.8.5")) { arg := "" for labelName, labelValue := range val { if arg != "" { @@ -304,7 +304,7 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp if len(opts.Components) > 0 { // components only supported in kustomize >= v3.7.0 // https://github.com/kubernetes-sigs/kustomize/blob/master/examples/components.md - if getSemverSafe().LessThan(semver.MustParse("v3.7.0")) { + if getSemverSafe(k).LessThan(semver.MustParse("v3.7.0")) { return nil, nil, nil, errors.New("kustomize components require kustomize v3.7.0 and above") } @@ -324,7 +324,7 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp var cmd *exec.Cmd if kustomizeOptions != nil && kustomizeOptions.BuildOptions != "" { - params := parseKustomizeBuildOptions(k.path, kustomizeOptions.BuildOptions, buildOpts) + params := parseKustomizeBuildOptions(k, kustomizeOptions.BuildOptions, buildOpts) cmd = exec.Command(k.getBinaryPath(), params...) } else { cmd = exec.Command(k.getBinaryPath(), "build", k.path) @@ -351,10 +351,10 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp return objs, getImageParameters(objs), redactedCommands, nil } -func parseKustomizeBuildOptions(path string, buildOptions string, buildOpts *BuildOpts) []string { - buildOptsParams := append([]string{"build", path}, strings.Fields(buildOptions)...) +func parseKustomizeBuildOptions(k *kustomize, buildOptions string, buildOpts *BuildOpts) []string { + buildOptsParams := append([]string{"build", k.path}, strings.Fields(buildOptions)...) - if buildOpts != nil && !getSemverSafe().LessThan(semver.MustParse("v5.3.0")) && isHelmEnabled(buildOptions) { + if buildOpts != nil && !getSemverSafe(k).LessThan(semver.MustParse("v5.3.0")) && isHelmEnabled(buildOptions) { if buildOpts.KubeVersion != "" { buildOptsParams = append(buildOptsParams, "--helm-kube-version", buildOpts.KubeVersion) } @@ -396,8 +396,8 @@ var ( ) // getSemver returns parsed kustomize version -func getSemver() (*semver.Version, error) { - verStr, err := Version(true) +func getSemver(k *kustomize) (*semver.Version, error) { + verStr, err := versionWithBinaryPath(true, k) if err != nil { return nil, err } @@ -413,12 +413,12 @@ func getSemver() (*semver.Version, error) { // getSemverSafe returns parsed kustomize version; // if version cannot be parsed assumes that "kustomize version" output format changed again // and fallback to latest ( v99.99.99 ) -func getSemverSafe() *semver.Version { +func getSemverSafe(k *kustomize) *semver.Version { if semVer == nil { semVerLock.Lock() defer semVerLock.Unlock() - if ver, err := getSemver(); err != nil { + if ver, err := getSemver(k); err != nil { semVer = unknownVersion log.Warnf("Failed to parse kustomize version: %v", err) } else { @@ -429,7 +429,11 @@ func getSemverSafe() *semver.Version { } func Version(shortForm bool) (string, error) { - executable := "kustomize" + return versionWithBinaryPath(shortForm, &kustomize{}) +} + +func versionWithBinaryPath(shortForm bool, k *kustomize) (string, error) { + executable := k.getBinaryPath() cmdArgs := []string{"version"} if shortForm { cmdArgs = append(cmdArgs, "--short") diff --git a/util/kustomize/kustomize_test.go b/util/kustomize/kustomize_test.go index a8aac92709436..d1c5b91b42a77 100644 --- a/util/kustomize/kustomize_test.go +++ b/util/kustomize/kustomize_test.go @@ -149,7 +149,7 @@ func TestIsKustomization(t *testing.T) { } func TestParseKustomizeBuildOptions(t *testing.T) { - built := parseKustomizeBuildOptions("guestbook", "-v 6 --logtostderr", &BuildOpts{ + built := parseKustomizeBuildOptions(&kustomize{path: "guestbook"}, "-v 6 --logtostderr", &BuildOpts{ KubeVersion: "1.27", APIVersions: []string{"foo", "bar"}, }) // Helm is not enabled so helm options are not in the params @@ -157,7 +157,7 @@ func TestParseKustomizeBuildOptions(t *testing.T) { } func TestParseKustomizeBuildHelmOptions(t *testing.T) { - built := parseKustomizeBuildOptions("guestbook", "-v 6 --logtostderr --enable-helm", &BuildOpts{ + built := parseKustomizeBuildOptions(&kustomize{path: "guestbook"}, "-v 6 --logtostderr --enable-helm", &BuildOpts{ KubeVersion: "1.27", APIVersions: []string{"foo", "bar"}, }) @@ -175,8 +175,14 @@ func TestVersion(t *testing.T) { assert.NotEmpty(t, ver) } +func TestVersionWithBinaryPath(t *testing.T) { + ver, err := versionWithBinaryPath(false, &kustomize{binaryPath: "kustomize"}) + require.NoError(t, err) + assert.NotEmpty(t, ver) +} + func TestGetSemver(t *testing.T) { - ver, err := getSemver() + ver, err := getSemver(&kustomize{}) require.NoError(t, err) assert.NotEmpty(t, ver) }