diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 30db237a29..0000000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,46 +0,0 @@ -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "name": "Launch test function", - "type": "go", - "request": "launch", - "mode": "test", - "program": "${workspaceRoot}/pkg/eks", - "args": [ - "-test.run", - "TestEks" - ] - }, - { - "name": "Connect to server", - "type": "go", - "request": "launch", - "mode": "remote", - "remotePath": "${workspaceRoot}", - "port": 25000, - "host": "127.0.0.1", - "program": "${workspaceRoot}", - "env": {}, - "args": [] - }, - { - "name": "Get clusters (no profile)", - "type": "go", - "request": "launch", - "mode": "debug", - "program": "${workspaceRoot}/cmd/eksctl", - "args": [ - "get", - "clusters", - "-n", - "test1", - "-o", - "table" - ] - } - ] -} \ No newline at end of file diff --git a/cmd/eksctl/get.go b/cmd/eksctl/get.go index 217005b680..1522404d4c 100644 --- a/cmd/eksctl/get.go +++ b/cmd/eksctl/get.go @@ -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 ( @@ -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 } @@ -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) - }) -} diff --git a/pkg/eks/eks.go b/pkg/eks/eks.go index a9b22e2e52..7bf32b2df2 100644 --- a/pkg/eks/eks.go +++ b/pkg/eks/eks.go @@ -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" @@ -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, } @@ -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) @@ -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 + }) +} diff --git a/pkg/eks/eks_test.go b/pkg/eks/eks_test.go index 42c33c9b0b..5fed592d71 100644 --- a/pkg/eks/eks_test.go +++ b/pkg/eks/eks_test.go @@ -11,7 +11,6 @@ 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" ) @@ -19,11 +18,11 @@ 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() { @@ -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() { @@ -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() {