Skip to content

Commit

Permalink
Clean up loadbalancer pool members before delete instance
Browse files Browse the repository at this point in the history
Signed-off-by: ekko <tuuwtu1@gmail.com>
  • Loading branch information
0ekk committed Jan 21, 2025
1 parent c3420f7 commit 22c2d33
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion api/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ type Instance struct {
// +optional
DataVolumes []Volume `json:"dataVolumes,omitempty"`

// Addresses contains the AWS instance associated addresses.
// Addresses contains the ECS instance associated addresses.
Addresses []clusterv1.MachineAddress `json:"addresses,omitempty"`

// Availability zone of instance
Expand Down
6 changes: 6 additions & 0 deletions internal/controller/huaweicloudmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ func (r *HuaweiCloudMachineReconciler) reconcileDelete(machineScope *scope.Machi

machineScope.Logger.Info("ECS instance found matching deleted HuaweiCloudMachine", "instance-id", instance.ID)

if machineScope.IsControlPlane() {
if err := ecsSvc.DetachInstanceFromElb(instance); err != nil {
return ctrl.Result{}, err
}
}

// Check the instance state. If it's already shutting down or terminated,
// do nothing. Otherwise attempt to delete it.
switch instance.State {
Expand Down
6 changes: 5 additions & 1 deletion pkg/services/ecs/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func (s *Service) CreateInstance(scope *scope.MachineScope, userData []byte,
return nil, err
}

// Set the providerID and instanceID as soon as we create an instance so that we keep it in case of errors afterward
scope.SetProviderID(out.ID, out.AvailabilityZone)
scope.SetInstanceID(out.ID)

return out, nil
}

Expand Down Expand Up @@ -406,5 +410,5 @@ func (s *Service) AttachInstanceToElb(instance *infrav1.Instance) error {
}

func (s *Service) DetachInstanceFromElb(instance *infrav1.Instance) error {
return errors.New("DetachInstanceFromElb not implemented")
return s.elbService.DeleteMember(s.scope.ELB().Pools, instance)
}
28 changes: 28 additions & 0 deletions pkg/services/elb/loadbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,31 @@ func (s *Service) CreateMember(pools []infrav1alpha1.PoolRef, instance *infrav1a
}
return nil
}

func (s *Service) DeleteMember(pools []infrav1alpha1.PoolRef, instance *infrav1alpha1.Instance) error {
for _, pool := range pools {
if pool.Id == "" {
continue
}
sdkPool, err := s.getPool(pool.Id)
if err != nil {
return err
}

members := sdkPool.Members
for _, member := range members {
if member.Id == "" {
continue
}
deleteMembersRequest := &elbmodel.BatchDeleteMembersRequest{Body: &elbmodel.BatchDeleteMembersRequestBody{Members: make([]elbmodel.BatchDeleteMembersOption, 0)}}
deleteMembersRequest.PoolId = sdkPool.Id
deleteMember := elbmodel.BatchDeleteMembersOption{Id: &member.Id}
deleteMembersRequest.Body.Members = append(deleteMembersRequest.Body.Members, deleteMember)
_, err = s.elbClient.BatchDeleteMembers(deleteMembersRequest)
if err != nil {
return err
}
}
}
return nil
}

0 comments on commit 22c2d33

Please sign in to comment.