Skip to content

Commit

Permalink
Fix all silly mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
yorinasub17 committed Nov 30, 2018
1 parent 454b51e commit d791082
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 26 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,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
21 changes: 6 additions & 15 deletions aws/eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/gruntwork-io/cloud-nuke/util"
"github.com/gruntwork-io/gruntwork-cli/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand All @@ -17,13 +16,11 @@ import (
func TestListEksClusters(t *testing.T) {
t.Parallel()

region := getRandomEksSupportedRegion()
region := getRandomEksSupportedRegion(t)
awsSession, err := session.NewSession(&awsgo.Config{
Region: awsgo.String(region),
})
if err != nil {
require.Fail(t, errors.WithStackTrace(err).Error())
}
require.NoError(t, err)

uniqueID := util.UniqueID()

Expand Down Expand Up @@ -51,13 +48,11 @@ func TestListEksClusters(t *testing.T) {
func TestNukeEksClusters(t *testing.T) {
t.Parallel()

region := getRandomEksSupportedRegion()
region := getRandomEksSupportedRegion(t)
awsSession, err := session.NewSession(&awsgo.Config{
Region: awsgo.String(region),
})
if err != nil {
assert.Fail(t, errors.WithStackTrace(err).Error())
}
require.NoError(t, err)

uniqueID := util.UniqueID()

Expand All @@ -66,13 +61,9 @@ func TestNukeEksClusters(t *testing.T) {

cluster := createEksCluster(t, awsSession, uniqueID, *role.Arn)
err = nukeAllEksClusters(awsSession, []*string{cluster.Name})
if err != nil {
assert.Fail(t, err.Error())
}
require.NoError(t, err)

eksClusterNames, err := getAllEksClusters(awsSession, time.Now().Add(1*time.Hour))
if err != nil {
assert.Failf(t, "Unable to fetch list of clusters: %s", err.Error())
}
require.NoError(t, err)
assert.NotContains(t, awsgo.StringValueSlice(eksClusterNames), *cluster.Name)
}
34 changes: 34 additions & 0 deletions aws/eks_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package aws

import (
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/gruntwork-io/gruntwork-cli/errors"
)

// EKSClusters - Represents all EKS clusters found in a region
type EKSClusters struct {
Clusters []string
}

// ResourceName - The simple name of the aws resource
func (clusters EKSClusters) ResourceName() string {
return "ekscluster"
}

// ResourceIdentifiers - The Name of the collected EKS clusters
func (clusters EKSClusters) ResourceIdentifiers() []string {
return clusters.Clusters
}

func (clusters EKSClusters) MaxBatchSize() int {
return 200
}

// Nuke - nuke all EKS Cluster resources
func (clusters EKSClusters) Nuke(awsSession *session.Session, identifiers []string) error {
if err := nukeAllEksClusters(awsSession, awsgo.StringSlice(identifiers)); err != nil {
return errors.WithStackTrace(err)
}
return nil
}
16 changes: 5 additions & 11 deletions aws/eks_utils_for_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package aws

import (
"fmt"
"math/rand"
"testing"
"time"

Expand All @@ -11,22 +10,17 @@ import (
"github.com/aws/aws-sdk-go/service/eks"
"github.com/aws/aws-sdk-go/service/iam"
gruntworkerrors "github.com/gruntwork-io/gruntwork-cli/errors"
terraAws "github.com/gruntwork-io/terratest/modules/aws"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// getRandomEksSupportedRegion - Returns a random AWS region that supports EKS.
// Refer to https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services/
func getRandomEksSupportedRegion() string {
supportedRegions := []string{
"us-east-1",
"us-east-2",
"us-west-2",
"eu-west-1",
}
rand.Seed(time.Now().UnixNano())
randIndex := rand.Intn(len(supportedRegions))
return supportedRegions[randIndex]
func getRandomEksSupportedRegion(t *testing.T) string {
// Approve only regions where EKS and the EKS optimized Linux AMI are available
approvedRegions := []string{"us-west-2", "us-east-1", "us-east-2", "eu-west-1"}
return terraAws.GetRandomRegion(t, approvedRegions, []string{})
}

func createEksCluster(
Expand Down

0 comments on commit d791082

Please sign in to comment.