/
pipelines.go
414 lines (383 loc) · 11.7 KB
/
pipelines.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
package pipelines
import (
"fmt"
"strconv"
"strings"
"time"
v1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-helpers/v3/pkg/kube/activities"
"github.com/jenkins-x/jx-helpers/v3/pkg/kube/naming"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"golang.org/x/text/cases"
"golang.org/x/text/language"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
)
// ToPipelineActivityName creates an activity name from a pipeline run
func ToPipelineActivityName(pr *v1beta1.PipelineRun, paList []v1.PipelineActivity) string {
labels := pr.Labels
if labels == nil {
return ""
}
build := labels["build"]
owner := naming.ToValidName(activities.GetLabel(labels, activities.OwnerLabels))
repository := naming.ToValidName(activities.GetLabel(labels, activities.RepoLabels))
branch := naming.ToValidName(activities.GetLabel(labels, activities.BranchLabels))
if owner == "" || repository == "" || branch == "" {
return ""
}
prefix := owner + "-" + repository + "-" + branch + "-"
if build == "" {
buildID := labels["lighthouse.jenkins-x.io/buildNum"]
if buildID == "" {
return ""
}
for i := range paList {
pa := &paList[i]
if pa.Labels == nil {
continue
}
if pa.Labels["buildID"] == buildID || pa.Labels["lighthouse.jenkins-x.io/buildNum"] == buildID {
if pa.Spec.Build != "" {
pr.Labels["build"] = pa.Spec.Build
return pa.Name
}
}
}
// no PA has the buildNum yet so lets try find the next PA build number...
b := 1
found := false
var name string
for i := range paList {
pa := &paList[i]
if strings.HasPrefix(pa.Name, prefix) {
buildNum, _ := strconv.Atoi(strings.Split(pa.Name, prefix)[1])
if buildNum >= b {
b = buildNum
found = true
}
}
}
if found {
b++
}
build = strconv.Itoa(b)
pr.Labels["build"] = build
name = naming.ToValidName(prefix + build)
return name
}
if build == "" {
return ""
}
return naming.ToValidName(prefix + build)
}
func ToPipelineActivity(pr *v1beta1.PipelineRun, pa *v1.PipelineActivity, overwriteSteps bool) {
annotations := pr.Annotations
labels := pr.Labels
if pa.APIVersion == "" {
pa.APIVersion = "jenkins.io/v1"
}
if pa.Kind == "" {
pa.Kind = "PipelineActivity"
}
pa.Namespace = pr.Namespace
if pa.Annotations == nil {
pa.Annotations = map[string]string{}
}
if pa.Labels == nil {
pa.Labels = map[string]string{}
}
for k, v := range annotations {
switch k {
case "lighthouse.jenkins-x.io/traceparent", "lighthouse.jenkins-x.io/tracestate":
// the opentelemetry annotations holding trace context shouldn't be copied to other resources
default:
pa.Annotations[k] = v
}
}
for k, v := range labels {
pa.Labels[k] = v
}
ps := &pa.Spec
if labels != nil {
if ps.GitOwner == "" {
ps.GitOwner = activities.GetLabel(labels, activities.OwnerLabels)
}
if ps.GitRepository == "" {
ps.GitRepository = activities.GetLabel(labels, activities.RepoLabels)
}
if ps.GitBranch == "" {
ps.GitBranch = activities.GetLabel(labels, activities.BranchLabels)
}
if ps.Build == "" {
ps.Build = activities.GetLabel(labels, activities.BuildLabels)
}
if ps.Context == "" {
ps.Context = activities.GetLabel(labels, activities.ContextLabels)
}
if ps.BaseSHA == "" {
ps.BaseSHA = labels["lighthouse.jenkins-x.io/baseSHA"]
}
if ps.LastCommitSHA == "" {
ps.LastCommitSHA = labels["lighthouse.jenkins-x.io/lastCommitSHA"]
}
}
if ps.GitOwner != "" && ps.GitRepository != "" && ps.GitBranch != "" && ps.Pipeline == "" {
ps.Pipeline = fmt.Sprintf("%s/%s/%s", ps.GitOwner, ps.GitRepository, ps.GitBranch)
}
if annotations != nil {
if ps.GitURL == "" {
ps.GitURL = annotations["lighthouse.jenkins-x.io/cloneURI"]
}
}
podName := ""
stageNames := map[string]bool{}
var steps []v1.PipelineActivityStep
if pr.Status.TaskRuns != nil {
for _, v := range pr.Status.TaskRuns {
stageName := strings.ReplaceAll(v.PipelineTaskName, "-", " ")
stageNames[stageName] = true
var stage *v1.PipelineActivityStep
if v.Status == nil {
continue
}
if podName == "" {
podName = v.Status.PodName
}
previousStepTerminated := false
for _, step := range v.Status.Steps {
name := step.Name
var started *metav1.Time
var completed *metav1.Time
status := v1.ActivityStatusTypePending
terminated := step.Terminated
if terminated != nil {
if terminated.ExitCode == 0 {
status = v1.ActivityStatusTypeSucceeded
} else if !terminated.FinishedAt.IsZero() {
switch terminated.Reason {
case v1beta1.TaskRunReasonTimedOut.String():
status = v1.ActivityStatusTypeTimedOut
case v1beta1.TaskRunReasonCancelled.String():
status = v1.ActivityStatusTypeCancelled
default:
status = v1.ActivityStatusTypeFailed
}
}
started = &terminated.StartedAt
completed = &terminated.FinishedAt
previousStepTerminated = true
} else if step.Running != nil {
if previousStepTerminated {
started = &step.Running.StartedAt
status = v1.ActivityStatusTypeRunning
}
previousStepTerminated = false
}
if status.IsTerminated() && completed == nil {
completed = &metav1.Time{
Time: time.Now(),
}
}
step := v1.CoreActivityStep{
Name: Humanize(name),
Description: "",
Status: status,
StartedTimestamp: started,
CompletedTimestamp: completed,
}
if stage == nil {
stage = &v1.PipelineActivityStep{
Kind: v1.ActivityStepKindTypeStage,
Stage: &v1.StageActivityStep{
CoreActivityStep: v1.CoreActivityStep{
// Name: Humanize(stageName),
Name: stageName,
Description: "",
Status: status,
StartedTimestamp: started,
},
},
}
}
stage.Stage.Steps = append(stage.Stage.Steps, step)
}
if len(v.Status.Steps) == 0 {
for _, m := range v.Status.Conditions {
// Only set the stage if the tekton pipeline run has succeeded and status is not unknown
if m.Type == apis.ConditionSucceeded && m.Status != corev1.ConditionUnknown {
// By default lets set the status to failed
// This is ok as a pipeline that has succeeded will have steps and status set to true
status := v1.ActivityStatusTypeFailed
switch m.Reason {
case v1beta1.TaskRunReasonTimedOut.String():
status = v1.ActivityStatusTypeTimedOut
case v1beta1.TaskRunReasonFailed.String():
status = v1.ActivityStatusTypeFailed
case v1beta1.TaskRunReasonCancelled.String():
status = v1.ActivityStatusTypeCancelled
}
stage = createStep(stageName, v.Status.StartTime, status)
}
}
}
if stage != nil {
// lets check we have a started time if we have at least 1 step
if stage.Stage != nil && len(stage.Stage.Steps) > 0 {
if stage.Stage.Steps[0].StartedTimestamp == nil {
stage.Stage.Steps[0].StartedTimestamp = &metav1.Time{
Time: time.Now(),
}
}
if stage.Stage.StartedTimestamp == nil {
stage.Stage.StartedTimestamp = stage.Stage.Steps[0].StartedTimestamp
}
// lets check the last step
lastStep := stage.Stage.Steps[len(stage.Stage.Steps)-1]
if stage.Stage.CompletedTimestamp == nil {
stage.Stage.CompletedTimestamp = lastStep.CompletedTimestamp
}
}
steps = append(steps, *stage)
}
}
}
if overwriteSteps {
for _, stage := range steps {
if stage.Stage == nil {
continue
}
idx := -1
found := false
for i := range ps.Steps {
s := &ps.Steps[i]
if s.Stage != nil && s.Stage.Name == stage.Stage.Name {
s.Stage = stage.Stage
found = true
break
}
if s.Kind == v1.ActivityStepKindTypePreview || s.Kind == v1.ActivityStepKindTypePromote {
if idx < 9 {
idx = i
}
}
}
if !found {
if idx < 0 {
ps.Steps = append(ps.Steps, stage)
} else {
// lets add the new stage before the preview/promote stages
var remaining []v1.PipelineActivityStep
if idx < len(ps.Steps) {
remaining = ps.Steps[idx:]
}
ps.Steps = append(ps.Steps[0:idx], stage)
ps.Steps = append(ps.Steps, remaining...)
}
}
}
} else {
// if the PipelineActivity has some real steps lets trust it; otherwise lets merge any preview/promote steps
// with steps from the PipelineRun
// lets add any missing steps from the PipelineActivity as they may have been created via a `jx promote` step
hasStep := false
for _, s := range ps.Steps {
if s.Kind == v1.ActivityStepKindTypeStage && s.Stage != nil && s.Stage.Name != "Release" {
hasStep = true
break
}
}
if !hasStep {
for _, s := range ps.Steps {
if s.Kind == v1.ActivityStepKindTypePreview || s.Kind == v1.ActivityStepKindTypePromote {
steps = append(steps, s)
}
}
ps.Steps = steps
}
}
if len(ps.Steps) > 0 && ps.StartedTimestamp == nil {
// lets default a start time
if ps.Steps[0].Stage != nil {
ps.StartedTimestamp = ps.Steps[0].Stage.StartedTimestamp
}
}
if ps.StartedTimestamp == nil {
ps.StartedTimestamp = &metav1.Time{
Time: time.Now(),
}
}
if len(ps.Steps) == 0 && !overwriteSteps {
ps.Steps = append(ps.Steps, v1.PipelineActivityStep{
Kind: v1.ActivityStepKindTypeStage,
Stage: &v1.StageActivityStep{
CoreActivityStep: v1.CoreActivityStep{
Name: "initialising",
Status: v1.ActivityStatusTypeRunning,
},
},
})
}
if podName != "" {
pa.Labels["podName"] = podName
}
activities.UpdateStatus(pa, false, nil)
addConditionsMessage(pr, pa)
addTaskRunsMessage(pr, pa)
}
// addConditionsMessage reads the pr and gets the message for each condition then add it to the pa as Spec.Message
// It also edit the message so that it says PipelineActivity instead of PipelineRun
// It also replaces the pr.name with pa.name
func addConditionsMessage(pr *v1beta1.PipelineRun, pa *v1.PipelineActivity) {
for k := range pr.Status.Conditions {
msg := pr.Status.Conditions[k].GetMessage()
if strings.Contains(msg, "PipelineRun") {
msg = strings.ReplaceAll(msg, "PipelineRun", "PipelineActivity")
}
if strings.Contains(msg, pa.Labels["tekton.dev/pipeline"]) {
msg = strings.ReplaceAll(msg, pa.Labels["tekton.dev/pipeline"], pa.Name)
}
pa.Spec.Message = v1.ActivityMessageType(msg)
}
}
// addConditionsMessage reads the pr and gets the message for each taskrun then add it to the pa as Spec.Steps[k].Stage.Message
// It replace TaskRun with Stage
func addTaskRunsMessage(pr *v1beta1.PipelineRun, pa *v1.PipelineActivity) {
k1 := 0
for k := range pr.Status.TaskRuns {
msg := ""
for k2 := range pr.Status.TaskRuns[k].Status.Conditions {
msg = pr.Status.TaskRuns[k].Status.Conditions[k2].Message
}
msg = strings.ReplaceAll(msg, "TaskRun", "Stage")
// Without this check, there is a panic in the codebase
// ToDo(@maintainers): Test case for this
if len(pa.Spec.Steps) > k1 {
pa.Spec.Steps[k1].Stage.Message = v1.ActivityMessageType(msg)
}
k1++
}
}
// Humanize splits into words and capitalises
func Humanize(text string) string {
wordsText := strings.ReplaceAll(strings.ReplaceAll(text, "-", " "), "_", " ")
words := strings.Split(wordsText, " ")
for i := range words {
words[i] = cases.Title(language.Und).String(words[i])
}
return strings.Join(words, " ")
}
func createStep(stageName string, startTime *metav1.Time, status v1.ActivityStatusType) *v1.PipelineActivityStep {
return &v1.PipelineActivityStep{
Kind: v1.ActivityStepKindTypeStage,
Stage: &v1.StageActivityStep{
CoreActivityStep: v1.CoreActivityStep{
Name: stageName,
Description: "",
Status: status,
StartedTimestamp: startTime,
},
},
}
}