From a677a822ecdc9de4779e0d6873a9e91e4975aa8f Mon Sep 17 00:00:00 2001 From: Richard Case Date: Thu, 25 Oct 2018 07:48:56 +0100 Subject: [PATCH] starting to work on create --- pkg/cfn/manager/nodegroup.go | 33 +++++++++++++++++++++++++++++++++ pkg/ctl/create/nodegroup.go | 15 +++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pkg/cfn/manager/nodegroup.go b/pkg/cfn/manager/nodegroup.go index d1747009dc5..17d61dd4f42 100644 --- a/pkg/cfn/manager/nodegroup.go +++ b/pkg/cfn/manager/nodegroup.go @@ -44,6 +44,20 @@ func (c *StackCollection) CreateInitialNodeGroup(errs chan error) error { return c.CreateNodeGroup(0, errs) } +// CreateNextNodeGroup creates a node group with the next sequence number +func (c *StackCollection) CreateNextNodeGroup(errs chan error) error { + currentSeq, err := c.GetMaxNodeGroupSeq() + if err != nil { + return errors.Wrap(err, "getting current node group sequence") + } + logger.Debug("max node group sequence: %d", currentSeq) + + newSeq := currentSeq + 1 + logger.Info("new node group sequence number: %d", newSeq) + + return c.CreateNodeGroup(newSeq, errs) +} + // CreateNodeGroup creates the node group func (c *StackCollection) CreateNodeGroup(seq int, errs chan error) error { name := c.makeNodeGroupStackName(seq) @@ -130,6 +144,24 @@ func (c *StackCollection) ScaleNodeGroup(sequence int) error { return c.UpdateStack(name, "scale-nodegroup", descriptionBuffer.String(), []byte(template), nil) } +// GetMaxNodeGroupSeq returns the sequence number og the highest node group +func (c *StackCollection) GetMaxNodeGroupSeq() (int, error) { + stacks, err := c.ListStacks(fmt.Sprintf("^(eksctl|EKS)-%s-nodegroup-\\d+$", c.spec.ClusterName)) + if err != nil { + return -1, errors.Wrap(err, "getting nodegroup stacks") + } + + seq := -1 + for _, stack := range stacks { + stackSeq := getNodeGroupID(stack.Tags) + if stackSeq > seq { + seq = stackSeq + } + } + + return seq, nil +} + // GetNodeGroupSummaries returns a list of summaries for the nodegroups of a cluster func (c *StackCollection) GetNodeGroupSummaries() ([]*NodeGroupSummary, error) { stacks, err := c.ListStacks(fmt.Sprintf("^(eksctl|EKS)-%s-nodegroup-\\d+$", c.spec.ClusterName), cloudformation.StackStatusCreateComplete) @@ -147,6 +179,7 @@ func (c *StackCollection) GetNodeGroupSummaries() ([]*NodeGroupSummary, error) { return nil, errors.Wrapf(err, "error getting Cloudformation template for stack %s", *stack.StackName) } + //TODO: create a map function seq := getNodeGroupID(stack.Tags) maxSize := gjson.Get(template, maxSizePath) minSize := gjson.Get(template, minSizePath) diff --git a/pkg/ctl/create/nodegroup.go b/pkg/ctl/create/nodegroup.go index 898d64d4057..fca62b20a5b 100644 --- a/pkg/ctl/create/nodegroup.go +++ b/pkg/ctl/create/nodegroup.go @@ -4,6 +4,8 @@ import ( "fmt" "os" + "github.com/weaveworks/eksctl/pkg/cfn/manager" + "github.com/kubicorn/kubicorn/pkg/logger" "github.com/spf13/cobra" "github.com/weaveworks/eksctl/pkg/ctl" @@ -71,6 +73,7 @@ func doAddNodeGroup(cfg *api.ClusterConfig, name string) error { logger.Debug("cfg = %#v", cfg) + // Check the cluster exists and is active eksCluster, err := ctl.DescribeControlPlane() if err != nil { return err @@ -81,9 +84,17 @@ func doAddNodeGroup(cfg *api.ClusterConfig, name string) error { logger.Info("found cluster %s", eksCluster.Name) logger.Debug("cluster = %#v", eksCluster) - //TODO: get the next nodegroup sequnce number + errs := []error{} + appendErr := func(err error) { + errs = append(errs, err) + } + + stackManager := ctl.NewStackManager() + if manager.Run(appendErr, stackManager.CreateNextNodeGroup); len(errs) > 0 { + //TODO: how to handle this???? + //return errs + } - //TODO: finish return nil }