-
Notifications
You must be signed in to change notification settings - Fork 785
/
values_tree.go
178 lines (167 loc) · 5.03 KB
/
values_tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package helm
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jenkins-x/jx/pkg/util"
"github.com/ghodss/yaml"
"github.com/jenkins-x/jx/pkg/log"
)
//DefaultValuesTreeIgnores is the default set of ignored files for collapsing the values tree which are used if
// ignores is nil
var DefaultValuesTreeIgnores = []string{
"templates/*",
}
//GenerateValues will generate a values.yaml file in dir. It scans all subdirectories for values.yaml files,
// and merges them into the values.yaml in the root directory,
// creating a nested key structure that matches the directory structure.
// Any keys used that match files with the same name in the directory (
// and have empty values) will be inlined as block scalars.
// Standard UNIX glob patterns can be passed to IgnoreFile directories.
func GenerateValues(dir string, ignores []string, verbose bool) ([]byte, error) {
info, err := os.Stat(dir)
if err != nil {
return nil, err
} else if os.IsNotExist(err) {
return nil, fmt.Errorf("%s does not exist", dir)
} else if !info.IsDir() {
return nil, fmt.Errorf("%s is not a directory", dir)
}
if ignores == nil {
ignores = DefaultValuesTreeIgnores
}
files := make(map[string]map[string]string)
values := make(map[string]interface{})
err = filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
rPath, err := filepath.Rel(dir, path)
if err != nil {
return err
}
// Check if should IgnoreFile the path
if ignore, err := util.IgnoreFile(rPath, ignores); err != nil {
return err
} else if !ignore {
rDir, file := filepath.Split(rPath)
// For the root dir we just consider directories (which the walk func does for us)
if rDir != "" {
// If it's values.yaml, then read and parse it
if file == "values.yaml" {
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
v := make(map[string]interface{})
err = yaml.Unmarshal(b, &v)
if err != nil {
return err
}
values[rDir] = v
} else {
// for other files, just store a reference
if _, ok := files[rDir]; !ok {
files[rDir] = make(map[string]string)
}
files[rDir][file] = path
}
}
} else {
if verbose {
log.Infof("Ignoring %s\n", rPath)
}
}
return nil
})
if err != nil {
return nil, err
}
// Load the root values.yaml
rootValuesFileName := filepath.Join(dir, ValuesFileName)
rootValues, err := LoadValuesFile(rootValuesFileName)
if err != nil {
return nil, err
}
// externalFileHandler is used to read any inline any files that match into the values.yaml
externalFileHandler := func(path string, element map[string]interface{}, key string) error {
b, err := ioutil.ReadFile(path)
if err != nil {
return err
}
element[key] = string(b)
return nil
}
for p, v := range values {
// First, do file substitution - but only if any files were actually found
if dirFiles := files[p]; dirFiles != nil && len(dirFiles) > 0 {
err := HandleExternalFileRefs(v, dirFiles, "", externalFileHandler)
if err != nil {
return nil, err
}
}
// Then, merge the values to the root file
keys := strings.Split(strings.TrimSuffix(p, "/"), string(os.PathSeparator))
x := rootValues
jsonPath := "$"
for i, k := range keys {
k = strings.TrimSuffix(k, "/")
jsonPath = fmt.Sprintf("%s.%s", jsonPath, k)
v1, ok1 := x[k]
if i < len(keys)-1 {
// Create the nested file object structure
if !ok1 {
// Easy, just create the nested object!
new := make(map[string]interface{})
x[k] = new
x = new
} else {
// Need to do a type check
v2, ok2 := v1.(map[string]interface{})
if !ok2 {
return nil, fmt.Errorf("%s is not an associative array", jsonPath)
}
x = v2
}
} else {
// Apply
x[k] = v
}
}
}
return yaml.Marshal(rootValues)
}
// HandleExternalFileRefs recursively scans the element map structure,
// looking for nested maps. If it finds keys that match any key-value pair in possibles it will call the handler.
// The jsonPath is used for referencing the path in the map structure when reporting errors.
func HandleExternalFileRefs(element interface{}, possibles map[string]string, jsonPath string,
handler func(path string, element map[string]interface{}, key string) error) error {
if jsonPath == "" {
// set zero value
jsonPath = "$"
}
if e, ok := element.(map[string]interface{}); ok {
for k, v := range e {
if paths, ok := possibles[k]; ok {
if v == nil || util.IsZeroOfUnderlyingType(v) {
// There is a filename in the directory structure that matches this key, and it has no value,
// so we handle it
err := handler(paths, e, k)
if err != nil {
return err
}
} else {
return fmt.Errorf("value at %s must be empty but is %v", jsonPath, v)
}
} else {
// keep on recursing
jsonPath = fmt.Sprintf("%s.%s", jsonPath, k)
err := HandleExternalFileRefs(v, possibles, jsonPath, handler)
if err != nil {
return err
}
}
}
}
// If it's not an object, we can't do much with it
return nil
}