Add support for importing single values from dependencies

Signed-off-by: Mateusz Jenek <mateusz.jenek@gmail.com>
pull/10515/head
Mateusz Jenek 4 years ago
parent d3dbd65997
commit d90015b4d3

@ -198,19 +198,23 @@ Loop:
} }
// pathToMap creates a nested map given a YAML path in dot notation. // pathToMap creates a nested map given a YAML path in dot notation.
func pathToMap(path string, data map[string]interface{}) map[string]interface{} { func pathToMap(path string, data interface{}) map[string]interface{} {
if path == "." { if path == "." {
return data if istable(data) {
return data.(map[string]interface{})
}
return nil
} }
return set(parsePath(path), data) return set(parsePath(path), data)
} }
func set(path []string, data map[string]interface{}) map[string]interface{} { func set(path []string, data interface{}) map[string]interface{} {
if len(path) == 0 { if len(path) == 0 {
return nil return nil
} }
cur := data cur := make(map[string]interface{})
for i := len(path) - 1; i >= 0; i-- { cur[path[len(path)-1]] = data
for i := len(path) - 2; i >= 0; i-- {
cur = map[string]interface{}{path[i]: cur} cur = map[string]interface{}{path[i]: cur}
} }
return cur return cur
@ -231,37 +235,24 @@ func processImportValues(c *chart.Chart) error {
for _, r := range c.Metadata.Dependencies { for _, r := range c.Metadata.Dependencies {
var outiv []interface{} var outiv []interface{}
for _, riv := range r.ImportValues { for _, riv := range r.ImportValues {
switch iv := riv.(type) { params := make(map[string]string)
case map[string]interface{}:
child := iv["child"].(string)
parent := iv["parent"].(string)
outiv = append(outiv, map[string]string{ if iv, ok := riv.(map[string]interface{}); ok {
"child": child, params["child"] = iv["child"].(string)
"parent": parent, params["parent"] = iv["parent"].(string)
}) } else if iv, ok := riv.(string); ok {
params["child"] = "exports." + iv
// get child table params["parent"] = "."
vv, err := cvals.Table(r.Name + "." + child)
if err != nil {
log.Printf("Warning: ImportValues missing table from chart %s: %v", r.Name, err)
continue
} }
// create value map from child to be merged into parent
b = CoalesceTables(cvals, pathToMap(parent, vv.AsMap())) outiv = append(outiv, params)
case string: vm, err := cvals.PathValue(r.Name + "." + params["child"])
child := "exports." + iv
outiv = append(outiv, map[string]string{
"child": child,
"parent": ".",
})
vm, err := cvals.Table(r.Name + "." + child)
if err != nil { if err != nil {
log.Printf("Warning: ImportValues missing table: %v", err) log.Printf("Warning: Importing value from chart %s failed: %v", r.Name, err)
continue continue
} }
b = CoalesceTables(b, vm.AsMap())
} b = CoalesceTables(cvals, pathToMap(params["parent"], vm))
} }
// set our formatted import values // set our formatted import values
r.ImportValues = outiv r.ImportValues = outiv

@ -206,6 +206,11 @@ func TestProcessDependencyImportValues(t *testing.T) {
e["overridden-chartA-B.SCBextra1"] = "13" e["overridden-chartA-B.SCBextra1"] = "13"
e["overridden-chartA-B.SC1extra6"] = "77" e["overridden-chartA-B.SC1extra6"] = "77"
e["imported-chart1-SC1bool"] = "true"
e["imported-chart1-SC1float"] = "3.14"
e["imported-chart1-SC1int"] = "100"
e["imported-chart1-SC1string"] = "dollywood"
// `exports` style // `exports` style
e["SCBexported1B"] = "1965" e["SCBexported1B"] = "1965"
e["SC1extra7"] = "true" e["SC1extra7"] = "true"

@ -25,6 +25,14 @@ dependencies:
parent: . parent: .
- SCBexported2 - SCBexported2
- SC1exported1 - SC1exported1
- child: SC1data.SC1bool
parent: imported-chart1-SC1bool
- child: SC1data.SC1float
parent: imported-chart1-SC1float
- child: SC1data.SC1int
parent: imported-chart1-SC1int
- child: SC1data.SC1string
parent: imported-chart1-SC1string
- name: subchart2 - name: subchart2
repository: http://localhost:10191 repository: http://localhost:10191

@ -187,8 +187,7 @@ func (v Values) PathValue(path string) (interface{}, error) {
func (v Values) pathValue(path []string) (interface{}, error) { func (v Values) pathValue(path []string) (interface{}, error) {
if len(path) == 1 { if len(path) == 1 {
// if exists must be root key not table if _, ok := v[path[0]]; ok {
if _, ok := v[path[0]]; ok && !istable(v[path[0]]) {
return v[path[0]], nil return v[path[0]], nil
} }
return nil, ErrNoValue{path[0]} return nil, ErrNoValue{path[0]}
@ -200,8 +199,8 @@ func (v Values) pathValue(path []string) (interface{}, error) {
if err != nil { if err != nil {
return nil, ErrNoValue{key} return nil, ErrNoValue{key}
} }
// check table for key and ensure value is not a table // check table for key
if k, ok := t[key]; ok && !istable(k) { if k, ok := t[key]; ok {
return k, nil return k, nil
} }
return nil, ErrNoValue{key} return nil, ErrNoValue{key}

Loading…
Cancel
Save