forked from kubernetes/kops
/
options_loader.go
158 lines (126 loc) · 3.75 KB
/
options_loader.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
package loader
import (
"bytes"
"encoding/json"
"fmt"
"github.com/golang/glog"
"k8s.io/kops/upup/pkg/fi/utils"
"os"
"reflect"
"sort"
"strings"
"text/template"
)
const maxIterations = 10
type OptionsTemplate struct {
Name string
Tags []string
Template *template.Template
}
type OptionsLoader struct {
templates OptionsTemplateList
TemplateFunctions template.FuncMap
}
type OptionsTemplateList []*OptionsTemplate
func (a OptionsTemplateList) Len() int {
return len(a)
}
func (a OptionsTemplateList) Swap(i, j int) {
a[i], a[j] = a[j], a[i]
}
func (a OptionsTemplateList) Less(i, j int) bool {
l := a[i]
r := a[j]
// First ordering criteria: Execute things with fewer tags first (more generic)
if len(l.Tags) != len(r.Tags) {
return len(l.Tags) < len(r.Tags)
}
// TODO: lexicographic sort on tags, for full determinism?
// Final ordering criteria: order by name
return l.Name < r.Name
}
func NewOptionsLoader(templateFunctions template.FuncMap) *OptionsLoader {
l := &OptionsLoader{}
l.TemplateFunctions = make(template.FuncMap)
for k, v := range templateFunctions {
l.TemplateFunctions[k] = v
}
return l
}
func (l *OptionsLoader) AddTemplate(t *OptionsTemplate) {
l.templates = append(l.templates, t)
}
// iterate performs a single iteration of all the templates, executing each template in order
func (l *OptionsLoader) iterate(userConfig interface{}, current interface{}) (interface{}, error) {
sort.Sort(l.templates)
t := reflect.TypeOf(current).Elem()
next := reflect.New(t).Interface()
// Copy the current state before applying rules; they act as defaults
utils.JsonMergeStruct(next, current)
for _, t := range l.templates {
glog.V(2).Infof("executing template %s (tags=%s)", t.Name, t.Tags)
var buffer bytes.Buffer
err := t.Template.ExecuteTemplate(&buffer, t.Name, current)
if err != nil {
return nil, fmt.Errorf("error executing template %q: %v", t.Name, err)
}
yamlBytes := buffer.Bytes()
jsonBytes, err := utils.YamlToJson(yamlBytes)
if err != nil {
// TODO: It would be nice if yaml returned us the line number here
glog.Infof("error parsing yaml. yaml follows:")
for i, line := range strings.Split(string(yamlBytes), "\n") {
fmt.Fprintf(os.Stderr, "%3d: %s\n", i, line)
}
return nil, fmt.Errorf("error parsing yaml %q: %v", t.Name, err)
}
err = json.Unmarshal(jsonBytes, next)
if err != nil {
return nil, fmt.Errorf("error parsing yaml (converted to JSON) %q: %v", t.Name, err)
}
}
// Also copy the user-provided values after applying rules; they act as overrides now
utils.JsonMergeStruct(next, userConfig)
return next, nil
}
// Build executes the options configuration templates, until they converge
// It bails out after maxIterations
func (l *OptionsLoader) Build(userConfig interface{}) (interface{}, error) {
options := userConfig
iteration := 0
for {
nextOptions, err := l.iterate(userConfig, options)
if err != nil {
return nil, err
}
if reflect.DeepEqual(options, nextOptions) {
return options, nil
}
iteration++
if iteration > maxIterations {
return nil, fmt.Errorf("options did not converge after %d iterations", maxIterations)
}
options = nextOptions
}
}
// HandleOptions is the file handler for options files
// It builds a template with the file, and adds it to the list of options templates
func (l *OptionsLoader) HandleOptions(i *TreeWalkItem) error {
contents, err := i.ReadString()
if err != nil {
return err
}
t := template.New(i.RelativePath)
t.Funcs(l.TemplateFunctions)
_, err = t.Parse(contents)
if err != nil {
return fmt.Errorf("error parsing options template %q: %v", i.Path, err)
}
t.Option("missingkey=zero")
l.AddTemplate(&OptionsTemplate{
Name: i.RelativePath,
Tags: i.Tags,
Template: t,
})
return nil
}