Skip to content

Commit

Permalink
Allow garbage collector to delete ec2 instances
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Oct 10, 2023
1 parent 0abb2b0 commit 7c2d0b5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/v1beta2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ const (
type GCTask string

var (
// GCTaskEC2Instance defines a task to cleaning up resources for AWS EC2 instances.
GCTaskEC2Instance = GCTask("instance")

// GCTaskLoadBalancer defines a task to cleaning up resources for AWS load balancers.
GCTaskLoadBalancer = GCTask("load-balancer")

Expand Down
1 change: 1 addition & 0 deletions pkg/cloud/services/gc/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func (s *Service) deleteResources(ctx context.Context) error {

if val, found := annotations.Get(s.scope.InfraCluster(), infrav1.ExternalResourceGCTasksAnnotation); found {
var gcTaskToFunc = map[infrav1.GCTask]ResourceCleanupFunc{
infrav1.GCTaskEC2Instance: s.deleteEC2Instances,
infrav1.GCTaskLoadBalancer: s.deleteLoadBalancers,
infrav1.GCTaskTargetGroup: s.deleteTargetGroups,
infrav1.GCTaskSecurityGroup: s.deleteSecurityGroups,
Expand Down
39 changes: 39 additions & 0 deletions pkg/cloud/services/gc/ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,45 @@ import (
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/filter"
)

func (s *Service) deleteEC2Instances(ctx context.Context, resources []*AWSResource) error {
for _, resource := range resources {
if !s.isEC2InstanceToDelete(resource) {
s.scope.Debug("Resource not an EC2 instance for deletion", "arn", resource.ARN.String())
continue
}

instanceID := strings.ReplaceAll(resource.ARN.Resource, "instance/", "")
if err := s.deleteEC2Instance(ctx, instanceID); err != nil {
return fmt.Errorf("deleting EC2 instance %s: %w", instanceID, err)
}
}
s.scope.Debug("Finished processing resources for EC2 instance deletion")

return nil
}

func (s *Service) isEC2InstanceToDelete(resource *AWSResource) bool {
if !s.isMatchingResource(resource, ec2.ServiceName, "instance") {
return false
}
if eksClusterName := resource.Tags[eksClusterNameTag]; eksClusterName != "" {
s.scope.Debug("EC2 instance was created by EKS directly", "arn", resource.ARN.String(), "check", "instance", "cluster_name", eksClusterName)
return false
}
s.scope.Debug("Resource is an EC2 instance to delete", "arn", resource.ARN.String(), "check", "instance")
return true
}

func (s *Service) deleteEC2Instance(ctx context.Context, instanceID string) error {
input := ec2.TerminateInstancesInput{
InstanceIds: []*string{aws.String(instanceID)},
}
if _, err := s.ec2Client.TerminateInstancesWithContext(ctx, &input); err != nil {
return fmt.Errorf("terminating EC2 instance: %w", err)
}
return nil
}

func (s *Service) deleteSecurityGroups(ctx context.Context, resources []*AWSResource) error {
for _, resource := range resources {
if !s.isSecurityGroupToDelete(resource) {
Expand Down
1 change: 1 addition & 0 deletions pkg/cloud/services/gc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func NewService(clusterScope cloud.ClusterScoper, opts ...ServiceOption) *Servic

func addDefaultCleanupFuncs(s *Service) {
s.cleanupFuncs = []ResourceCleanupFunc{
s.deleteEC2Instances,
s.deleteLoadBalancers,
s.deleteTargetGroups,
s.deleteSecurityGroups,
Expand Down

0 comments on commit 7c2d0b5

Please sign in to comment.