Skip to content

Commit

Permalink
fix(helm): fix werf_secret_file not working in werf helm template com…
Browse files Browse the repository at this point in the history
…mand

Signed-off-by: Timofey Kirillov <timofey.kirillov@flant.com>
  • Loading branch information
distorhead committed Jun 6, 2022
1 parent 23d3469 commit b2cec4b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 24 deletions.
26 changes: 26 additions & 0 deletions pkg/deploy/helm/chart_extender/helpers/common_template_funcs.go
Expand Up @@ -2,8 +2,13 @@ package helpers

import (
"context"
"fmt"
"path"
"strings"
"text/template"

"github.com/werf/werf/pkg/deploy/helm/chart_extender/helpers/secrets"

"github.com/werf/logboek"
)

Expand All @@ -26,3 +31,24 @@ func SetupWerfImageDeprecationFunc(ctx context.Context, funcMap template.FuncMap
return "", nil
}
}

func SetupWerfSecretFile(secretsRuntimeData *secrets.SecretsRuntimeData, funcMap template.FuncMap) {
funcMap["werf_secret_file"] = func(secretRelativePath string) (string, error) {
if path.IsAbs(secretRelativePath) {
return "", fmt.Errorf("expected relative secret file path, given path %v", secretRelativePath)
}

decodedData, ok := secretsRuntimeData.DecodedSecretFilesData[secretRelativePath]

if !ok {
var secretFiles []string
for key := range secretsRuntimeData.DecodedSecretFilesData {
secretFiles = append(secretFiles, key)
}

return "", fmt.Errorf("secret file %q not found, you may use one of the following: %q", secretRelativePath, strings.Join(secretFiles, "', '"))
}

return decodedData, nil
}
}
22 changes: 1 addition & 21 deletions pkg/deploy/helm/chart_extender/werf_chart.go
Expand Up @@ -6,9 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"text/template"

"github.com/mitchellh/copystructure"
Expand Down Expand Up @@ -196,25 +194,7 @@ func (wc *WerfChart) MakeBundleValues(chrt *chart.Chart, inputVals map[string]in

// SetupTemplateFuncs method for the chart.Extender interface
func (wc *WerfChart) SetupTemplateFuncs(t *template.Template, funcMap template.FuncMap) {
funcMap["werf_secret_file"] = func(secretRelativePath string) (string, error) {
if path.IsAbs(secretRelativePath) {
return "", fmt.Errorf("expected relative secret file path, given path %v", secretRelativePath)
}

decodedData, ok := wc.SecretsRuntimeData.DecodedSecretFilesData[secretRelativePath]

if !ok {
var secretFiles []string
for key := range wc.SecretsRuntimeData.DecodedSecretFilesData {
secretFiles = append(secretFiles, key)
}

return "", fmt.Errorf("secret file %q not found, you may use one of the following: %q", secretRelativePath, strings.Join(secretFiles, "', '"))
}

return decodedData, nil
}

helpers.SetupWerfSecretFile(wc.SecretsRuntimeData, funcMap)
helpers.SetupIncludeWrapperFuncs(funcMap)
helpers.SetupWerfImageDeprecationFunc(wc.ChartExtenderContext, funcMap)
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/deploy/helm/chart_extender/werf_chart_stub.go
Expand Up @@ -114,9 +114,7 @@ func (wc *WerfChartStub) MakeValues(inputVals map[string]interface{}) (map[strin

// SetupTemplateFuncs method for the chart.Extender interface
func (wc *WerfChartStub) SetupTemplateFuncs(t *template.Template, funcMap template.FuncMap) {
funcMap["werf_secret_file"] = func(secretRelativePath string) (string, error) {
return "stub_data", nil
}
helpers.SetupWerfSecretFile(wc.SecretsRuntimeData, funcMap)
helpers.SetupIncludeWrapperFuncs(funcMap)
helpers.SetupWerfImageDeprecationFunc(wc.ChartExtenderContext, funcMap)
}
Expand Down

0 comments on commit b2cec4b

Please sign in to comment.