Skip to content

Commit 638cad1

Browse files
authored
Merge pull request #144 from aleofreddi/master
Allow the autoneg deletion finalizer to complete if the backend service is already deleted
2 parents e8ddb7a + 50b21fe commit 638cad1

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

Diff for: controllers/autoneg.go

+16-3
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,17 @@ func (b *ProdBackendController) ReconcileBackends(actual, intended AutonegStatus
203203
for idx, remove := range _removes {
204204
var oldSvc *compute.BackendService
205205
oldSvc, err = b.getBackendService(remove.name, remove.region)
206-
if err != nil {
206+
var svcUpdated = false
207+
var e *errNotFound
208+
if errors.As(err, &e) {
209+
// If the backend service is gone, we construct a BackendService with the same name
210+
// and an empty list of backends.
211+
err = nil
212+
oldSvc = &compute.BackendService{
213+
Name: remove.name,
214+
Backends: make([]*compute.Backend, 0),
215+
}
216+
} else if err != nil {
207217
return
208218
}
209219

@@ -222,6 +232,7 @@ func (b *ProdBackendController) ReconcileBackends(actual, intended AutonegStatus
222232
for _, d := range remove.backends {
223233
for i, be := range oldSvc.Backends {
224234
if d.Group == be.Group {
235+
svcUpdated = true
225236
copy(oldSvc.Backends[i:], oldSvc.Backends[i+1:])
226237
oldSvc.Backends = oldSvc.Backends[:len(oldSvc.Backends)-1]
227238
break
@@ -230,7 +241,7 @@ func (b *ProdBackendController) ReconcileBackends(actual, intended AutonegStatus
230241
}
231242

232243
// If we are changing backend services, save the old service
233-
if upsert.name != remove.name {
244+
if upsert.name != remove.name && svcUpdated {
234245
if err = b.updateBackends(remove.name, remove.region, oldSvc, forceCapacity); err != nil {
235246
return
236247
}
@@ -273,7 +284,9 @@ func (b *ProdBackendController) ReconcileBackends(actual, intended AutonegStatus
273284
newSvc.Backends = append(newSvc.Backends, &newBackend)
274285
}
275286
}
276-
err = b.updateBackends(upsert.name, upsert.region, newSvc, forceCapacity)
287+
if len(upsert.backends) > 0 {
288+
err = b.updateBackends(upsert.name, upsert.region, newSvc, forceCapacity)
289+
}
277290
if err != nil {
278291
return err
279292
}

Diff for: controllers/autoneg_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"context"
21+
"google.golang.org/api/option"
2022
"math"
23+
"net/http"
24+
"net/http/httptest"
2125
"reflect"
2226
"testing"
2327

@@ -979,3 +983,26 @@ func Test_checkOperation(t *testing.T) {
979983
}
980984
}
981985
}
986+
987+
func TestReconcileBackendsDeletionWithMissingBackend(t *testing.T) {
988+
s := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
989+
// Return not found on backend service get.
990+
res.WriteHeader(http.StatusNotFound)
991+
}))
992+
cs, err := compute.NewService(context.Background(), option.WithEndpoint(s.URL), option.WithoutAuthentication())
993+
if err != nil {
994+
t.Fatalf("Failed to instantiate compute service: %v", err)
995+
}
996+
bc := ProdBackendController{
997+
project: "test-project",
998+
s: cs,
999+
}
1000+
err = bc.ReconcileBackends(statusBasicWithNEGs, AutonegStatus{
1001+
// On deletion, the intended state is set to empty.
1002+
AutonegConfig: AutonegConfig{},
1003+
NEGStatus: negStatus,
1004+
})
1005+
if err != nil {
1006+
t.Errorf("ReconcileBackends() got err: %v, want none", err)
1007+
}
1008+
}

0 commit comments

Comments
 (0)