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

Allow garbage collector to delete ec2 instances #4568

Closed
wants to merge 1 commit into from
Closed
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
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I have a new request here because I stumbled over non-actionable error messages in existing GC code (they were missing the region and humans shouldn't be required to loop through all accounts and regions to find an object where only the ID is logged). I'm fixing existing code in a new PR.

Suggested change
return fmt.Errorf("deleting EC2 instance %s: %w", instanceID, err)
return fmt.Errorf("deleting EC2 instance %s with ID %s: %w", resource.ARN, 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