Skip to content

Commit

Permalink
Refactor pollImmediateWithContext to share boilerplate
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmoor committed Oct 20, 2020
1 parent 9f964d6 commit 27925a1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 48 deletions.
39 changes: 15 additions & 24 deletions test/v1alpha1/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ const (
// ConditionAccessorFn is a condition function used polling functions
type ConditionAccessorFn func(ca apis.ConditionAccessor) (bool, error)

func pollImmediateWithContext(ctx context.Context, fn func() (bool, error)) error {
return wait.PollImmediate(interval, timeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
return fn()
})
}

// WaitForTaskRunState polls the status of the TaskRun called name from client every
// interval until inState returns `true` indicating it is done, returns an
// error or timeout. desc will be used to name the metric that is emitted to
Expand All @@ -74,12 +85,7 @@ func WaitForTaskRunState(ctx context.Context, c *clients, name string, inState C
_, span := trace.StartSpan(context.Background(), metricName)
defer span.End()

return wait.PollImmediate(interval, timeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
return pollImmediateWithContext(ctx, func() (bool, error) {
r, err := c.TaskRunClient.Get(ctx, name, metav1.GetOptions{})
if err != nil {
return true, err
Expand All @@ -97,12 +103,7 @@ func WaitForDeploymentState(ctx context.Context, c *clients, name string, namesp
_, span := trace.StartSpan(context.Background(), metricName)
defer span.End()

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

return wait.PollImmediate(interval, timeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
return pollImmediateWithContext(ctx, func() (bool, error) {
r, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return true, err
Expand Down Expand Up @@ -166,12 +162,7 @@ func WaitForServiceExternalIPState(ctx context.Context, c *clients, namespace, n
_, span := trace.StartSpan(context.Background(), metricName)
defer span.End()

return wait.PollImmediate(interval, timeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
return pollImmediateWithContext(ctx, func() (bool, error) {
r, err := c.KubeClient.Kube.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return true, err
Expand Down
39 changes: 15 additions & 24 deletions test/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ const (
// ConditionAccessorFn is a condition function used polling functions
type ConditionAccessorFn func(ca apis.ConditionAccessor) (bool, error)

func pollImmediateWithContext(ctx context.Context, fn func() (bool, error)) error {
return wait.PollImmediate(interval, timeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
return fn()
})
}

// WaitForTaskRunState polls the status of the TaskRun called name from client every
// interval until inState returns `true` indicating it is done, returns an
// error or timeout. desc will be used to name the metric that is emitted to
Expand All @@ -74,12 +85,7 @@ func WaitForTaskRunState(ctx context.Context, c *clients, name string, inState C
_, span := trace.StartSpan(context.Background(), metricName)
defer span.End()

return wait.PollImmediate(interval, timeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
return pollImmediateWithContext(ctx, func() (bool, error) {
r, err := c.TaskRunClient.Get(ctx, name, metav1.GetOptions{})
if err != nil {
return true, err
Expand All @@ -97,12 +103,7 @@ func WaitForDeploymentState(ctx context.Context, c *clients, name string, namesp
_, span := trace.StartSpan(context.Background(), metricName)
defer span.End()

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

return wait.PollImmediate(interval, timeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
return pollImmediateWithContext(ctx, func() (bool, error) {
r, err := c.KubeClient.Kube.CoreV1().Pods(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return true, err
Expand Down Expand Up @@ -166,12 +162,7 @@ func WaitForServiceExternalIPState(ctx context.Context, c *clients, namespace, n
_, span := trace.StartSpan(context.Background(), metricName)
defer span.End()

return wait.PollImmediate(interval, timeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
return pollImmediateWithContext(ctx, func() (bool, error) {
r, err := c.KubeClient.Kube.CoreV1().Services(namespace).Get(ctx, name, metav1.GetOptions{})
if err != nil {
return true, err
Expand Down

0 comments on commit 27925a1

Please sign in to comment.