-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathexpand.go
348 lines (288 loc) · 9.2 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
// SPDX-License-Identifier: Apache-2.0
package native
import (
"fmt"
"strings"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/pipeline"
"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/spf13/afero"
"github.com/go-vela/types/raw"
"github.com/go-vela/types/yaml"
"github.com/sirupsen/logrus"
)
// ExpandStages injects the template for each
// templated step in every stage in a yaml configuration.
func (c *client) ExpandStages(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(&yaml.Build{Steps: stage.Steps, Secrets: s.Secrets, Services: s.Services, Environment: s.Environment}, tmpls, r, c.TemplateDepth)
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.
func (c *client) ExpandSteps(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.TemplateDepth)
return s, retErr
}
steps := yaml.StepSlice{}
secrets := s.Secrets
services := s.Services
environment := s.Environment
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
}
bytes, err := c.getTemplate(tmpl, step.Template.Name)
if err != nil {
return s, err
}
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)
}
tmplBuild, err = c.ExpandSteps(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
return s, nil
}
func (c *client) getTemplate(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(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")
// use private (authenticated) github instance to pull from
bytes, err = c.PrivateGithub.Template(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(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")
// use private (authenticated) github instance to pull from
bytes, err = c.PrivateGithub.Template(c.user, src)
if err != nil {
return bytes, err
}
}
default:
return bytes, fmt.Errorf("unsupported template type: %v", tmpl.Type)
}
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.StarlarkExecLimit)
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
}