Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions util/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,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 @@ -307,7 +307,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 Down Expand Up @@ -343,7 +343,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 @@ -370,10 +370,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 @@ -415,8 +415,8 @@ var (
)

// getSemver returns parsed kustomize version
func getSemver() (*semver.Version, error) {
verStr, err := Version()
func getSemver(k *kustomize) (*semver.Version, error) {
verStr, err := versionWithBinaryPath(k)
if err != nil {
return nil, err
}
Expand All @@ -432,12 +432,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 @@ -448,7 +448,12 @@ func getSemverSafe() *semver.Version {
}

func Version() (string, error) {
cmd := exec.Command("kustomize", "version", "--short")
return versionWithBinaryPath(&kustomize{})
}

func versionWithBinaryPath(k *kustomize) (string, error) {
executable := k.getBinaryPath()
cmd := exec.Command(executable, "version", "--short")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--short has been deprecated, should we move it? It also makes parsing the version way easier now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rumstead I understand your point, but this is outside the scope of this PR. It should be addressed in a separate PR.

// example version output:
// short: "{kustomize/v3.8.1 2020-07-16T00:58:46Z }"
version, err := executil.Run(cmd)
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(&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
Loading