Skip to content

Commit

Permalink
Consolidate cancel and timeout logic
Browse files Browse the repository at this point in the history
Cancel and timeout do very similar things when they happen: they
update the status of the taskrun, set the completion time and try
and delete the pod.

Today this is done for the two cases in different places, the code
structured differently and the behaviour slightly different:
- log levels of the messages are different
- cancel does not set the completion time
- cancel does not check if the error on pod deletion is a NotFound

This commit introduces "HasTimedOut" to tasktun_types, which
matches what "IsCancelled" does. It introduces a "killTaskRun"
function that can be used by both cancel and timeout, with the
only different being the "Reason" and termination message.
The timeout_check module is not necessary anymore.

The check for IsCancelled and HasTimedOut are move out of
"reconcile" into "Reconcile", so that now "Reconcile" checks:
- HasStarted
- isDone
- IsCancelled
- HasTimedOut
and finally, if applicable, it invokes "reconcile".
  • Loading branch information
afrittoli committed Apr 14, 2020
1 parent fd7ffe3 commit 882452e
Show file tree
Hide file tree
Showing 8 changed files with 317 additions and 414 deletions.
25 changes: 25 additions & 0 deletions pkg/apis/pipeline/v1alpha1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ package v1alpha1

import (
"fmt"
"time"

apisconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -209,6 +211,29 @@ func (tr *TaskRun) IsCancelled() bool {
return tr.Spec.Status == TaskRunSpecStatusCancelled
}

// HasTimedOut returns true if the TaskRun runtime is beyond the allowed timeout
func (tr *TaskRun) HasTimedOut() bool {
if tr.Status.StartTime.IsZero() {
return false
}
timeout := tr.GetTimeout()
// If timeout is set to 0 or defaulted to 0, there is no timeout.
if timeout == apisconfig.NoTimeoutDuration {
return false
}
runtime := time.Since(tr.Status.StartTime.Time)
return runtime > timeout
}

func (tr *TaskRun) GetTimeout() time.Duration {
// Use the platform default is no timeout is set
if tr.Spec.Timeout == nil {
return apisconfig.DefaultTimeoutMinutes * time.Minute
} else {
return tr.Spec.Timeout.Duration
}
}

// GetRunKey return the taskrun key for timeout handler map
func (tr *TaskRun) GetRunKey() string {
// The address of the pointer is a threadsafe unique identifier for the taskrun
Expand Down
37 changes: 37 additions & 0 deletions pkg/apis/pipeline/v1alpha1/taskrun_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,40 @@ func TestTaskRunIsOfPipelinerun(t *testing.T) {
})
}
}

func TestHasTimedOut(t *testing.T) {
// IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC
zeroTime := time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC)
testCases := []struct {
name string
taskRun *v1alpha1.TaskRun
expectedStatus bool
}{{
name: "TaskRun not started",
taskRun: tb.TaskRun("test-taskrun-not-started", "foo", tb.TaskRunSpec(
tb.TaskRunTaskRef("task-name"),
), tb.TaskRunStatus(tb.StatusCondition(apis.Condition{}), tb.TaskRunStartTime(zeroTime))),
expectedStatus: false,
}, {
name: "TaskRun no timeout",
taskRun: tb.TaskRun("test-taskrun-no-timeout", "foo", tb.TaskRunSpec(
tb.TaskRunTaskRef("task-name"), tb.TaskRunTimeout(0),
), tb.TaskRunStatus(tb.StatusCondition(apis.Condition{}), tb.TaskRunStartTime(time.Now().Add(-15*time.Hour)))),
expectedStatus: false,
}, {
name: "TaskRun timed out",
taskRun: tb.TaskRun("test-taskrun-timeout", "foo", tb.TaskRunSpec(
tb.TaskRunTaskRef("task-name"), tb.TaskRunTimeout(10*time.Second),
), tb.TaskRunStatus(tb.StatusCondition(apis.Condition{}), tb.TaskRunStartTime(time.Now().Add(-15*time.Second)))),
expectedStatus: true,
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := tc.taskRun.HasTimedOut()
if d := cmp.Diff(result, tc.expectedStatus); d != "" {
t.Fatalf("-want, +got: %v", d)
}
})
}
}
53 changes: 0 additions & 53 deletions pkg/reconciler/taskrun/cancel.go

This file was deleted.

100 changes: 0 additions & 100 deletions pkg/reconciler/taskrun/cancel_test.go

This file was deleted.

Loading

0 comments on commit 882452e

Please sign in to comment.