Skip to content

Commit 764e32d

Browse files
Handle nil pointer in Lb delete (#277)
* Handle nil pointer in LB delete
1 parent 18012bf commit 764e32d

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

cloud/scope/machine.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,6 @@ func (m *MachineScope) Name() string {
387387
return m.OCIMachine.Name
388388
}
389389

390-
// IP returns the OCIMachine IP.
391-
func (m *MachineScope) IP() string {
392-
return m.OCIMachine.Status.Addresses[0].Address
393-
}
394-
395390
// GetInstanceId returns the OCIMachine instance id.
396391
func (m *MachineScope) GetInstanceId() *string {
397392
return m.OCIMachine.Spec.InstanceId
@@ -515,7 +510,7 @@ func (m *MachineScope) ReconcileCreateInstanceOnLB(ctx context.Context) error {
515510
backendSet := lb.BackendSets[APIServerLBBackendSetName]
516511
// When creating a LB, there is no way to set the backend Name, default backend name is the instance IP and port
517512
// So we use default backend name instead of machine name
518-
backendName := m.IP() + ":" + strconv.Itoa(int(m.OCICluster.Spec.ControlPlaneEndpoint.Port))
513+
backendName := instanceIp + ":" + strconv.Itoa(int(m.OCICluster.Spec.ControlPlaneEndpoint.Port))
519514
if !m.containsLBBackend(backendSet, backendName) {
520515
logger := m.Logger.WithValues("backend-set", *backendSet.Name)
521516
workRequest := m.OCIMachine.Status.CreateBackendWorkRequestId
@@ -613,7 +608,17 @@ func (m *MachineScope) ReconcileDeleteInstanceOnLB(ctx context.Context) error {
613608
return err
614609
}
615610
backendSet := lb.BackendSets[APIServerLBBackendSetName]
616-
backendName := m.IP() + ":" + strconv.Itoa(int(m.OCICluster.Spec.ControlPlaneEndpoint.Port))
611+
// in case of delete from LB backend, if the instance does not have an IP, we consider
612+
// the instance to not have been added in first place and hence return nil
613+
if len(m.OCIMachine.Status.Addresses) <= 0 {
614+
m.Logger.Info("Instance does not have IP Address, hence ignoring LBaaS reconciliation on delete")
615+
return nil
616+
}
617+
instanceIp, err := m.GetMachineIPFromStatus()
618+
if err != nil {
619+
return err
620+
}
621+
backendName := instanceIp + ":" + strconv.Itoa(int(m.OCICluster.Spec.ControlPlaneEndpoint.Port))
617622
if m.containsLBBackend(backendSet, backendName) {
618623
logger := m.Logger.WithValues("backend-set", *backendSet.Name)
619624
workRequest := m.OCIMachine.Status.DeleteBackendWorkRequestId

cloud/scope/machine_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2070,6 +2070,16 @@ func TestLBReconciliationDeletion(t *testing.T) {
20702070
LoadBalancer: loadbalancer.LoadBalancer{}}, errors.New("could not get lb"))
20712071
},
20722072
},
2073+
{
2074+
name: "no ip",
2075+
errorExpected: false,
2076+
testSpecificSetup: func(machineScope *MachineScope, nlbClient *mock_lb.MockLoadBalancerClient) {
2077+
nlbClient.EXPECT().GetLoadBalancer(gomock.Any(), gomock.Eq(loadbalancer.GetLoadBalancerRequest{
2078+
LoadBalancerId: common.String("lbid"),
2079+
})).Return(loadbalancer.GetLoadBalancerResponse{
2080+
LoadBalancer: loadbalancer.LoadBalancer{}}, nil)
2081+
},
2082+
},
20732083
{
20742084
name: "backend exists",
20752085
errorExpected: false,

0 commit comments

Comments
 (0)