Skip to content

Commit

Permalink
Handle nil pointer in Lb delete (#277)
Browse files Browse the repository at this point in the history
* Handle nil pointer in LB delete
  • Loading branch information
shyamradhakrishnan committed Jun 1, 2023
1 parent 18012bf commit 764e32d
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
19 changes: 12 additions & 7 deletions cloud/scope/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,11 +387,6 @@ func (m *MachineScope) Name() string {
return m.OCIMachine.Name
}

// IP returns the OCIMachine IP.
func (m *MachineScope) IP() string {
return m.OCIMachine.Status.Addresses[0].Address
}

// GetInstanceId returns the OCIMachine instance id.
func (m *MachineScope) GetInstanceId() *string {
return m.OCIMachine.Spec.InstanceId
Expand Down Expand Up @@ -515,7 +510,7 @@ func (m *MachineScope) ReconcileCreateInstanceOnLB(ctx context.Context) error {
backendSet := lb.BackendSets[APIServerLBBackendSetName]
// When creating a LB, there is no way to set the backend Name, default backend name is the instance IP and port
// So we use default backend name instead of machine name
backendName := m.IP() + ":" + strconv.Itoa(int(m.OCICluster.Spec.ControlPlaneEndpoint.Port))
backendName := instanceIp + ":" + strconv.Itoa(int(m.OCICluster.Spec.ControlPlaneEndpoint.Port))
if !m.containsLBBackend(backendSet, backendName) {
logger := m.Logger.WithValues("backend-set", *backendSet.Name)
workRequest := m.OCIMachine.Status.CreateBackendWorkRequestId
Expand Down Expand Up @@ -613,7 +608,17 @@ func (m *MachineScope) ReconcileDeleteInstanceOnLB(ctx context.Context) error {
return err
}
backendSet := lb.BackendSets[APIServerLBBackendSetName]
backendName := m.IP() + ":" + strconv.Itoa(int(m.OCICluster.Spec.ControlPlaneEndpoint.Port))
// in case of delete from LB backend, if the instance does not have an IP, we consider
// the instance to not have been added in first place and hence return nil
if len(m.OCIMachine.Status.Addresses) <= 0 {
m.Logger.Info("Instance does not have IP Address, hence ignoring LBaaS reconciliation on delete")
return nil
}
instanceIp, err := m.GetMachineIPFromStatus()
if err != nil {
return err
}
backendName := instanceIp + ":" + strconv.Itoa(int(m.OCICluster.Spec.ControlPlaneEndpoint.Port))
if m.containsLBBackend(backendSet, backendName) {
logger := m.Logger.WithValues("backend-set", *backendSet.Name)
workRequest := m.OCIMachine.Status.DeleteBackendWorkRequestId
Expand Down
10 changes: 10 additions & 0 deletions cloud/scope/machine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,16 @@ func TestLBReconciliationDeletion(t *testing.T) {
LoadBalancer: loadbalancer.LoadBalancer{}}, errors.New("could not get lb"))
},
},
{
name: "no ip",
errorExpected: false,
testSpecificSetup: func(machineScope *MachineScope, nlbClient *mock_lb.MockLoadBalancerClient) {
nlbClient.EXPECT().GetLoadBalancer(gomock.Any(), gomock.Eq(loadbalancer.GetLoadBalancerRequest{
LoadBalancerId: common.String("lbid"),
})).Return(loadbalancer.GetLoadBalancerResponse{
LoadBalancer: loadbalancer.LoadBalancer{}}, nil)
},
},
{
name: "backend exists",
errorExpected: false,
Expand Down

0 comments on commit 764e32d

Please sign in to comment.