-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
cloudevent.go
271 lines (253 loc) · 10.4 KB
/
cloudevent.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
/*
Copyright 2019 The Tekton Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cloudevent
import (
"context"
"errors"
"fmt"
"strings"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/google/uuid"
v1 "github.com/tektoncd/pipeline/pkg/apis/pipeline/v1"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"knative.dev/pkg/apis"
)
// TektonEventType holds the types of cloud events sent by Tekton
type TektonEventType string
const (
// TaskRunStartedEventV1 is sent for TaskRuns with "ConditionSucceeded" "Unknown"
// the first time they are picked up by the reconciler
TaskRunStartedEventV1 TektonEventType = "dev.tekton.event.taskrun.started.v1"
// TaskRunRunningEventV1 is sent for TaskRuns with "ConditionSucceeded" "Unknown"
// once the TaskRun is validated and Pod created
TaskRunRunningEventV1 TektonEventType = "dev.tekton.event.taskrun.running.v1"
// TaskRunUnknownEventV1 is sent for TaskRuns with "ConditionSucceeded" "Unknown"
// It can be used as a confirmation that the TaskRun is still running.
TaskRunUnknownEventV1 TektonEventType = "dev.tekton.event.taskrun.unknown.v1"
// TaskRunSuccessfulEventV1 is sent for TaskRuns with "ConditionSucceeded" "True"
TaskRunSuccessfulEventV1 TektonEventType = "dev.tekton.event.taskrun.successful.v1"
// TaskRunFailedEventV1 is sent for TaskRuns with "ConditionSucceeded" "False"
TaskRunFailedEventV1 TektonEventType = "dev.tekton.event.taskrun.failed.v1"
// PipelineRunStartedEventV1 is sent for PipelineRuns with "ConditionSucceeded" "Unknown"
// the first time they are picked up by the reconciler
PipelineRunStartedEventV1 TektonEventType = "dev.tekton.event.pipelinerun.started.v1"
// PipelineRunRunningEventV1 is sent for PipelineRuns with "ConditionSucceeded" "Unknown"
// once the PipelineRun is validated and Pod created
PipelineRunRunningEventV1 TektonEventType = "dev.tekton.event.pipelinerun.running.v1"
// PipelineRunUnknownEventV1 is sent for PipelineRuns with "ConditionSucceeded" "Unknown"
// It can be used as a confirmation that the PipelineRun is still running.
PipelineRunUnknownEventV1 TektonEventType = "dev.tekton.event.pipelinerun.unknown.v1"
// PipelineRunSuccessfulEventV1 is sent for PipelineRuns with "ConditionSucceeded" "True"
PipelineRunSuccessfulEventV1 TektonEventType = "dev.tekton.event.pipelinerun.successful.v1"
// PipelineRunFailedEventV1 is sent for PipelineRuns with "ConditionSucceeded" "False"
PipelineRunFailedEventV1 TektonEventType = "dev.tekton.event.pipelinerun.failed.v1"
// CustomRunStartedEventV1 is sent for CustomRuns with "ConditionSucceeded" "Unknown"
// the first time they are picked up by the reconciler
CustomRunStartedEventV1 TektonEventType = "dev.tekton.event.customrun.started.v1"
// CustomRunRunningEventV1 is sent for CustomRuns with "ConditionSucceeded" "Unknown"
// once the CustomRun is validated and Pod created
CustomRunRunningEventV1 TektonEventType = "dev.tekton.event.customrun.running.v1"
// CustomRunSuccessfulEventV1 is sent for CustomRuns with "ConditionSucceeded" "True"
CustomRunSuccessfulEventV1 TektonEventType = "dev.tekton.event.customrun.successful.v1"
// CustomRunFailedEventV1 is sent for CustomRuns with "ConditionSucceeded" "False"
CustomRunFailedEventV1 TektonEventType = "dev.tekton.event.customrun.failed.v1"
)
func (t TektonEventType) String() string {
return string(t)
}
// CEClient wraps the `Client` interface from github.com/cloudevents/sdk-go/v2/cloudevents
// and has methods to count the cloud events being sent, those methods are for testing purposes.
type CEClient interface {
cloudevents.Client
// addCount increments the count of events to be sent
addCount()
// decreaseCount decrements the count of events to be sent, indicating the event has been sent
decreaseCount()
}
// TektonCloudEventData type is used to marshal and unmarshal the payload of
// a Tekton cloud event. It can include a TaskRun or a PipelineRun
type TektonCloudEventData struct {
TaskRun *v1beta1.TaskRun `json:"taskRun,omitempty"`
PipelineRun *v1beta1.PipelineRun `json:"pipelineRun,omitempty"`
CustomRun *v1beta1.CustomRun `json:"customRun,omitempty"`
}
// newTektonCloudEventData returns a new instance of TektonCloudEventData
func newTektonCloudEventData(ctx context.Context, runObject objectWithCondition) (TektonCloudEventData, error) {
tektonCloudEventData := TektonCloudEventData{}
switch v := runObject.(type) {
case *v1beta1.TaskRun:
tektonCloudEventData.TaskRun = v
case *v1beta1.PipelineRun:
tektonCloudEventData.PipelineRun = v
case *v1.TaskRun:
v1beta1TaskRun := &v1beta1.TaskRun{}
if err := v1beta1TaskRun.ConvertFrom(ctx, v); err != nil {
return TektonCloudEventData{}, err
}
tektonCloudEventData.TaskRun = v1beta1TaskRun
case *v1.PipelineRun:
v1beta1PipelineRun := &v1beta1.PipelineRun{}
if err := v1beta1PipelineRun.ConvertFrom(ctx, v); err != nil {
return TektonCloudEventData{}, err
}
tektonCloudEventData.PipelineRun = v1beta1PipelineRun
case *v1beta1.CustomRun:
tektonCloudEventData.CustomRun = v
}
return tektonCloudEventData, nil
}
// eventForObjectWithCondition creates a new event based for a objectWithCondition,
// or return an error if not possible.
func eventForObjectWithCondition(ctx context.Context, runObject objectWithCondition) (*cloudevents.Event, error) {
event := cloudevents.NewEvent()
event.SetID(uuid.New().String())
event.SetSubject(runObject.GetObjectMeta().GetName())
// TODO: SelfLink is deprecated https://github.com/tektoncd/pipeline/issues/2676
source := runObject.GetObjectMeta().GetSelfLink()
if source == "" {
gvk := runObject.GetObjectKind().GroupVersionKind()
source = fmt.Sprintf("/apis/%s/%s/namespaces/%s/%s/%s",
gvk.Group,
gvk.Version,
runObject.GetObjectMeta().GetNamespace(),
gvk.Kind,
runObject.GetObjectMeta().GetName())
}
event.SetSource(source)
eventType, err := getEventType(runObject)
if err != nil {
return nil, err
}
if eventType == nil {
return nil, errors.New("no matching event type found")
}
event.SetType(eventType.String())
tektonCloudEventData, err := newTektonCloudEventData(ctx, runObject)
if err != nil {
return nil, err
}
if err := event.SetData(cloudevents.ApplicationJSON, tektonCloudEventData); err != nil {
return nil, err
}
return &event, nil
}
func getEventType(runObject objectWithCondition) (*TektonEventType, error) {
var eventType TektonEventType
c := runObject.GetStatusCondition().GetCondition(apis.ConditionSucceeded)
if c == nil {
// When the `Run` is created, it may not have any condition until it's
// picked up by the `Run` reconciler. In that case we consider the run
// as started. In all other cases, conditions have to be initialised
switch runObject.(type) {
case *v1beta1.CustomRun:
eventType = CustomRunStartedEventV1
return &eventType, nil
default:
return nil, fmt.Errorf("no condition for ConditionSucceeded in %T", runObject)
}
}
switch {
case c.IsUnknown():
switch runObject.(type) {
case *v1beta1.TaskRun:
switch c.Reason {
case v1beta1.TaskRunReasonStarted.String():
eventType = TaskRunStartedEventV1
case v1beta1.TaskRunReasonRunning.String():
eventType = TaskRunRunningEventV1
default:
eventType = TaskRunUnknownEventV1
}
case *v1.TaskRun:
switch c.Reason {
case v1.TaskRunReasonStarted.String():
eventType = TaskRunStartedEventV1
case v1.TaskRunReasonRunning.String():
eventType = TaskRunRunningEventV1
default:
eventType = TaskRunUnknownEventV1
}
case *v1beta1.PipelineRun:
switch c.Reason {
case v1beta1.PipelineRunReasonStarted.String():
eventType = PipelineRunStartedEventV1
case v1beta1.PipelineRunReasonRunning.String():
eventType = PipelineRunRunningEventV1
default:
eventType = PipelineRunUnknownEventV1
}
case *v1.PipelineRun:
switch c.Reason {
case v1.PipelineRunReasonStarted.String():
eventType = PipelineRunStartedEventV1
case v1.PipelineRunReasonRunning.String():
eventType = PipelineRunRunningEventV1
default:
eventType = PipelineRunUnknownEventV1
}
case *v1beta1.CustomRun:
// CustomRun controller have the freedom of setting reasons as they wish
// so we cannot make many assumptions here. If a condition is set
// to unknown (not finished), we sent the running event
eventType = CustomRunRunningEventV1
}
case c.IsFalse():
switch runObject.(type) {
case *v1.TaskRun:
eventType = TaskRunFailedEventV1
case *v1.PipelineRun:
eventType = PipelineRunFailedEventV1
case *v1beta1.TaskRun:
eventType = TaskRunFailedEventV1
case *v1beta1.PipelineRun:
eventType = PipelineRunFailedEventV1
case *v1beta1.CustomRun:
eventType = CustomRunFailedEventV1
}
case c.IsTrue():
switch runObject.(type) {
case *v1beta1.TaskRun:
eventType = TaskRunSuccessfulEventV1
case *v1beta1.PipelineRun:
eventType = PipelineRunSuccessfulEventV1
case *v1.TaskRun:
eventType = TaskRunSuccessfulEventV1
case *v1.PipelineRun:
eventType = PipelineRunSuccessfulEventV1
case *v1beta1.CustomRun:
eventType = CustomRunSuccessfulEventV1
}
default:
return nil, fmt.Errorf("unknown condition for in %T.Status %s", runObject, c.Status)
}
return &eventType, nil
}
// GetCloudEventDeliveryCompareOptions returns compare options to sort
// and compare a list of CloudEventDelivery
func GetCloudEventDeliveryCompareOptions() []cmp.Option {
// Setup cmp options
cloudDeliveryStateCompare := func(x, y v1beta1.CloudEventDeliveryState) bool {
return cmp.Equal(x.Condition, y.Condition) && cmp.Equal(x.RetryCount, y.RetryCount)
}
less := func(x, y v1beta1.CloudEventDelivery) bool {
return strings.Compare(x.Target, y.Target) < 0 || (strings.Compare(x.Target, y.Target) == 0 && x.Status.SentAt.Before(y.Status.SentAt))
}
return []cmp.Option{
cmpopts.SortSlices(less),
cmp.Comparer(func(x, y v1beta1.CloudEventDelivery) bool {
return (strings.Compare(x.Target, y.Target) == 0) && cloudDeliveryStateCompare(x.Status, y.Status)
}),
}
}