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.
func pathToMap(path string, data map[string]interface{}) map[string]interface{} {
func pathToMap(path string, data interface{}) map[string]interface{} {
if path == "." {
return data
if istable(data) {
return data.(map[string]interface{})
}
return nil
}
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 {
return nil
}
cur := data
for i := len(path) - 1; i >= 0; i-- {
cur := make(map[string]interface{})
cur[path[len(path)-1]] = data
for i := len(path) - 2; i >= 0; i-- {
cur = map[string]interface{}{path[i]: cur}
}
return cur
@ -231,37 +235,24 @@ func processImportValues(c *chart.Chart) error {
for _, r := range c.Metadata.Dependencies {
var outiv []interface{}
for _, riv := range r.ImportValues {
switch iv := riv.(type) {
case map[string]interface{}:
child := iv["child"].(string)
parent := iv["parent"].(string)
params := make(map[string]string)
outiv = append(outiv, map[string]string{
"child": child,
"parent": parent,
})
if iv, ok := riv.(map[string]interface{}); ok {
params["child"] = iv["child"].(string)
params["parent"] = iv["parent"].(string)
} else if iv, ok := riv.(string); ok {
params["child"] = "exports." + iv
params["parent"] = "."
}
// get child table
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()))
case string:
child := "exports." + iv
outiv = append(outiv, map[string]string{
"child": child,
"parent": ".",
})
vm, err := cvals.Table(r.Name + "." + child)
if err != nil {
log.Printf("Warning: ImportValues missing table: %v", err)
continue
}
b = CoalesceTables(b, vm.AsMap())
outiv = append(outiv, params)
vm, err := cvals.PathValue(r.Name + "." + params["child"])
if err != nil {
log.Printf("Warning: Importing value from chart %s failed: %v", r.Name, err)
continue
}
b = CoalesceTables(cvals, pathToMap(params["parent"], vm))
}
// set our formatted import values
r.ImportValues = outiv

@ -206,6 +206,11 @@ func TestProcessDependencyImportValues(t *testing.T) {
e["overridden-chartA-B.SCBextra1"] = "13"
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
e["SCBexported1B"] = "1965"
e["SC1extra7"] = "true"

@ -25,6 +25,14 @@ dependencies:
parent: .
- SCBexported2
- 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
repository: http://localhost:10191

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

Loading…
Cancel
Save