-
-
Notifications
You must be signed in to change notification settings - Fork 359
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
Nuke EKS clusters #43
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0060166
Nuke EKS clusters
yorinasub17 3e877eb
Downgrade to terratest compatible version
yorinasub17 0b055c2
Add sleep
yorinasub17 7578319
Fix all silly mistakes
yorinasub17 45605cd
Merge branch 'master' into yori-nuke-eks
tonerdo ff482d5
Merge branch 'master' into yori-nuke-eks
tonerdo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package aws | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/eks" | ||
"github.com/gruntwork-io/cloud-nuke/logging" | ||
"github.com/gruntwork-io/gruntwork-cli/errors" | ||
) | ||
|
||
// getAllEksClusters returns a list of strings of EKS Cluster Names that uniquely identify each cluster. | ||
func getAllEksClusters(awsSession *session.Session, excludeAfter time.Time) ([]*string, error) { | ||
svc := eks.New(awsSession) | ||
result, err := svc.ListClusters(&eks.ListClustersInput{}) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
filteredClusters, err := filterOutRecentEksClusters(svc, result.Clusters, excludeAfter) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
return filteredClusters, nil | ||
} | ||
|
||
// filterOutRecentEksClusters will take in the list of clusters and filter out any clusters that were created after | ||
// `excludeAfter`. | ||
func filterOutRecentEksClusters(svc *eks.EKS, clusterNames []*string, excludeAfter time.Time) ([]*string, error) { | ||
var filteredEksClusterNames []*string | ||
for _, clusterName := range clusterNames { | ||
describeResult, err := svc.DescribeCluster(&eks.DescribeClusterInput{ | ||
Name: clusterName, | ||
}) | ||
if err != nil { | ||
return nil, errors.WithStackTrace(err) | ||
} | ||
cluster := describeResult.Cluster | ||
if excludeAfter.After(*cluster.CreatedAt) { | ||
filteredEksClusterNames = append(filteredEksClusterNames, cluster.Name) | ||
} | ||
} | ||
return filteredEksClusterNames, nil | ||
} | ||
|
||
// deleteEksClusters deletes all clusters requested. Returns a list of cluster names that have been accepted by AWS | ||
// for deletion. | ||
func deleteEksClusters(svc *eks.EKS, eksClusterNames []*string) []*string { | ||
var requestedDeletes []*string | ||
for _, eksClusterName := range eksClusterNames { | ||
_, err := svc.DeleteCluster(&eks.DeleteClusterInput{Name: eksClusterName}) | ||
if err != nil { | ||
logging.Logger.Errorf("[Failed] Failed deleting EKS cluster %s: %s", *eksClusterName, err) | ||
} else { | ||
requestedDeletes = append(requestedDeletes, eksClusterName) | ||
} | ||
} | ||
return requestedDeletes | ||
} | ||
|
||
// waitUntilEksClustersDeleted waits until the EKS cluster has been actually deleted from AWS. Returns a list of EKS | ||
// cluster names that have been successfully deleted. | ||
func waitUntilEksClustersDeleted(svc *eks.EKS, eksClusterNames []*string) []*string { | ||
var successfullyDeleted []*string | ||
for _, eksClusterName := range eksClusterNames { | ||
err := svc.WaitUntilClusterDeleted(&eks.DescribeClusterInput{Name: eksClusterName}) | ||
if err != nil { | ||
logging.Logger.Errorf("[Failed] Failed waiting for EKS cluster to be deleted %s: %s", *eksClusterName, err) | ||
} else { | ||
logging.Logger.Infof("Deleted EKS cluster: %s", *eksClusterName) | ||
successfullyDeleted = append(successfullyDeleted, eksClusterName) | ||
} | ||
} | ||
return successfullyDeleted | ||
} | ||
|
||
// nukeAllEksClusters deletes all provided EKS clusters, waiting for them to be deleted before returning. | ||
func nukeAllEksClusters(awsSession *session.Session, eksClusterNames []*string) error { | ||
numNuking := len(eksClusterNames) | ||
svc := eks.New(awsSession) | ||
|
||
if numNuking == 0 { | ||
logging.Logger.Infof("No EKS clusters to nuke in region %s", *awsSession.Config.Region) | ||
return nil | ||
} | ||
|
||
logging.Logger.Infof("Deleting %d EKS clusters in region %s", numNuking, *awsSession.Config.Region) | ||
|
||
requestedDeletes := deleteEksClusters(svc, eksClusterNames) | ||
successfullyDeleted := waitUntilEksClustersDeleted(svc, requestedDeletes) | ||
|
||
numNuked := len(successfullyDeleted) | ||
logging.Logger.Infof("[OK] %d of %d EKS cluster(s) deleted in %s", numNuked, numNuking, *awsSession.Config.Region) | ||
return nil | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't you need to call this EKS code from somewhere? I think there's a struct of some sort you need to add and then some extra API calls in the main CLI app...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup... I can't believe I missed that...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was so tied up in the tests and it was getting late that I got a bit too trigger happy I guess.