Skip to content

Commit

Permalink
Have a consistent GetTimeout on *Run types
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmoor committed Nov 6, 2020
1 parent a9213f2 commit 92e6f2f
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
10 changes: 10 additions & 0 deletions pkg/apis/pipeline/v1beta1/pipelinerun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

import (
"context"
"time"

"github.com/tektoncd/pipeline/pkg/apis/config"
Expand Down Expand Up @@ -98,6 +99,15 @@ func (pr *PipelineRun) IsCancelled() bool {
return pr.Spec.Status == PipelineRunSpecStatusCancelled
}

func (pr *PipelineRun) GetTimeout(ctx context.Context) time.Duration {
// Use the platform default is no timeout is set
if pr.Spec.Timeout == nil {
defaultTimeout := time.Duration(config.FromContextOrDefaults(ctx).Defaults.DefaultTimeoutMinutes)
return defaultTimeout * time.Minute
}
return pr.Spec.Timeout.Duration
}

// GetNamespacedName returns a k8s namespaced name that identifies this PipelineRun
func (pr *PipelineRun) GetNamespacedName() types.NamespacedName {
return types.NamespacedName{Namespace: pr.Namespace, Name: pr.Name}
Expand Down
11 changes: 7 additions & 4 deletions pkg/apis/pipeline/v1beta1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package v1beta1

import (
"context"
"fmt"
"time"

"github.com/tektoncd/pipeline/pkg/apis/config"
apisconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
corev1 "k8s.io/api/core/v1"
Expand Down Expand Up @@ -404,11 +406,11 @@ func (tr *TaskRun) IsCancelled() bool {
}

// HasTimedOut returns true if the TaskRun runtime is beyond the allowed timeout
func (tr *TaskRun) HasTimedOut() bool {
func (tr *TaskRun) HasTimedOut(ctx context.Context) bool {
if tr.Status.StartTime.IsZero() {
return false
}
timeout := tr.GetTimeout()
timeout := tr.GetTimeout(ctx)
// If timeout is set to 0 or defaulted to 0, there is no timeout.
if timeout == apisconfig.NoTimeoutDuration {
return false
Expand All @@ -417,10 +419,11 @@ func (tr *TaskRun) HasTimedOut() bool {
return runtime > timeout
}

func (tr *TaskRun) GetTimeout() time.Duration {
func (tr *TaskRun) GetTimeout(ctx context.Context) time.Duration {
// Use the platform default is no timeout is set
if tr.Spec.Timeout == nil {
return apisconfig.DefaultTimeoutMinutes * time.Minute
defaultTimeout := time.Duration(config.FromContextOrDefaults(ctx).Defaults.DefaultTimeoutMinutes)
return defaultTimeout * time.Minute
}
return tr.Spec.Timeout.Duration
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/apis/pipeline/v1beta1/taskrun_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1_test

import (
"context"
"testing"
"time"

Expand Down Expand Up @@ -329,7 +330,7 @@ func TestHasTimedOut(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := tc.taskRun.HasTimedOut()
result := tc.taskRun.HasTimedOut(context.Background())
if d := cmp.Diff(result, tc.expectedStatus); d != "" {
t.Fatalf(diff.PrintWantGot(d))
}
Expand Down
10 changes: 1 addition & 9 deletions pkg/reconciler/pipelinerun/pipelinerun.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (c *Reconciler) ReconcileKind(ctx context.Context, pr *v1beta1.PipelineRun)
// Compute the time since the task started.
elapsed := time.Since(pr.Status.StartTime.Time)
// Snooze this resource until the timeout has elapsed.
c.snooze(pr, getPipelineRunTimeout(ctx, pr)-elapsed)
c.snooze(pr, pr.GetTimeout(ctx)-elapsed)
}()

// Reconcile this copy of the pipelinerun and then write back any status or label
Expand Down Expand Up @@ -804,14 +804,6 @@ func combineTaskRunAndTaskSpecAnnotations(pr *v1beta1.PipelineRun, pipelineTask
return annotations
}

func getPipelineRunTimeout(ctx context.Context, pr *v1beta1.PipelineRun) time.Duration {
if pr.Spec.Timeout == nil {
defaultTimeout := time.Duration(config.FromContextOrDefaults(ctx).Defaults.DefaultTimeoutMinutes)
return defaultTimeout * time.Minute
}
return pr.Spec.Timeout.Duration
}

func getTaskRunTimeout(ctx context.Context, pr *v1beta1.PipelineRun, rprt *resources.ResolvedPipelineRunTask) *metav1.Duration {
var taskRunTimeout = &metav1.Duration{Duration: apisconfig.NoTimeoutDuration}

Expand Down
6 changes: 3 additions & 3 deletions pkg/reconciler/taskrun/taskrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ func (c *Reconciler) ReconcileKind(ctx context.Context, tr *v1beta1.TaskRun) pkg

// Check if the TaskRun has timed out; if it is, this will set its status
// accordingly.
if tr.HasTimedOut() {
message := fmt.Sprintf("TaskRun %q failed to finish within %q", tr.Name, tr.GetTimeout())
if tr.HasTimedOut(ctx) {
message := fmt.Sprintf("TaskRun %q failed to finish within %q", tr.Name, tr.GetTimeout(ctx))
err := c.failTaskRun(ctx, tr, v1beta1.TaskRunReasonTimedOut, message)
return c.finishReconcileUpdateEmitEvents(ctx, tr, before, err)
}
Expand All @@ -171,7 +171,7 @@ func (c *Reconciler) ReconcileKind(ctx context.Context, tr *v1beta1.TaskRun) pkg
// Compute the time since the task started.
elapsed := time.Since(tr.Status.StartTime.Time)
// Snooze this resource until the timeout has elapsed.
c.snooze(tr, tr.GetTimeout()-elapsed)
c.snooze(tr, tr.GetTimeout(ctx)-elapsed)
}()

// prepare fetches all required resources, validates them together with the
Expand Down

0 comments on commit 92e6f2f

Please sign in to comment.