Skip to content

Commit

Permalink
fix: When using custom kustomize versions, obtain the correct path
Browse files Browse the repository at this point in the history
  • Loading branch information
Asuforce committed Jan 17, 2025
1 parent 37a7231 commit c489fed
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
26 changes: 15 additions & 11 deletions util/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 != "" {
Expand Down Expand Up @@ -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")
}

Expand All @@ -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)
Expand All @@ -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)
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand All @@ -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")
Expand Down
12 changes: 9 additions & 3 deletions util/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ 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
assert.Equal(t, []string{"build", "guestbook", "-v", "6", "--logtostderr"}, built)
}

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"},
})
Expand All @@ -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)
}
Expand Down

0 comments on commit c489fed

Please sign in to comment.