-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
307 lines (272 loc) · 6.73 KB
/
parser.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package main
import (
"bufio"
"bytes"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
"text/template"
)
func applyLoadTemplates(buf []byte, fn string, cfg *SK8config, prefix string) ([]byte, error) {
if bytes.ContainsAny(buf, "{{") {
if cfg != nil {
log.Debugf("%sTemplating defaults in %s", prefix, fn)
tmpl := template.New(fn).Funcs(FuncMap())
tmpl, err := tmpl.Parse(string(buf))
if err != nil {
return nil, err
}
var bufStream bytes.Buffer
err = tmpl.Execute(&bufStream, cfg)
if err != nil {
return nil, err
}
buf = bufStream.Bytes()
} else {
log.Debugf("%sTemplating generics in %s", prefix, fn)
tmpl := template.New(fn).Funcs(FuncMap())
tmpl, err := tmpl.Parse(string(buf))
if err != nil {
return nil, err
}
var bufStream bytes.Buffer
err = tmpl.Execute(&bufStream, nil)
if err != nil {
return nil, err
}
buf = bufStream.Bytes()
}
}
return buf, nil
}
func splitDocuments(input []byte) [][]byte {
var bufs [][]byte
currbuf := []byte{}
rd := bytes.NewReader(input)
scanner := bufio.NewScanner(rd)
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "---") {
if len(currbuf) > 0 {
bufs = append(bufs, currbuf)
currbuf = []byte{}
}
} else {
currbuf = append(currbuf, line...)
currbuf = append(currbuf, '\n')
}
}
if len(currbuf) > 0 {
bufs = append(bufs, currbuf)
}
return bufs
}
func isKubectl(buf []byte) bool {
if len(buf) < 10 {
return false
}
first := string(buf[0:9])
return first == "#!kubectl"
}
func loadFile(fn string, depth int, cfg *SK8config) ([]*SK8config, error) {
var result []*SK8config
if strings.HasPrefix(fn, "?") {
fn = fn[1:]
if _, err := os.Stat(fn); err != nil {
log.Debugf("Skipping optional parent: %s", fn)
return nil, nil
}
}
prefix := strings.Repeat("| ", depth)
log.Debugf("%sLoading from %s", prefix, fn)
prefix = prefix + "+-"
filedata, err := ioutil.ReadFile(fn)
if err != nil {
return nil, fmt.Errorf("readfile error %q: %q", fn, err.Error())
}
filedata, err = applyLoadTemplates(filedata, fn, cfg, prefix)
if err != nil {
return nil, fmt.Errorf("apply template-error %q: %q", fn, err.Error())
}
bufs := splitDocuments(filedata)
if len(bufs) > 1 {
log.Infof("%s %s splitted into %d documents", prefix, fn, len(bufs))
}
for _, buf := range bufs {
var o = SK8config{}
if isKubectl(buf) {
o.cfgType = typeKubectl
o.RawYAML = buf
} else {
o.cfgType = typeSK8
var raw interface{}
//err = yaml_mapstr.Unmarshal(buf, &raw)
err = yaml_Unmarshal(buf, &raw)
if err != nil {
fmt.Println(string(buf))
return nil, fmt.Errorf("yaml parse error %q: %q", fn, err.Error())
}
buf2, err := json_Marshal(raw)
if err != nil {
fmt.Println(string(buf))
return nil, fmt.Errorf("json_Marshal(raw) error: %v", err)
}
err = json_Unmarshal(buf2, &o)
if err != nil {
return nil, fmt.Errorf("json_Unmarshal(buf2,o) error: %v", err)
}
if o.Kind != "" {
if o.RawMetadata == nil {
log.Fatalf("Metadata missing in raw object: %s in %s", o.Kind, fn)
}
if o.RawMetadata.Name == "" {
log.Fatalf("Name missing in raw object: %s in %s", o.Kind, fn)
}
if o.RawMetadata.Namespace == "" {
log.Fatalf("Namespace missing in raw object: %s %q in %s ", o.Kind, o.RawMetadata.Name, fn)
}
o = SK8config{
cfgType: typeKubectl,
RawYAML: buf,
Kind: o.Kind,
}
} else {
if depth < 5 && len(o.Parents) > 0 {
parent := &SK8config{}
parents := o.Parents
o.Parents = nil
log.Debugf("About to load parents: %v", parents)
for _, pn := range parents {
log.Debugf("%sInherit from %s", prefix, pn)
gps, err := loadFile(pn, depth+1, cfg)
if err != nil {
return nil, err
}
if len(gps) > 0 {
if len(gps) != 1 {
return nil, fmt.Errorf("multiple documents in parent %s", pn)
}
gp := gps[0]
if args.Debug && args.Verbose {
log.Debugf("Dump of intermediate parent: %s", pn)
dump(gp)
}
parent.mergeWith(gp)
}
}
parent.mergeWith(&o)
o = *parent
o.cfgType = typeSK8
}
}
}
result = append(result, &o)
}
return result, nil
}
func (cfg *SK8config) mergeWith(copyfrom *SK8config) *SK8config {
if copyfrom == nil {
return cfg
}
override := cfg.Override
cfg.Override = nil
features := make(map[string]bool)
for _, f := range cfg.Features {
features[f] = true
}
for _, f := range copyfrom.Features {
features[f] = true
}
containers := getContainers(cfg.Containers, copyfrom.Containers)
volumes := getVolumes(cfg.Volume, copyfrom.Volume)
overrideEnv(cfg, copyfrom)
v, _ := json_Marshal(copyfrom)
err := json_Unmarshal(v, cfg)
if err != nil {
panic(err)
}
var list []string
for f := range features {
list = append(list, f)
}
cfg.Features = list
cfg.Containers = containers
cfg.Volume = volumes
if override != nil {
return cfg.mergeWith(override)
}
return cfg
}
func getContainers(cfg, copyfrom []Container) []Container {
result := make([]Container, 0, len(cfg)+len(copyfrom))
names := make(map[string]Container)
for _, c := range cfg {
names[c.Name] = c
}
for _, c := range copyfrom {
if org, found := names[c.Name]; found {
v, _ := json_Marshal(c) // merge
err := json_Unmarshal(v, &org)
if err != nil {
log.Fatalf("Container '%s' could not be merged with override: %s", c.Name, err)
}
log.Infof("Container '%s' already exists, merge attempted", c.Name)
names[c.Name] = org
} else {
names[c.Name] = c
}
}
for _, c := range names {
result = append(result, c)
}
return result
}
func getVolumes(cfg, copyfrom []VolumeType) []VolumeType {
result := make([]VolumeType, 0, len(cfg)+len(copyfrom))
names := make(map[string]VolumeType)
for _, c := range cfg {
names[c.Name] = c
}
for _, c := range copyfrom {
if _, found := names[c.Name]; found {
log.Fatalf("Volume '%s' already exists, cannot overwrite", c.Name)
}
names[c.Name] = c
}
for _, c := range names {
result = append(result, c)
}
return result
}
func overrideEnv(cfg, copyfrom *SK8config) {
for k := range copyfrom.Env.Values {
delete(cfg.Env.Config, k)
delete(cfg.Env.Secret, k)
}
for k := range copyfrom.Env.Config {
delete(cfg.Env.Values, k)
delete(cfg.Env.Secret, k)
}
for k := range copyfrom.Env.Secret {
delete(cfg.Env.Values, k)
delete(cfg.Env.Config, k)
}
}
func (cfg *SK8config) fixFile() {
if cfg.Image == "" {
cfg.Image = cfg.Name
}
log.Debugf("%s: fixFile()", cfg.Name)
cwd, _ := os.Getwd()
for t, f := range cfg.Templates {
if !path.IsAbs(f) {
f2 := path.Join(cwd, f)
log.Debugf("change template %q -> %q", f, f2)
cfg.Templates[t] = f2
} else {
log.Debugf("template %q is already absolute", f)
}
}
}