@ -16,6 +16,7 @@ limitations under the License.
package chartutil
package chartutil
import (
import (
"fmt"
"log"
"log"
"strings"
"strings"
@ -27,7 +28,7 @@ func ProcessDependencies(c *chart.Chart, v Values) error {
if err := processDependencyEnabled ( c , v , "" ) ; err != nil {
if err := processDependencyEnabled ( c , v , "" ) ; err != nil {
return err
return err
}
}
return processDependencyImport Values( c )
return processDependencyImport Export Values( c )
}
}
// processDependencyConditions disables charts based on condition path value in values
// processDependencyConditions disables charts based on condition path value in values
@ -216,8 +217,9 @@ func set(path []string, data map[string]interface{}) map[string]interface{} {
return cur
return cur
}
}
// processImportValues merges values from child to parent based on the chart's dependencies' ImportValues field.
// processImportExportValues merges values between child and parent based on the chart's dependencies' ImportValues
func processImportValues ( c * chart . Chart ) error {
// and ExportValues fields.
func processImportExportValues ( c * chart . Chart ) error {
if c . Metadata . Dependencies == nil {
if c . Metadata . Dependencies == nil {
return nil
return nil
}
}
@ -227,8 +229,22 @@ func processImportValues(c *chart.Chart) error {
return err
return err
}
}
b := make ( map [ string ] interface { } )
b := make ( map [ string ] interface { } )
// import values from each dependency if specified in import-values
// For each dependency export values from parent or import values from child if export-values or import-values
// specified.
for _ , r := range c . Metadata . Dependencies {
for _ , r := range c . Metadata . Dependencies {
b = CoalesceTables ( b , getExportedValues ( c , r , cvals ) )
b = CoalesceTables ( b , getImportedValues ( r , cvals ) )
}
// set the new values
c . Values = CoalesceTables ( cvals , b )
return nil
}
// Generates values map which is imported from specified child to parent via import-values.
func getImportedValues ( r * chart . Dependency , cvals Values ) map [ string ] interface { } {
b := make ( map [ string ] interface { } )
var outiv [ ] interface { }
var outiv [ ] interface { }
for _ , riv := range r . ImportValues {
for _ , riv := range r . ImportValues {
switch iv := riv . ( type ) {
switch iv := riv . ( type ) {
@ -265,21 +281,112 @@ func processImportValues(c *chart.Chart) error {
}
}
// set our formatted import values
// set our formatted import values
r . ImportValues = outiv
r . ImportValues = outiv
return b
}
}
// set the new values
// Generates values map which is exported from parent to specified child via export-values.
c . Values = CoalesceTables ( cvals , b )
func getExportedValues ( c * chart . Chart , r * chart . Dependency , cvals Values ) map [ string ] interface { } {
b := make ( map [ string ] interface { } )
var exportValues [ ] interface { }
for _ , rev := range r . ExportValues {
parent , child , err := parseExportValues ( rev , r )
if err != nil {
log . Printf ( "Warning: invalid ExportValues defined in chart %q for its dependency %q: %s" , c . Name ( ) , r . Name , err )
continue
}
return nil
exportValues = append ( exportValues , map [ string ] string {
"parent" : parent ,
"child" : child ,
} )
var childValMap map [ string ] interface { }
// try to get parent table
vm , err := cvals . Table ( parent )
if err == nil {
childValMap = pathToMap ( child , vm . AsMap ( ) )
} else {
// still it might be not a table but a simple value
value , e := cvals . PathValue ( parent )
if e != nil {
log . Printf ( "Warning: ExportValues defined in chart %q for its dependency %q can't get the parent path: %v" , c . Name ( ) , r . Name , err )
continue
}
childSlice := parsePath ( child )
childLen := len ( childSlice )
if childLen == 1 {
log . Printf ( "Warning: in ExportValues defined in chart %q for its dependency %q you are trying to assign a primitive data type (string, int, etc) to the root of your dependent chart values. We will ignore this ExportValues, because this is most likely not what you want. Fix the ExportValues to hide this warning." , c . Name ( ) , r . Name )
continue
}
childValMap = pathToMap (
joinPath ( childSlice [ : childLen - 1 ] ... ) ,
map [ string ] interface { } {
childSlice [ childLen - 1 ] : value ,
} ,
)
}
// merge new map with values exported to the child into the resulting values map
b = CoalesceTables ( childValMap , b )
}
// set formatted export values
r . ExportValues = exportValues
return b
}
// Parse and validate export-values.
func parseExportValues ( rev interface { } , r * chart . Dependency ) ( string , string , error ) {
var parent , child string
switch ev := rev . ( type ) {
case map [ string ] interface { } :
var ok bool
parent , ok = ev [ "parent" ] . ( string )
if ! ok {
return "" , "" , fmt . Errorf ( "parent can't be of null type" )
}
child , ok = ev [ "child" ] . ( string )
if ! ok {
return "" , "" , fmt . Errorf ( "child can't be of null type" )
}
switch parent {
case "" , "." :
return "" , "" , fmt . Errorf ( "parent %q is not allowed" , parent )
}
switch child {
case "" , "." :
child = r . Name
default :
child = r . Name + "." + child
}
case string :
switch parent = ev ; parent {
case "" , "." :
parent = "exports"
default :
parent = "exports." + parent
}
child = r . Name
default :
return "" , "" , fmt . Errorf ( "invalid type of ExportValues" )
}
return parent , child , nil
}
}
// processDependencyImportValues imports specified chart values from child to parent.
// processDependencyImportExportValues imports (child to parent) and/or exports (parent to child) values if
func processDependencyImportValues ( c * chart . Chart ) error {
// import-values or export-values specified for the dependency.
func processDependencyImportExportValues ( c * chart . Chart ) error {
for _ , d := range c . Dependencies ( ) {
for _ , d := range c . Dependencies ( ) {
// recurse
// recurse
if err := processDependencyImportValues ( d ) ; err != nil {
if err := processDependencyImport Export Values( d ) ; err != nil {
return err
return err
}
}
}
}
return processImportValues ( c )
return processImport Export Values( c )
}
}