Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nil pointer in Lb delete #277

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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