Skip to content

Commit

Permalink
Merge pull request #301 from gotjosh/list-across-all-regions
Browse files Browse the repository at this point in the history
Add `--all-regions` flag for `get cluster`
  • Loading branch information
errordeveloper authored Nov 6, 2018
2 parents faa7fa2 + ae6c894 commit cc274b1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 28 deletions.
21 changes: 17 additions & 4 deletions pkg/ctl/get/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package get
import (
"fmt"
"os"
"strings"

"github.com/kubicorn/kubicorn/pkg/logger"
"github.com/spf13/cobra"
Expand All @@ -11,6 +12,8 @@ import (
"github.com/weaveworks/eksctl/pkg/eks/api"
)

var listAllRegions bool

func getClusterCmd() *cobra.Command {
cfg := api.NewClusterConfig()

Expand All @@ -29,6 +32,7 @@ func getClusterCmd() *cobra.Command {
fs := cmd.Flags()

fs.StringVarP(&cfg.ClusterName, "name", "n", "", "EKS cluster name")
fs.BoolVarP(&listAllRegions, "all-regions", "A", false, "List clusters across all supported regions")
fs.IntVar(&chunkSize, "chunk-size", defaultChunkSize, "Return large lists in chunks rather than all at once. Pass 0 to disable.")

fs.StringVarP(&cfg.Region, "region", "r", "", "AWS region")
Expand All @@ -39,10 +43,15 @@ func getClusterCmd() *cobra.Command {
}

func doGetCluster(cfg *api.ClusterConfig, name string) error {
regionGiven := cfg.Region != "" // eks.New resets this field, so we need to check if it was set in the fist place
ctl := eks.New(cfg)

if err := ctl.CheckAuth(); err != nil {
return err
if !cfg.IsSupportedRegion() {
return fmt.Errorf("--region=%s is not supported - use one of: %s", cfg.Region, strings.Join(api.SupportedRegions(), ", "))
}

if regionGiven && listAllRegions {
logger.Warning("--region=%s is ignored, as --all-regions is given", cfg.Region)
}

if cfg.ClusterName != "" && name != "" {
Expand All @@ -53,9 +62,13 @@ func doGetCluster(cfg *api.ClusterConfig, name string) error {
cfg.ClusterName = name
}

if err := ctl.ListClusters(chunkSize, output); err != nil {
if cfg.ClusterName != "" && listAllRegions {
return fmt.Errorf("--all-regions is for listing all clusters, it must be used without cluster name flag/argument")
}

if err := ctl.CheckAuth(); err != nil {
return err
}

return nil
return ctl.ListClusters(chunkSize, output, listAllRegions)
}
51 changes: 32 additions & 19 deletions pkg/eks/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"time"

"github.com/weaveworks/eksctl/pkg/eks/api"
"github.com/weaveworks/eksctl/pkg/printers"

"github.com/pkg/errors"
Expand Down Expand Up @@ -66,7 +67,7 @@ func (c *ClusterProvider) GetCredentials(cluster awseks.Cluster) error {
}

// ListClusters display details of all the EKS cluster in your account
func (c *ClusterProvider) ListClusters(chunkSize int, output string) error {
func (c *ClusterProvider) ListClusters(chunkSize int, output string, eachRegion bool) error {
// NOTE: this needs to be reworked in the future so that the functionality
// is combined. This require the ability to return details of all clusters
// in a single call.
Expand All @@ -85,33 +86,49 @@ func (c *ClusterProvider) ListClusters(chunkSize int, output string) error {
if output == "table" {
addListTableColumns(printer.(*printers.TablePrinter))
}
return c.doListClusters(int64(chunkSize), printer)
allClusters := []*clusterWithRegion{}
if err := c.doListClusters(int64(chunkSize), printer, &allClusters, eachRegion); err != nil {
return err
}
return printer.PrintObj("clusters", allClusters, os.Stdout)
}

func (c *ClusterProvider) doListClusters(chunkSize int64, printer printers.OutputPrinter) error {
allClusters := []*clusterWithRegion{}
func (c *ClusterProvider) getClustersRequest(chunkSize int64, nextToken string) ([]*string, *string, error) {
input := &awseks.ListClustersInput{MaxResults: &chunkSize}
if nextToken != "" {
input = input.SetNextToken(nextToken)
}
output, err := c.Provider.EKS().ListClusters(input)
if err != nil {
return nil, nil, errors.Wrap(err, "listing control planes")
}
return output.Clusters, output.NextToken, nil
}

getFunc := func(chunkSize int64, nextToken string) ([]*string, *string, error) {
input := &awseks.ListClustersInput{MaxResults: &chunkSize}
if nextToken != "" {
input = input.SetNextToken(nextToken)
}
output, err := c.Provider.EKS().ListClusters(input)
if err != nil {
return nil, nil, errors.Wrap(err, "listing control planes")
func (c *ClusterProvider) doListClusters(chunkSize int64, printer printers.OutputPrinter, allClusters *[]*clusterWithRegion, eachRegion bool) error {
if eachRegion {
// reset region and re-create the client, then make a recursive call
for _, region := range api.SupportedRegions() {
c.Spec.Region = region
if err := New(c.Spec).doListClusters(chunkSize, printer, allClusters, false); err != nil {
return err
}
}
return output.Clusters, output.NextToken, nil
return nil
}

token := ""
for {
clusters, nextToken, err := getFunc(chunkSize, token)
clusters, nextToken, err := c.getClustersRequest(chunkSize, token)
if err != nil {
return err
}

for _, clusterName := range clusters {
allClusters = append(allClusters, &clusterWithRegion{Name: *clusterName, Region: c.Spec.Region})
*allClusters = append(*allClusters, &clusterWithRegion{
Name: *clusterName,
Region: c.Spec.Region,
})
}

if nextToken != nil && *nextToken != "" {
Expand All @@ -121,10 +138,6 @@ func (c *ClusterProvider) doListClusters(chunkSize int64, printer printers.Outpu
}
}

if err := printer.PrintObj("clusters", allClusters, os.Stdout); err != nil {
return err
}

return nil
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/eks/eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ var _ = Describe("Eks", func() {
})

JustBeforeEach(func() {
err = c.ListClusters(100, output)
err = c.ListClusters(100, output, false)
})

It("should not error", func() {
Expand Down Expand Up @@ -92,7 +92,7 @@ var _ = Describe("Eks", func() {
})

JustBeforeEach(func() {
err = c.ListClusters(100, output)
err = c.ListClusters(100, output, false)
})

It("should not error", func() {
Expand Down Expand Up @@ -143,7 +143,7 @@ var _ = Describe("Eks", func() {
})

JustBeforeEach(func() {
err = c.ListClusters(100, output)
err = c.ListClusters(100, output, false)
})

AfterEach(func() {
Expand Down Expand Up @@ -222,7 +222,7 @@ var _ = Describe("Eks", func() {
})

JustBeforeEach(func() {
err = c.ListClusters(chunkSize, output)
err = c.ListClusters(chunkSize, output, false)
})

It("should not error", func() {
Expand Down Expand Up @@ -257,7 +257,7 @@ var _ = Describe("Eks", func() {
})

JustBeforeEach(func() {
err = c.ListClusters(chunkSize, output)
err = c.ListClusters(chunkSize, output, false)
})

It("should not error", func() {
Expand Down

0 comments on commit cc274b1

Please # to comment.