/
jx_pipelineactivity_handler.go
324 lines (277 loc) · 9.87 KB
/
jx_pipelineactivity_handler.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
package trace
import (
"context"
"errors"
"fmt"
"strings"
jxv1 "github.com/jenkins-x/jx-api/v4/pkg/apis/jenkins.io/v1"
jenkinsiov1 "github.com/jenkins-x/jx-api/v4/pkg/client/clientset/versioned/typed/jenkins.io/v1"
lhv1alpha1 "github.com/jenkins-x/lighthouse/pkg/apis/lighthouse/v1alpha1"
lhutil "github.com/jenkins-x/lighthouse/pkg/util"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)
type JenkinsXPipelineActivityHandler struct {
BaseResourceEventHandler
Tracer trace.Tracer
PipelineActivityClient jenkinsiov1.PipelineActivityInterface
}
func (h *JenkinsXPipelineActivityHandler) OnAdd(obj interface{}) {
pa, ok := obj.(*jxv1.PipelineActivity)
if !ok {
h.Logger.Warningf("JenkinsXPipelineActivityHandler called with non PipelineActivity object: %T", obj)
return
}
if h.isJenkinsPipelineActivity(pa) {
return
}
log := h.Logger.WithField("PipelineActivity", pa.Name)
log.Trace("Handling PipelineActivity Added Event")
h.Store.AddJxPipelineActivity(pa)
err := h.handlePipelineActivity(pa)
if err != nil {
log.WithError(err).Error("failed to handle PipelineActivity")
return
}
}
func (h *JenkinsXPipelineActivityHandler) OnUpdate(oldObj, newObj interface{}) {
_, ok := oldObj.(*jxv1.PipelineActivity)
if !ok {
h.Logger.Warningf("JenkinsXPipelineActivityHandler called with non PipelineActivity object: %T", oldObj)
return
}
newPA, ok := newObj.(*jxv1.PipelineActivity)
if !ok {
h.Logger.Warningf("JenkinsXPipelineActivityHandler called with non PipelineActivity object: %T", newObj)
return
}
if h.isJenkinsPipelineActivity(newPA) {
return
}
log := h.Logger.WithField("PipelineActivity", newPA.Name)
log.Trace("Handling PipelineActivity Updated Event")
h.Store.AddJxPipelineActivity(newPA)
err := h.handlePipelineActivity(newPA)
if err != nil {
log.WithError(err).Error("failed to handle PipelineActivity")
return
}
}
func (h *JenkinsXPipelineActivityHandler) OnDelete(obj interface{}) {
pa, ok := obj.(*jxv1.PipelineActivity)
if !ok {
h.Logger.Warningf("JenkinsXPipelineActivityHandler called with non PipelineActivity object: %T", obj)
return
}
if h.isJenkinsPipelineActivity(pa) {
return
}
log := h.Logger.WithField("PipelineActivity", pa.Name)
log.Trace("Handling PipelineActivity Deleted Event")
h.Store.DeleteJxPipelineActivity(pa.Name)
}
func (h *JenkinsXPipelineActivityHandler) handlePipelineActivity(pa *jxv1.PipelineActivity) error {
log := h.Logger.WithField("PipelineActivity", pa.Name)
if pa.Annotations == nil {
pa.Annotations = make(map[string]string)
}
if pa.Spec.StartedTimestamp == nil {
return nil
}
span, err := h.getSpanFor(pa)
if errors.Is(err, ErrEventTraceNotFound) {
return nil
}
if errors.Is(err, ErrTraceNotFound) {
return nil
}
if errors.Is(err, ErrParentEntityNotFound) && pa.Spec.Status.IsTerminated() {
// PA is finished and its parent job may have been garbage-collected, ignore it
return nil
}
if err != nil {
return err
}
if !span.IsRecording() {
// span has already been ended, there's nothing more to do
return nil
}
if pa.Spec.CompletedTimestamp == nil {
// PA is not finished, let's wait until it's finished
return nil
}
log.WithField("traceID", span.SpanContext().TraceID()).WithField("spanID", span.SpanContext().SpanID()).Info("PipelineActivity complete - ending span")
ctx := trace.ContextWithSpan(context.Background(), span)
for i := range pa.Spec.Steps {
stageStep := pa.Spec.Steps[i]
if stageStep.Stage == nil {
continue
}
stage := stageStep.Stage
// ignore pending/running stages, which don't have started/completed timestamps
if !stage.Status.IsTerminated() {
continue
}
if stage.StartedTimestamp == nil {
continue
}
stageCtx, stageSpan := h.Tracer.Start(ctx, stage.Name,
trace.WithTimestamp(stage.StartedTimestamp.Time),
)
for j := range stage.Steps {
step := stage.Steps[j]
// ignore pending/running steps, which don't have started/completed timestamps
if !step.Status.IsTerminated() {
continue
}
if step.StartedTimestamp == nil {
continue
}
_, stepSpan := h.Tracer.Start(stageCtx, step.Name,
trace.WithTimestamp(step.StartedTimestamp.Time),
)
switch step.Status {
case jxv1.ActivityStatusTypeFailed:
stepSpan.SetStatus(codes.Error, stage.Description)
stepSpan.RecordError(errors.New(stage.Description))
default:
stepSpan.SetStatus(codes.Ok, step.Description)
}
if step.CompletedTimestamp == nil {
step.CompletedTimestamp = stage.CompletedTimestamp
}
if step.CompletedTimestamp == nil {
step.CompletedTimestamp = pa.Spec.CompletedTimestamp
}
stepSpan.End(
trace.WithTimestamp(step.CompletedTimestamp.Time),
)
}
switch stage.Status {
case jxv1.ActivityStatusTypeFailed:
stageSpan.SetStatus(codes.Error, stage.Description)
stageSpan.RecordError(errors.New(stage.Description))
default:
stageSpan.SetStatus(codes.Ok, stage.Description)
}
if stage.CompletedTimestamp == nil {
stage.CompletedTimestamp = pa.Spec.CompletedTimestamp
}
stageSpan.End(
trace.WithTimestamp(stage.CompletedTimestamp.Time),
)
}
switch pa.Spec.Status {
case jxv1.ActivityStatusTypeFailed:
span.SetStatus(codes.Error, "pipeline failed")
default:
span.SetStatus(codes.Ok, "")
}
span.End(
trace.WithTimestamp(pa.Spec.CompletedTimestamp.Time),
)
return nil
}
func (h *JenkinsXPipelineActivityHandler) getSpanFor(pa *jxv1.PipelineActivity) (*EventSpan, error) {
_, spanContext, err := extractTraceFrom(pa.Annotations)
if errors.Is(err, ErrTraceNotFound) {
err := h.createSpanFor(pa)
if err != nil {
return nil, err
}
_, spanContext, err = extractTraceFrom(pa.Annotations)
if err != nil {
return nil, fmt.Errorf("failed to find or create a span for PipelineActivity %s", pa.Name)
}
} else if err != nil {
return nil, err
}
eventTrace, err := h.Store.GetEventTrace(spanContext.TraceID())
if err != nil {
return nil, err
}
span, ok := eventTrace.GetSpan(spanContext.SpanID())
if !ok {
return nil, fmt.Errorf("no span found in event trace for spanID %q", spanContext.SpanID())
}
return &span, nil
}
func (h *JenkinsXPipelineActivityHandler) createSpanFor(pa *jxv1.PipelineActivity) error {
job, err := h.getParentLighthouseJob(pa)
if err != nil {
return fmt.Errorf("failed to find the parent LighthouseJob for Jenkins X PipelineActivity %s: %w", pa.Name, err)
}
ctx, parentSpanContext, err := extractTraceFrom(job.Annotations)
if err != nil {
return fmt.Errorf("failed to extract the trace from the LighthouseJob %s: %w", job.Name, err)
}
eventTrace, err := h.Store.GetEventTrace(parentSpanContext.TraceID())
if err != nil {
return err
}
if span, ok := eventTrace.FindSpanFor(EntityTypeJxPipelineActivity, pa.Name); ok {
injectTraceInto(trace.ContextWithSpan(context.Background(), span), pa.Annotations)
return nil
}
ctx, span := h.Tracer.Start(ctx, pa.Name,
trace.WithTimestamp(pa.Spec.StartedTimestamp.Time),
trace.WithAttributes(
attribute.Key("pipeline.owner").String(labelValue(pa.Labels, lhutil.OrgLabel, "owner")),
attribute.Key("pipeline.repository").String(labelValue(pa.Labels, lhutil.RepoLabel, "repository")),
attribute.Key("pipeline.branch").String(labelValue(pa.Labels, lhutil.BranchLabel, "branch")),
attribute.Key("pipeline.build").String(labelValue(pa.Labels, lhutil.BuildNumLabel, "build")),
attribute.Key("pipeline.context").String(labelValue(pa.Labels, lhutil.ContextLabel, "context")),
),
)
patch, err := injectTraceInto(ctx, pa.Annotations)
if err != nil {
return fmt.Errorf("failed to inject SpanContext into PipelineActivity %q: %w", pa.Name, err)
}
_, err = h.PipelineActivityClient.Patch(ctx, pa.Name, types.MergePatchType, []byte(patch), metav1.PatchOptions{})
if err != nil {
// TODO reset local PA instance (remove annotations?)
return fmt.Errorf("failed to patch PipelineActivity %q with patch %q: %w", pa.Name, patch, err)
}
// ensure the activity has a traceID annotation that can be used directly from the web UI
patch = fmt.Sprintf(`{"metadata": {"annotations": {"%s": "%s"}}}`, "lighthouse.jenkins-x.io/traceID", span.SpanContext().TraceID())
_, err = h.PipelineActivityClient.Patch(ctx, pa.Name, types.MergePatchType, []byte(patch), metav1.PatchOptions{})
if err != nil {
return fmt.Errorf("failed to patch PipelineActivity %q with patch %q: %w", pa.Name, patch, err)
}
h.Store.AddJxPipelineActivity(pa)
eventTrace.AddSpan(EventSpan{
Span: span,
Entity: Entity{
Type: EntityTypeJxPipelineActivity,
Name: pa.Name,
},
})
h.Logger.WithField("PipelineActivity", pa.Name).WithField("traceID", span.SpanContext().TraceID()).WithField("spanID", span.SpanContext().SpanID()).Info("Creating new Span")
return nil
}
func (h *JenkinsXPipelineActivityHandler) getParentLighthouseJob(pa *jxv1.PipelineActivity) (*lhv1alpha1.LighthouseJob, error) {
job := h.Store.FindLighthouseJob(map[string]string{
lhutil.OrgLabel: labelValue(pa.Labels, lhutil.OrgLabel, "owner"),
lhutil.RepoLabel: labelValue(pa.Labels, lhutil.RepoLabel, "repository"),
lhutil.BranchLabel: labelValue(pa.Labels, lhutil.BranchLabel, "branch"),
lhutil.BuildNumLabel: labelValue(pa.Labels, lhutil.BuildNumLabel, "build"),
})
if job == nil {
return nil, ErrParentEntityNotFound
}
return job, nil
}
// isJenkinsPipelineActivity returns true if the given PipelineActivity has been created by Jenkins
// see https://github.com/jenkinsci/jx-resources-plugin/blob/master/src/main/java/org/jenkinsci/plugins/jx/resources/BuildSyncRunListener.java#L106
func (h *JenkinsXPipelineActivityHandler) isJenkinsPipelineActivity(pa *jxv1.PipelineActivity) bool {
if strings.Contains(pa.Spec.BuildURL, "/blue/organizations/jenkins/") {
return true
}
if strings.Contains(pa.Spec.BuildLogsURL, "/job/") && strings.HasSuffix(pa.Spec.BuildLogsURL, "/console") {
return true
}
return false
}