Skip to content

Commit

Permalink
Fix the chart functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lewis Marshall authored and lewismarshall committed Mar 14, 2019
1 parent 24a18bd commit 33a5b39
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 20 deletions.
86 changes: 77 additions & 9 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
version = "1.19.1"

[[constraint]]
name = "github.com/helm/helm"
name = "k8s.io/helm"
version = "v2.11.0"

[prune]
Expand Down
32 changes: 24 additions & 8 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"bytes"
"crypto/rand"
"encoding/base64"
"fmt"
"io/ioutil"
"math/big"
"strings"
"text/template"

"github.com/Masterminds/sprig"
"github.com/helm/helm/pkg/strvals"
yaml "gopkg.in/yaml.v2"
"k8s.io/helm/pkg/chartutil"
)

var (
Expand All @@ -20,6 +22,9 @@ var (

// Render - the function used for rendering templates (with Sprig support)
func Render(k K8Api, tmpl string, vars interface{}) (string, bool, error) {

// Must cast interface back to map[string]{interface} to work with
// helm function ToYAML
fm := sprig.TxtFuncMap()
// Preserve old KD functionality (strings param order vs sprig)
fm["contains"] = strings.Contains
Expand All @@ -34,13 +39,11 @@ func Render(k K8Api, tmpl string, vars interface{}) (string, bool, error) {
k8Api = k
fm["k8lookup"] = k8lookup
// Added some oft used helm functions
fm["toYaml"] = strvals.ToYAML
fm["parse"] = strvals.Parse
fm["parseFile"] = strvals.ParseFile
fm["parseInto"] = strvals.ParseInto
fm["parseIntoFile"] = strvals.ParseIntoFile
fm["parseIntoString"] = strvals.ParseIntoString
fm["parseString"] = strvals.ParseString
fm["toToml"] = chartutil.ToYaml
fm["toYaml"] = toYaml
fm["fromYaml"] = chartutil.FromYaml
fm["toJson"] = chartutil.ToJson
fm["fromJson"] = chartutil.FromJson

secretUsed = false
defer func() {
Expand Down Expand Up @@ -131,3 +134,16 @@ func k8lookup(kind, name, path string) string {
}
return data
}

// Copied the function from helm but use the golang yaml parser
// as it's compatible with the generic map[insterface{}]interface{} types
// we cope with here
func toYaml(v interface{}) string {
data, err := yaml.Marshal(v)
if err != nil {
// Swallow errors inside of a template.
fmt.Printf("ToYaml err:%q", err)
return ""
}
return string(data)
}
8 changes: 8 additions & 0 deletions test/TestConfigData/chart-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@ spec:
image: "quay.io/sample:v1.2.3"
imagePullPolicy: IfNotPresent
env:
resources:
limits:
cpu: 100m
memory: 128Mi
requests:
cpu: 100m
memory: 128Mi

4 changes: 2 additions & 2 deletions test/TestConfigData/chart-deploy.yaml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ spec:
value: {{ .Values.agent.toleration }}
{{- end }}
{{- end }}
resources:
{{ toYaml .Values.resources | indent 10 }}
{{- if .Values.nodeSelector }}
nodeSelector:
{{ toYaml .Values.nodeSelector | indent 8 }}
resources:
{{ toYaml .Values.resources | indent 10 }}
{{- end }}

0 comments on commit 33a5b39

Please sign in to comment.