Skip to content

Commit

Permalink
Define new types for Cloud Events
Browse files Browse the repository at this point in the history
Define new types in TaskRun to hold the information and status
about cloud events to be delivered once a TaskRun is complete.

Signed-off-by: Andrea Frittoli <andrea.frittoli@uk.ibm.com>
  • Loading branch information
afrittoli authored and tekton-robot committed Jul 22, 2019
1 parent 75bd14f commit 17a6a87
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 0 deletions.
61 changes: 61 additions & 0 deletions pkg/apis/pipeline/v1alpha1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,12 @@ type TaskRunStatus struct {
// Steps describes the state of each build step container.
// +optional
Steps []StepState `json:"steps,omitempty"`

// CloudEvents describe the state of each cloud event requested via a
// CloudEventResource.
// +optional
CloudEvents []CloudEventDelivery `json:"cloudEvents,omitempty"`

// RetriesStatus contains the history of TaskRunStatus in case of a retry in order to keep record of failures.
// All TaskRunStatus stored in RetriesStatus will have no date within the RetriesStatus as is redundant.
// +optional
Expand Down Expand Up @@ -165,12 +171,67 @@ func (tr *TaskRunStatus) SetCondition(newCond *apis.Condition) {
}
}

// InitializeCloudEvents initializes the CloudEvents part of the TaskRunStatus
// from a list of event targets
func (tr *TaskRunStatus) InitializeCloudEvents(targets []string) {
// len(nil slice) is 0
if len(targets) > 0 {
initialState := CloudEventDeliveryState{
Condition: CloudEventConditionUnknown,
RetryCount: 0,
}
events := make([]CloudEventDelivery, len(targets))
for idx, target := range targets {
events[idx] = CloudEventDelivery{
Target: target,
Status: initialState,
}
}
tr.CloudEvents = events
}
}

// StepState reports the results of running a step in the Task.
type StepState struct {
corev1.ContainerState
Name string `json:"name,omitempty"`
}

// CloudEventDelivery is the target of a cloud event along with the state of
// delivery.
type CloudEventDelivery struct {
// Target points to an addressable
Target string `json:"target,omitempty"`
Status CloudEventDeliveryState `json:"status,omitempty"`
}

// CloudEventCondition is a string that represents the condition of the event.
type CloudEventCondition string

const (
// CloudEventConditionUnknown means that the condition for the event to be
// triggered was not met yet, or we don't know the state yet.
CloudEventConditionUnknown CloudEventCondition = "Unknown"
// CloudEventConditionSent means that the event was sent successfully
CloudEventConditionSent CloudEventCondition = "Sent"
// CloudEventConditionFailed means that there was one or more attempts to
// send the event, and none was successful so far.
CloudEventConditionFailed CloudEventCondition = "Failed"
)

// CloudEventDeliveryState reports the state of a cloud event to be sent.
type CloudEventDeliveryState struct {
// Current status
Condition CloudEventCondition `json:"condition,omitempty"`
// SentAt is the time at which the last attempt to send the event was made
// +optional
SentAt *metav1.Time `json:"sentAt,omitempty"`
// Error is the text of error (if any)
Error string `json:"message"`
// RetryCount is the number of attempts of sending the cloud event
RetryCount int32 `json:"retryCount"`
}

// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

Expand Down
50 changes: 50 additions & 0 deletions pkg/apis/pipeline/v1alpha1/taskrun_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,53 @@ func TestTaskRunHasStarted(t *testing.T) {
})
}
}

func TestInitializeCloudEvents(t *testing.T) {
tests := []struct {
name string
targets []string
wantCloudEvents []v1alpha1.CloudEventDelivery
}{{
name: "testWithNilTarget",
targets: nil,
wantCloudEvents: nil,
},{
name: "testWithEmptyListTarget",
targets: make([]string, 0),
wantCloudEvents: nil,
},{
name: "testWithTwoTargets",
targets: []string{"target1", "target2"},
wantCloudEvents: []v1alpha1.CloudEventDelivery{
v1alpha1.CloudEventDelivery{
Target: "target1",
Status: v1alpha1.CloudEventDeliveryState{
Condition: v1alpha1.CloudEventConditionUnknown,
SentAt: nil,
Error: "",
RetryCount: 0,
},
},
v1alpha1.CloudEventDelivery{
Target: "target2",
Status: v1alpha1.CloudEventDeliveryState{
Condition: v1alpha1.CloudEventConditionUnknown,
SentAt: nil,
Error: "",
RetryCount: 0,
},
},
},
}}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
tr := tb.TaskRun("taskrunname", "testns", tb.TaskRunStatus())
trs := tr.Status
trs.InitializeCloudEvents(tc.targets)
gotCloudEvents := trs.CloudEvents
if diff := cmp.Diff(tc.wantCloudEvents, gotCloudEvents); diff != "" {
t.Errorf("Wrong Cloud Events (-want +got) = %s", diff)
}
})
}
}
44 changes: 44 additions & 0 deletions pkg/apis/pipeline/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 17a6a87

Please sign in to comment.