Skip to content

Commit

Permalink
Remove duplication
Browse files Browse the repository at this point in the history
Signed-off-by: David Gageot <[email protected]>
  • Loading branch information
dgageot committed Jun 6, 2019
1 parent efb6803 commit f6222e1
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions pkg/skaffold/kubernetes/wait.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ import (
)

// WatchUntil reads items from the watch until the provided condition succeeds or the context is cancelled.
func watchUntil(ctx context.Context, w watch.Interface, condition func(event *watch.Event) (bool, error)) error {
func watchUntilTimeout(ctx context.Context, timeout time.Duration, w watch.Interface, condition func(event *watch.Event) (bool, error)) error {
ctx, cancelTimeout := context.WithTimeout(ctx, timeout)
defer cancelTimeout()

for {
select {
case <-ctx.Done():
Expand Down Expand Up @@ -65,10 +68,7 @@ func WaitForPodComplete(ctx context.Context, pods corev1.PodInterface, podName s
}
defer w.Stop()

ctx, cancelTimeout := context.WithTimeout(ctx, timeout)
defer cancelTimeout()

return watchUntil(ctx, w, func(event *watch.Event) (bool, error) {
return watchUntilTimeout(ctx, timeout, w, func(event *watch.Event) (bool, error) {
if event.Object == nil {
return false, nil
}
Expand Down Expand Up @@ -103,10 +103,7 @@ func WaitForPodInitialized(ctx context.Context, pods corev1.PodInterface, podNam
}
defer w.Stop()

ctx, cancelTimeout := context.WithTimeout(ctx, 10*time.Minute)
defer cancelTimeout()

return watchUntil(ctx, w, func(event *watch.Event) (bool, error) {
return watchUntilTimeout(ctx, 10*time.Minute, w, func(event *watch.Event) (bool, error) {
pod := event.Object.(*v1.Pod)
if pod.Name != podName {
return false, nil
Expand Down Expand Up @@ -136,10 +133,7 @@ func WaitForDeploymentToStabilize(ctx context.Context, c kubernetes.Interface, n
return fmt.Errorf("initializing deployment watcher: %s", err)
}

ctx, cancelTimeout := context.WithTimeout(ctx, timeout)
defer cancelTimeout()

return watchUntil(ctx, w, func(event *watch.Event) (bool, error) {
return watchUntilTimeout(ctx, timeout, w, func(event *watch.Event) (bool, error) {
if event.Type == watch.Deleted {
return false, apierrs.NewNotFound(schema.GroupResource{Resource: "deployments"}, "")
}
Expand Down

0 comments on commit f6222e1

Please sign in to comment.