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 Dec 8, 2023
1 parent ce73982 commit afc1346
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
7 changes: 5 additions & 2 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1374,7 +1374,10 @@ func GenerateManifests(ctx context.Context, appPath, repoRoot, revision string,
kustomizeBinary = q.KustomizeOptions.BinaryPath
}
k := kustomize.NewKustomizeApp(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 @@ -2115,7 +2118,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
26 changes: 20 additions & 6 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 @@ -85,7 +90,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 @@ -293,13 +298,22 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
}
}

var cmd *exec.Cmd
buildOptsParams := []string{}
if buildOpts != nil {
if buildOpts.KubeVersion != "" {
buildOptsParams = append(buildOptsParams, "--helm-kube-version", buildOpts.KubeVersion)
}
for _, v := range buildOpts.APIVersions {
buildOptsParams = append(buildOptsParams, "--helm-api-versions", v)
}
}
if kustomizeOptions != nil && kustomizeOptions.BuildOptions != "" {
params := parseKustomizeBuildOptions(k.path, kustomizeOptions.BuildOptions)
cmd = exec.Command(k.getBinaryPath(), params...)
buildOptsParams = append(buildOptsParams, parseKustomizeBuildOptions(k.path, kustomizeOptions.BuildOptions)...)
} else {
cmd = exec.Command(k.getBinaryPath(), "build", k.path)
buildOptsParams = append(buildOptsParams, k.path)
}

cmd := exec.Command(k.getBinaryPath(), buildOptsParams...)
cmd.Env = env
out, err := executil.Run(cmd)
if err != nil {
Expand Down
17 changes: 10 additions & 7 deletions util/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ 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 +135,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 Down Expand Up @@ -223,7 +226,7 @@ func TestKustomizeBuildForceCommonLabels(t *testing.T) {
appPath, err := testDataDir(t, tc.TestData)
assert.Nil(t, err)
kustomize := NewKustomizeApp(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 @@ -315,7 +318,7 @@ func TestKustomizeBuildForceCommonAnnotations(t *testing.T) {
appPath, err := testDataDir(t, tc.TestData)
assert.Nil(t, err)
kustomize := NewKustomizeApp(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 All @@ -341,7 +344,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 +364,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 +397,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 afc1346

Please sign in to comment.