Skip to content

Commit

Permalink
Get VPC configuration from stack outputs
Browse files Browse the repository at this point in the history
This allows us to also obtain public and private subnets separately,
so that we have complete set of identifiers that would be of use
to create build the nodegroup stack.
  • Loading branch information
errordeveloper committed Jan 15, 2019
1 parent f7bca1c commit 87ceb6f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 18 deletions.
37 changes: 37 additions & 0 deletions pkg/cfn/manager/cluster.go
@@ -1,7 +1,11 @@
package manager

import (
"strings"

cfn "github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/kris-nova/logger"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha3"
"github.com/weaveworks/eksctl/pkg/cfn/builder"
)

Expand All @@ -22,6 +26,24 @@ func (c *StackCollection) CreateCluster(errs chan error, _ interface{}) error {
return c.CreateStack(name, stack, nil, nil, errs)
}

// DescribeClusterStack calls DescribeStacks and filters out cluster stack
func (c *StackCollection) DescribeClusterStack() (*Stack, error) {
stacks, err := c.DescribeStacks()
if err != nil {
return nil, err
}

for _, s := range stacks {
if *s.StackStatus == cfn.StackStatusDeleteComplete {
continue
}
if getClusterName(s) != "" {
return s, nil
}
}
return nil, nil
}

// DeleteCluster deletes the cluster
func (c *StackCollection) DeleteCluster() error {
_, err := c.DeleteStack(c.makeClusterStackName())
Expand All @@ -32,3 +54,18 @@ func (c *StackCollection) DeleteCluster() error {
func (c *StackCollection) WaitDeleteCluster() error {
return c.BlockingWaitDeleteStack(c.makeClusterStackName())
}

func getClusterName(s *Stack) string {
for _, tag := range s.Tags {
if *tag.Key == api.ClusterNameTag {
if strings.HasSuffix(*s.StackName, "-cluster") {
return *tag.Value
}
}
}

if strings.HasPrefix(*s.StackName, "EKS-") && strings.HasSuffix(*s.StackName, "-ControlPlane") {
return strings.TrimPrefix("EKS-", strings.TrimSuffix(*s.StackName, "-ControlPlane"))
}
return ""
}
9 changes: 0 additions & 9 deletions pkg/cfn/manager/nodegroup.go
Expand Up @@ -249,12 +249,3 @@ func getNodeGroupName(s *Stack) string {
}
return ""
}

func getClusterName(s *Stack) string {
for _, tag := range s.Tags {
if *tag.Key == api.ClusterNameTag {
return *tag.Value
}
}
return ""
}
37 changes: 28 additions & 9 deletions pkg/eks/eks.go
Expand Up @@ -7,12 +7,14 @@ import (
"strings"
"time"

"github.com/weaveworks/eksctl/pkg/cfn/builder"
"github.com/weaveworks/eksctl/pkg/vpc"

awseks "github.com/aws/aws-sdk-go/service/eks"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha3"
"github.com/weaveworks/eksctl/pkg/printers"
"github.com/weaveworks/eksctl/pkg/vpc"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/kubernetes"
)
Expand Down Expand Up @@ -86,25 +88,42 @@ func (c *ClusterProvider) GetCredentials(spec *api.ClusterConfig) error {

// GetClusterVPC retrieves the VPC configuration
func (c *ClusterProvider) GetClusterVPC(spec *api.ClusterConfig) error {
// Check the cluster exists and is active
cluster, err := c.DescribeControlPlaneMustBeActive(spec.Metadata)
cluster, err := c.NewStackManager(spec).DescribeClusterStack()
if err != nil {
return err
}
logger.Debug("cluster = %#v", cluster)

outputs := map[string]string{}
for _, x := range cluster.Outputs {
outputs[*x.OutputKey] = *x.OutputValue
}

if spec.VPC == nil {
spec.VPC = &api.ClusterVPC{}
}

if err := vpc.ImportVPC(c.Provider, spec, *cluster.ResourcesVpcConfig.VpcId); err != nil {
return err
requiredKeyErrFmt := "cluster stack has no output key %q"

if vpc, ok := outputs[builder.CfnOutputClusterVPC]; ok {
spec.VPC.ID = vpc
} else {
return fmt.Errorf(requiredKeyErrFmt, builder.CfnOutputClusterVPC)
}

if numSGs := len(cluster.ResourcesVpcConfig.SecurityGroupIds); numSGs == 1 {
spec.VPC.SecurityGroup = *cluster.ResourcesVpcConfig.SecurityGroupIds[0]
if securityGroup, ok := outputs[builder.CfnOutputClusterSecurityGroup]; ok {
spec.VPC.SecurityGroup = securityGroup
} else {
return fmt.Errorf("cluster %q has %d security groups, expected 1", spec.Metadata.Name, numSGs)
return fmt.Errorf(requiredKeyErrFmt, builder.CfnOutputClusterSecurityGroup)
}

for _, topology := range api.SubnetTopologies() {
// either of subnet topologies are optional
if subnets, ok := outputs[builder.CfnOutputClusterSubnets+string(topology)]; ok {
subnets := strings.Split(subnets, ",")
if err := vpc.UseSubnets(c.Provider, spec, topology, subnets); err != nil {
return err
}
}
}

return nil
Expand Down

0 comments on commit 87ceb6f

Please sign in to comment.