Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define new types for Cloud Events #1092

Merged
merged 1 commit into from
Jul 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -128,6 +128,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 @@ -160,12 +166,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.