Skip to content

Commit f6222e1

Browse files
committed
Remove duplication
Signed-off-by: David Gageot <[email protected]>
1 parent efb6803 commit f6222e1

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

pkg/skaffold/kubernetes/wait.go

+7-13
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@ import (
3636
)
3737

3838
// WatchUntil reads items from the watch until the provided condition succeeds or the context is cancelled.
39-
func watchUntil(ctx context.Context, w watch.Interface, condition func(event *watch.Event) (bool, error)) error {
39+
func watchUntilTimeout(ctx context.Context, timeout time.Duration, w watch.Interface, condition func(event *watch.Event) (bool, error)) error {
40+
ctx, cancelTimeout := context.WithTimeout(ctx, timeout)
41+
defer cancelTimeout()
42+
4043
for {
4144
select {
4245
case <-ctx.Done():
@@ -65,10 +68,7 @@ func WaitForPodComplete(ctx context.Context, pods corev1.PodInterface, podName s
6568
}
6669
defer w.Stop()
6770

68-
ctx, cancelTimeout := context.WithTimeout(ctx, timeout)
69-
defer cancelTimeout()
70-
71-
return watchUntil(ctx, w, func(event *watch.Event) (bool, error) {
71+
return watchUntilTimeout(ctx, timeout, w, func(event *watch.Event) (bool, error) {
7272
if event.Object == nil {
7373
return false, nil
7474
}
@@ -103,10 +103,7 @@ func WaitForPodInitialized(ctx context.Context, pods corev1.PodInterface, podNam
103103
}
104104
defer w.Stop()
105105

106-
ctx, cancelTimeout := context.WithTimeout(ctx, 10*time.Minute)
107-
defer cancelTimeout()
108-
109-
return watchUntil(ctx, w, func(event *watch.Event) (bool, error) {
106+
return watchUntilTimeout(ctx, 10*time.Minute, w, func(event *watch.Event) (bool, error) {
110107
pod := event.Object.(*v1.Pod)
111108
if pod.Name != podName {
112109
return false, nil
@@ -136,10 +133,7 @@ func WaitForDeploymentToStabilize(ctx context.Context, c kubernetes.Interface, n
136133
return fmt.Errorf("initializing deployment watcher: %s", err)
137134
}
138135

139-
ctx, cancelTimeout := context.WithTimeout(ctx, timeout)
140-
defer cancelTimeout()
141-
142-
return watchUntil(ctx, w, func(event *watch.Event) (bool, error) {
136+
return watchUntilTimeout(ctx, timeout, w, func(event *watch.Event) (bool, error) {
143137
if event.Type == watch.Deleted {
144138
return false, apierrs.NewNotFound(schema.GroupResource{Resource: "deployments"}, "")
145139
}

0 commit comments

Comments
 (0)