Skip to content

Commit

Permalink
Pagination and logic split
Browse files Browse the repository at this point in the history
The functionality of `get clusters` has been split. So if no name
is supplied then a list of cluster names will be returned. If a name
is supplied then a summary of the cluster will be returned. Also the
AWS API pagination has been implemented.
  • Loading branch information
richardcase committed Aug 1, 2018
1 parent 8558219 commit 9e86857
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 103 deletions.
46 changes: 0 additions & 46 deletions .vscode/launch.json

This file was deleted.

42 changes: 2 additions & 40 deletions cmd/eksctl/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,11 @@ package main
import (
"fmt"
"os"
"strings"
"time"

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

awseks "github.com/aws/aws-sdk-go/service/eks"
"github.com/kubicorn/kubicorn/pkg/logger"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/sets"
)

const (
Expand Down Expand Up @@ -61,7 +56,7 @@ func getClusterCmd() *cobra.Command {
fs.StringVarP(&cfg.Region, "region", "r", DEFAULT_EKS_REGION, "AWS region")
fs.StringVarP(&cfg.Profile, "profile", "p", "", "AWS creditials profile to use (overrides the AWS_PROFILE environment variable)")

fs.StringVarP(&output, "output", "o", "log", "Specifies the output printer to use")
fs.StringVarP(&output, "output", "o", "table", "Specifies the output format. Choose from table,json,yaml,log. Defaults to table.")
return cmd
}

Expand All @@ -80,42 +75,9 @@ func doGetCluster(cfg *eks.ClusterConfig, name string) error {
cfg.ClusterName = name
}

printer, err := printers.NewPrinter(output)
if err != nil {
return err
}
if output == "table" {
addTableColumns(printer.(*printers.TablePrinter))
}

if err := ctl.ListClusters(chunkSize, printer); err != nil {
if err := ctl.ListClusters(chunkSize, output); err != nil {
return err
}

return nil
}

func addTableColumns(printer *printers.TablePrinter) {

printer.AddColumn("CLUSTERNAME", func(c *awseks.Cluster) string {
return *c.Name
})
printer.AddColumn("ARN", func(c *awseks.Cluster) string {
return *c.Arn
})
printer.AddColumn("VPC", func(c *awseks.Cluster) string {
return *c.ResourcesVpcConfig.VpcId
})
printer.AddColumn("SUBNETS", func(c *awseks.Cluster) string {
subnets := sets.NewString()
for _, subnetid := range c.ResourcesVpcConfig.SubnetIds {
if *subnetid != "" {
subnets.Insert(*subnetid)
}
}
return strings.Join(subnets.List(), ",")
})
printer.AddColumn("CREATED", func(c *awseks.Cluster) string {
return c.CreatedAt.Format(time.RFC3339)
})
}
91 changes: 79 additions & 12 deletions pkg/eks/eks.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/weaveworks/eksctl/pkg/printers"

"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"

"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -147,27 +148,64 @@ 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, printer printers.OutputPrinter) error {
func (c *ClusterProvider) ListClusters(chunkSize int, output string) 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.
printer, err := printers.NewPrinter(output)
if err != nil {
return err
}

if c.Spec.ClusterName != "" {
return c.doListCluster(&c.Spec.ClusterName, printer)
if output == "table" {
addSummaryTableColumns(printer.(*printers.TablePrinter))
}
return c.doGetCluster(&c.Spec.ClusterName, printer)
}

// TODO: https://github.com/weaveworks/eksctl/issues/27
input := &awseks.ListClustersInput{}
output, err := c.Provider.EKS().ListClusters(input)
if err != nil {
return errors.Wrap(err, "listing control planes")
if output == "table" {
addListTableColumns(printer.(*printers.TablePrinter))
}
return c.doListClusters(int64(chunkSize), printer)
}

func (c *ClusterProvider) doListClusters(chunkSize int64, printer printers.OutputPrinter) error {
allClusterNames := []*string{}

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")
}
return output.Clusters, output.NextToken, nil
}
logger.Debug("clusters = %#v", output)
for _, clusterName := range output.Clusters {
if err := c.doListCluster(clusterName, printer); err != nil {

token := ""
for {
clusters, nextToken, err := getFunc(chunkSize, token)
if err != nil {
return err
}
allClusterNames = append(allClusterNames, clusters...)
if nextToken != nil && *nextToken != "" {
token = *nextToken
} else {
break
}
}

logger.Debug("clusters = %#v", allClusterNames)
printer.PrintObj(allClusterNames, os.Stdout)

return nil
}

func (c *ClusterProvider) doListCluster(clusterName *string, printer printers.OutputPrinter) error {
func (c *ClusterProvider) doGetCluster(clusterName *string, printer printers.OutputPrinter) error {
input := &awseks.DescribeClusterInput{
Name: clusterName,
}
Expand All @@ -177,7 +215,6 @@ func (c *ClusterProvider) doListCluster(clusterName *string, printer printers.Ou
}
logger.Debug("cluster = %#v", output)
if *output.Cluster.Status == awseks.ClusterStatusActive {

clusters := []*awseks.Cluster{output.Cluster} // TODO: in the future this will have multiple clusters
printer.PrintObj(clusters, os.Stdout)

Expand Down Expand Up @@ -223,3 +260,33 @@ func (c *ClusterConfig) WaitForControlPlane(clientSet *kubernetes.Clientset) err
}
}
}

func addSummaryTableColumns(printer *printers.TablePrinter) {
printer.AddColumn("NAME", func(c *awseks.Cluster) string {
return *c.Name
})
printer.AddColumn("ARN", func(c *awseks.Cluster) string {
return *c.Arn
})
printer.AddColumn("VPC", func(c *awseks.Cluster) string {
return *c.ResourcesVpcConfig.VpcId
})
printer.AddColumn("SUBNETS", func(c *awseks.Cluster) string {
subnets := sets.NewString()
for _, subnetid := range c.ResourcesVpcConfig.SubnetIds {
if *subnetid != "" {
subnets.Insert(*subnetid)
}
}
return strings.Join(subnets.List(), ",")
})
printer.AddColumn("CREATED", func(c *awseks.Cluster) string {
return c.CreatedAt.Format(time.RFC3339)
})
}

func addListTableColumns(printer *printers.TablePrinter) {
printer.AddColumn("NAME", func(c *string) string {
return *c
})
}
9 changes: 4 additions & 5 deletions pkg/eks/eks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,18 @@ import (
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
. "github.com/weaveworks/eksctl/pkg/eks"
"github.com/weaveworks/eksctl/pkg/printers"
"github.com/weaveworks/eksctl/pkg/testutils"
)

var _ = Describe("Eks", func() {
var (
c *ClusterProvider
p *testutils.MockProvider
printer printers.OutputPrinter
output string
)

BeforeEach(func() {
printer, _ = printers.NewPrinter("log")
output = "log"
})

Describe("ListAll", func() {
Expand Down Expand Up @@ -69,7 +68,7 @@ var _ = Describe("Eks", func() {
})

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

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

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

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

0 comments on commit 9e86857

Please sign in to comment.