@@ -201,15 +201,21 @@ func checkOperation(op *compute.Operation) error {
201
201
202
202
// ReconcileBackends takes the actual and intended AutonegStatus
203
203
// and attempts to apply the intended status or return an error
204
- func (b * ProdBackendController ) ReconcileBackends (actual , intended AutonegStatus ) (err error ) {
204
+ func (b * ProdBackendController ) ReconcileBackends (ctx context.Context , actual , intended AutonegStatus ) (err error ) {
205
+ logger := log .FromContext (ctx )
206
+ // Determine which backends to remove and which to insert/update.
205
207
removes , upserts := ReconcileStatus (b .project , actual , intended )
208
+ logger .Info ("Reconciling backends" , "removes" , removes , "upserts" , upserts )
206
209
207
210
var forceCapacity map [int ]bool = make (map [int ]bool , 0 )
211
+ // Iterate over each port that has backends to be removed.
208
212
for port , _removes := range removes {
213
+ // Iterate over each backend service to be removed.
209
214
for idx , remove := range _removes {
210
215
var oldSvc * compute.BackendService
211
- oldSvc , err = b .getBackendService (remove .name , remove .region )
212
216
var svcUpdated = false
217
+ // Get the current state of the backend service.
218
+ oldSvc , err = b .getBackendService (remove .name , remove .region )
213
219
var e * errNotFound
214
220
if errors .As (err , & e ) {
215
221
// If the backend service is gone, we construct a BackendService with the same name
@@ -226,16 +232,18 @@ func (b *ProdBackendController) ReconcileBackends(actual, intended AutonegStatus
226
232
var newSvc * compute.BackendService
227
233
upsert := upserts [port ][idx ]
228
234
229
- if upsert .name != remove .name {
235
+ // Check if the same port is in the upsert map and if upsert needs to happen on a different backend service.
236
+ if upsert .name != "" && upsert .name != remove .name {
230
237
if newSvc , err = b .getBackendService (upsert .name , upsert .region ); err != nil {
231
238
return
232
239
}
233
240
} else {
234
241
newSvc = oldSvc
235
242
}
236
243
237
- // Remove backends in the list to be deleted
244
+ // Remove backends that are in the list to be deleted for this port.
238
245
for _ , d := range remove .backends {
246
+ // Remove only the requested backends and keep the rest.
239
247
for i , be := range oldSvc .Backends {
240
248
if d .Group == be .Group {
241
249
svcUpdated = true
@@ -246,8 +254,9 @@ func (b *ProdBackendController) ReconcileBackends(actual, intended AutonegStatus
246
254
}
247
255
}
248
256
249
- // If we are changing backend services, save the old service
250
- if upsert .name != remove .name && svcUpdated {
257
+ // If a different service needs to be updated based on the upsert map entry for this port,
258
+ // then save the existing backend service and update the new service.
259
+ if svcUpdated && (upsert .name == "" || upsert .name != remove .name ) {
251
260
if err = b .updateBackends (remove .name , remove .region , oldSvc , forceCapacity ); err != nil {
252
261
return
253
262
}
@@ -263,7 +272,7 @@ func (b *ProdBackendController) ReconcileBackends(actual, intended AutonegStatus
263
272
be .MaxConnectionsPerEndpoint = u .MaxConnectionsPerEndpoint
264
273
if intended .AutonegSyncConfig != nil {
265
274
var syncConfig AutonegSyncConfig = * intended .AutonegSyncConfig
266
- if syncConfig .CapacityScaler != nil && * syncConfig .CapacityScaler == true {
275
+ if syncConfig .CapacityScaler != nil && * syncConfig .CapacityScaler {
267
276
be .CapacityScaler = u .CapacityScaler
268
277
}
269
278
} else {
@@ -477,6 +486,7 @@ func getStatuses(ctx context.Context, namespace string, name string, annotations
477
486
if ok {
478
487
// Found a status, decode
479
488
if err = json .Unmarshal ([]byte (tmp ), & s .negConfig ); err != nil {
489
+ err = fmt .Errorf ("failed to decode neg annotation %s: %w" , negAnnotation , err )
480
490
return
481
491
}
482
492
}
@@ -489,12 +499,14 @@ func getStatuses(ctx context.Context, namespace string, name string, annotations
489
499
490
500
var tempConfig AutonegConfigTemp
491
501
if err = json .Unmarshal ([]byte (tmp ), & tempConfig ); err != nil {
502
+ err = fmt .Errorf ("failed to decode autoneg annotation %s: %w" , autonegAnnotation , err )
492
503
return
493
504
}
494
505
495
506
tmpSync , syncOk := annotations [autonegSyncAnnotation ]
496
507
if syncOk {
497
508
if err = json .Unmarshal ([]byte (tmpSync ), & s .syncConfig ); err != nil {
509
+ err = fmt .Errorf ("failed to decode autoneg-sync annotation %s: %w" , autonegSyncAnnotation , err )
498
510
return
499
511
}
500
512
}
@@ -540,6 +552,7 @@ func getStatuses(ctx context.Context, namespace string, name string, annotations
540
552
}
541
553
// Found a status, decode
542
554
if err = json .Unmarshal ([]byte (tmp ), & s .status ); err != nil {
555
+ err = fmt .Errorf ("failed to decode autoneg-status annotation %s: %w" , autonegStatusAnnotation , err )
543
556
return
544
557
}
545
558
}
@@ -551,6 +564,7 @@ func getStatuses(ctx context.Context, namespace string, name string, annotations
551
564
valid = true
552
565
553
566
if err = json .Unmarshal ([]byte (tmp ), & s .oldConfig ); err != nil {
567
+ err = fmt .Errorf ("failed to decode %s annotation %s: %w" , oldAutonegAnnotation , tmp , err )
554
568
return
555
569
}
556
570
@@ -579,7 +593,7 @@ func getStatuses(ctx context.Context, namespace string, name string, annotations
579
593
Connections : 0 ,
580
594
}
581
595
} else {
582
- err = errors . New ( fmt .Sprintf ("more than one port in %s, but autoneg configuration is for one or no ports" , negAnnotation ) )
596
+ err = fmt .Errorf ("more than one port in %s, but autoneg configuration is for one or no ports" , negAnnotation )
583
597
return
584
598
}
585
599
}
@@ -593,6 +607,7 @@ func getStatuses(ctx context.Context, namespace string, name string, annotations
593
607
}
594
608
// Found a status, decode
595
609
if err = json .Unmarshal ([]byte (tmp ), & s .oldStatus ); err != nil {
610
+ err = fmt .Errorf ("failed to decode %s annotation %s: %w" , oldAutonegStatusAnnotation , tmp , err )
596
611
return
597
612
}
598
613
}
0 commit comments