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

Related to #2849: Allows ValuesFiles to be templatable #3111

Merged
merged 2 commits into from
Jan 15, 2020
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
34 changes: 22 additions & 12 deletions pkg/skaffold/deploy/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,6 @@ func (h *HelmDeployer) deployRelease(ctx context.Context, out io.Writer, r lates
args = append(args, "-f", constants.HelmOverridesFilename)
}

// ValuesFiles
for _, valuesFile := range expandPaths(r.ValuesFiles) {
args = append(args, "-f", valuesFile)
}

// TODO(dgageot): we should merge `Values`, `SetValues` and `SetValueTemplates`
// as much as possible.

Expand Down Expand Up @@ -311,18 +306,21 @@ func (h *HelmDeployer) deployRelease(ctx context.Context, out io.Writer, r lates
logrus.Debugf("EnvVarMap: %#v\n", envMap)

for k, v := range r.SetValueTemplates {
t, err := util.ParseEnvTemplate(v)
v, err := templatedField(v, envMap)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse setValueTemplates")
return nil, err
}
valuesSet[v] = true
args = append(args, "--set", fmt.Sprintf("%s=%s", k, v))
}

v, err := util.ExecuteEnvTemplate(t, envMap)
// ValuesFiles
for _, v := range expandPaths(r.ValuesFiles) {
v, err := templatedField(v, envMap)
if err != nil {
return nil, errors.Wrapf(err, "failed to generate setValueTemplates")
return nil, err
}

valuesSet[v] = true
args = append(args, "--set", fmt.Sprintf("%s=%s", k, v))
args = append(args, "-f", v)
}

// Let's make sure that every image tag is set with `--set`.
Expand All @@ -344,6 +342,18 @@ func (h *HelmDeployer) deployRelease(ctx context.Context, out io.Writer, r lates
return h.getDeployResults(ctx, ns, releaseName), helmErr
}

func templatedField(tmpl string, envMap map[string]string) (string, error) {
t, err := util.ParseEnvTemplate(tmpl)
if err != nil {
return "", errors.Wrapf(err, "failed to parse template")
}
v, err := util.ExecuteEnvTemplate(t, envMap)
if err != nil {
return "", errors.Wrapf(err, "failed to generate template")
}
return v, nil
}

func createEnvVarMap(imageName string, fqn string) map[string]string {
customMap := map[string]string{
"IMAGE_NAME": imageName,
Expand Down
24 changes: 24 additions & 0 deletions pkg/skaffold/deploy/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ var testDeployConfigTemplated = latest.HelmDeploy{
}},
}

var testDeployConfigValuesFilesTemplated = latest.HelmDeploy{
Releases: []latest.HelmRelease{{
Name: "skaffold-helm",
ChartPath: "examples/test",
Values: map[string]string{
"image": "skaffold-helm",
},
Overrides: schemautil.HelmOverrides{Values: map[string]interface{}{"foo": "bar"}},
ValuesFiles: []string{
"/some/file-{{.FOO}}.yaml",
},
}},
}

var testDeployRecreatePodsConfig = latest.HelmDeploy{
Releases: []latest.HelmRelease{{
Name: "skaffold-helm",
Expand Down Expand Up @@ -469,6 +483,16 @@ func TestHelmDeploy(t *testing.T) {
runContext: makeRunContext(testDeployConfigTemplated, false),
builds: testBuilds,
},
{
description: "deploy with valuesFiles templated",
commands: &MockHelm{
upgradeMatcher: func(cmd *exec.Cmd) bool {
return util.StrSliceContains(cmd.Args, "/some/file-FOOBAR.yaml")
},
},
runContext: makeRunContext(testDeployConfigValuesFilesTemplated, false),
builds: testBuilds,
},
{
description: "deploy without actual tags",
commands: &MockHelm{},
Expand Down