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

Replace Clock package with k8s Clock package #4607

Merged
merged 1 commit into from
Feb 22, 2022
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
2 changes: 1 addition & 1 deletion cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (

"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/clock"
"github.com/tektoncd/pipeline/pkg/reconciler/pipelinerun"
"github.com/tektoncd/pipeline/pkg/reconciler/taskrun"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/client-go/rest"
filteredinformerfactory "knative.dev/pkg/client/injection/kube/informers/factory/filtered"
"knative.dev/pkg/controller"
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1alpha1/pipelinerun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/clock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/clock"
"knative.dev/pkg/apis"
)

Expand Down Expand Up @@ -161,7 +161,7 @@ func (pr *PipelineRun) GetRunKey() string {
}

// IsTimedOut returns true if a pipelinerun has exceeded its spec.Timeout based on its status.Timeout
func (pr *PipelineRun) IsTimedOut(c clock.Clock) bool {
func (pr *PipelineRun) IsTimedOut(c clock.PassiveClock) bool {
pipelineTimeout := pr.Spec.Timeout
startTime := pr.Status.StartTime

Expand Down
13 changes: 5 additions & 8 deletions pkg/apis/pipeline/v1alpha1/pipelinerun_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,12 @@ import (
"github.com/tektoncd/pipeline/test/diff"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/clock"
"knative.dev/pkg/apis"
)

var now = time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC)

type testClock struct{}

func (tc testClock) Now() time.Time { return now }
func (tc testClock) Since(t time.Time) time.Duration { return now.Sub(t) }
var testClock = clock.NewFakePassiveClock(now)

func TestPipelineRunStatusConditions(t *testing.T) {
p := &v1alpha1.PipelineRun{}
Expand Down Expand Up @@ -76,7 +73,7 @@ func TestInitializeConditions(t *testing.T) {
Namespace: "test-ns",
},
}
p.Status.InitializeConditions(testClock{})
p.Status.InitializeConditions(testClock)

if p.Status.TaskRuns == nil {
t.Fatalf("PipelineRun status not initialized correctly")
Expand All @@ -88,7 +85,7 @@ func TestInitializeConditions(t *testing.T) {

p.Status.TaskRuns["fooTask"] = &v1alpha1.PipelineRunTaskRunStatus{}

p.Status.InitializeConditions(testClock{})
p.Status.InitializeConditions(testClock)
if len(p.Status.TaskRuns) != 1 {
t.Fatalf("PipelineRun status getting reset")
}
Expand Down Expand Up @@ -233,7 +230,7 @@ func TestPipelineRunHasTimedOut(t *testing.T) {
},
}

if pr.IsTimedOut(testClock{}) != tc.expected {
if pr.IsTimedOut(testClock) != tc.expected {
t.Fatalf("Expected isTimedOut to be %t", tc.expected)
}
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1alpha1/run_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
runv1alpha1 "github.com/tektoncd/pipeline/pkg/apis/run/v1alpha1"
"github.com/tektoncd/pipeline/pkg/clock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/clock"
"knative.dev/pkg/apis"
duckv1 "knative.dev/pkg/apis/duck/v1"
)
Expand Down Expand Up @@ -206,7 +206,7 @@ func (r *Run) GetRunKey() string {
}

// HasTimedOut returns true if the Run's running time is beyond the allowed timeout
func (r *Run) HasTimedOut(c clock.Clock) bool {
func (r *Run) HasTimedOut(c clock.PassiveClock) bool {
if r.Status.StartTime == nil || r.Status.StartTime.IsZero() {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1alpha1/run_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ func TestRunHasTimedOut(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := tc.run.HasTimedOut(testClock{})
result := tc.run.HasTimedOut(testClock)
if d := cmp.Diff(result, tc.expectedValue); d != "" {
t.Fatalf(diff.PrintWantGot(d))
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1alpha1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
apisconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
"github.com/tektoncd/pipeline/pkg/clock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/clock"
"knative.dev/pkg/apis"
)

Expand Down Expand Up @@ -212,7 +212,7 @@ func (tr *TaskRun) IsCancelled() bool {
}

// HasTimedOut returns true if the TaskRun runtime is beyond the allowed timeout
func (tr *TaskRun) HasTimedOut(c clock.Clock) bool {
func (tr *TaskRun) HasTimedOut(c clock.PassiveClock) bool {
if tr.Status.StartTime.IsZero() {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1alpha1/taskrun_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func TestHasTimedOut(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := tc.taskRun.HasTimedOut(testClock{})
result := tc.taskRun.HasTimedOut(testClock)
if d := cmp.Diff(result, tc.expectedStatus); d != "" {
t.Fatalf(diff.PrintWantGot(d))
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/apis/pipeline/v1beta1/pipelinerun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ import (
apisconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
runv1alpha1 "github.com/tektoncd/pipeline/pkg/apis/run/v1alpha1"
"github.com/tektoncd/pipeline/pkg/clock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/clock"
"knative.dev/pkg/apis"
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
)
Expand Down Expand Up @@ -154,7 +154,7 @@ func (pr *PipelineRun) GetNamespacedName() types.NamespacedName {
}

// HasTimedOut returns true if a pipelinerun has exceeded its spec.Timeout based on its status.Timeout
func (pr *PipelineRun) HasTimedOut(ctx context.Context, c clock.Clock) bool {
func (pr *PipelineRun) HasTimedOut(ctx context.Context, c clock.PassiveClock) bool {
timeout := pr.PipelineTimeout(ctx)
startTime := pr.Status.StartTime

Expand Down Expand Up @@ -347,7 +347,7 @@ func (pr *PipelineRunStatus) GetCondition(t apis.ConditionType) *apis.Condition

// InitializeConditions will set all conditions in pipelineRunCondSet to unknown for the PipelineRun
// and set the started time to the current time
func (pr *PipelineRunStatus) InitializeConditions(c clock.Clock) {
func (pr *PipelineRunStatus) InitializeConditions(c clock.PassiveClock) {
started := false
if pr.TaskRuns == nil {
pr.TaskRuns = make(map[string]*PipelineRunTaskRunStatus)
Expand Down
15 changes: 6 additions & 9 deletions pkg/apis/pipeline/v1beta1/pipelinerun_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,12 @@ import (
"github.com/tektoncd/pipeline/test/diff"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/clock"
"knative.dev/pkg/apis"
)

var now = time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC)

type testClock struct{}

func (tc testClock) Now() time.Time { return now }
func (tc testClock) Since(t time.Time) time.Duration { return now.Sub(t) }
var testClock = clock.NewFakePassiveClock(now)

func TestPipelineRunStatusConditions(t *testing.T) {
p := &v1beta1.PipelineRun{}
Expand Down Expand Up @@ -77,7 +74,7 @@ func TestInitializePipelineRunConditions(t *testing.T) {
Namespace: "test-ns",
},
}
p.Status.InitializeConditions(testClock{})
p.Status.InitializeConditions(testClock)

if p.Status.TaskRuns == nil {
t.Fatalf("PipelineRun TaskRun status not initialized correctly")
Expand Down Expand Up @@ -106,7 +103,7 @@ func TestInitializePipelineRunConditions(t *testing.T) {
Message: "hello",
})

p.Status.InitializeConditions(testClock{})
p.Status.InitializeConditions(testClock)
if len(p.Status.TaskRuns) != 1 {
t.Fatalf("PipelineRun TaskRun status getting reset")
}
Expand Down Expand Up @@ -270,7 +267,7 @@ func TestPipelineRunHasTimedOut(t *testing.T) {
StartTime: &metav1.Time{Time: tc.starttime},
}},
}
if pr.HasTimedOut(context.Background(), testClock{}) != tc.expected {
if pr.HasTimedOut(context.Background(), testClock) != tc.expected {
t.Errorf("Expected HasTimedOut to be %t when using pipeline.timeout", tc.expected)
}
})
Expand All @@ -285,7 +282,7 @@ func TestPipelineRunHasTimedOut(t *testing.T) {
}},
}

if pr.HasTimedOut(context.Background(), testClock{}) != tc.expected {
if pr.HasTimedOut(context.Background(), testClock) != tc.expected {
t.Errorf("Expected HasTimedOut to be %t when using pipeline.timeouts.pipeline", tc.expected)
}
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/apis/pipeline/v1beta1/taskrun_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/config"
apisconfig "github.com/tektoncd/pipeline/pkg/apis/config"
"github.com/tektoncd/pipeline/pkg/apis/pipeline"
"github.com/tektoncd/pipeline/pkg/clock"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/clock"
"knative.dev/pkg/apis"
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
)
Expand Down Expand Up @@ -413,7 +413,7 @@ func (tr *TaskRun) IsCancelled() bool {
}

// HasTimedOut returns true if the TaskRun runtime is beyond the allowed timeout
func (tr *TaskRun) HasTimedOut(ctx context.Context, c clock.Clock) bool {
func (tr *TaskRun) HasTimedOut(ctx context.Context, c clock.PassiveClock) bool {
if tr.Status.StartTime.IsZero() {
return false
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/apis/pipeline/v1beta1/taskrun_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ func TestHasTimedOut(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
result := tc.taskRun.HasTimedOut(context.Background(), testClock{})
result := tc.taskRun.HasTimedOut(context.Background(), testClock)
if d := cmp.Diff(result, tc.expectedStatus); d != "" {
t.Fatalf(diff.PrintWantGot(d))
}
Expand Down
34 changes: 0 additions & 34 deletions pkg/clock/clock.go

This file was deleted.

4 changes: 2 additions & 2 deletions pkg/reconciler/events/cloudevent/cloud_event_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ import (
"github.com/tektoncd/pipeline/pkg/apis/pipeline/v1beta1"
resource "github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1"
"github.com/tektoncd/pipeline/pkg/apis/resource/v1alpha1/cloudevent"
"github.com/tektoncd/pipeline/pkg/clock"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/clock"
controller "knative.dev/pkg/controller"
"knative.dev/pkg/logging"
)
Expand Down Expand Up @@ -73,7 +73,7 @@ func cloudEventDeliveryFromTargets(targets []string) []v1beta1.CloudEventDeliver

// SendCloudEvents is used by the TaskRun controller to send cloud events once
// the TaskRun is complete. `tr` is used to obtain the list of targets
func SendCloudEvents(tr *v1beta1.TaskRun, ceclient CEClient, logger *zap.SugaredLogger, c clock.Clock) error {
func SendCloudEvents(tr *v1beta1.TaskRun, ceclient CEClient, logger *zap.SugaredLogger, c clock.PassiveClock) error {
logger = logger.With(zap.String("taskrun", tr.Name))

// Make the event we would like to send:
Expand Down
11 changes: 4 additions & 7 deletions pkg/reconciler/events/cloudevent/cloud_event_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
eventstest "github.com/tektoncd/pipeline/test/events"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/client-go/tools/record"
"knative.dev/pkg/apis"
duckv1beta1 "knative.dev/pkg/apis/duck/v1beta1"
Expand All @@ -37,11 +38,7 @@ import (
)

var now = time.Date(2022, time.January, 1, 0, 0, 0, 0, time.UTC)

type testClock struct{}

func (testClock) Now() time.Time { return now }
func (testClock) Since(t time.Time) time.Duration { return now.Sub(t) }
var testClock = clock.NewFakePassiveClock(now)

func TestCloudEventDeliveryFromTargets(t *testing.T) {
tests := []struct {
Expand Down Expand Up @@ -236,7 +233,7 @@ func TestSendCloudEvents(t *testing.T) {
successfulBehaviour := FakeClientBehaviour{
SendSuccessfully: true,
}
err := SendCloudEvents(tc.taskRun, newFakeClient(&successfulBehaviour), logger, testClock{})
err := SendCloudEvents(tc.taskRun, newFakeClient(&successfulBehaviour), logger, testClock)
if err != nil {
t.Fatalf("Unexpected error sending cloud events: %v", err)
}
Expand Down Expand Up @@ -337,7 +334,7 @@ func TestSendCloudEventsErrors(t *testing.T) {
unsuccessfulBehaviour := FakeClientBehaviour{
SendSuccessfully: false,
}
err := SendCloudEvents(tc.taskRun, newFakeClient(&unsuccessfulBehaviour), logger, testClock{})
err := SendCloudEvents(tc.taskRun, newFakeClient(&unsuccessfulBehaviour), logger, testClock)
if err == nil {
t.Fatalf("Unexpected success sending cloud events: %v", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/reconciler/pipelinerun/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ import (
taskruninformer "github.com/tektoncd/pipeline/pkg/client/injection/informers/pipeline/v1beta1/taskrun"
pipelinerunreconciler "github.com/tektoncd/pipeline/pkg/client/injection/reconciler/pipeline/v1beta1/pipelinerun"
resourceinformer "github.com/tektoncd/pipeline/pkg/client/resource/injection/informers/resource/v1alpha1/pipelineresource"
"github.com/tektoncd/pipeline/pkg/clock"
"github.com/tektoncd/pipeline/pkg/pipelinerunmetrics"
cloudeventclient "github.com/tektoncd/pipeline/pkg/reconciler/events/cloudevent"
"github.com/tektoncd/pipeline/pkg/reconciler/volumeclaim"
"k8s.io/apimachinery/pkg/util/clock"
"k8s.io/client-go/tools/cache"
kubeclient "knative.dev/pkg/client/injection/kube/client"
"knative.dev/pkg/configmap"
Expand All @@ -41,7 +41,7 @@ import (
)

// NewController instantiates a new controller.Impl from knative.dev/pkg/controller
func NewController(opts *pipeline.Options, clock clock.Clock) func(context.Context, configmap.Watcher) *controller.Impl {
func NewController(opts *pipeline.Options, clock clock.PassiveClock) func(context.Context, configmap.Watcher) *controller.Impl {
return func(ctx context.Context, cmw configmap.Watcher) *controller.Impl {
logger := logging.FromContext(ctx)
kubeclientset := kubeclient.Get(ctx)
Expand Down
Loading