feat(*): Setting values from directory

Signed-off-by: Piotr Resztak <piotr.resztak@gmail.com>
pull/10197/head
Piotr Resztak 4 years ago
parent 9fafb4ad68
commit b2d109a517

@ -131,6 +131,11 @@ func TestTemplateCmd(t *testing.T) {
cmd: fmt.Sprintf(`template '%s' --skip-tests`, chartPath), cmd: fmt.Sprintf(`template '%s' --skip-tests`, chartPath),
golden: "output/template-skip-tests.txt", golden: "output/template-skip-tests.txt",
}, },
{
name: "chart with values as a directory",
cmd: fmt.Sprintf("template '%s' -f %s", "testdata/testcharts/chart-with-values-directory", "testdata/testcharts/chart-with-values-directory/values"),
golden: "output/template-with-values-directory.txt",
},
} }
runTestCmd(t, tests) runTestCmd(t, tests)
} }

@ -0,0 +1,17 @@
---
# Source: chart-with-values-directory/templates/service.yaml
apiVersion: v1
kind: Service
metadata:
name: chart-with-values-directory
labels:
helm.sh/chart: "chart-with-values-directory-0.1.0"
spec:
type: ClusterIP
ports:
- port: 80
targetPort: 80
protocol: TCP
name: apache
selector:
app.kubernetes.io/name: chart-with-values-directory

@ -0,0 +1,4 @@
apiVersion: v1
description: A Helm chart for Kubernetes
name: chart-with-values-directory
version: 0.1.0

@ -0,0 +1,15 @@
apiVersion: v1
kind: Service
metadata:
name: {{ .Chart.Name }}
labels:
helm.sh/chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
spec:
type: {{ .Values.service.type }}
ports:
- port: {{ .Values.service.externalPort }}
targetPort: {{ .Values.service.internalPort }}
protocol: TCP
name: {{ .Values.service.name }}
selector:
app.kubernetes.io/name: {{ .Chart.Name }}

@ -0,0 +1,3 @@
service:
type: ClusterIP
externalPort: 80

@ -0,0 +1,3 @@
service:
name: apache
internalPort: 80

@ -20,6 +20,7 @@ import (
"io/ioutil" "io/ioutil"
"net/url" "net/url"
"os" "os"
"path/filepath"
"strings" "strings"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -43,18 +44,32 @@ func (opts *Options) MergeValues(p getter.Providers) (map[string]interface{}, er
// User specified a values files via -f/--values // User specified a values files via -f/--values
for _, filePath := range opts.ValueFiles { for _, filePath := range opts.ValueFiles {
currentMap := map[string]interface{}{} isDir, err := isDirectory(filePath)
bytes, err := readFile(filePath, p)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if isDir {
files, err := ioutil.ReadDir(filePath)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(bytes, &currentMap); err != nil { for _, file := range files {
return nil, errors.Wrapf(err, "failed to parse %s", filePath) currentMap, err := unmarshallYaml(filepath.Join(filePath, file.Name()), p)
if err != nil {
return nil, err
}
// Merge with the previous map
base = mergeMaps(base, currentMap)
}
} else {
currentMap, err := unmarshallYaml(filePath, p)
if err != nil {
return nil, err
}
// Merge with the previous map
base = mergeMaps(base, currentMap)
} }
// Merge with the previous map
base = mergeMaps(base, currentMap)
} }
// User specified a value via --set // User specified a value via --set
@ -85,6 +100,31 @@ func (opts *Options) MergeValues(p getter.Providers) (map[string]interface{}, er
return base, nil return base, nil
} }
func isDirectory(filePath string) (bool, error) {
if strings.TrimSpace(filePath) == "-" {
return false, nil
}
fileInfo, err := os.Stat(filePath)
if err != nil {
return false, err
}
return fileInfo.Mode().IsDir(), nil
}
func unmarshallYaml(filePath string, p getter.Providers) (map[string]interface{}, error) {
currentMap := map[string]interface{}{}
bytes, err := readFile(filePath, p)
if err != nil {
return nil, err
}
if err := yaml.Unmarshal(bytes, &currentMap); err != nil {
return nil, errors.Wrapf(err, "failed to parse %s", filePath)
}
return currentMap, nil
}
func mergeMaps(a, b map[string]interface{}) map[string]interface{} { func mergeMaps(a, b map[string]interface{}) map[string]interface{} {
out := make(map[string]interface{}, len(a)) out := make(map[string]interface{}, len(a))
for k, v := range a { for k, v := range a {

Loading…
Cancel
Save