-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathcloudevent.go
113 lines (95 loc) · 3.86 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
/*
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 (
"errors"
"fmt"
"strings"
"github.com/google/uuid"
cloudevents "github.com/cloudevents/sdk-go/v2"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
"knative.dev/pkg/apis"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
)
// TektonEventType holds the types of cloud events sent by Tekton
type TektonEventType string
const (
// TektonTaskRunUnknownV1 is sent for TaskRuns with "ConditionSucceeded" "Unknown"
TektonTaskRunUnknownV1 TektonEventType = "dev.tekton.event.task.unknown.v1"
// TektonTaskRunSuccessfulV1 is sent for TaskRuns with "ConditionSucceeded" "True"
TektonTaskRunSuccessfulV1 TektonEventType = "dev.tekton.event.task.successful.v1"
// TektonTaskRunFailedV1 is sent for TaskRuns with "ConditionSucceeded" "False"
TektonTaskRunFailedV1 TektonEventType = "dev.tekton.event.task.failed.v1"
)
func (t TektonEventType) String() string {
return string(t)
}
// CEClient matches the `Client` interface from github.com/cloudevents/sdk-go/v2/cloudevents
type CEClient cloudevents.Client
// TektonCloudEventData type is used to marshal and unmarshal the payload of
// a Tekton cloud event. It only includes a TaskRun for now. Using a type opens
// the possibility for the future to add more data to the payload
type TektonCloudEventData struct {
TaskRun *v1beta1.TaskRun `json:"taskRun"`
}
// NewTektonCloudEventData returns a new instance of NewTektonCloudEventData
func NewTektonCloudEventData(taskRun *v1beta1.TaskRun) TektonCloudEventData {
return TektonCloudEventData{
TaskRun: taskRun,
}
}
// EventForTaskRun will create a new event based on a TaskRun,
// or return an error if not possible.
func EventForTaskRun(taskRun *v1beta1.TaskRun) (*cloudevents.Event, error) {
// Check if the TaskRun is defined
if taskRun == nil {
return nil, errors.New("Cannot send an event for an empty TaskRun")
}
event := cloudevents.NewEvent()
event.SetID(uuid.New().String())
event.SetSubject(taskRun.ObjectMeta.Name)
event.SetSource(taskRun.ObjectMeta.SelfLink) // TODO: SelfLink is deprecated
c := taskRun.Status.GetCondition(apis.ConditionSucceeded)
switch {
case c.IsUnknown():
event.SetType(TektonTaskRunUnknownV1.String())
case c.IsFalse():
event.SetType(TektonTaskRunFailedV1.String())
case c.IsTrue():
event.SetType(TektonTaskRunSuccessfulV1.String())
default:
return nil, fmt.Errorf("unknown condition for in TaskRun.Status %s", c.Status)
}
if err := event.SetData(cloudevents.ApplicationJSON, NewTektonCloudEventData(taskRun)); err != nil {
return nil, err
}
return &event, 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)
}),
}
}