Added support map.yaml.

Signed-off-by: Jorge C. Leitao <jorgecarleitao@gmail.com>
pull/8580/head
Jorge C. Leitao 5 years ago
parent 621c2020c6
commit edef360863
No known key found for this signature in database
GPG Key ID: 94AA4241B0B39DA5

@ -201,6 +201,12 @@ func TestInstall(t *testing.T) {
name: "install chart with only crds",
cmd: "install crd-test testdata/testcharts/chart-with-only-crds --namespace default",
},
// Install, base case with map and subcharts
{
name: "basic install with a map and subcharts",
cmd: "install aeneas testdata/testcharts/chart-with-map --namespace default",
golden: "output/install.txt",
},
}
runTestActionCmd(t, tests)

@ -0,0 +1,7 @@
apiVersion: v1
description: Chart with map and subcharts
name: chart-with-map
version: 0.0.1
dependencies:
- name: subchart
version: 0.0.1

@ -0,0 +1,4 @@
apiVersion: v1
description: subchart
name: subchart
version: 0.0.1

@ -0,0 +1 @@
derived: {{ printf "%s-%s" .Values.default "a" }}

@ -0,0 +1,7 @@
apiVersion: v1
kind: Pod
metadata:
name: "aaa"
labels:
derived: {{ .Values.derived }}
default: {{ .Values.default }}

@ -0,0 +1,4 @@
derived: {{ printf "%s-%s" .Values.public1 "a" }}
subchart:
default: "aa"

@ -0,0 +1,7 @@
apiVersion: v1
kind: Pod
metadata:
name: "aaa"
labels:
derived: {{ .Values.derived }}
public1: {{ .Values.public1 }}

@ -42,6 +42,8 @@ type Chart struct {
Templates []*File `json:"templates"`
// Values are default config for this chart.
Values map[string]interface{} `json:"values"`
// Map are default derivations of values of this chart.
Map *File `json:"map"`
// Schema is an optional JSON schema for imposing structure on Values
Schema []byte `json:"schema"`
// Files are miscellaneous files in a chart archive,

@ -99,6 +99,8 @@ func LoadFiles(files []*BufferedFile) (*chart.Chart, error) {
if err := yaml.Unmarshal(f.Data, &c.Values); err != nil {
return c, errors.Wrap(err, "cannot load values.yaml")
}
case f.Name == "map.yaml":
c.Map = &chart.File{Name: f.Name, Data: f.Data}
case f.Name == "values.schema.json":
c.Schema = f.Data

@ -28,6 +28,7 @@ import (
"github.com/pkg/errors"
"k8s.io/client-go/rest"
"sigs.k8s.io/yaml"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chartutil"
@ -351,6 +352,33 @@ func recAllTpls(c *chart.Chart, templates map[string]renderable, vals chartutil.
next["Values"] = vs
}
// apply the map's transformation and coalesce it with `next`
if c.Map != nil {
// render the map's template
mapTemplate := make(map[string]renderable)
mapTemplate["map"] = renderable{
tpl: string(c.Map.Data),
vals: next,
basePath: c.ChartFullPath(),
}
// todo: pass conf to Engine here
val, err := Engine{}.render(mapTemplate)
if err != nil {
// todo
fmt.Println("ERRRRORRR:", err)
return
}
// construct the map values from the resulting yaml
mapped := map[string]interface{}{}
if err := yaml.Unmarshal([]byte(val["map"]), &mapped); err != nil {
// todo
fmt.Println("ERRRRORRR:", err)
return
}
// coalesce with preference to `mapped`
next["Values"] = chartutil.CoalesceTables(mapped, next["Values"].(chartutil.Values).AsMap())
}
for _, child := range c.Dependencies() {
recAllTpls(child, templates, next)
}

@ -738,3 +738,50 @@ func TestRenderRecursionLimit(t *testing.T) {
}
}
func TestRenderMap(t *testing.T) {
c := &chart.Chart{
Metadata: &chart.Metadata{
Name: "moby",
Version: "1.2.3",
},
Templates: []*chart.File{
{Name: "templates/test1", Data: []byte("{{.Values.outer | title }} {{.Values.inner | title}}")},
{Name: "templates/test2", Data: []byte("{{toJson .Values}}")},
},
// maps "outer"
Map: &chart.File{
Name: "map",
Data: []byte("outer: {{.Values.inner}}-a"),
},
Values: map[string]interface{}{"inner": "DEFAULT"},
}
// no "outer"
vals := map[string]interface{}{
"Values": chartutil.Values{
"inner": "inn",
},
}
v, err := chartutil.CoalesceValues(c, vals)
if err != nil {
t.Fatalf("Failed to coalesce values: %s", err)
}
out, err := Render(c, v)
if err != nil {
t.Errorf("Failed to render templates: %s", err)
}
// "outer" was mapped
expect := map[string]string{
"moby/templates/test1": "Inn-A Inn",
"moby/templates/test2": `{"inner":"inn","outer":"inn-a"}`,
}
for name, data := range expect {
if out[name] != data {
t.Errorf("Expected %q, got %q", data, out[name])
}
}
}

Loading…
Cancel
Save