Skip to content

Commit

Permalink
feat(kustomize): add support for new args to pass api and kube versions
Browse files Browse the repository at this point in the history
Signed-off-by: Arthur Outhenin-Chalandre <arthur.outhenin-chalandre@ledger.fr>
  • Loading branch information
MrFreezeex committed Feb 7, 2024
1 parent 2857f6f commit 0b9021f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 19 deletions.
7 changes: 5 additions & 2 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,10 @@ func GenerateManifests(ctx context.Context, appPath, repoRoot, revision string,
kustomizeBinary = q.KustomizeOptions.BinaryPath
}
k := kustomize.NewKustomizeApp(repoRoot, appPath, q.Repo.GetGitCreds(gitCredsStore), repoURL, kustomizeBinary)
targetObjs, _, err = k.Build(q.ApplicationSource.Kustomize, q.KustomizeOptions, env)
targetObjs, _, err = k.Build(q.ApplicationSource.Kustomize, q.KustomizeOptions, env, &kustomize.BuildOpts{
KubeVersion: q.KubeVersion,
APIVersions: q.ApiVersions,
})
case v1alpha1.ApplicationSourceTypePlugin:
pluginName := ""
if q.ApplicationSource.Plugin != nil {
Expand Down Expand Up @@ -2131,7 +2134,7 @@ func populateKustomizeAppDetails(res *apiclient.RepoAppDetailsResponse, q *apicl
ApplicationSource: q.Source,
}
env := newEnv(&fakeManifestRequest, reversion)
_, images, err := k.Build(q.Source.Kustomize, q.KustomizeOptions, env)
_, images, err := k.Build(q.Source.Kustomize, q.KustomizeOptions, env, nil)
if err != nil {
return err
}
Expand Down
40 changes: 30 additions & 10 deletions util/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,15 @@ import (
// represents a Docker image in the format NAME[:TAG].
type Image = string

type BuildOpts struct {
KubeVersion string
APIVersions []string
}

// Kustomize provides wrapper functionality around the `kustomize` command.
type Kustomize interface {
// Build returns a list of unstructured objects from a `kustomize build` command and extract supported parameters
Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOptions *v1alpha1.KustomizeOptions, envVars *v1alpha1.Env) ([]*unstructured.Unstructured, []Image, error)
Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOptions *v1alpha1.KustomizeOptions, envVars *v1alpha1.Env, buildOpts *BuildOpts) ([]*unstructured.Unstructured, []Image, error)
}

// NewKustomizeApp create a new wrapper to run commands on the `kustomize` command-line tool.
Expand Down Expand Up @@ -88,7 +93,7 @@ func mapToEditAddArgs(val map[string]string) []string {
return args
}

func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOptions *v1alpha1.KustomizeOptions, envVars *v1alpha1.Env) ([]*unstructured.Unstructured, []Image, error) {
func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOptions *v1alpha1.KustomizeOptions, envVars *v1alpha1.Env, buildOpts *BuildOpts) ([]*unstructured.Unstructured, []Image, error) {

env := os.Environ()
if envVars != nil {
Expand Down Expand Up @@ -296,13 +301,11 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
}
}

var cmd *exec.Cmd
if kustomizeOptions != nil && kustomizeOptions.BuildOptions != "" {
params := parseKustomizeBuildOptions(k.path, kustomizeOptions.BuildOptions)
cmd = exec.Command(k.getBinaryPath(), params...)
} else {
cmd = exec.Command(k.getBinaryPath(), "build", k.path)
buildOptions := ""
if kustomizeOptions != nil {
buildOptions = kustomizeOptions.BuildOptions
}
cmd := exec.Command(k.getBinaryPath(), parseKustomizeBuildOptions(k.path, buildOptions, buildOpts)...)
cmd.Env = env
cmd.Dir = k.repoRoot
out, err := executil.Run(cmd)
Expand All @@ -318,8 +321,25 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
return objs, getImageParameters(objs), nil
}

