-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #134 from zalsader/add-values-options
Use values options for additional settings
- Loading branch information
Showing
4 changed files
with
107 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,45 @@ | ||
package helmclient | ||
|
||
import ( | ||
"github.com/pkg/errors" | ||
"helm.sh/helm/v3/pkg/getter" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
// GetValuesMap returns the mapped out values of a chart | ||
func (spec *ChartSpec) GetValuesMap() (map[string]interface{}, error) { | ||
var values map[string]interface{} | ||
// GetValuesMap returns the merged mapped out values of a chart, | ||
// using both ValuesYaml and ValuesOptions | ||
func (spec *ChartSpec) GetValuesMap(p getter.Providers) (map[string]interface{}, error) { | ||
valuesYaml := map[string]interface{}{} | ||
|
||
err := yaml.Unmarshal([]byte(spec.ValuesYaml), &values) | ||
err := yaml.Unmarshal([]byte(spec.ValuesYaml), &valuesYaml) | ||
if err != nil { | ||
return nil, err | ||
return nil, errors.Wrap(err, "Failed to Parse ValuesYaml") | ||
} | ||
|
||
return values, nil | ||
valuesOptions, err := spec.ValuesOptions.MergeValues(p) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "Failed to Parse ValuesOptions") | ||
} | ||
|
||
return mergeMaps(valuesYaml, valuesOptions), nil | ||
} | ||
|
||
// Copied from https://github.com/helm/helm/blob/ced54b12896ae933017782e2bbd722e2bc8c0914/pkg/cli/values/options.go#L99 | ||
func mergeMaps(a, b map[string]interface{}) map[string]interface{} { | ||
out := make(map[string]interface{}, len(a)) | ||
for k, v := range a { | ||
out[k] = v | ||
} | ||
for k, v := range b { | ||
if v, ok := v.(map[string]interface{}); ok { | ||
if bv, ok := out[k]; ok { | ||
if bv, ok := bv.(map[string]interface{}); ok { | ||
out[k] = mergeMaps(bv, v) | ||
continue | ||
} | ||
} | ||
} | ||
out[k] = v | ||
} | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package helmclient | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestMergeValues(t *testing.T) { | ||
nestedMap := map[string]interface{}{ | ||
"foo": "bar", | ||
"baz": map[string]string{ | ||
"cool": "stuff", | ||
}, | ||
} | ||
anotherNestedMap := map[string]interface{}{ | ||
"foo": "bar", | ||
"baz": map[string]string{ | ||
"cool": "things", | ||
"awesome": "stuff", | ||
}, | ||
} | ||
flatMap := map[string]interface{}{ | ||
"foo": "bar", | ||
"baz": "stuff", | ||
} | ||
anotherFlatMap := map[string]interface{}{ | ||
"testing": "fun", | ||
} | ||
|
||
testMap := mergeMaps(flatMap, nestedMap) | ||
equal := reflect.DeepEqual(testMap, nestedMap) | ||
if !equal { | ||
t.Errorf("Expected a nested map to overwrite a flat value. Expected: %v, got %v", nestedMap, testMap) | ||
} | ||
|
||
testMap = mergeMaps(nestedMap, flatMap) | ||
equal = reflect.DeepEqual(testMap, flatMap) | ||
if !equal { | ||
t.Errorf("Expected a flat value to overwrite a map. Expected: %v, got %v", flatMap, testMap) | ||
} | ||
|
||
testMap = mergeMaps(nestedMap, anotherNestedMap) | ||
equal = reflect.DeepEqual(testMap, anotherNestedMap) | ||
if !equal { | ||
t.Errorf("Expected a nested map to overwrite another nested map. Expected: %v, got %v", anotherNestedMap, testMap) | ||
} | ||
|
||
testMap = mergeMaps(anotherFlatMap, anotherNestedMap) | ||
expectedMap := map[string]interface{}{ | ||
"testing": "fun", | ||
"foo": "bar", | ||
"baz": map[string]string{ | ||
"cool": "things", | ||
"awesome": "stuff", | ||
}, | ||
} | ||
equal = reflect.DeepEqual(testMap, expectedMap) | ||
if !equal { | ||
t.Errorf("Expected a map with different keys to merge properly with another map. Expected: %v, got %v", expectedMap, testMap) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters