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 15, 2020
1 parent 5d9c881 commit 2eb7777
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 454 deletions.
24 changes: 24 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 @@ -223,6 +225,28 @@ 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
}
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 @@ -215,3 +215,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)
}
})
}
}
11 changes: 11 additions & 0 deletions pkg/apis/pipeline/v1beta1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ func (trs *TaskRunStatus) MarkResourceNotConvertible(err *CannotConvertError) {
})
}

// MarkResourceFailed sets the ConditionSucceeded condition to ConditionFalse
// based on an error that occurred and a reason
func (trs *TaskRunStatus) MarkResourceFailed(reason string, err error) {
taskRunCondSet.Manage(trs).SetCondition(apis.Condition{
Type: apis.ConditionSucceeded,
Status: corev1.ConditionFalse,
Reason: reason,
Message: err.Error(),
})
}

// TaskRunStatusFields holds the fields of TaskRun's status. This is defined
// separately and inlined so that other types can readily consume these fields
// via duck typing.
Expand Down
1 change: 0 additions & 1 deletion pkg/reconciler/pipelinerun/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,4 @@ func assertErrIsNil(err error, message string, t *testing.T) {

func unregisterMetrics() {
metricstest.Unregister("pipelinerun_duration_seconds", "pipelinerun_count", "running_pipelineruns_count")

}
1 change: 1 addition & 0 deletions pkg/reconciler/pipelinerun/pipelinerun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func getRunName(pr *v1alpha1.PipelineRun) string {
// getPipelineRunController returns an instance of the PipelineRun controller/reconciler that has been seeded with
// d, where d represents the state of the system (existing resources) needed for the test.
func getPipelineRunController(t *testing.T, d test.Data) (test.Assets, func()) {
unregisterMetrics()
ctx, _ := ttesting.SetupFakeContext(t)
c, _ := test.SeedTestData(t, ctx, d)
configMapWatcher := configmap.NewInformedWatcher(c.Kube, system.GetNamespace())
Expand Down
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 2eb7777

Please sign in to comment.