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: do not interpolate env variables in helm template options when forceString is true #20002

Closed
wants to merge 3 commits into from
Closed
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
3 changes: 0 additions & 3 deletions reposerver/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -1195,9 +1195,6 @@ func helmTemplate(appPath string, repoRoot string, env *v1alpha1.Env, q *apiclie
for i, j := range templateOpts.Set {
templateOpts.Set[i] = env.Envsubst(j)
}
for i, j := range templateOpts.SetString {
templateOpts.SetString[i] = env.Envsubst(j)
}

var proxy string
if q.Repo != nil {
Expand Down
142 changes: 97 additions & 45 deletions reposerver/repository/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4135,62 +4135,114 @@ func Test_GenerateManifests_Commands(t *testing.T) {
service := newService(t, "testdata/my-chart")

// Fill the manifest request with as many parameters affecting Helm commands as possible.
q := apiclient.ManifestRequest{
AppName: "test-app",
Namespace: "test-namespace",
KubeVersion: "1.2.3",
ApiVersions: []string{"v1/Test", "v2/Test"},
Repo: &argoappv1.Repository{},
ApplicationSource: &argoappv1.ApplicationSource{
Path: ".",
Helm: &argoappv1.ApplicationSourceHelm{
FileParameters: []argoappv1.HelmFileParameter{
{
Name: "test-file-param-name",
Path: "test-file-param.yaml",

t.Run("default parameters", func(t *testing.T) {
q := apiclient.ManifestRequest{
AppName: "test-app",
Namespace: "test-namespace",
KubeVersion: "1.2.3",
ApiVersions: []string{"v1/Test", "v2/Test"},
Repo: &argoappv1.Repository{},
ApplicationSource: &argoappv1.ApplicationSource{
Path: ".",
Helm: &argoappv1.ApplicationSourceHelm{
FileParameters: []argoappv1.HelmFileParameter{
{
Name: "test-file-param-name",
Path: "test-file-param.yaml",
},
},
},
Parameters: []argoappv1.HelmParameter{
{
Name: "test-param-name",
// Use build env var to test substitution.
Value: "test-value-$ARGOCD_APP_NAME",
ForceString: true,
Parameters: []argoappv1.HelmParameter{
{
Name: "test-param-forcestring-name",
// Use build env var to test substitution.
Value: "test-value-$ARGOCD_APP_NAME",
ForceString: true,
},
{
Name: "test-param-bool-name",
// Use build env var to test substitution.
Value: "false",
},
},
{
Name: "test-param-bool-name",
// Use build env var to test substitution.
Value: "false",
PassCredentials: true,
SkipCrds: true,
ValueFiles: []string{
"my-chart-values.yaml",
},
Values: "test: values",
},
PassCredentials: true,
SkipCrds: true,
ValueFiles: []string{
"my-chart-values.yaml",
},
Values: "test: values",
},
},
ProjectName: "something",
ProjectSourceRepos: []string{"*"},
}
ProjectName: "something",
ProjectSourceRepos: []string{"*"},
}

res, err := service.GenerateManifest(context.Background(), &q)
res, err := service.GenerateManifest(context.Background(), &q)

require.NoError(t, err)
assert.Equal(t, []string{"helm template . --name-template test-app --namespace test-namespace --kube-version 1.2.3 --set test-param-bool-name=false --set-string test-param-name=test-value-test-app --set-file test-file-param-name=./test-file-param.yaml --values ./my-chart-values.yaml --values <temp file with values from source.helm.values/valuesObject> --api-versions v1/Test --api-versions v2/Test"}, res.Commands)
require.NoError(t, err)
assert.Equal(t, []string{"helm template . --name-template test-app --namespace test-namespace --kube-version 1.2.3 --set test-param-bool-name=false --set-string test-param-forcestring-name=test-value-$ARGOCD_APP_NAME --set-file test-file-param-name=./test-file-param.yaml --values ./my-chart-values.yaml --values <temp file with values from source.helm.values/valuesObject> --api-versions v1/Test --api-versions v2/Test"}, res.Commands)

t.Run("with overrides", func(t *testing.T) {
// These can be set explicitly instead of using inferred values. Make sure the overrides apply.
q.ApplicationSource.Helm.APIVersions = []string{"v3", "v4"}
q.ApplicationSource.Helm.KubeVersion = "5.6.7"
q.ApplicationSource.Helm.Namespace = "different-namespace"
q.ApplicationSource.Helm.ReleaseName = "different-release-name"

t.Run("with overrides", func(t *testing.T) {
// These can be set explicitly instead of using inferred values. Make sure the overrides apply.
q.ApplicationSource.Helm.APIVersions = []string{"v3", "v4"}
q.ApplicationSource.Helm.KubeVersion = "5.6.7"
q.ApplicationSource.Helm.Namespace = "different-namespace"
q.ApplicationSource.Helm.ReleaseName = "different-release-name"
res, err = service.GenerateManifest(context.Background(), &q)

require.NoError(t, err)
assert.Equal(t, []string{"helm template . --name-template different-release-name --namespace different-namespace --kube-version 5.6.7 --set test-param-bool-name=false --set-string test-param-forcestring-name=test-value-$ARGOCD_APP_NAME --set-file test-file-param-name=./test-file-param.yaml --values ./my-chart-values.yaml --values <temp file with values from source.helm.values/valuesObject> --api-versions v3 --api-versions v4"}, res.Commands)
})
})

t.Run("env variable interpolation", func(t *testing.T) {
q := apiclient.ManifestRequest{
AppName: "test-app",
Namespace: "test-namespace",
KubeVersion: "1.2.3",
ApiVersions: []string{"v1/Test", "v2/Test"},
Repo: &argoappv1.Repository{},
ApplicationSource: &argoappv1.ApplicationSource{
Path: ".",
Helm: &argoappv1.ApplicationSourceHelm{
FileParameters: []argoappv1.HelmFileParameter{
{
Name: "test-file-param-name",
Path: "test-file-param.yaml",
},
},
// Use build env var to test substitution.
Parameters: []argoappv1.HelmParameter{
{
// do not interpolate with env var
Name: "test-param-forcestring-name",
Value: "test-value-$ARGOCD_APP_NAME",
ForceString: true,
},
{
// do interpolate with env var
Name: "test-param-name",
Value: "test-value-$ARGOCD_APP_NAME",
ForceString: false,
},
},
PassCredentials: true,
SkipCrds: true,
ValueFiles: []string{
"my-chart-values.yaml",
},
Values: "test: values",
},
},
ProjectName: "something",
ProjectSourceRepos: []string{"*"},
}

res, err = service.GenerateManifest(context.Background(), &q)
res, err := service.GenerateManifest(context.Background(), &q)

require.NoError(t, err)
assert.Equal(t, []string{"helm template . --name-template different-release-name --namespace different-namespace --kube-version 5.6.7 --set test-param-bool-name=false --set-string test-param-name=test-value-test-app --set-file test-file-param-name=./test-file-param.yaml --values ./my-chart-values.yaml --values <temp file with values from source.helm.values/valuesObject> --api-versions v3 --api-versions v4"}, res.Commands)
assert.Equal(t, []string{"helm template . --name-template test-app --namespace test-namespace --kube-version 1.2.3 --set test-param-name=test-value-test-app --set-string test-param-forcestring-name=test-value-$ARGOCD_APP_NAME --set-file test-file-param-name=./test-file-param.yaml --values ./my-chart-values.yaml --values <temp file with values from source.helm.values/valuesObject> --api-versions v1/Test --api-versions v2/Test"}, res.Commands)
})
})

Expand Down
2 changes: 1 addition & 1 deletion test/e2e/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func TestHelmSetStringEnv(t *testing.T) {
Expect(OperationPhaseIs(OperationSucceeded)).
Expect(SyncStatusIs(SyncStatusCodeSynced)).
And(func(app *Application) {
assert.Equal(t, Name(), FailOnErr(Run(".", "kubectl", "-n", DeploymentNamespace(), "get", "cm", "my-map", "-o", "jsonpath={.data.foo}")).(string))
assert.Equal(t, "$ARGOCD_APP_NAME", FailOnErr(Run(".", "kubectl", "-n", DeploymentNamespace(), "get", "cm", "my-map", "-o", "jsonpath={.data.foo}")).(string))
})
}

Expand Down