Skip to content

Commit 329f0f0

Browse files
mattmoortekton-robot
authored andcommitted
Refactor pollImmediateWithContext to share boilerplate
1 parent 1bd63aa commit 329f0f0

File tree

2 files changed

+36
-60
lines changed

2 files changed

+36
-60
lines changed

test/v1alpha1/wait.go

+18-30
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ const (
6565
// ConditionAccessorFn is a condition function used polling functions
6666
type ConditionAccessorFn func(ca apis.ConditionAccessor) (bool, error)
6767

68+
func pollImmediateWithContext(ctx context.Context, fn func() (bool, error)) error {
69+
return wait.PollImmediate(interval, timeout, func() (bool, error) {
70+
select {
71+
case <-ctx.Done():
72+
return true, ctx.Err()
73+
default:
74+
}
75+
return fn()
76+
})
77+
}
78+
6879
// WaitForTaskRunState polls the status of the TaskRun called name from client every
6980
// interval until inState returns `true` indicating it is done, returns an
7081
// error or timeout. desc will be used to name the metric that is emitted to
@@ -74,12 +85,7 @@ func WaitForTaskRunState(ctx context.Context, c *clients, name string, inState C
7485
_, span := trace.StartSpan(context.Background(), metricName)
7586
defer span.End()
7687

77-
return wait.PollImmediate(interval, timeout, func() (bool, error) {
78-
select {
79-
case <-ctx.Done():
80-
return true, ctx.Err()
81-
default:
82-
}
88+
return pollImmediateWithContext(ctx, func() (bool, error) {
8389
r, err := c.TaskRunClient.Get(ctx, name, metav1.GetOptions{})
8490
if err != nil {
8591
return true, err
@@ -97,12 +103,7 @@ func WaitForDeploymentState(ctx context.Context, c *clients, name string, namesp
97103
_, span := trace.StartSpan(context.Background(), metricName)
98104
defer span.End()
99105

100-
return wait.PollImmediate(interval, timeout, func() (bool, error) {
101-
select {
102-
case <-ctx.Done():
103-
return true, ctx.Err()
104-
default:
105-
}
106+
return pollImmediateWithContext(ctx, func() (bool, error) {
106107
d, err := c.KubeClient.Kube.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{})
107108
if err != nil {
108109
return true, err
@@ -120,12 +121,7 @@ func WaitForPodState(ctx context.Context, c *clients, name string, namespace str
120121
_, span := trace.StartSpan(context.Background(), metricName)
121122
defer span.End()
122123

123-
return wait.PollImmediate(interval, timeout, func() (bool, error) {
124-
select {
125-
case <-ctx.Done():
126-
return true, ctx.Err()
127-
default:
128-
}
124+
return pollImmediateWithContext(ctx, func() (bool, error) {
129125
r, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
130126
if err != nil {
131127
return true, err
@@ -143,12 +139,9 @@ func WaitForPipelineRunState(ctx context.Context, c *clients, name string, pollt
143139
_, span := trace.StartSpan(context.Background(), metricName)
144140
defer span.End()
145141

146-
return wait.PollImmediate(interval, polltimeout, func() (bool, error) {
147-
select {
148-
case <-ctx.Done():
149-
return true, ctx.Err()
150-
default:
151-
}
142+
ctx, cancel := context.WithTimeout(ctx, polltimeout)
143+
defer cancel()
144+
return pollImmediateWithContext(ctx, func() (bool, error) {
152145
r, err := c.PipelineRunClient.Get(ctx, name, metav1.GetOptions{})
153146
if err != nil {
154147
return true, err
@@ -166,12 +159,7 @@ func WaitForServiceExternalIPState(ctx context.Context, c *clients, namespace, n
166159
_, span := trace.StartSpan(context.Background(), metricName)
167160
defer span.End()
168161

169-
return wait.PollImmediate(interval, timeout, func() (bool, error) {
170-
select {
171-
case <-ctx.Done():
172-
return true, ctx.Err()
173-
default:
174-
}
162+
return pollImmediateWithContext(ctx, func() (bool, error) {
175163
r, err := c.KubeClient.Kube.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{})
176164
if err != nil {
177165
return true, err

test/wait.go

+18-30
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ const (
6565
// ConditionAccessorFn is a condition function used polling functions
6666
type ConditionAccessorFn func(ca apis.ConditionAccessor) (bool, error)
6767

68+
func pollImmediateWithContext(ctx context.Context, fn func() (bool, error)) error {
69+
return wait.PollImmediate(interval, timeout, func() (bool, error) {
70+
select {
71+
case <-ctx.Done():
72+
return true, ctx.Err()
73+
default:
74+
}
75+
return fn()
76+
})
77+
}
78+
6879
// WaitForTaskRunState polls the status of the TaskRun called name from client every
6980
// interval until inState returns `true` indicating it is done, returns an
7081
// error or timeout. desc will be used to name the metric that is emitted to
@@ -74,12 +85,7 @@ func WaitForTaskRunState(ctx context.Context, c *clients, name string, inState C
7485
_, span := trace.StartSpan(context.Background(), metricName)
7586
defer span.End()
7687

77-
return wait.PollImmediate(interval, timeout, func() (bool, error) {
78-
select {
79-
case <-ctx.Done():
80-
return true, ctx.Err()
81-
default:
82-
}
88+
return pollImmediateWithContext(ctx, func() (bool, error) {
8389
r, err := c.TaskRunClient.Get(ctx, name, metav1.GetOptions{})
8490
if err != nil {
8591
return true, err
@@ -97,12 +103,7 @@ func WaitForDeploymentState(ctx context.Context, c *clients, name string, namesp
97103
_, span := trace.StartSpan(context.Background(), metricName)
98104
defer span.End()
99105

100-
return wait.PollImmediate(interval, timeout, func() (bool, error) {
101-
select {
102-
case <-ctx.Done():
103-
return true, ctx.Err()
104-
default:
105-
}
106+
return pollImmediateWithContext(ctx, func() (bool, error) {
106107
d, err := c.KubeClient.Kube.AppsV1().Deployments(namespace).Get(ctx, name, metav1.GetOptions{})
107108
if err != nil {
108109
return true, err
@@ -120,12 +121,7 @@ func WaitForPodState(ctx context.Context, c *clients, name string, namespace str
120121
_, span := trace.StartSpan(context.Background(), metricName)
121122
defer span.End()
122123

123-
return wait.PollImmediate(interval, timeout, func() (bool, error) {
124-
select {
125-
case <-ctx.Done():
126-
return true, ctx.Err()
127-
default:
128-
}
124+
return pollImmediateWithContext(ctx, func() (bool, error) {
129125
r, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
130126
if err != nil {
131127
return true, err
@@ -143,12 +139,9 @@ func WaitForPipelineRunState(ctx context.Context, c *clients, name string, pollt
143139
_, span := trace.StartSpan(context.Background(), metricName)
144140
defer span.End()
145141

146-
return wait.PollImmediate(interval, polltimeout, func() (bool, error) {
147-
select {
148-
case <-ctx.Done():
149-
return true, ctx.Err()
150-
default:
151-
}
142+
ctx, cancel := context.WithTimeout(ctx, polltimeout)
143+
defer cancel()
144+
return pollImmediateWithContext(ctx, func() (bool, error) {
152145
r, err := c.PipelineRunClient.Get(ctx, name, metav1.GetOptions{})
153146
if err != nil {
154147
return true, err
@@ -166,12 +159,7 @@ func WaitForServiceExternalIPState(ctx context.Context, c *clients, namespace, n
166159
_, span := trace.StartSpan(context.Background(), metricName)
167160
defer span.End()
168161

169-
return wait.PollImmediate(interval, timeout, func() (bool, error) {
170-
select {
171-
case <-ctx.Done():
172-
return true, ctx.Err()
173-
default:
174-
}
162+
return pollImmediateWithContext(ctx, func() (bool, error) {
175163
r, err := c.KubeClient.Kube.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{})
176164
if err != nil {
177165
return true, err

0 commit comments

Comments
 (0)