Skip to content
Merged
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
4 changes: 1 addition & 3 deletions docs/user-guide/build-environment.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Build Environment

> v1.4

[Custom tools](config-management-plugins.md), [Helm](helm.md), and [Jsonnet](jsonnet.md) support the following build env vars:
[Custom tools](config-management-plugins.md), [Helm](helm.md), [Jsonnet](jsonnet.md), and [Kustomize](kustomize.md) support the following build env vars:

* `ARGOCD_APP_NAME` - name of application
* `ARGOCD_APP_NAMESPACE` - destination application namespace.
Expand Down
15 changes: 11 additions & 4 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,7 @@ func GenerateManifests(ctx context.Context, appPath, repoRoot, revision string,
kustomizeBinary = q.KustomizeOptions.BinaryPath
}
k := kustomize.NewKustomizeApp(appPath, q.Repo.GetGitCreds(), repoURL, kustomizeBinary)
targetObjs, _, err = k.Build(q.ApplicationSource.Kustomize, q.KustomizeOptions)
targetObjs, _, err = k.Build(q.ApplicationSource.Kustomize, q.KustomizeOptions, env)
case v1alpha1.ApplicationSourceTypePlugin:
if q.ApplicationSource.Plugin != nil && q.ApplicationSource.Plugin.Name != "" {
targetObjs, err = runConfigManagementPlugin(appPath, repoRoot, env, q, q.Repo.GetGitCreds())
Expand Down Expand Up @@ -1276,7 +1276,7 @@ func (s *Service) GetAppDetails(ctx context.Context, q *apiclient.RepoServerAppD
return err
}
case v1alpha1.ApplicationSourceTypeKustomize:
if err := populateKustomizeAppDetails(res, q, opContext.appPath); err != nil {
if err := populateKustomizeAppDetails(res, q, opContext.appPath, commitSHA); err != nil {
return err
}
}
Expand Down Expand Up @@ -1423,14 +1423,21 @@ func findHelmValueFilesInPath(path string) ([]string, error) {
return result, nil
}

func populateKustomizeAppDetails(res *apiclient.RepoAppDetailsResponse, q *apiclient.RepoServerAppDetailsQuery, appPath string) error {
func populateKustomizeAppDetails(res *apiclient.RepoAppDetailsResponse, q *apiclient.RepoServerAppDetailsQuery, appPath string, reversion string) error {
res.Kustomize = &apiclient.KustomizeAppSpec{}
kustomizeBinary := ""
if q.KustomizeOptions != nil {
kustomizeBinary = q.KustomizeOptions.BinaryPath
}
k := kustomize.NewKustomizeApp(appPath, q.Repo.GetGitCreds(), q.Repo.Repo, kustomizeBinary)
_, images, err := k.Build(q.Source.Kustomize, q.KustomizeOptions)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think kustomize should receive the same env variables as all other tools. Please use newEnv instead of creating env with one variable.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your review.

Yeah. I know I should use the newEnv defined here. However it needs to pass the apiclient.ManifestRequest as the param, and there is no such variable in this context. I don't want to change to much code at the first contribution. I thought maybe adding the ARGOCD_APP_NAME is also acceptable.
Anyway, I can investigate more if you'd prefer to use the newEnv here also.

Copy link
Copy Markdown
Contributor Author

@wd wd Jan 19, 2022

Choose a reason for hiding this comment

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

It looks like I can construct the ManifestRequest with most of the necessary properties by myself without the Namespace. I don't know where to get the destination application namespace, it would be great if you can give me some ideas

	mq := apiclient.ManifestRequest{
		AppName: q.AppName,
		Namespace: "",
		Repo: q.Repo,
		ApplicationSource: q.Source,
 	}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This approach looks good. I would omit namespace for now. We can add the namespace to API in a separate PR

fakeManifestRequest := apiclient.ManifestRequest{
AppName: q.AppName,
Namespace: "", // FIXME: omit it for now
Repo: q.Repo,
ApplicationSource: q.Source,
}
env := newEnv(&fakeManifestRequest, reversion)
_, images, err := k.Build(q.Source.Kustomize, q.KustomizeOptions, env)
if err != nil {
return err
}
Expand Down
10 changes: 7 additions & 3 deletions util/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Image = 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) ([]*unstructured.Unstructured, []Image, error)
Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOptions *v1alpha1.KustomizeOptions, envVars *v1alpha1.Env) ([]*unstructured.Unstructured, []Image, error)
}

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

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

