@@ -15,6 +15,7 @@ import (
15
15
"sigs.k8s.io/gateway-api/apis/v1beta1"
16
16
17
17
ngfAPI "github.com/nginxinc/nginx-gateway-fabric/apis/v1alpha1"
18
+ "github.com/nginxinc/nginx-gateway-fabric/internal/framework/controller"
18
19
)
19
20
20
21
//go:generate go run github.com/maxbrunsfeld/counterfeiter/v6 . Updater
@@ -242,47 +243,7 @@ func (upd *UpdaterImpl) writeStatuses(
242
243
Cap : time .Millisecond * 3000 ,
243
244
},
244
245
// Function returns true if the condition is satisfied, or an error if the loop should be aborted.
245
- func (ctx context.Context ) (bool , error ) {
246
- // The function handles errors by reporting them in the logs.
247
- // We need to get the latest version of the resource.
248
- // Otherwise, the Update status API call can fail.
249
- // Note: the default client uses a cache for reads, so we're not making an unnecessary API call here.
250
- // the default is configurable in the Manager options.
251
- if err := upd .cfg .Client .Get (ctx , nsname , obj ); err != nil {
252
- // apierrors.IsNotFound(err) can happen when the resource is deleted,
253
- // so no need to retry or return an error.
254
- if apierrors .IsNotFound (err ) {
255
- upd .cfg .Logger .V (1 ).Info (
256
- "Resource was not found when trying to update status" ,
257
- "error" , err ,
258
- "namespace" , nsname .Namespace ,
259
- "name" , nsname .Name ,
260
- "kind" , obj .GetObjectKind ().GroupVersionKind ().Kind )
261
- return true , nil
262
- }
263
- upd .cfg .Logger .V (1 ).Info (
264
- "Encountered error when getting resource to update status" ,
265
- "error" , err ,
266
- "namespace" , nsname .Namespace ,
267
- "name" , nsname .Name ,
268
- "kind" , obj .GetObjectKind ().GroupVersionKind ().Kind )
269
- return false , nil
270
- }
271
-
272
- statusSetter (obj )
273
-
274
- if err := upd .cfg .Client .Status ().Update (ctx , obj ); err != nil {
275
- upd .cfg .Logger .V (1 ).Info (
276
- "Encountered error updating status" ,
277
- "error" , err ,
278
- "namespace" , nsname .Namespace ,
279
- "name" , nsname .Name ,
280
- "kind" , obj .GetObjectKind ().GroupVersionKind ().Kind )
281
- return false , nil
282
- }
283
-
284
- return true , nil
285
- },
246
+ ConditionWithContextFunc (upd .cfg .Client , upd .cfg .Client .Status (), nsname , obj , upd .cfg .Logger , statusSetter ),
286
247
)
287
248
if err != nil && ! errors .Is (err , context .Canceled ) {
288
249
upd .cfg .Logger .Error (
@@ -293,3 +254,56 @@ func (upd *UpdaterImpl) writeStatuses(
293
254
"kind" , obj .GetObjectKind ().GroupVersionKind ().Kind )
294
255
}
295
256
}
257
+
258
+ // ConditionWithContextFunc returns a function which will be used in wait.ExponentialBackoffWithContext.
259
+ // Exported for testing purposes.
260
+ func ConditionWithContextFunc (
261
+ getter controller.Getter ,
262
+ updater StatusUpdater ,
263
+ nsname types.NamespacedName ,
264
+ obj client.Object ,
265
+ logger logr.Logger ,
266
+ statusSetter func (client.Object ),
267
+ ) func (ctx context.Context ) (bool , error ) {
268
+ return func (ctx context.Context ) (bool , error ) {
269
+ // The function handles errors by reporting them in the logs.
270
+ // We need to get the latest version of the resource.
271
+ // Otherwise, the Update status API call can fail.
272
+ // Note: the default client uses a cache for reads, so we're not making an unnecessary API call here.
273
+ // the default is configurable in the Manager options.
274
+ if err := getter .Get (ctx , nsname , obj ); err != nil {
275
+ // apierrors.IsNotFound(err) can happen when the resource is deleted,
276
+ // so no need to retry or return an error.
277
+ if apierrors .IsNotFound (err ) {
278
+ logger .V (1 ).Info (
279
+ "Resource was not found when trying to update status" ,
280
+ "error" , err ,
281
+ "namespace" , nsname .Namespace ,
282
+ "name" , nsname .Name ,
283
+ "kind" , obj .GetObjectKind ().GroupVersionKind ().Kind )
284
+ return true , nil
285
+ }
286
+ logger .V (1 ).Info (
287
+ "Encountered error when getting resource to update status" ,
288
+ "error" , err ,
289
+ "namespace" , nsname .Namespace ,
290
+ "name" , nsname .Name ,
291
+ "kind" , obj .GetObjectKind ().GroupVersionKind ().Kind )
292
+ return false , nil
293
+ }
294
+
295
+ statusSetter (obj )
296
+
297
+ if err := updater .Update (ctx , obj ); err != nil {
298
+ logger .V (1 ).Info (
299
+ "Encountered error updating status" ,
300
+ "error" , err ,
301
+ "namespace" , nsname .Namespace ,
302
+ "name" , nsname .Name ,
303
+ "kind" , obj .GetObjectKind ().GroupVersionKind ().Kind )
304
+ return false , nil
305
+ }
306
+
307
+ return true , nil
308
+ }
309
+ }
0 commit comments