This repository has been archived by the owner on Jan 8, 2024. It is now read-only.
/
pipeline.go
469 lines (398 loc) · 13.1 KB
/
pipeline.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
package config
import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/waypoint-plugin-sdk/component"
pb "github.com/hashicorp/waypoint/pkg/server/gen"
)
// Pipeline represents a single pipeline definition
type Pipeline struct {
Name string `hcl:",label"`
StepRaw []*hclStep `hcl:"step,block"`
Steps []*Step
ctx *hcl.EvalContext
Config *Config
}
// hclPipeline represents a raw HCL version of a pipeline config
type hclPipeline struct {
Name string `hcl:",label"`
// We need these raw values to determine the plugins need to be used.
StepRaw []*hclStep `hcl:"step,block"`
Body hcl.Body `hcl:",body"`
Remain hcl.Body `hcl:",remain"`
}
// Step are the step settings for pipelines
type Step struct {
Labels map[string]string `hcl:"labels,optional"`
Use *Use `hcl:"use,block"`
// Give this step a name
Name string `hcl:",label"`
// If set, this step will depend on the defined step. The default step
// will be the previously defined step in order that it was defined
// in a waypoint.hcl
DependsOn []string `hcl:"depends_on,optional"`
// The OCI image to use for executing this step
ImageURL string `hcl:"image_url,optional"`
// An optional embedded pipeline stanza
Pipeline *Pipeline `hcl:"pipeline,block"`
ctx *hcl.EvalContext
// Optional workspace scoping
Workspace string `hcl:"workspace,optional"`
}
// hclStep represents a raw HCL version of a step stanza in a pipeline config
type hclStep struct {
Name string `hcl:",label"`
// An optional embedded pipeline stanza
PipelineRaw *hclPipeline `hcl:"pipeline,block"`
Body hcl.Body `hcl:",body"`
Remain hcl.Body `hcl:",remain"`
}
// Pipelines returns the id of all the defined pipelines
func (c *Config) Pipelines() []string {
var result []string
for _, p := range c.hclConfig.Pipelines {
result = append(result, p.Name)
}
return result
}
// Pipeline returns the configured pipeline named n. If the pipeline doesn't
// exist, this will return (nil, nil).
// Note that currently this parsing function does not attempt to detect cycles
// between embedded pipelines.
func (c *Config) Pipeline(id string, ctx *hcl.EvalContext) (*Pipeline, error) {
// Find the pipeline by progressively decoding
var rawPipeline *hclPipeline
for _, p := range c.hclConfig.Pipelines {
if p.Name == id {
rawPipeline = p
break
}
}
if rawPipeline == nil {
return nil, nil
}
// Full decode
var pipeline Pipeline
if diag := gohcl.DecodeBody(rawPipeline.Body, finalizeContext(ctx), &pipeline); diag.HasErrors() {
return nil, diag
}
pipeline.Name = rawPipeline.Name
pipeline.ctx = ctx
pipeline.Config = c
if pipeline.Config != nil {
pipeline.Config.ctx = ctx
}
// decode all of the defined raw steps for a pipeline
var steps []*Step
for _, stepRaw := range pipeline.StepRaw {
// turn stepRaw into a staged Step
var step Step
if diag := gohcl.DecodeBody(stepRaw.Body, finalizeContext(ctx), &step); diag.HasErrors() {
return nil, diag
}
step.ctx = ctx
step.Name = stepRaw.Name
// Parse a nested pipeline step if defined
// TODO(briancain): At the moment, we're supporting singly nestested Pipeline
// stanzas. If we want to support fully nested embedded pipelines we'll need
// to turn this into a recursive function.
if stepRaw.PipelineRaw != nil {
var embedPipeline Pipeline
if diag := gohcl.DecodeBody(stepRaw.PipelineRaw.Body, finalizeContext(ctx), &embedPipeline); diag.HasErrors() {
return nil, diag
}
step.Pipeline = &Pipeline{
Name: stepRaw.PipelineRaw.Name,
ctx: ctx,
Config: c,
}
if step.Pipeline.Config != nil {
step.Pipeline.Config.ctx = ctx
}
// Parse all the steps
var embSteps []*Step
for _, embedStepRaw := range embedPipeline.StepRaw {
if embedStepRaw.PipelineRaw != nil {
// NOTE(briancain): For now, we artificially don't allow for more than
// one level of embedded pipeline nesting. That means this is invalid:
/*
pipeline "example" {
step "invalid" {
pipeline "level-one" {
step "nested" {
pipeline "level-two" {
// etc etc...
}
}
}
}
}
*/
return nil, status.Errorf(codes.FailedPrecondition,
"Step %q defined 2 levels of nesting for an embedded pipeline %q. "+
"Currently Waypoint only supports 1 level of nesting for embedded pipelines. "+
"You can instead define a pipeline and refer to it as a step rather than "+
"defining it directly inside a step.", embedStepRaw.Name, pipeline.Name)
}
// turn stepRaw into a staged Step
var embedStep Step
if diag := gohcl.DecodeBody(embedStepRaw.Body, finalizeContext(ctx), &embedStep); diag.HasErrors() {
return nil, diag
}
embedStep.ctx = ctx
embedStep.Name = embedStepRaw.Name
embSteps = append(embSteps, &embedStep)
}
step.Pipeline.Steps = embSteps
}
steps = append(steps, &step)
}
pipeline.Steps = steps
return &pipeline, nil
}
// PipelineProtos will take the existing HCL eval context, eval the config
// and translate the HCL result into a Pipeline Proto to be returned for
// operations such as ConfigSync.
// If a pipeline has an embedded pipeline defined, PipelineProtos will return
// each as its own separate Pipeline proto message where the step that defined
// the embedded pipeline is actually a Pipeline Step reference.
func (c *Config) PipelineProtos() ([]*pb.Pipeline, error) {
if c == nil {
// This is likely an internal error if this happens.
panic("attempted to construct pipeline proto on a nil genericConfig")
}
// Load HCL config and convert to a Pipeline proto
var result []*pb.Pipeline
for _, pl := range c.hclConfig.Pipelines {
pipeline, err := c.Pipeline(pl.Name, c.ctx)
if err != nil {
return nil, err
}
pipes, err := c.buildPipelineProto(pipeline)
if err != nil {
return nil, err
}
result = append(result, pipes...)
}
// We should validate cycles across pipelines here
return result, nil
}
// buildPipelineProto will recursively translate an hclPipeline into a protobuf
// Pipeline message.
func (c *Config) buildPipelineProto(pl *Pipeline) ([]*pb.Pipeline, error) {
var result []*pb.Pipeline
pipe := &pb.Pipeline{
Name: pl.Name,
Owner: &pb.Pipeline_Project{
Project: &pb.Ref_Project{
Project: c.hclConfig.Project,
},
},
}
steps := make(map[string]*pb.Pipeline_Step)
for i, step := range pl.Steps {
s := &pb.Pipeline_Step{
Name: step.Name,
DependsOn: step.DependsOn,
Image: step.ImageURL,
}
if step.Workspace != "" {
s.Workspace = &pb.Ref_Workspace{
Workspace: step.Workspace,
}
}
// If no dependency was explictily set, we rely on the previous step
if i != 0 && len(step.DependsOn) == 0 {
s.DependsOn = []string{pl.Steps[i-1].Name}
}
// We have an embeded pipeline for this step. This can either be an hclPipeline
// defined directly in the step, or a pipeline reference to another pipeline
// defined else where. If this is a ref, the raw hcl for the pipeline should
// be a "built-in" step of type "pipeline"
if step.Pipeline != nil {
// Parse the embedded pipeline assuming it has steps
if len(step.Pipeline.Steps) > 0 {
// This means this is an embedded pipeline, i.e. the HCL definition
// is nested within the step PipelineRaw. we parse that pipeline
// directly and store it as a separate pipeline, and make _this_ step
// a reference to the pipeline
// Parse nested pipeline steps
pipelines, err := c.buildPipelineProto(step.Pipeline)
if err != nil {
return nil, err
}
result = append(result, pipelines...)
// We check if this step references a separate pipeline by Owner
pipeName := step.Pipeline.Name
pipeProject := c.hclConfig.Project
// Add pipeline reference as a pipeline ref step for parent pipeline
s.Kind = &pb.Pipeline_Step_Pipeline_{
Pipeline: &pb.Pipeline_Step_Pipeline{
Ref: &pb.Ref_Pipeline{
Ref: &pb.Ref_Pipeline_Owner{
Owner: &pb.Ref_PipelineOwner{
Project: &pb.Ref_Project{
Project: pipeProject,
},
PipelineName: pipeName,
},
},
},
},
}
steps[step.Name] = s
}
continue // continue to build the rest of the parent pipeline
} // else handle any "built-in" steps
// NOTE(briancain): This is what you'd change to support future Step plugins
// or future built-in step operations.
switch step.Use.Type {
case "build":
var buildBody struct {
DisablePush bool `hcl:"disable_push,optional"`
}
if diag := gohcl.DecodeBody(step.Use.Body, finalizeContext(c.ctx), &buildBody); diag.HasErrors() {
return nil, diag
}
s.Kind = &pb.Pipeline_Step_Build_{
Build: &pb.Pipeline_Step_Build{
DisablePush: buildBody.DisablePush,
},
}
case "deploy":
var deployBody struct {
Release bool `hcl:"release,optional"`
}
if diag := gohcl.DecodeBody(step.Use.Body, finalizeContext(c.ctx), &deployBody); diag.HasErrors() {
return nil, diag
}
s.Kind = &pb.Pipeline_Step_Deploy_{
Deploy: &pb.Pipeline_Step_Deploy{
Release: deployBody.Release,
},
}
case "release":
var releaseBody struct {
DeploymentRef uint64 `hcl:"deployment_ref,optional"` // 0 or "unset" means latest
Prune *bool `hcl:"prune,optional"` // nil means unset, we default to "true"
PruneRetain int32 `hcl:"prune_retain,optional"`
PruneRetainOverride bool `hcl:"prune_retain_override,optional"`
}
if diag := gohcl.DecodeBody(step.Use.Body, finalizeContext(c.ctx), &releaseBody); diag.HasErrors() {
return nil, diag
}
// For parsing pipeline configs, an unset `deployment_ref` translates
// to the "latest" deployment. Otherwise if people want to release
// a specific deployment by sequence number, they can set it explicity.
deployRef := &pb.Ref_Deployment{
Ref: &pb.Ref_Deployment_Latest{
Latest: true,
},
}
if releaseBody.DeploymentRef != 0 {
deployRef = &pb.Ref_Deployment{
Ref: &pb.Ref_Deployment_Sequence{
Sequence: releaseBody.DeploymentRef,
},
}
}
// unset, so default to true
if releaseBody.Prune == nil {
b := true
releaseBody.Prune = &b
}
s.Kind = &pb.Pipeline_Step_Release_{
Release: &pb.Pipeline_Step_Release{
Deployment: deployRef,
Prune: *releaseBody.Prune,
PruneRetain: releaseBody.PruneRetain,
PruneRetainOverride: releaseBody.PruneRetainOverride,
},
}
case "up":
var upBody struct {
Prune bool `hcl:"prune,optional"`
PruneRetain int32 `hcl:"prune_retain,optional"`
PruneRetainOverride bool `hcl:"prune_retain_override,optional"`
}
if diag := gohcl.DecodeBody(step.Use.Body, finalizeContext(c.ctx), &upBody); diag.HasErrors() {
return nil, diag
}
s.Kind = &pb.Pipeline_Step_Up_{
Up: &pb.Pipeline_Step_Up{
Prune: upBody.Prune,
PruneRetain: upBody.PruneRetain,
PruneRetainOverride: upBody.PruneRetainOverride,
},
}
case "exec":
var execBody struct {
Command string `hcl:"command"`
Args []string `hcl:"args,optional"`
}
// Evaluate the step body hcl to get options
if diag := gohcl.DecodeBody(step.Use.Body, finalizeContext(c.ctx), &execBody); diag.HasErrors() {
return nil, diag
}
s.Kind = &pb.Pipeline_Step_Exec_{
Exec: &pb.Pipeline_Step_Exec{
Image: step.ImageURL,
Command: execBody.Command,
Args: execBody.Args,
},
}
case "pipeline":
var pipelineBody struct {
Project string `hcl:"project"`
Name string `hcl:"name"`
}
// Evaluate the step body hcl to get options
if diag := gohcl.DecodeBody(step.Use.Body, finalizeContext(c.ctx), &pipelineBody); diag.HasErrors() {
return nil, diag
}
// set to *Current* project if not set
if pipelineBody.Project == "" {
pipelineBody.Project = c.hclConfig.Project
}
// Add pipeline reference as a pipeline ref step for parent pipeline
s.Kind = &pb.Pipeline_Step_Pipeline_{
Pipeline: &pb.Pipeline_Step_Pipeline{
Ref: &pb.Ref_Pipeline{
Ref: &pb.Ref_Pipeline_Owner{
Owner: &pb.Ref_PipelineOwner{
Project: &pb.Ref_Project{
Project: pipelineBody.Project,
},
PipelineName: pipelineBody.Name,
},
},
},
},
}
case "":
return nil, status.Error(codes.FailedPrecondition, "step use label cannot be empty")
default:
return nil, status.Errorf(codes.Internal, "unsupported step plugin type: %q", step.Use.Type)
}
steps[step.Name] = s
}
pipe.Steps = steps
result = append(result, pipe)
return result, nil
}
// Ref returns the ref for this pipeline.
func (c *Pipeline) Ref() *pb.Ref_Pipeline {
return &pb.Ref_Pipeline{
Ref: &pb.Ref_Pipeline_Id{
Id: c.Name,
},
}
}
// Configure configures the plugin for a given Step with the use body of this operation.
func (s *Step) Configure(plugin interface{}, ctx *hcl.EvalContext) hcl.Diagnostics {
ctx = appendContext(s.ctx, ctx)
ctx = finalizeContext(ctx)
return component.Configure(plugin, s.Use.Body, ctx)
}