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

Nuke EKS clusters #43

Merged
merged 6 commits into from
Feb 21, 2019
Merged
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
131 changes: 114 additions & 17 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

[[constraint]]
name = "github.com/aws/aws-sdk-go"
version = "1.12.65"
version = "1.14.26"

[[constraint]]
name = "github.com/go-ini/ini"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ The currently supported functionality includes:
* Deleting all Elastic IPs in an AWS account
* Deleting all Launch Configurations in an AWS account
* Deleting all ECS services in an AWS account
* Deleting all EKS clusters in an AWS account

### Caveats

Expand Down
12 changes: 12 additions & 0 deletions aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,18 @@ func GetAllResources(regions []string, excludedRegions []string, excludeAfter ti
resourcesInRegion.Resources = append(resourcesInRegion.Resources, ecsServices)
// End ECS resources

// EKS resources
eksClusterNames, err := getAllEksClusters(session, excludeAfter)
if err != nil {
return nil, errors.WithStackTrace(err)
}

eksClusters := EKSClusters{
Clusters: awsgo.StringValueSlice(eksClusterNames),
}
resourcesInRegion.Resources = append(resourcesInRegion.Resources, eksClusters)
// End EKS resources

account.Resources[region] = resourcesInRegion
}

Expand Down
95 changes: 95 additions & 0 deletions aws/eks.go
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) {
Copy link
Member

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...

Copy link
Contributor Author

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...

Copy link
Contributor Author

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.

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

}
Loading