This repository has been archived by the owner on Jan 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
template_generator.go
453 lines (425 loc) · 15.7 KB
/
template_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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
package acsengine
import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"runtime/debug"
"strconv"
"strings"
"text/template"
"github.com/Azure/dcos-engine/pkg/api"
"github.com/Azure/dcos-engine/pkg/helpers"
"github.com/Azure/dcos-engine/pkg/i18n"
)
// TemplateGenerator represents the object that performs the template generation.
type TemplateGenerator struct {
ClassicMode bool
Translator *i18n.Translator
}
// InitializeTemplateGenerator creates a new template generator object
func InitializeTemplateGenerator(ctx Context, classicMode bool) (*TemplateGenerator, error) {
t := &TemplateGenerator{
ClassicMode: classicMode,
Translator: ctx.Translator,
}
if err := t.verifyFiles(); err != nil {
return nil, err
}
return t, nil
}
// GenerateTemplate generates the template from the API Model
func (t *TemplateGenerator) GenerateTemplate(containerService *api.ContainerService, generatorCode string, isUpgrade bool) (templateRaw string, parametersRaw string, certsGenerated bool, err error) {
// named return values are used in order to set err in case of a panic
templateRaw = ""
parametersRaw = ""
err = nil
var templ *template.Template
properties := containerService.Properties
// save the current orchestrator version and restore it after deploying.
// this allows us to deploy agents on the most recent patch without updating the orchestator version in the object
orchVersion := properties.OrchestratorProfile.OrchestratorVersion
defer func() {
properties.OrchestratorProfile.OrchestratorVersion = orchVersion
}()
setPropertiesDefaults(containerService, isUpgrade)
templ = template.New("dcos template").Funcs(t.getTemplateFuncMap(containerService))
files, baseFile, e := t.prepareTemplateFiles(properties)
if e != nil {
return "", "", false, e
}
for _, file := range files {
bytes, e := Asset(file)
if e != nil {
err = t.Translator.Errorf("Error reading file %s, Error: %s", file, e.Error())
return templateRaw, parametersRaw, certsGenerated, err
}
if _, err = templ.New(file).Parse(string(bytes)); err != nil {
return templateRaw, parametersRaw, certsGenerated, err
}
}
// template generation may have panics in the called functions. This catches those panics
// and ensures the panic is returned as an error
defer func() {
if r := recover(); r != nil {
s := debug.Stack()
err = fmt.Errorf("%v - %s", r, s)
// invalidate the template and the parameters
templateRaw = ""
parametersRaw = ""
}
}()
if !validateDistro(containerService) {
return templateRaw, parametersRaw, certsGenerated, fmt.Errorf("Invalid distro")
}
var b bytes.Buffer
if err = templ.ExecuteTemplate(&b, baseFile, properties); err != nil {
return templateRaw, parametersRaw, certsGenerated, err
}
templateRaw = b.String()
var parametersMap paramsMap
if parametersMap, err = getParameters(containerService, t.ClassicMode, generatorCode); err != nil {
return templateRaw, parametersRaw, certsGenerated, err
}
var parameterBytes []byte
if parameterBytes, err = helpers.JSONMarshal(parametersMap, false); err != nil {
return templateRaw, parametersRaw, certsGenerated, err
}
parametersRaw = string(parameterBytes)
return templateRaw, parametersRaw, certsGenerated, err
}
func (t *TemplateGenerator) verifyFiles() error {
allFiles := commonTemplateFiles
allFiles = append(allFiles, dcosTemplateFiles...)
for _, file := range allFiles {
if _, err := Asset(file); err != nil {
return t.Translator.Errorf("template file %s does not exist", file)
}
}
return nil
}
func (t *TemplateGenerator) prepareTemplateFiles(properties *api.Properties) ([]string, string, error) {
var files []string
var baseFile string
switch properties.OrchestratorProfile.OrchestratorType {
case api.DCOS:
files = append(commonTemplateFiles, dcosTemplateFiles...)
baseFile = dcosBaseFile
default:
return nil, "", t.Translator.Errorf("orchestrator '%s' is unsupported", properties.OrchestratorProfile.OrchestratorType)
}
return files, baseFile, nil
}
// getTemplateFuncMap returns all functions used in template generation
func (t *TemplateGenerator) getTemplateFuncMap(cs *api.ContainerService) template.FuncMap {
return template.FuncMap{
"IsHostedMaster": func() bool {
return false
},
"HasPrivateRegistry": func() bool {
if cs.Properties.OrchestratorProfile.DcosConfig != nil {
return len(cs.Properties.OrchestratorProfile.DcosConfig.Registry) > 0
}
return false
},
"RequiresFakeAgentOutput": func() bool {
return false
},
"IsPublic": func(ports []int) bool {
return len(ports) > 0
},
"RequireRouteTable": func() bool {
return cs.Properties.OrchestratorProfile.RequireRouteTable()
},
"IsPrivateCluster": func() bool {
return false
},
"GetVNETSubnetDependencies": func() string {
return getVNETSubnetDependencies(cs.Properties)
},
"GetLBRules": func(name string, ports []int) string {
return getLBRules(name, ports)
},
"GetProbes": func(ports []int) string {
return getProbes(ports)
},
"GetSecurityRules": func(ports []int) string {
return getSecurityRules(ports)
},
"GetUniqueNameSuffix": func() string {
return GenerateClusterID(cs.Properties)
},
"GetVNETAddressPrefixes": func() string {
return getVNETAddressPrefixes(cs.Properties)
},
"GetVNETSubnets": func(addNSG bool) string {
return getVNETSubnets(cs.Properties, addNSG)
},
"GetDataDisks": func(profile *api.AgentPoolProfile) string {
return getDataDisks(profile)
},
"HasBootstrapPublicIP": func() bool {
return false
},
"IsHostedBootstrap": func() bool {
return false
},
"GetDCOSBootstrapCustomData": func() string {
masterIPList := generateIPList(cs.Properties.MasterProfile.Count, cs.Properties.MasterProfile.FirstConsecutiveStaticIP)
for i, v := range masterIPList {
masterIPList[i] = " - " + v
}
str := getSingleLineDCOSCustomData(
cs.Properties.OrchestratorProfile.OrchestratorType,
dcosBootstrapCustomdata, 0,
map[string]string{
"PROVISION_SOURCE_STR": getDCOSProvisionScript(dcosProvisionSource),
"PROVISION_STR": getDCOSProvisionScript(dcosBootstrapProvision),
"MASTER_IP_LIST": strings.Join(masterIPList, "\n"),
"BOOTSTRAP_IP": cs.Properties.OrchestratorProfile.DcosConfig.BootstrapProfile.StaticIP,
"BOOTSTRAP_OAUTH_ENABLED": strconv.FormatBool(cs.Properties.OrchestratorProfile.DcosConfig.BootstrapProfile.OAuthEnabled)})
return fmt.Sprintf("\"customData\": \"[base64(concat('#cloud-config\\n\\n', '%s'))]\",", str)
},
"GetDCOSMasterCustomData": func() string {
masterAttributeContents := getDCOSMasterCustomNodeLabels()
masterPreprovisionExtension := ""
if cs.Properties.MasterProfile.PreprovisionExtension != nil {
masterPreprovisionExtension += "\n"
masterPreprovisionExtension += makeMasterExtensionScriptCommands(cs)
}
var bootstrapIP string
if cs.Properties.OrchestratorProfile.DcosConfig != nil && cs.Properties.OrchestratorProfile.DcosConfig.BootstrapProfile != nil {
bootstrapIP = cs.Properties.OrchestratorProfile.DcosConfig.BootstrapProfile.StaticIP
}
str := getSingleLineDCOSCustomData(
cs.Properties.OrchestratorProfile.OrchestratorType,
getDCOSCustomDataTemplate(cs.Properties.OrchestratorProfile.OrchestratorType, cs.Properties.OrchestratorProfile.OrchestratorVersion),
cs.Properties.MasterProfile.Count,
map[string]string{
"PROVISION_SOURCE_STR": getDCOSProvisionScript(dcosProvisionSource),
"PROVISION_STR": getDCOSMasterProvisionScript(cs.Properties.OrchestratorProfile, bootstrapIP),
"ATTRIBUTES_STR": masterAttributeContents,
"PREPROVISION_EXTENSION": masterPreprovisionExtension,
"ROLENAME": "master"})
return fmt.Sprintf("\"customData\": \"[base64(concat('#cloud-config\\n\\n', '%s'))]\",", str)
},
"GetDCOSAgentCustomData": func(profile *api.AgentPoolProfile) string {
attributeContents := getDCOSAgentCustomNodeLabels(profile)
agentPreprovisionExtension := ""
if profile.PreprovisionExtension != nil {
agentPreprovisionExtension += "\n"
agentPreprovisionExtension += makeAgentExtensionScriptCommands(cs, profile)
}
var agentRoleName, bootstrapIP string
if len(profile.Ports) > 0 {
agentRoleName = "slave_public"
} else {
agentRoleName = "slave"
}
if cs.Properties.OrchestratorProfile.DcosConfig != nil && cs.Properties.OrchestratorProfile.DcosConfig.BootstrapProfile != nil {
bootstrapIP = cs.Properties.OrchestratorProfile.DcosConfig.BootstrapProfile.StaticIP
}
str := getSingleLineDCOSCustomData(
cs.Properties.OrchestratorProfile.OrchestratorType,
getDCOSCustomDataTemplate(cs.Properties.OrchestratorProfile.OrchestratorType, cs.Properties.OrchestratorProfile.OrchestratorVersion),
cs.Properties.MasterProfile.Count,
map[string]string{
"PROVISION_SOURCE_STR": getDCOSProvisionScript(dcosProvisionSource),
"PROVISION_STR": getDCOSAgentProvisionScript(profile, cs.Properties.OrchestratorProfile, bootstrapIP),
"ATTRIBUTES_STR": attributeContents,
"PREPROVISION_EXTENSION": agentPreprovisionExtension,
"ROLENAME": agentRoleName})
return fmt.Sprintf("\"customData\": \"[base64(concat('#cloud-config\\n\\n', '%s'))]\",", str)
},
"GetDCOSWindowsAgentCustomData": func(profile *api.AgentPoolProfile) string {
agentPreprovisionExtension := ""
if profile.PreprovisionExtension != nil {
agentPreprovisionExtension += "\n"
agentPreprovisionExtension += makeAgentExtensionScriptCommands(cs, profile)
}
b, err := Asset(dcosWindowsProvision)
if err != nil {
// this should never happen and this is a bug
panic(fmt.Sprintf("BUG: %s", err.Error()))
}
// translate the parameters
csStr := string(b)
csStr = strings.Replace(csStr, "PREPROVISION_EXTENSION", agentPreprovisionExtension, -1)
csStr = strings.Replace(csStr, "\r\n", "\n", -1)
str := getBase64CustomScriptFromStr(csStr)
return fmt.Sprintf("\"customData\": \"%s\"", str)
},
"GetDCOSWindowsAgentCustomNodeAttributes": func(profile *api.AgentPoolProfile) string {
return getDCOSWindowsAgentCustomAttributes(profile)
},
"GetDCOSWindowsAgentPreprovisionParameters": func(profile *api.AgentPoolProfile) string {
agentPreprovisionExtensionParameters := ""
if profile.PreprovisionExtension != nil {
agentPreprovisionExtensionParameters = getDCOSWindowsAgentPreprovisionParameters(cs, profile)
}
return agentPreprovisionExtensionParameters
},
"GetMasterAllowedSizes": func() string {
if t.ClassicMode {
return GetClassicAllowedSizes()
} else if cs.Properties.OrchestratorProfile.OrchestratorType == api.DCOS {
return GetDCOSMasterAllowedSizes()
}
return GetMasterAgentAllowedSizes()
},
"GetAgentAllowedSizes": func() string {
if t.ClassicMode {
return GetClassicAllowedSizes()
}
return GetMasterAgentAllowedSizes()
},
"GetSizeMap": func() string {
if t.ClassicMode {
return GetClassicSizeMap()
}
return GetSizeMap()
},
"GetClassicMode": func() bool {
return t.ClassicMode
},
"Base64": func(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
},
"WriteLinkedTemplatesForExtensions": func() string {
extensions := getLinkedTemplatesForExtensions(cs.Properties)
return extensions
},
"GetLocation": func() string {
return cs.Location
},
"WrapAsVariable": func(s string) string {
return fmt.Sprintf("',variables('%s'),'", s)
},
"WrapAsVerbatim": func(s string) string {
return fmt.Sprintf("',%s,'", s)
},
"AnyAgentUsesAvailabilitySets": func() bool {
for _, agentProfile := range cs.Properties.AgentPoolProfiles {
if agentProfile.IsAvailabilitySets() {
return true
}
}
return false
},
"AnyAgentUsesVirtualMachineScaleSets": func() bool {
for _, agentProfile := range cs.Properties.AgentPoolProfiles {
if agentProfile.IsVirtualMachineScaleSets() {
return true
}
}
return false
},
"HasLinuxAgents": func() bool {
for _, agentProfile := range cs.Properties.AgentPoolProfiles {
if agentProfile.IsLinux() {
return true
}
}
return false
},
"HasLinuxSecrets": func() bool {
return cs.Properties.LinuxProfile.HasSecrets()
},
"HasCustomSearchDomain": func() bool {
return cs.Properties.LinuxProfile.HasSearchDomain()
},
"HasCustomNodesDNS": func() bool {
return cs.Properties.LinuxProfile.HasCustomNodesDNS()
},
"HasWindowsSecrets": func() bool {
return cs.Properties.WindowsProfile.HasSecrets()
},
"HasWindowsCustomImage": func() bool {
return cs.Properties.WindowsProfile.HasCustomImage()
},
"GetMasterOSImageOffer": func() string {
cloudSpecConfig := getCloudSpecConfig(cs.Location)
return fmt.Sprintf("\"%s\"", cloudSpecConfig.OSImageConfig[cs.Properties.MasterProfile.Distro].ImageOffer)
},
"GetMasterOSImagePublisher": func() string {
cloudSpecConfig := getCloudSpecConfig(cs.Location)
return fmt.Sprintf("\"%s\"", cloudSpecConfig.OSImageConfig[cs.Properties.MasterProfile.Distro].ImagePublisher)
},
"GetMasterOSImageSKU": func() string {
cloudSpecConfig := getCloudSpecConfig(cs.Location)
return fmt.Sprintf("\"%s\"", cloudSpecConfig.OSImageConfig[cs.Properties.MasterProfile.Distro].ImageSku)
},
"GetMasterOSImageVersion": func() string {
cloudSpecConfig := getCloudSpecConfig(cs.Location)
return fmt.Sprintf("\"%s\"", cloudSpecConfig.OSImageConfig[cs.Properties.MasterProfile.Distro].ImageVersion)
},
"GetAgentOSImageOffer": func(profile *api.AgentPoolProfile) string {
cloudSpecConfig := getCloudSpecConfig(cs.Location)
return fmt.Sprintf("\"%s\"", cloudSpecConfig.OSImageConfig[profile.Distro].ImageOffer)
},
"GetAgentOSImagePublisher": func(profile *api.AgentPoolProfile) string {
cloudSpecConfig := getCloudSpecConfig(cs.Location)
return fmt.Sprintf("\"%s\"", cloudSpecConfig.OSImageConfig[profile.Distro].ImagePublisher)
},
"GetAgentOSImageSKU": func(profile *api.AgentPoolProfile) string {
cloudSpecConfig := getCloudSpecConfig(cs.Location)
return fmt.Sprintf("\"%s\"", cloudSpecConfig.OSImageConfig[profile.Distro].ImageSku)
},
"GetAgentOSImageVersion": func(profile *api.AgentPoolProfile) string {
cloudSpecConfig := getCloudSpecConfig(cs.Location)
return fmt.Sprintf("\"%s\"", cloudSpecConfig.OSImageConfig[profile.Distro].ImageVersion)
},
"UseAgentCustomImage": func(profile *api.AgentPoolProfile) bool {
imageRef := profile.ImageRef
return imageRef != nil && len(imageRef.Name) > 0 && len(imageRef.ResourceGroup) > 0
},
"UseMasterCustomImage": func() bool {
imageRef := cs.Properties.MasterProfile.ImageRef
return imageRef != nil && len(imageRef.Name) > 0 && len(imageRef.ResourceGroup) > 0
},
"PopulateClassicModeDefaultValue": func(attr string) string {
var val string
if !t.ClassicMode {
val = ""
} else {
switch attr {
case "generatorCode":
val = DefaultGeneratorCode
case "orchestratorName":
val = DefaultOrchestratorName
default:
val = ""
}
}
return fmt.Sprintf("\"defaultValue\": \"%s\",", val)
},
// inspired by http://stackoverflow.com/questions/18276173/calling-a-template-with-several-pipeline-parameters/18276968#18276968
"dict": func(values ...interface{}) (map[string]interface{}, error) {
if len(values)%2 != 0 {
return nil, errors.New("invalid dict call")
}
dict := make(map[string]interface{}, len(values)/2)
for i := 0; i < len(values); i += 2 {
key, ok := values[i].(string)
if !ok {
return nil, errors.New("dict keys must be strings")
}
dict[key] = values[i+1]
}
return dict, nil
},
"loop": func(min, max int) []int {
var s []int
for i := min; i <= max; i++ {
s = append(s, i)
}
return s
},
"subtract": func(a, b int) int {
return a - b
},
"IsCustomVNET": func() bool {
return isCustomVNET(cs.Properties.AgentPoolProfiles)
},
}
}