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 authored and tekton-robot committed Oct 21, 2020
1 parent 1bd63aa commit 329f0f0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 60 deletions.
48 changes: 18 additions & 30 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 All @@ -143,12 +139,9 @@ func WaitForPipelineRunState(ctx context.Context, c *clients, name string, pollt
_, span := trace.StartSpan(context.Background(), metricName)
defer span.End()

return wait.PollImmediate(interval, polltimeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
ctx, cancel := context.WithTimeout(ctx, polltimeout)
defer cancel()
return pollImmediateWithContext(ctx, func() (bool, error) {
r, err := c.PipelineRunClient.Get(ctx, name, metav1.GetOptions{})
if err != nil {
return true, err
Expand All @@ -166,12 +159,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
48 changes: 18 additions & 30 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 All @@ -143,12 +139,9 @@ func WaitForPipelineRunState(ctx context.Context, c *clients, name string, pollt
_, span := trace.StartSpan(context.Background(), metricName)
defer span.End()

return wait.PollImmediate(interval, polltimeout, func() (bool, error) {
select {
case <-ctx.Done():
return true, ctx.Err()
default:
}
ctx, cancel := context.WithTimeout(ctx, polltimeout)
defer cancel()
return pollImmediateWithContext(ctx, func() (bool, error) {
r, err := c.PipelineRunClient.Get(ctx, name, metav1.GetOptions{})
if err != nil {
return true, err
Expand All @@ -166,12 +159,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 329f0f0

Please sign in to comment.