Skip to content

Commit

Permalink
Merge pull request #43 from gruntwork-io/yori-nuke-eks
Browse files Browse the repository at this point in the history
Nuke EKS clusters
  • Loading branch information
yorinasub17 authored Feb 21, 2019
2 parents a1dd176 + ff482d5 commit bead08e
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 18 deletions.
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) {
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

0 comments on commit bead08e

Please sign in to comment.