-
Notifications
You must be signed in to change notification settings - Fork 29
/
expand.go
431 lines (356 loc) · 11.9 KB
/
expand.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
// SPDX-License-Identifier: Apache-2.0
package native
import (
"context"
"fmt"
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
"github.com/go-vela/server/compiler/registry"
"github.com/go-vela/server/compiler/template/native"
"github.com/go-vela/server/compiler/template/starlark"
"github.com/go-vela/server/compiler/types/pipeline"
"github.com/go-vela/server/compiler/types/raw"
"github.com/go-vela/server/compiler/types/yaml/yaml"
"github.com/go-vela/server/constants"
)
// ExpandStages injects the template for each
// templated step in every stage in a yaml configuration.
func (c *client) ExpandStages(ctx context.Context, s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData) (*yaml.Build, error) {
if len(tmpls) == 0 {
return s, nil
}
// iterate through all stages
for _, stage := range s.Stages {
// inject the templates into the steps for the stage
p, err := c.ExpandSteps(ctx, &yaml.Build{Steps: stage.Steps, Secrets: s.Secrets, Services: s.Services, Environment: s.Environment}, tmpls, r, c.GetTemplateDepth())
if err != nil {
return nil, err
}
stage.Steps = p.Steps
s.Secrets = p.Secrets
s.Services = p.Services
s.Environment = p.Environment
}
return s, nil
}
// ExpandSteps injects the template for each
// templated step in a yaml configuration.
//
//nolint:funlen,gocyclo // ignore function length
func (c *client) ExpandSteps(ctx context.Context, s *yaml.Build, tmpls map[string]*yaml.Template, r *pipeline.RuleData, depth int) (*yaml.Build, error) {
if len(tmpls) == 0 {
return s, nil
}
// return if max template depth has been reached
if depth == 0 {
retErr := fmt.Errorf("max template depth of %d exceeded", c.GetTemplateDepth())
return s, retErr
}
steps := yaml.StepSlice{}
secrets := s.Secrets
services := s.Services
environment := s.Environment
templates := s.Templates
if len(environment) == 0 {
environment = make(raw.StringSliceMap)
}
// iterate through each step
for _, step := range s.Steps {
// skip if no template is provided for the step
if len(step.Template.Name) == 0 {
// add existing step if no template
steps = append(steps, step)
continue
}
// lookup step template name
tmpl, ok := tmpls[step.Template.Name]
if !ok {
return s, fmt.Errorf("missing template source for template %s in pipeline for step %s", step.Template.Name, step.Name)
}
// if ruledata is nil (CompileLite), continue with expansion
if r != nil {
// form a one-step pipeline to prep for purge check
check := &yaml.StepSlice{step}
pipeline := &pipeline.Build{
Steps: *check.ToPipeline(),
}
pipeline, err := pipeline.Purge(r)
if err != nil {
return nil, fmt.Errorf("unable to purge pipeline: %w", err)
}
// if step purged, do not proceed with expansion
if len(pipeline.Steps) == 0 {
continue
}
}
// Create some default global environment inject vars
// these are used below to overwrite to an empty
// map if they should not be injected into a container
envGlobalSteps := s.Environment
if !s.Metadata.HasEnvironment("steps") {
envGlobalSteps = make(raw.StringSliceMap)
}
// inject environment information for template
step, err := c.EnvironmentStep(step, envGlobalSteps)
if err != nil {
return s, err
}
var (
bytes []byte
found bool
)
if bytes, found = c.TemplateCache[tmpl.Source]; !found {
bytes, err = c.getTemplate(ctx, tmpl, step.Template.Name)
if err != nil {
return s, err
}
}
// initialize variable map if not parsed from config
if len(step.Template.Variables) == 0 {
step.Template.Variables = make(map[string]interface{})
}
// inject template name into variables
step.Template.Variables["VELA_TEMPLATE_NAME"] = step.Template.Name
tmplBuild, err := c.mergeTemplate(bytes, tmpl, step)
if err != nil {
return s, err
}
// if template references other templates, expand again
if len(tmplBuild.Templates) != 0 {
// if the tmplBuild has render_inline but the parent build does not, abort
if tmplBuild.Metadata.RenderInline && !s.Metadata.RenderInline {
return s, fmt.Errorf("cannot use render_inline inside a called template (%s)", step.Template.Name)
}
templates = append(templates, tmplBuild.Templates...)
tmplBuild, err = c.ExpandSteps(ctx, tmplBuild, mapFromTemplates(tmplBuild.Templates), r, depth-1)
if err != nil {
return s, err
}
}
// loop over secrets within template
for _, secret := range tmplBuild.Secrets {
found := false
// loop over secrets within base configuration
for _, sec := range secrets {
// check if the template secret and base secret name match
if sec.Name == secret.Name {
found = true
}
}
// only append template secret if it does not exist within base configuration
if !secret.Origin.Empty() || !found {
secrets = append(secrets, secret)
}
}
// loop over services within template
for _, service := range tmplBuild.Services {
found := false
for _, serv := range services {
if serv.Name == service.Name {
found = true
}
}
// only append template service if it does not exist within base configuration
if !found {
services = append(services, service)
}
}
// loop over environment within template
for key, value := range tmplBuild.Environment {
found := false
for env := range environment {
if key == env {
found = true
}
}
// only append template environment if it does not exist within base configuration
if !found {
environment[key] = value
}
}
// add templated steps
steps = append(steps, tmplBuild.Steps...)
}
s.Steps = steps
s.Secrets = secrets
s.Services = services
s.Environment = environment
s.Templates = templates
return s, nil
}
// ExpandDeployment injects the template for a
// templated deployment config in a yaml configuration.
func (c *client) ExpandDeployment(ctx context.Context, b *yaml.Build, tmpls map[string]*yaml.Template) (*yaml.Build, error) {
if len(tmpls) == 0 {
return b, nil
}
if len(b.Deployment.Template.Name) == 0 {
return b, nil
}
// lookup step template name
tmpl, ok := tmpls[b.Deployment.Template.Name]
if !ok {
return b, fmt.Errorf("missing template source for template %s in pipeline for deployment config", b.Deployment.Template.Name)
}
bytes, err := c.getTemplate(ctx, tmpl, b.Deployment.Template.Name)
if err != nil {
return b, err
}
// initialize variable map if not parsed from config
if len(b.Deployment.Template.Variables) == 0 {
b.Deployment.Template.Variables = make(map[string]interface{})
}
tmplBuild, err := c.mergeDeployTemplate(bytes, tmpl, &b.Deployment)
if err != nil {
return b, err
}
b.Deployment = tmplBuild.Deployment
return b, nil
}
func (c *client) getTemplate(ctx context.Context, tmpl *yaml.Template, name string) ([]byte, error) {
var (
bytes []byte
err error
)
switch {
case c.local:
a := &afero.Afero{
Fs: afero.NewOsFs(),
}
// iterate over locally provided templates
for _, t := range c.localTemplates {
parts := strings.Split(t, ":")
if len(parts) != 2 {
return nil, fmt.Errorf("local templates must be provided in the form <name>:<path>, got %s", t)
}
// if local template has a match, read file path provided
if strings.EqualFold(tmpl.Name, parts[0]) {
bytes, err = a.ReadFile(parts[1])
if err != nil {
return bytes, err
}
return bytes, nil
}
}
// file type templates can be retrieved locally using `source`
if strings.EqualFold(tmpl.Type, "file") {
bytes, err = a.ReadFile(tmpl.Source)
if err != nil {
return nil, fmt.Errorf("unable to read file for template %s. `File` type templates must be located at `source` or supplied to local template files", tmpl.Name)
}
return bytes, nil
}
// local exec may still request remote templates
if !strings.EqualFold(tmpl.Type, "github") {
return nil, fmt.Errorf("unable to find template %s: not supplied in list %s", tmpl.Name, c.localTemplates)
}
fallthrough
case strings.EqualFold(tmpl.Type, "github"):
// parse source from template
src, err := c.Github.Parse(tmpl.Source)
if err != nil {
return bytes, fmt.Errorf("invalid template source provided for %s: %w", name, err)
}
// pull from github without auth when the host isn't provided or is set to github.com
if !c.UsePrivateGithub && (len(src.Host) == 0 || strings.Contains(src.Host, "github.com")) {
logrus.WithFields(logrus.Fields{
"org": src.Org,
"repo": src.Repo,
"path": src.Name,
"host": src.Host,
}).Tracef("Using GitHub client to pull template")
bytes, err = c.Github.Template(ctx, nil, src)
if err != nil {
return bytes, err
}
} else {
logrus.WithFields(logrus.Fields{
"org": src.Org,
"repo": src.Repo,
"path": src.Name,
"host": src.Host,
}).Tracef("Using authenticated GitHub client to pull template")
// verify private GitHub is actually set up
if c.PrivateGithub == nil {
return nil, fmt.Errorf("unable to fetch template %s: missing credentials", src.Name)
}
// use private (authenticated) github instance to pull from
bytes, err = c.PrivateGithub.Template(ctx, c.user, src)
if err != nil {
return bytes, err
}
}
case strings.EqualFold(tmpl.Type, "file"):
src := ®istry.Source{
Org: c.repo.GetOrg(),
Repo: c.repo.GetName(),
Name: tmpl.Source,
Ref: c.commit,
}
if !c.UsePrivateGithub {
logrus.WithFields(logrus.Fields{
"org": src.Org,
"repo": src.Repo,
"path": src.Name,
}).Tracef("Using GitHub client to pull template")
bytes, err = c.Github.Template(ctx, nil, src)
if err != nil {
return bytes, err
}
} else {
logrus.WithFields(logrus.Fields{
"org": src.Org,
"repo": src.Repo,
"path": src.Name,
}).Tracef("Using authenticated GitHub client to pull template")
if c.PrivateGithub == nil {
return nil, fmt.Errorf("unable to fetch template %s: missing credentials", src.Name)
}
// use private (authenticated) github instance to pull from
bytes, err = c.PrivateGithub.Template(ctx, c.user, src)
if err != nil {
return bytes, err
}
}
default:
return bytes, fmt.Errorf("unsupported template type: %v", tmpl.Type)
}
c.TemplateCache[tmpl.Source] = bytes
return bytes, nil
}
//nolint:lll // ignore long line length due to input arguments
func (c *client) mergeTemplate(bytes []byte, tmpl *yaml.Template, step *yaml.Step) (*yaml.Build, error) {
switch tmpl.Format {
case constants.PipelineTypeGo, "golang", "":
//nolint:lll // ignore long line length due to return
return native.Render(string(bytes), step.Name, step.Template.Name, step.Environment, step.Template.Variables)
case constants.PipelineTypeStarlark:
//nolint:lll // ignore long line length due to return
return starlark.Render(string(bytes), step.Name, step.Template.Name, step.Environment, step.Template.Variables, c.GetStarlarkExecLimit())
default:
//nolint:lll // ignore long line length due to return
return &yaml.Build{}, fmt.Errorf("format of %s is unsupported", tmpl.Format)
}
}
func (c *client) mergeDeployTemplate(bytes []byte, tmpl *yaml.Template, d *yaml.Deployment) (*yaml.Build, error) {
switch tmpl.Format {
case constants.PipelineTypeGo, "golang", "":
//nolint:lll // ignore long line length due to return
return native.Render(string(bytes), "", d.Template.Name, make(raw.StringSliceMap), d.Template.Variables)
case constants.PipelineTypeStarlark:
//nolint:lll // ignore long line length due to return
return starlark.Render(string(bytes), "", d.Template.Name, make(raw.StringSliceMap), d.Template.Variables, c.GetStarlarkExecLimit())
default:
//nolint:lll // ignore long line length due to return
return &yaml.Build{}, fmt.Errorf("format of %s is unsupported", tmpl.Format)
}
}
// helper function that creates a map of templates from a yaml configuration.
func mapFromTemplates(templates []*yaml.Template) map[string]*yaml.Template {
m := make(map[string]*yaml.Template)
for _, tmpl := range templates {
m[tmpl.Name] = tmpl
}
return m
}