-
Notifications
You must be signed in to change notification settings - Fork 28
/
graph.go
670 lines (557 loc) · 16.2 KB
/
graph.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
// SPDX-License-Identifier: Apache-2.0
package build
import (
"fmt"
"net/http"
"sort"
"strings"
"github.com/gin-gonic/gin"
"github.com/go-vela/server/compiler"
"github.com/go-vela/server/database"
"github.com/go-vela/server/router/middleware/build"
"github.com/go-vela/server/router/middleware/org"
"github.com/go-vela/server/router/middleware/repo"
"github.com/go-vela/server/router/middleware/user"
"github.com/go-vela/server/scm"
"github.com/go-vela/server/util"
"github.com/go-vela/types"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/go-vela/types/pipeline"
"github.com/sirupsen/logrus"
)
// Graph contains nodes, and relationships between nodes, or edges.
//
// a node is a pipeline stage and its relevant steps.
// an edge is a relationship between nodes, defined by the 'needs' tag.
//
// swagger:model Graph
type Graph struct {
BuildID int64 `json:"build_id"`
BuildNumber int `json:"build_number"`
Org string `json:"org"`
Repo string `json:"repo"`
Nodes map[int]*node `json:"nodes"`
Edges []*edge `json:"edges"`
}
// node represents a pipeline stage and its relevant steps.
type node struct {
ID int `json:"id"`
Cluster int `json:"cluster"`
Name string `json:"name"`
// vela metadata
Status string `json:"status"`
StartedAt int `json:"started_at"`
FinishedAt int `json:"finished_at"`
Steps []*library.Step `json:"steps"`
// unexported data used for building edges
Stage *pipeline.Stage `json:"-"`
}
// edge represents a relationship between nodes, primarily defined by a stage 'needs' tag.
type edge struct {
Cluster int `json:"cluster"`
Source int `json:"source"`
Destination int `json:"destination"`
// vela metadata
Status string `json:"status"`
}
// stg represents a stage's steps and some metadata for producing node/edge information.
type stg struct {
steps []*library.Step
// used for tracking stage status
success int
running int
failure int
killed int
canceled int
skipped int
errored int
startedAt int
finishedAt int
}
const (
// clusters determine graph orientation.
BuiltInCluster = 2
PipelineCluster = 1
ServiceCluster = 0
GraphComplexityLimit = 1000 // arbitrary value to limit render complexity.
)
// swagger:operation GET /api/v1/repos/{org}/{repo}/builds/{build}/graph builds GetBuildGraph
//
// Get directed a-cyclical graph for a build in the configured backend
//
// ---
// produces:
// - application/json
// parameters:
// - in: path
// name: org
// description: Name of the org
// required: true
// type: string
// - in: path
// name: repo
// description: Name of the repo
// required: true
// type: string
// - in: path
// name: build
// description: Build number
// required: true
// type: integer
// security:
// - ApiKeyAuth: []
// responses:
// '200':
// description: Successfully retrieved graph for the build
// type: json
// schema:
// "$ref": "#/definitions/Graph"
// '401':
// description: Unable to retrieve graph for the build — unauthorized
// schema:
// "$ref": "#/definitions/Error"
// '404':
// description: Unable to retrieve graph for the build — not found
// schema:
// "$ref": "#/definitions/Error"
// '500':
// description: Unable to retrieve graph for the build
// schema:
// "$ref": "#/definitions/Error"
// GetBuildGraph represents the API handler to capture a
// directed a-cyclical graph for a build from the configured backend.
//
//nolint:funlen,goconst,gocyclo // ignore function length and constants
func GetBuildGraph(c *gin.Context) {
// capture middleware values
b := build.Retrieve(c)
o := org.Retrieve(c)
r := repo.Retrieve(c)
u := user.Retrieve(c)
m := c.MustGet("metadata").(*types.Metadata)
ctx := c.Request.Context()
// update engine logger with API metadata
//
// https://pkg.go.dev/github.com/sirupsen/logrus?tab=doc#Entry.WithFields
entry := fmt.Sprintf("%s/%d", r.GetFullName(), b.GetNumber())
logger := logrus.WithFields(logrus.Fields{
"build": b.GetNumber(),
"org": o,
"repo": r.GetName(),
"user": u.GetName(),
})
baseErr := "unable to retrieve graph"
logger.Infof("constructing graph for build %s", entry)
logger.Info("retrieving pipeline configuration")
var config []byte
lp, err := database.FromContext(c).GetPipelineForRepo(ctx, b.GetCommit(), r)
if err != nil { // assume the pipeline doesn't exist in the database yet (before pipeline support was added)
// send API call to capture the pipeline configuration file
config, err = scm.FromContext(c).ConfigBackoff(ctx, u, r, b.GetCommit())
if err != nil {
retErr := fmt.Errorf("unable to get pipeline configuration for %s: %w", r.GetFullName(), err)
util.HandleError(c, http.StatusNotFound, retErr)
return
}
} else {
config = lp.GetData()
}
if config == nil {
retErr := fmt.Errorf("unable to get pipeline configuration for %s: config is nil", r.GetFullName())
util.HandleError(c, http.StatusNotFound, retErr)
return
}
// variable to store changeset files
var files []string
// check if the build event is not issue_comment
if !strings.EqualFold(b.GetEvent(), constants.EventComment) {
// check if the build event is not pull_request
if !strings.EqualFold(b.GetEvent(), constants.EventPull) {
// send API call to capture list of files changed for the commit
files, err = scm.FromContext(c).Changeset(ctx, u, r, b.GetCommit())
if err != nil {
retErr := fmt.Errorf("%s: failed to get changeset for %s: %w", baseErr, r.GetFullName(), err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
}
}
logger.Info("compiling pipeline configuration")
// parse and compile the pipeline configuration file
p, _, err := compiler.FromContext(c).
Duplicate().
WithBuild(b).
WithFiles(files).
WithCommit(b.GetCommit()).
WithMetadata(m).
WithRepo(r).
WithUser(u).
Compile(config)
if err != nil {
// format the error message with extra information
err = fmt.Errorf("unable to compile pipeline configuration for %s: %w", r.GetFullName(), err)
logger.Error(err.Error())
retErr := fmt.Errorf("%s: %w", baseErr, err)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
if p == nil {
retErr := fmt.Errorf("unable to compile pipeline configuration for %s: pipeline is nil", r.GetFullName())
logger.Error(retErr)
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
// skip the build if only the init or clone steps are found
skip := SkipEmptyBuild(p)
if skip != "" {
c.JSON(http.StatusOK, skip)
return
}
// retrieve the steps for the build from the step table
steps := []*library.Step{}
page := 1
perPage := 100
// only fetch steps when necessary
if len(p.Stages) > 0 || len(p.Steps) > 0 {
for page > 0 {
// retrieve build steps (per page) from the database
stepsPart, _, err := database.FromContext(c).ListStepsForBuild(ctx, b, nil, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to retrieve steps for build %s: %w", entry, err)
util.HandleError(c, http.StatusNotFound, retErr)
return
}
// add page of steps to list steps
steps = append(steps, stepsPart...)
// assume no more pages exist if under 100 results are returned
if len(stepsPart) < perPage {
page = 0
} else {
page++
}
}
}
if len(steps) == 0 {
retErr := fmt.Errorf("no steps found for build %s", entry)
util.HandleError(c, http.StatusNotFound, retErr)
return
}
// retrieve the services for the build from the service table
services := []*library.Service{}
page = 1
perPage = 100
// only fetch services when necessary
if len(p.Services) > 0 {
for page > 0 {
// retrieve build services (per page) from the database
servicesPart, _, err := database.FromContext(c).ListServicesForBuild(ctx, b, nil, page, perPage)
if err != nil {
retErr := fmt.Errorf("unable to retrieve services for build %s: %w", entry, err)
util.HandleError(c, http.StatusNotFound, retErr)
return
}
// add page of services to list services
services = append(services, servicesPart...)
// assume no more pages exist if under 100 results are returned
if len(servicesPart) < perPage {
page = 0
} else {
page++
}
}
}
// this is a simple check
// but it will save on processing a massive build that should not be rendered
complexity := len(steps) + len(p.Stages) + len(services)
if complexity > GraphComplexityLimit {
retErr := fmt.Errorf("build is too complex, too many resources")
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
logger.Info("generating build graph")
// create nodes from pipeline stages
nodes := make(map[int]*node)
// create edges from nodes
// an edge is as a relationship between two nodes
// that is defined by the 'needs' tag
edges := []*edge{}
// initialize a map for grouping steps by stage name
// and tracking stage information
stageMap := map[string]*stg{}
// build a map for step_id to pipeline step
stepMap := map[int]*pipeline.Container{}
for _, pStep := range p.Steps {
stepMap[pStep.Number] = pStep
}
for _, pStage := range p.Stages {
for _, pStep := range pStage.Steps {
stepMap[pStep.Number] = pStep
}
}
for _, step := range steps {
if step == nil {
continue
}
name := step.GetStage()
if len(name) == 0 {
name = step.GetName()
}
// initialize a stage tracker
if _, ok := stageMap[name]; !ok {
stageMap[name] = &stg{
steps: []*library.Step{},
}
}
// retrieve the stage to update
s, ok := stageMap[name]
if !ok {
continue
}
// count each step status
switch step.GetStatus() {
case constants.StatusRunning:
s.running++
case constants.StatusSuccess:
s.success++
case constants.StatusFailure:
stp, ok := stepMap[step.GetNumber()]
if ok && stp.Ruleset.Continue {
s.success++
} else {
s.failure++
}
case constants.StatusKilled:
s.killed++
case constants.StatusCanceled:
s.canceled++
case constants.StatusSkipped:
s.skipped++
case constants.StatusError:
s.errored++
default:
}
// keep track of the widest time window possible
if s.finishedAt == 0 || s.finishedAt < int(step.GetFinished()) {
s.finishedAt = int(step.GetFinished())
}
if s.startedAt == 0 || s.startedAt > int(step.GetStarted()) {
s.startedAt = int(step.GetStarted())
}
s.steps = append(s.steps, step)
}
// construct services nodes separately
// services are grouped via cluster and manually-constructed edges
for _, service := range services {
if service == nil {
continue
}
// create the node
nodeID := len(nodes)
node := nodeFromService(nodeID, service)
nodes[nodeID] = node
// create a sequential edge for nodes after the first
if nodeID > 0 {
edge := &edge{
Cluster: ServiceCluster,
Source: nodeID - 1,
Destination: nodeID,
Status: service.GetStatus(),
}
edges = append(edges, edge)
}
}
// construct pipeline stages nodes when stages exist
for _, stage := range p.Stages {
if stage == nil {
continue
}
// skip steps/stages that were not present in the build
// this fixes the scenario where mutable templates are updated
s, ok := stageMap[stage.Name]
if !ok {
continue
}
// scrub the environment
for _, step := range stage.Steps {
step.Environment = nil
}
// create the node
nodeID := len(nodes)
cluster := PipelineCluster
// override the cluster id for built-in nodes
// this allows for better layout control because all stages need 'clone'
if stage.Name == "init" {
cluster = BuiltInCluster
}
if stage.Name == "clone" {
cluster = BuiltInCluster
}
node := nodeFromStage(nodeID, cluster, stage, s)
nodes[nodeID] = node
}
// create single-step stages when no stages exist
if len(p.Stages) == 0 {
// sort steps by number
sort.Slice(steps, func(i, j int) bool {
return steps[i].GetNumber() < steps[j].GetNumber()
})
for _, step := range steps {
// mock stage for edge creation
stage := &pipeline.Stage{
Name: step.GetName(),
Needs: []string{},
}
s, ok := stageMap[stage.Name]
if !ok {
continue
}
// create the node
nodeID := len(nodes)
// no built-in step separation for graphs without stages
cluster := PipelineCluster
node := nodeFromStage(nodeID, cluster, stage, s)
nodes[nodeID] = node
}
}
// loop over all nodes and create edges based on 'needs'
for _, destinationNode := range nodes {
if destinationNode == nil {
continue
}
// if theres no stage, skip because the edge is already created
if destinationNode.Stage == nil {
continue
}
// compare all nodes against all nodes
for _, sourceNode := range nodes {
if sourceNode.Stage == nil {
continue
}
// create a sequential edge for built-in nodes
if sourceNode.Cluster == BuiltInCluster &&
destinationNode.Cluster == BuiltInCluster &&
sourceNode.ID < destinationNode.ID &&
destinationNode.ID-sourceNode.ID == 1 {
edge := &edge{
Cluster: sourceNode.Cluster,
Source: sourceNode.ID,
Destination: destinationNode.ID,
Status: sourceNode.Status,
}
edges = append(edges, edge)
}
// skip normal processing for built-in nodes
if destinationNode.Cluster == BuiltInCluster ||
sourceNode.Cluster == BuiltInCluster {
continue
}
// dont compare the same node
if destinationNode.ID == sourceNode.ID {
continue
}
if destinationNode.Stage == nil {
continue
}
// use needs to create an edge
if len((*destinationNode.Stage).Needs) > 0 {
// check destination node needs
for _, need := range (*destinationNode.Stage).Needs {
// all stages need "clone"
if need == "clone" {
continue
}
// check destination needs source stage
if sourceNode.Stage.Name != need {
continue
}
// create an edge for source->destination
edge := &edge{
Cluster: sourceNode.Cluster,
Source: sourceNode.ID,
Destination: destinationNode.ID,
Status: sourceNode.Status,
}
edges = append(edges, edge)
}
} else {
// create an edge for source->next
edge := &edge{
Cluster: sourceNode.Cluster,
Source: sourceNode.ID,
Destination: sourceNode.ID + 1,
Status: sourceNode.Status,
}
edges = append(edges, edge)
}
}
}
// validate the generated graph's complexity is beneath the limit
if len(nodes)+len(edges) > GraphComplexityLimit {
retErr := fmt.Errorf("build is too complex, too many resources")
util.HandleError(c, http.StatusInternalServerError, retErr)
return
}
// construct the response
graph := Graph{
BuildID: b.GetID(),
BuildNumber: b.GetNumber(),
Org: r.GetOrg(),
Repo: r.GetName(),
Nodes: nodes,
Edges: edges,
}
c.JSON(http.StatusOK, graph)
}
// nodeFromStage returns a new node from a stage.
func nodeFromStage(nodeID, cluster int, stage *pipeline.Stage, s *stg) *node {
return &node{
ID: nodeID,
Cluster: cluster,
Name: stage.Name,
Stage: stage,
Steps: s.steps,
Status: s.GetOverallStatus(),
StartedAt: s.startedAt,
FinishedAt: s.finishedAt,
}
}
// nodeFromService returns a new node from a service.
func nodeFromService(nodeID int, service *library.Service) *node {
return &node{
ID: nodeID,
Cluster: ServiceCluster,
Name: service.GetName(),
Stage: nil,
Steps: []*library.Step{},
Status: service.GetStatus(),
StartedAt: int(service.GetStarted()),
FinishedAt: int(service.GetFinished()),
}
}
// GetOverallStatus determines the "status" for a stage based on the steps within it.
// this could potentially get complicated with ruleset logic (continue/detach).
func (s *stg) GetOverallStatus() string {
if s.running > 0 {
return constants.StatusRunning
}
if s.failure > 0 {
return constants.StatusFailure
}
if s.errored > 0 {
return constants.StatusError
}
if s.killed >= len(s.steps) {
return constants.StatusKilled
}
if s.skipped > 0 {
return constants.StatusSkipped
}
if s.success > 0 {
return constants.StatusSuccess
}
if s.canceled > 0 {
return constants.StatusCanceled
}
return constants.StatusPending
}