@@ -36,7 +36,10 @@ import (
36
36
)
37
37
38
38
// 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
+
40
43
for {
41
44
select {
42
45
case <- ctx .Done ():
@@ -65,10 +68,7 @@ func WaitForPodComplete(ctx context.Context, pods corev1.PodInterface, podName s
65
68
}
66
69
defer w .Stop ()
67
70
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 ) {
72
72
if event .Object == nil {
73
73
return false , nil
74
74
}
@@ -103,10 +103,7 @@ func WaitForPodInitialized(ctx context.Context, pods corev1.PodInterface, podNam
103
103
}
104
104
defer w .Stop ()
105
105
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 ) {
110
107
pod := event .Object .(* v1.Pod )
111
108
if pod .Name != podName {
112
109
return false , nil
@@ -136,10 +133,7 @@ func WaitForDeploymentToStabilize(ctx context.Context, c kubernetes.Interface, n
136
133
return fmt .Errorf ("initializing deployment watcher: %s" , err )
137
134
}
138
135
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 ) {
143
137
if event .Type == watch .Deleted {
144
138
return false , apierrs .NewNotFound (schema.GroupResource {Resource : "deployments" }, "" )
145
139
}
0 commit comments