@@ -160,59 +160,38 @@ func (r *EndpointBindingReconciler) SetupWithManager(mgr ctrl.Manager) error {
160
160
// - ExternalName Target Service in the Target Namespace/Service name pointed at the Upstream Service
161
161
// - Upstream Service in the ngrok-op namespace pointed at the Pod Forwarders
162
162
func (r * EndpointBindingReconciler ) Reconcile (ctx context.Context , req ctrl.Request ) (ctrl.Result , error ) {
163
- return r .controller .Reconcile (ctx , req , new (bindingsv1alpha1.EndpointBinding ))
163
+ cr := & bindingsv1alpha1.EndpointBinding {}
164
+ if ctrlErr , err := r .controller .Reconcile (ctx , req , cr ); err != nil {
165
+ return ctrlErr , err
166
+ }
167
+
168
+ // update ngrok api resource status on upsert
169
+ if controller .IsUpsert (cr ) {
170
+ if err := postEndpointBindingUpdateToNgrokAPI (ctx , cr ); err != nil {
171
+ return controller .CtrlResultForErr (r .controller .ReconcileStatus (ctx , cr , err ))
172
+ }
173
+ }
174
+
175
+ // success
176
+ return ctrl.Result {}, nil
164
177
}
165
178
166
179
func (r * EndpointBindingReconciler ) create (ctx context.Context , cr * bindingsv1alpha1.EndpointBinding ) error {
167
180
targetService , upstreamService := r .convertEndpointBindingToServices (cr )
168
181
169
- // defer updating statuses
170
- defer func () {
171
- // best effort
172
- _ = r .updateEndpointBindingStatus (ctx , cr )
173
-
174
- // best effort for now TODO(hkatz) retries?
175
- _ = postEndpointBindingUpdateToNgrokAPI (ctx , cr )
176
- }()
177
-
178
182
if err := r .createUpstreamService (ctx , cr , upstreamService ); err != nil {
179
- return err
183
+ return r . controller . ReconcileStatus ( ctx , cr , err )
180
184
}
181
185
182
186
if err := r .createTargetService (ctx , cr , targetService ); err != nil {
183
- return err
187
+ return r . controller . ReconcileStatus ( ctx , cr , err )
184
188
}
185
189
186
- go r .tryToBindEndpointBinding (ctx , cr )
187
-
188
- return nil
189
- }
190
-
191
- // updateEndpointBindingStatus updates the status of the EndpointBinding to the desired state
192
- func (r * EndpointBindingReconciler ) updateEndpointBindingStatus (ctx context.Context , desired * bindingsv1alpha1.EndpointBinding ) error {
193
- log := ctrl .LoggerFrom (ctx ).WithValues ("name" , desired .Name )
194
-
195
- existing := desired .DeepCopy ()
196
- if err := r .Client .Get (ctx , client .ObjectKeyFromObject (desired ), existing ); err != nil {
197
- log .Error (err , "Failed to get EndpointBinding for status update" )
198
- r .Recorder .Event (desired , v1 .EventTypeWarning , "UpdateFailed" , "Failed to get EndpointBinding for status update" )
199
- return err
190
+ if err := r .tryToBindEndpointBinding (ctx , cr ); err != nil {
191
+ return r .controller .ReconcileStatus (ctx , cr , err )
200
192
}
201
193
202
- // use existing
203
-
204
- // set status to desired
205
- existing .Status = desired .Status
206
-
207
- if err := r .Client .Status ().Update (ctx , existing ); err != nil {
208
- log .Error (err , "Failed to update EndpointBinding status" )
209
- r .Recorder .Event (existing , v1 .EventTypeWarning , "UpdateFailed" , "Failed to update EndpointBinding status" )
210
- return err
211
- }
212
-
213
- log .Info ("Updated EndpointBinding status" )
214
- r .Recorder .Event (existing , v1 .EventTypeNormal , "Updated" , "Updated EndpointBinding status" )
215
- return nil
194
+ return r .controller .ReconcileStatus (ctx , cr , nil )
216
195
}
217
196
218
197
// setEndpointsStatus sets the status of every endpoint on endpointBinding to the desired status
@@ -251,7 +230,6 @@ func (r *EndpointBindingReconciler) createTargetService(ctx context.Context, own
251
230
r .Recorder .Event (service , v1 .EventTypeNormal , "Created" , "Created Target Service" )
252
231
r .Recorder .Event (owner , v1 .EventTypeNormal , "Created" , "Created Target Service" )
253
232
log .Info ("Created Upstream Service" , "service" , service .Name )
254
-
255
233
return nil
256
234
}
257
235
@@ -270,6 +248,7 @@ func (r *EndpointBindingReconciler) createUpstreamService(ctx context.Context, o
270
248
271
249
return err
272
250
}
251
+
273
252
r .Recorder .Event (service , v1 .EventTypeNormal , "Created" , "Created Upstream Service" )
274
253
r .Recorder .Event (owner , v1 .EventTypeNormal , "Created" , "Created Upstream Service" )
275
254
log .Info ("Created Upstream Service" , "service" , service .Name )
@@ -285,28 +264,19 @@ func (r *EndpointBindingReconciler) update(ctx context.Context, cr *bindingsv1al
285
264
var existingTargetService v1.Service
286
265
var existingUpstreamService v1.Service
287
266
288
- // defer updating statuses
289
- defer func () {
290
- // best effort
291
- _ = r .updateEndpointBindingStatus (ctx , cr )
292
-
293
- // best effort for now TODO(hkatz) retries?
294
- _ = postEndpointBindingUpdateToNgrokAPI (ctx , cr )
295
- }()
296
-
297
267
// upstream service
298
268
err := r .Get (ctx , client.ObjectKey {Namespace : desiredUpstreamService .Namespace , Name : desiredUpstreamService .Name }, & existingUpstreamService )
299
269
if err != nil {
300
270
if client .IgnoreNotFound (err ) == nil {
301
271
// Upstream Service doesn't exist, create it
302
272
log .Info ("Unable to find existing Upstream Service, creating..." , "name" , desiredUpstreamService .Name )
303
273
if err := r .createUpstreamService (ctx , cr , desiredUpstreamService ); err != nil {
304
- return err
274
+ return r . controller . ReconcileStatus ( ctx , cr , err )
305
275
}
306
276
} else {
307
277
// real error
308
278
log .Error (err , "Failed to find existing Upstream Service" , "name" , cr .Name , "uri" , cr .Spec .EndpointURI )
309
- return err
279
+ return r . controller . ReconcileStatus ( ctx , cr , err )
310
280
}
311
281
} else {
312
282
// update upstream service
@@ -319,7 +289,7 @@ func (r *EndpointBindingReconciler) update(ctx context.Context, cr *bindingsv1al
319
289
r .Recorder .Event (& existingUpstreamService , v1 .EventTypeWarning , "UpdateFailed" , "Failed to update Upstream Service" )
320
290
r .Recorder .Event (cr , v1 .EventTypeWarning , "UpdateFailed" , "Failed to update Upstream Service" )
321
291
log .Error (err , "Failed to update Upstream Service" )
322
- return err
292
+ return r . controller . ReconcileStatus ( ctx , cr , err )
323
293
}
324
294
r .Recorder .Event (& existingUpstreamService , v1 .EventTypeNormal , "Updated" , "Updated Upstream Service" )
325
295
}
@@ -331,12 +301,12 @@ func (r *EndpointBindingReconciler) update(ctx context.Context, cr *bindingsv1al
331
301
// Target Service doesn't exist, create it
332
302
log .Info ("Unable to find existing Target Service, creating..." , "name" , desiredTargetService .Name )
333
303
if err := r .createTargetService (ctx , cr , desiredTargetService ); err != nil {
334
- return err
304
+ return r . controller . ReconcileStatus ( ctx , cr , err )
335
305
}
336
306
} else {
337
307
// real error
338
308
log .Error (err , "Failed to find existing Target Service" , "name" , cr .Name , "uri" , cr .Spec .EndpointURI )
339
- return err
309
+ return r . controller . ReconcileStatus ( ctx , cr , err )
340
310
}
341
311
} else {
342
312
// update target service
@@ -349,15 +319,17 @@ func (r *EndpointBindingReconciler) update(ctx context.Context, cr *bindingsv1al
349
319
r .Recorder .Event (& existingTargetService , v1 .EventTypeWarning , "UpdateFailed" , "Failed to update Target Service" )
350
320
r .Recorder .Event (cr , v1 .EventTypeWarning , "UpdateFailed" , "Failed to update Target Service" )
351
321
log .Error (err , "Failed to update Target Service" )
352
- return err
322
+ return r . controller . ReconcileStatus ( ctx , cr , err )
353
323
}
354
324
r .Recorder .Event (& existingTargetService , v1 .EventTypeNormal , "Updated" , "Updated Target Service" )
355
325
}
356
326
357
- go r .tryToBindEndpointBinding (ctx , cr )
327
+ if err := r .tryToBindEndpointBinding (ctx , cr ); err != nil {
328
+ return r .controller .ReconcileStatus (ctx , cr , err )
329
+ }
358
330
359
331
r .Recorder .Event (cr , v1 .EventTypeNormal , "Updated" , "Updated Services" )
360
- return nil
332
+ return r . controller . ReconcileStatus ( ctx , cr , nil )
361
333
}
362
334
363
335
func (r * EndpointBindingReconciler ) delete (ctx context.Context , cr * bindingsv1alpha1.EndpointBinding ) error {
@@ -549,7 +521,7 @@ func (r *EndpointBindingReconciler) findEndpointBindingsForService(ctx context.C
549
521
}
550
522
551
523
// tryToBindEndpointBinding attempts a TCP connection through the provisioned services for the EndpointBinding
552
- func (r * EndpointBindingReconciler ) tryToBindEndpointBinding (ctx context.Context , endpointBinding * bindingsv1alpha1.EndpointBinding ) {
524
+ func (r * EndpointBindingReconciler ) tryToBindEndpointBinding (ctx context.Context , endpointBinding * bindingsv1alpha1.EndpointBinding ) error {
553
525
log := ctrl .LoggerFrom (ctx ).WithValues ("uri" , endpointBinding .Spec .EndpointURI )
554
526
555
527
retries := 5
@@ -613,10 +585,5 @@ func (r *EndpointBindingReconciler) tryToBindEndpointBinding(ctx context.Context
613
585
614
586
// set status
615
587
setEndpointsStatus (endpointBinding , desired )
616
-
617
- // best effort
618
- _ = r .updateEndpointBindingStatus (ctx , endpointBinding )
619
-
620
- // best effort for now TODO(hkatz) retries?
621
- _ = postEndpointBindingUpdateToNgrokAPI (ctx , endpointBinding )
588
+ return bindErr
622
589
}
0 commit comments