forked from syndesisio/syndesis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.go
264 lines (232 loc) · 6.31 KB
/
generator.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
//
//go:generate go run assets/assets_generate.go
package generator
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"reflect"
"sort"
"strings"
"text/template"
"github.com/hoisie/mustache"
"github.com/pkg/errors"
"github.com/syndesisio/syndesis/install/operator/pkg/apis/syndesis/v1alpha1"
"github.com/syndesisio/syndesis/install/operator/pkg/util"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"
)
type supportImages struct {
Postgresql string
OAuthProxy string
OAuthProxyImage string
Prometheus string
PostgresExporter string
}
type syndesisImages struct {
Rest string
Ui string
Verifier string
S2i string
Upgrade string
Komodo string
}
type images struct {
Support supportImages
Syndesis syndesisImages
ImageStreamNamespace string
SyndesisImagesPrefix string
OAuthProxyImagePrefix string
PrometheusImagePrefix string
PostgresExporterImagePrefix string
KomodoImagesPrefix string
CamelKBaseImagePrefix string
}
type tags struct {
Syndesis string
Postgresql string
OAuthProxy string
Prometheus string
Upgrade string
PostgresExporter string
Komodo string
CamelKBase string
}
type Context struct {
ProductName string
AllowLocalHost bool
Productized bool
EarlyAccess bool
Oso bool
Ocp bool
TagMinor string
TagMajor string
Registry string
Images images
Tags tags
Debug bool
PrometheusRules string
Env map[string]string
Syndesis *v1alpha1.Syndesis
ImagePullSecrets []string
Versions map[string]string
}
func AssetAsBytes(path string) ([]byte, error) {
file, err := GetAssetsFS().Open(path)
if err != nil {
return nil, err
}
defer file.Close()
prometheusRules, err := ioutil.ReadAll(file)
if err != nil {
return nil, err
}
return prometheusRules, nil
}
func RenderDir(directory string, context interface{}) ([]unstructured.Unstructured, error) {
return RenderFSDir(GetAssetsFS(), directory, context)
}
var templateFunctions = template.FuncMap{
"mapHasKey": func(item reflect.Value, key reflect.Value) (bool, error) {
if item.Kind() != reflect.Map {
return false, fmt.Errorf("mapHasKey requires a map type")
}
if x := item.MapIndex(key); x.IsValid() {
return true, nil
} else {
return false, nil
}
},
}
func RenderFSDir(assets http.FileSystem, directory string, context interface{}) ([]unstructured.Unstructured, error) {
f, err := assets.Open(directory)
if err != nil {
return nil, err
}
defer f.Close()
files, err := f.Readdir(-1)
if err != nil {
return nil, err
}
sort.Slice(files, func(i, j int) bool {
return files[i].Name() < files[j].Name()
})
response := []unstructured.Unstructured{}
for _, f := range files {
filePath := directory + f.Name()
r, err := Render(filePath, context)
if err != nil {
return nil, err
}
response = append(response, r...)
}
return response, nil
}
func Render(filePath string, context interface{}) ([]unstructured.Unstructured, error) {
var obj interface{} = nil
response := []unstructured.Unstructured{}
// We can load plain yml files..
if strings.HasSuffix(filePath, ".yml") || strings.HasSuffix(filePath, ".yaml") {
fileData, err := AssetAsBytes(filePath)
if err != nil {
return nil, err
}
err = util.UnmarshalYaml(fileData, &obj)
if err != nil {
return nil, errors.Errorf("%s:\n%s\n", err, string(fileData))
}
}
// We can process go lang templates.
if strings.HasSuffix(filePath, ".yml.tmpl") || strings.HasSuffix(filePath, ".yaml.tmpl") {
fileData, err := AssetAsBytes(filePath)
if err != nil {
return nil, err
}
tmpl, err := template.New(filePath).Funcs(templateFunctions).Parse(string(fileData))
if err != nil {
return nil, err
}
buffer := &bytes.Buffer{}
err = tmpl.Execute(buffer, context)
if err != nil {
return nil, err
}
rawYaml := buffer.Bytes()
err = util.UnmarshalYaml(rawYaml, &obj)
if err != nil {
return nil, errors.Errorf("%s: %s: \n%s\n", filePath, err, string(rawYaml))
}
}
// We can process mustache templates.
if strings.HasSuffix(filePath, ".yml.mustache") || strings.HasSuffix(filePath, ".yaml.mustache") {
fileData, err := AssetAsBytes(filePath)
if err != nil {
return nil, err
}
rawYaml := mustache.Render(string(fileData), context)
err = util.UnmarshalYaml([]byte(rawYaml), &obj)
if err != nil {
return nil, fmt.Errorf("%s:\n%s\n", err, rawYaml)
}
// This is only here to assist in migrating from mustache to go lang templates.
fileDataTmpl, err := AssetAsBytes(filePath + ".tmpl")
if err == nil {
t, err := template.New(filePath + ".tmpl").Parse(string(fileDataTmpl))
if err != nil {
return nil, err
}
x := &bytes.Buffer{}
err = t.Execute(x, context)
if err != nil {
return nil, err
}
var objTmpl interface{} = nil
err = util.UnmarshalYaml([]byte(x.String()), &objTmpl)
if err != nil {
return nil, fmt.Errorf("%s:\n%s\n", err, rawYaml)
}
must1 := string(Must(yaml.Marshal(obj)))
must2 := string(Must(yaml.Marshal(objTmpl)))
if must1 != must2 {
return nil, fmt.Errorf("template did not render the same way")
}
}
}
switch v := obj.(type) {
case []interface{}:
for _, value := range v {
if x, ok := value.(map[string]interface{}); ok {
u := unstructured.Unstructured{x}
//annotatedForDebugging(u, name, rawYaml)
response = append(response, u)
} else {
return nil, errors.New("list did not contain objects")
}
}
case map[string]interface{}:
u := unstructured.Unstructured{v}
//annotatedForDebugging(u, name, rawYaml)
response = append(response, u)
case nil:
// It's ok if a template chooses not to generate any resources..
default:
return nil, fmt.Errorf("Unexptected yaml unmarshal type: %v", obj)
}
return response, nil
}
func Must(data []byte, err error) []byte {
if err != nil {
panic(err)
}
return data
}
func annotatedForDebugging(u unstructured.Unstructured, location string, rawYaml string) {
annotations := u.GetAnnotations()
if annotations == nil {
annotations = map[string]string{}
}
annotations["template-location"] = location
annotations["template-rendered"] = rawYaml
u.SetAnnotations(annotations)
}