func parseKustomizeBuildOptions(path, buildOptions string) []string {
return append([]string{"build", path}, strings.Fields(buildOptions)...)
func parseKustomizeBuildOptions(path string, buildOptions string, buildOpts *BuildOpts) []string {
buildOptsParams := []string{"build", path}
if buildOpts != nil && !getSemverSafe().LessThan(semver.MustParse("v5.3.0")) && isHelmEnabled(buildOptions) {
if buildOpts.KubeVersion != "" {
buildOptsParams = append(buildOptsParams, "--helm-kube-version", buildOpts.KubeVersion)
}
for _, v := range buildOpts.APIVersions {
buildOptsParams = append(buildOptsParams, "--helm-api-versions", v)
}
}
if buildOptions != "" {
buildOptsParams = append(buildOptsParams, strings.Split(buildOptions, " ")...)
}

return buildOptsParams
}

func isHelmEnabled(buildOptions string) bool {
return strings.Contains(buildOptions, "--enable-helm")
}

var KustomizationNames = []string{"kustomization.yaml", "kustomization.yml", "Kustomization"}
Expand Down
37 changes: 30 additions & 7 deletions util/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ func TestKustomizeBuild(t *testing.T) {
},
},
}
objs, images, err := kustomize.Build(&kustomizeSource, nil, env)
objs, images, err := kustomize.Build(&kustomizeSource, nil, env, &BuildOpts{
KubeVersion: "1.27", APIVersions: []string{"foo", "bar"},
})
assert.Nil(t, err)
if err != nil {
assert.Equal(t, len(objs), 2)
Expand Down Expand Up @@ -132,7 +134,7 @@ func TestFailKustomizeBuild(t *testing.T) {
},
},
}
_, _, err = kustomize.Build(&kustomizeSource, nil, nil)
_, _, err = kustomize.Build(&kustomizeSource, nil, nil, nil)
assert.EqualError(t, err, "expected integer value for count. Received: garbage")
}

Expand All @@ -156,10 +158,26 @@ func TestIsKustomization(t *testing.T) {
}

func TestParseKustomizeBuildOptions(t *testing.T) {
built := parseKustomizeBuildOptions("guestbook", "-v 6 --logtostderr")
built := parseKustomizeBuildOptions("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{
KubeVersion: "1.27",
APIVersions: []string{"foo", "bar"},
})
assert.Equal(t, []string{
"build", "guestbook",
"--helm-kube-version", "1.27",
"--helm-api-versions", "foo", "--helm-api-versions", "bar",
"-v", "6", "--logtostderr", "--enable-helm",
}, built)
}

func TestVersion(t *testing.T) {
ver, err := Version(false)
assert.NoError(t, err)
Expand Down Expand Up @@ -223,7 +241,7 @@ func TestKustomizeBuildForceCommonLabels(t *testing.T) {
appPath, err := testDataDir(t, tc.TestData)
assert.Nil(t, err)
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "")
objs, _, err := kustomize.Build(&tc.KustomizeSource, nil, tc.Env)
objs, _, err := kustomize.Build(&tc.KustomizeSource, nil, tc.Env, nil)
switch tc.ExpectErr {
case true:
assert.Error(t, err)
Expand Down Expand Up @@ -314,8 +332,13 @@ func TestKustomizeBuildForceCommonAnnotations(t *testing.T) {
for _, tc := range testCases {
appPath, err := testDataDir(t, tc.TestData)
assert.Nil(t, err)
<<<<<<< HEAD
kustomize := NewKustomizeApp(appPath, appPath, git.NopCreds{}, "", "")
objs, _, err := kustomize.Build(&tc.KustomizeSource, nil, tc.Env)
=======
kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", "")
objs, _, err := kustomize.Build(&tc.KustomizeSource, nil, tc.Env, nil)
>>>>>>> e84510b26 (feat(kustomize): add support for new args to pass api and kube versions)
switch tc.ExpectErr {
case true:
assert.Error(t, err)
Expand All @@ -341,7 +364,7 @@ func TestKustomizeCustomVersion(t *testing.T) {
env := &v1alpha1.Env{
&v1alpha1.EnvEntry{Name: "ARGOCD_APP_NAME", Value: "argo-cd-tests"},
}
objs, images, err := kustomize.Build(&kustomizeSource, nil, env)
objs, images, err := kustomize.Build(&kustomizeSource, nil, env, nil)
assert.Nil(t, err)
if err != nil {
assert.Equal(t, len(objs), 2)
Expand All @@ -361,7 +384,7 @@ func TestKustomizeBuildComponents(t *testing.T) {
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
Components: []string{"./components"},
}
objs, _, err := kustomize.Build(&kustomizeSource, nil, nil)
objs, _, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
assert.Nil(t, err)
obj := objs[0]
assert.Equal(t, "nginx-deployment", obj.GetName())
Expand Down Expand Up @@ -394,7 +417,7 @@ func TestKustomizeBuildPatches(t *testing.T) {
},
},
}
objs, _, err := kustomize.Build(&kustomizeSource, nil, nil)
objs, _, err := kustomize.Build(&kustomizeSource, nil, nil, nil)
assert.Nil(t, err)
obj := objs[0]
containers, found, err := unstructured.NestedSlice(obj.Object, "spec", "template", "spec", "containers")
Expand Down

0 comments on commit 0b9021f

Please sign in to comment.