if opts != nil {
if opts.NamePrefix != "" {
Expand Down Expand Up @@ -155,7 +155,11 @@ func (k *kustomize) Build(opts *v1alpha1.ApplicationSourceKustomize, kustomizeOp
cmd = exec.Command(k.getBinaryPath(), "build", k.path)
}

cmd.Env = os.Environ()
env := os.Environ()
if envVars != nil {
env = append(env, envVars.Environ()...)
}
cmd.Env = env
closer, environ, err := k.creds.Environ()
if err != nil {
return nil, nil, err
Expand Down
33 changes: 30 additions & 3 deletions util/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kustomize
import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
Expand All @@ -18,6 +19,7 @@ const kustomization1 = "kustomization_yaml"
const kustomization2a = "kustomization_yml"
const kustomization2b = "Kustomization"
const kustomization3 = "force_common"
const kustomization4 = "custom_version"

func testDataDir(testData string) (string, error) {
res, err := ioutil.TempDir("", "kustomize-test")
Expand Down Expand Up @@ -50,7 +52,7 @@ func TestKustomizeBuild(t *testing.T) {
"app.kubernetes.io/part-of": "argo-cd-tests",
},
}
objs, images, err := kustomize.Build(&kustomizeSource, nil)
objs, images, err := kustomize.Build(&kustomizeSource, nil, nil)
assert.Nil(t, err)
if err != nil {
assert.Equal(t, len(objs), 2)
Expand Down Expand Up @@ -163,7 +165,7 @@ func TestKustomizeBuildForceCommonLabels(t *testing.T) {
appPath, err := testDataDir(tc.TestData)
assert.Nil(t, err)
kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", "")
objs, _, err := kustomize.Build(&tc.KustomizeSource, nil)
objs, _, err := kustomize.Build(&tc.KustomizeSource, nil, nil)
switch tc.ExpectErr {
case true:
assert.Error(t, err)
Expand Down Expand Up @@ -212,7 +214,7 @@ func TestKustomizeBuildForceCommonAnnotations(t *testing.T) {
appPath, err := testDataDir(tc.TestData)
assert.Nil(t, err)
kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", "")
objs, _, err := kustomize.Build(&tc.KustomizeSource, nil)
objs, _, err := kustomize.Build(&tc.KustomizeSource, nil, nil)
switch tc.ExpectErr {
case true:
assert.Error(t, err)
Expand All @@ -224,3 +226,28 @@ func TestKustomizeBuildForceCommonAnnotations(t *testing.T) {
}
}
}

func TestKustomizeCustomVersion(t *testing.T) {
appPath, err := testDataDir(kustomization1)
assert.Nil(t, err)
kustomizePath, err := testDataDir(kustomization4)
assert.Nil(t, err)
envOutputFile := kustomizePath + "/env_output"
kustomize := NewKustomizeApp(appPath, git.NopCreds{}, "", kustomizePath+"/kustomize.special")
kustomizeSource := v1alpha1.ApplicationSourceKustomize{
Version: "special",
}
env := &v1alpha1.Env{
&v1alpha1.EnvEntry{Name: "ARGOCD_APP_NAME", Value: "argo-cd-tests"},
}
objs, images, err := kustomize.Build(&kustomizeSource, nil, env)
assert.Nil(t, err)
if err != nil {
assert.Equal(t, len(objs), 2)
assert.Equal(t, len(images), 2)
}

content, err := os.ReadFile(envOutputFile)
assert.Nil(t, err)
assert.Equal(t, "ARGOCD_APP_NAME=argo-cd-tests\n", string(content))
}
8 changes: 8 additions & 0 deletions util/kustomize/testdata/custom_version/kustomize.special
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

current_dir=$(dirname $0)
output="$current_dir/env_output"

env | grep "ARGOCD_APP_NAME" > $output

kustomize $@