Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(repo-server): When using custom kustomize versions, obtain the correct path (#21449) #21537

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
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
Loading