Skip to content

Commit 095f60f

Browse files
committed
fixed panic issue around deletes
1 parent 6110ba8 commit 095f60f

File tree

4 files changed

+64
-18
lines changed

4 files changed

+64
-18
lines changed

pkg/oci/load_balancer.go

+14-8
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,18 @@ func (cp *CloudProvider) updateLoadBalancer(lb *baremetal.LoadBalancer, spec LBS
251251
return fmt.Errorf("error updating BackendSet: %v", err)
252252
}
253253
case *ListenerAction:
254-
err := cp.updateListener(lbOCID, spec, a, lbSubnets, nodeSubnets, sslConfigMap, sourceCIDRs)
254+
backendSet := spec.GetBackendSets()[a.Listener.DefaultBackendSetName]
255+
if a.Type() == Delete {
256+
// If we need to delete the backendset then it'll no longer be present
257+
// in the spec since that's what is desired, so we need to fetch it
258+
// from the load balancer object.
259+
backendSet = lb.BackendSets[a.Listener.DefaultBackendSetName]
260+
}
261+
262+
backendPort := uint64(getBackendPort(backendSet.Backends))
263+
err := cp.updateListener(lbOCID, a, backendPort, lbSubnets, nodeSubnets, sslConfigMap, sourceCIDRs)
255264
if err != nil {
256-
return fmt.Errorf("error updating BackendSet: %v", err)
265+
return fmt.Errorf("error updating Listener: %v", err)
257266
}
258267
}
259268
}
@@ -268,7 +277,7 @@ func (cp *CloudProvider) updateBackendSet(lbOCID string, action *BackendSetActio
268277
var err error
269278

270279
be := action.BackendSet
271-
glog.V(2).Infof("Applying `%s` action on backend set `%s` for lb `%s`", action.Type, be.Name, lbOCID)
280+
glog.V(2).Infof("Applying %q action on backend set `%s` for lb `%s`", action.Type(), be.Name, lbOCID)
272281

273282
backendPort := uint64(getBackendPort(be.Backends))
274283

@@ -322,8 +331,8 @@ func (cp *CloudProvider) updateBackendSet(lbOCID string, action *BackendSetActio
322331
}
323332

324333
func (cp *CloudProvider) updateListener(lbOCID string,
325-
spec LBSpec,
326334
action *ListenerAction,
335+
backendPort uint64,
327336
lbSubnets []*baremetal.Subnet,
328337
nodeSubnets []*baremetal.Subnet,
329338
sslConfigMap map[int]*baremetal.SSLConfiguration,
@@ -334,10 +343,7 @@ func (cp *CloudProvider) updateListener(lbOCID string,
334343
l := action.Listener
335344
listenerPort := uint64(l.Port)
336345

337-
backends := spec.GetBackendSets()[l.DefaultBackendSetName].Backends
338-
backendPort := uint64(getBackendPort(backends))
339-
340-
glog.V(2).Infof("Applying `%s` action on listener `%s` for lb `%s`", action.Type, l.Name, lbOCID)
346+
glog.V(2).Infof("Applying %q action on listener `%s` for lb `%s`", action.Type(), l.Name, lbOCID)
341347

342348
switch action.Type() {
343349
case Create:

pkg/oci/load_balancer_security_lists.go

-5
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,6 @@ func (s *securityListManagerImpl) updateSecurityListRules(securityListID string,
200200
return err
201201
}
202202

203-
func getBackendPort(backends []baremetal.Backend) uint64 {
204-
// TODO: what happens if this is 0? e.g. we scale the pods to 0 for a deployment
205-
return uint64(backends[0].Port)
206-
}
207-
208203
func getNodeIngressRules(securityList *baremetal.SecurityList, lbSubnets []*baremetal.Subnet, port uint64) []baremetal.IngressSecurityRule {
209204
desired := sets.NewString()
210205
for _, lbSubnet := range lbSubnets {

pkg/oci/load_balancer_util.go

+5
Original file line numberDiff line numberDiff line change
@@ -310,3 +310,8 @@ func sortAndCombineActions(backendSetActions []Action, listenerActions []Action)
310310
})
311311
return actions
312312
}
313+
314+
func getBackendPort(backends []baremetal.Backend) uint64 {
315+
// TODO: what happens if this is 0? e.g. we scale the pods to 0 for a deployment
316+
return uint64(backends[0].Port)
317+
}

test/integration/loadbalancer/loadbalancer_test.go

+45-5
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ func testLoadBalancer(t *testing.T, internal bool) {
125125
nodes = append(nodes, node)
126126
}
127127

128+
glog.Info("Stating test on creating initial load balancer")
129+
128130
status, err := loadbalancers.EnsureLoadBalancer("foo", service, nodes)
129131
if err != nil {
130132
t.Fatalf("Unable to ensure the load balancer: %v", err)
@@ -137,6 +139,8 @@ func testLoadBalancer(t *testing.T, internal bool) {
137139
t.Fatalf("validation error: %v", err)
138140
}
139141

142+
glog.Info("Stating test on decreasing node count to 1")
143+
140144
// Decrease the number of backends to 1
141145
lessNodes := []*api.Node{nodes[0]}
142146
status, err = loadbalancers.EnsureLoadBalancer("foo", service, lessNodes)
@@ -149,6 +153,8 @@ func testLoadBalancer(t *testing.T, internal bool) {
149153
t.Fatalf("validation error: %v", err)
150154
}
151155

156+
glog.Info("Stating test on increasing node count back to 2")
157+
152158
// Go back to 2 nodes
153159
status, err = loadbalancers.EnsureLoadBalancer("foo", service, nodes)
154160
if err != nil {
@@ -159,6 +165,33 @@ func testLoadBalancer(t *testing.T, internal bool) {
159165
if err != nil {
160166
t.Fatalf("validation error: %v", err)
161167
}
168+
169+
glog.Info("Stating test on changing service port")
170+
171+
// Validate changing the service port.
172+
service.Spec.Ports[0].Port = 81
173+
status, err = loadbalancers.EnsureLoadBalancer("foo", service, nodes)
174+
if err != nil {
175+
t.Fatalf("Unable to ensure the load balancer: %v", err)
176+
}
177+
178+
err = validateLoadBalancer(fw.Client, service, nodes)
179+
if err != nil {
180+
t.Fatalf("validation error: %v", err)
181+
}
182+
183+
glog.Info("Stating test on changing node port")
184+
// Validate changing the node port.
185+
service.Spec.Ports[0].NodePort = 8081
186+
status, err = loadbalancers.EnsureLoadBalancer("foo", service, nodes)
187+
if err != nil {
188+
t.Fatalf("Unable to ensure the load balancer: %v", err)
189+
}
190+
191+
err = validateLoadBalancer(fw.Client, service, nodes)
192+
if err != nil {
193+
t.Fatalf("validation error: %v", err)
194+
}
162195
}
163196

164197
func validateLoadBalancer(client client.Interface, service *api.Service, nodes []*api.Node) error {
@@ -171,20 +204,27 @@ func validateLoadBalancer(client client.Interface, service *api.Service, nodes [
171204
}
172205

173206
if len(lb.Listeners) != 1 {
174-
return fmt.Errorf("Expected 1 Listener but got %d", len(lb.Listeners))
207+
return fmt.Errorf("expected 1 Listener but got %d", len(lb.Listeners))
175208
}
176209

177210
if len(lb.BackendSets) != 1 {
178-
return fmt.Errorf("Expected 1 BackendSet but got %d", len(lb.BackendSets))
211+
return fmt.Errorf("expected 1 BackendSet but got %d", len(lb.BackendSets))
179212
}
180213

181-
backendSet, ok := lb.BackendSets["TCP-80"]
214+
name := fmt.Sprintf("TCP-%d", service.Spec.Ports[0].Port)
215+
backendSet, ok := lb.BackendSets[name]
182216
if !ok {
183-
return fmt.Errorf("Expected BackendSet with name `TCP-80` to exist but it doesn't")
217+
return fmt.Errorf("expected BackendSet with name %q to exist but it doesn't", name)
184218
}
185219

186220
if len(backendSet.Backends) != len(nodes) {
187-
return fmt.Errorf("Expected %d backends but got %d", len(nodes), len(backendSet.Backends))
221+
return fmt.Errorf("expected %d backends but got %d", len(nodes), len(backendSet.Backends))
222+
}
223+
224+
expectedBackendPort := service.Spec.Ports[0].NodePort
225+
actualBackendPort := backendSet.Backends[0].Port
226+
if int(expectedBackendPort) != int(actualBackendPort) {
227+
return fmt.Errorf("expected backend port %d but got %d", expectedBackendPort, actualBackendPort)
188228
}
189229

190230
return nil

0 commit comments

Comments
 (0)