@@ -65,6 +65,17 @@ const (
65
65
// ConditionAccessorFn is a condition function used polling functions
66
66
type ConditionAccessorFn func (ca apis.ConditionAccessor ) (bool , error )
67
67
68
+ func pollImmediateWithContext (ctx context.Context , fn func () (bool , error )) error {
69
+ return wait .PollImmediate (interval , timeout , func () (bool , error ) {
70
+ select {
71
+ case <- ctx .Done ():
72
+ return true , ctx .Err ()
73
+ default :
74
+ }
75
+ return fn ()
76
+ })
77
+ }
78
+
68
79
// WaitForTaskRunState polls the status of the TaskRun called name from client every
69
80
// interval until inState returns `true` indicating it is done, returns an
70
81
// error or timeout. desc will be used to name the metric that is emitted to
@@ -74,12 +85,7 @@ func WaitForTaskRunState(ctx context.Context, c *clients, name string, inState C
74
85
_ , span := trace .StartSpan (context .Background (), metricName )
75
86
defer span .End ()
76
87
77
- return wait .PollImmediate (interval , timeout , func () (bool , error ) {
78
- select {
79
- case <- ctx .Done ():
80
- return true , ctx .Err ()
81
- default :
82
- }
88
+ return pollImmediateWithContext (ctx , func () (bool , error ) {
83
89
r , err := c .TaskRunClient .Get (ctx , name , metav1.GetOptions {})
84
90
if err != nil {
85
91
return true , err
@@ -97,12 +103,7 @@ func WaitForDeploymentState(ctx context.Context, c *clients, name string, namesp
97
103
_ , span := trace .StartSpan (context .Background (), metricName )
98
104
defer span .End ()
99
105
100
- return wait .PollImmediate (interval , timeout , func () (bool , error ) {
101
- select {
102
- case <- ctx .Done ():
103
- return true , ctx .Err ()
104
- default :
105
- }
106
+ return pollImmediateWithContext (ctx , func () (bool , error ) {
106
107
d , err := c .KubeClient .Kube .AppsV1 ().Deployments (namespace ).Get (ctx , name , metav1.GetOptions {})
107
108
if err != nil {
108
109
return true , err
@@ -120,12 +121,7 @@ func WaitForPodState(ctx context.Context, c *clients, name string, namespace str
120
121
_ , span := trace .StartSpan (context .Background (), metricName )
121
122
defer span .End ()
122
123
123
- return wait .PollImmediate (interval , timeout , func () (bool , error ) {
124
- select {
125
- case <- ctx .Done ():
126
- return true , ctx .Err ()
127
- default :
128
- }
124
+ return pollImmediateWithContext (ctx , func () (bool , error ) {
129
125
r , err := c .KubeClient .Kube .CoreV1 ().Pods (namespace ).Get (ctx , name , metav1.GetOptions {})
130
126
if err != nil {
131
127
return true , err
@@ -143,12 +139,9 @@ func WaitForPipelineRunState(ctx context.Context, c *clients, name string, pollt
143
139
_ , span := trace .StartSpan (context .Background (), metricName )
144
140
defer span .End ()
145
141
146
- return wait .PollImmediate (interval , polltimeout , func () (bool , error ) {
147
- select {
148
- case <- ctx .Done ():
149
- return true , ctx .Err ()
150
- default :
151
- }
142
+ ctx , cancel := context .WithTimeout (ctx , polltimeout )
143
+ defer cancel ()
144
+ return pollImmediateWithContext (ctx , func () (bool , error ) {
152
145
r , err := c .PipelineRunClient .Get (ctx , name , metav1.GetOptions {})
153
146
if err != nil {
154
147
return true , err
@@ -166,12 +159,7 @@ func WaitForServiceExternalIPState(ctx context.Context, c *clients, namespace, n
166
159
_ , span := trace .StartSpan (context .Background (), metricName )
167
160
defer span .End ()
168
161
169
- return wait .PollImmediate (interval , timeout , func () (bool , error ) {
170
- select {
171
- case <- ctx .Done ():
172
- return true , ctx .Err ()
173
- default :
174
- }
162
+ return pollImmediateWithContext (ctx , func () (bool , error ) {
175
163
r , err := c .KubeClient .Kube .CoreV1 ().Services (namespace ).Get (ctx , name , metav1.GetOptions {})
176
164
if err != nil {
177
165
return true , err
0 commit comments