From 69cb9155ed7ea31b3de7126c2db823b6904e5dcf Mon Sep 17 00:00:00 2001 From: Marc Garcia Sastre Date: Tue, 22 Oct 2019 20:42:24 +0200 Subject: [PATCH 1/2] Related to #2849: Allows ValuesFiles to be templatable --- pkg/skaffold/deploy/helm.go | 18 +++++++++++++----- pkg/skaffold/deploy/helm_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/pkg/skaffold/deploy/helm.go b/pkg/skaffold/deploy/helm.go index b9c0a10e424..bce107a8b94 100644 --- a/pkg/skaffold/deploy/helm.go +++ b/pkg/skaffold/deploy/helm.go @@ -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. @@ -325,6 +320,19 @@ func (h *HelmDeployer) deployRelease(ctx context.Context, out io.Writer, r lates args = append(args, "--set", fmt.Sprintf("%s=%s", k, v)) } + // ValuesFiles + for _, tmplValuesFile := range expandPaths(r.ValuesFiles) { + t, err := util.ParseEnvTemplate(tmplValuesFile) + if err != nil { + return nil, errors.Wrapf(err, "failed to parse valuesFileTemplate") + } + v, err := util.ExecuteEnvTemplate(t, envMap) + if err != nil { + return nil, errors.Wrapf(err, "failed to generate valuesFileTemplate") + } + args = append(args, "-f", v) + } + // Let's make sure that every image tag is set with `--set`. // Otherwise, templates have no way to use the images that were built. for _, build := range builds { diff --git a/pkg/skaffold/deploy/helm_test.go b/pkg/skaffold/deploy/helm_test.go index 1cca519affb..f74c8e7d157 100644 --- a/pkg/skaffold/deploy/helm_test.go +++ b/pkg/skaffold/deploy/helm_test.go @@ -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", @@ -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{}, From c24bf7d3086d9a48b1c6c22933c632280827580d Mon Sep 17 00:00:00 2001 From: Marc Garcia Sastre Date: Tue, 7 Jan 2020 16:17:47 +0100 Subject: [PATCH 2/2] Uses function to simplify calls --- pkg/skaffold/deploy/helm.go | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/pkg/skaffold/deploy/helm.go b/pkg/skaffold/deploy/helm.go index bce107a8b94..e8f8819864e 100644 --- a/pkg/skaffold/deploy/helm.go +++ b/pkg/skaffold/deploy/helm.go @@ -306,29 +306,19 @@ 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") - } - - v, err := util.ExecuteEnvTemplate(t, 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)) } // ValuesFiles - for _, tmplValuesFile := range expandPaths(r.ValuesFiles) { - t, err := util.ParseEnvTemplate(tmplValuesFile) - if err != nil { - return nil, errors.Wrapf(err, "failed to parse valuesFileTemplate") - } - v, err := util.ExecuteEnvTemplate(t, envMap) + for _, v := range expandPaths(r.ValuesFiles) { + v, err := templatedField(v, envMap) if err != nil { - return nil, errors.Wrapf(err, "failed to generate valuesFileTemplate") + return nil, err } args = append(args, "-f", v) } @@ -352,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,