Skip to content

Commit

Permalink
Delete machine pool user data files that did not get deleted yet by t…
Browse files Browse the repository at this point in the history
…he lifecycle policy (#593)

* Delete machine pool user data files that did not get deleted yet by the lifecycle policy

* Use paging for S3 results

* Log S3 list operation

* Handle NotFound
  • Loading branch information
AndiDog authored Jun 20, 2024
1 parent 0bcf5b8 commit 97afcee
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,12 @@ func (t Template) ControllersPolicy() *iamv1.PolicyDocument {
Action: iamv1.Actions{
"s3:CreateBucket",
"s3:DeleteBucket",
"s3:PutObject",
"s3:DeleteObject",
"s3:ListBucket",
"s3:PutBucketPolicy",
"s3:PutBucketTagging",
"s3:PutLifecycleConfiguration",
"s3:PutObject",
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,12 @@ Resources:
- Action:
- s3:CreateBucket
- s3:DeleteBucket
- s3:PutObject
- s3:DeleteObject
- s3:ListBucket
- s3:PutBucketPolicy
- s3:PutBucketTagging
- s3:PutLifecycleConfiguration
- s3:PutObject
Effect: Allow
Resource:
- arn:*:s3:::cluster-api-provider-aws-*
Expand Down
41 changes: 41 additions & 0 deletions pkg/cloud/services/s3/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,47 @@ func (s *Service) DeleteBucket() error {

log.Info("Deleting S3 Bucket")

// Delete machine pool user data files that did not get deleted
// yet by the lifecycle policy
for {
log.Info("Listing S3 objects of machine pools")

out, err := s.S3Client.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(bucketName),
Prefix: aws.String("machine-pool/"),
})
if err != nil {
aerr, ok := err.(awserr.Error)
if !ok {
return errors.Wrap(err, "listing S3 bucket")
}

switch aerr.Code() {
case s3.ErrCodeNoSuchBucket:
log.Info("Bucket already removed")
return nil
default:
return errors.Wrap(aerr, "listing S3 bucket")
}
}

// Stop on last page of results
if len(out.Contents) == 0 {
break
}

log.Info("Deleting S3 objects of machine pools", "count", len(out.Contents))
for _, obj := range out.Contents {
_, err := s.S3Client.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(bucketName),
Key: obj.Key,
})
if err != nil {
return err
}
}
}

_, err := s.S3Client.DeleteBucket(&s3.DeleteBucketInput{
Bucket: aws.String(bucketName),
})
Expand Down

0 comments on commit 97afcee

Please sign in to comment.