Skip to content

Commit

Permalink
Allow cluster CIDR to be configurable
Browse files Browse the repository at this point in the history
fixes #27
  • Loading branch information
ipmb committed Aug 31, 2021
1 parent dc30277 commit 9cc31b8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
50 changes: 50 additions & 0 deletions cmd/createCluster.go
Expand Up @@ -17,17 +17,50 @@ package cmd

import (
"fmt"
"net"
"sort"
"strings"

"github.com/AlecAivazis/survey/v2"
"github.com/apparentlymart/go-cidr/cidr"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudformation"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/spf13/cobra"
)

// splitSubnet takes a CIDR string and splits it up into two groups of 3 comma-separated subnets
func splitSubnet(cidrStr string) (*string, *string, error) {
maxSubnetMask := 24
minSubnetMask := 16
_, clusterCIDR, err := net.ParseCIDR(cidrStr)
if err != nil {
return nil, nil, err
}
mask, _ := clusterCIDR.Mask.Size()
if mask < minSubnetMask || mask > maxSubnetMask {
return nil, nil, fmt.Errorf("valid subnet mask range is %d-%d", minSubnetMask, maxSubnetMask)
}
subnetMask := net.CIDRMask(mask+4, 32)
subnets := []*net.IPNet{}
subnets = append(subnets, &net.IPNet{IP: clusterCIDR.IP, Mask: subnetMask})
prefix, _ := subnetMask.Size()
for i := 0; i < 8; i++ {
nextCIDR, _ := cidr.NextSubnet(subnets[i], prefix)
subnets = append(subnets, nextCIDR)
}
publicSubnets := []string{}
for i := 0; i < 3; i++ {
publicSubnets = append(publicSubnets, subnets[i].String())
}
privateSubnets := []string{}
for i := 6; i < 9; i++ {
privateSubnets = append(privateSubnets, subnets[i].String())
}
return aws.String(strings.Join(publicSubnets, ",")), aws.String(strings.Join(privateSubnets, ",")), nil
}

func ec2InstanceTypes(sess *session.Session) ([]*ec2.InstanceTypeInfo, error) {
ec2Svc := ec2.New(sess)
input := ec2.DescribeInstanceTypesInput{
Expand Down Expand Up @@ -119,6 +152,9 @@ var createClusterCmd = &cobra.Command{
zone, err := hostedZoneForDomain(sess, *domain)
zoneID := strings.Split(*zone.Id, "/")[2]
checkErr(err)
vpcCIDR := getArgValue(cmd, &answers, "cidr", true)
publicSubnets, privateSubnets, err := splitSubnet(*vpcCIDR)
checkErr(err)
input := cloudformation.CreateStackInput{
StackName: aws.String(fmt.Sprintf("apppack-cluster-%s", clusterName)),
TemplateURL: aws.String(getReleaseUrl(clusterFormationURL)),
Expand All @@ -127,6 +163,18 @@ var createClusterCmd = &cobra.Command{
ParameterKey: aws.String("Name"),
ParameterValue: &clusterName,
},
{
ParameterKey: aws.String("Cidr"),
ParameterValue: vpcCIDR,
},
{
ParameterKey: aws.String("PublicSubnetCidrs"),
ParameterValue: publicSubnets,
},
{
ParameterKey: aws.String("PrivateSubnetCidrs"),
ParameterValue: privateSubnets,
},
{
ParameterKey: aws.String("AvailabilityZones"),
ParameterValue: aws.String(strings.Join(
Expand Down Expand Up @@ -164,4 +212,6 @@ func init() {
initCmd.Flags().BoolP("ec2", "e", false, "setup cluster with EC2 instances")
createClusterCmd.Flags().StringP("instance-class", "i", "", "autoscaling instance class -- see https://aws.amazon.com/ec2/pricing/on-demand/")
initCmd.Flags().StringP("instance-class", "i", "", "autoscaling instance class -- see https://aws.amazon.com/ec2/pricing/on-demand/")
createClusterCmd.Flags().StringP("cidr", "c", "10.100.0.0/16", "network CIDR for VPC")
initCmd.Flags().StringP("cidr", "c", "10.100.0.0/16", "network CIDR for VPC")
}
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -7,6 +7,7 @@ require (
github.com/Netflix/go-expect v0.0.0-20201125194554-85d881c3777e // indirect
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 // indirect
github.com/TylerBrock/saw v0.2.2
github.com/apparentlymart/go-cidr v1.1.0
github.com/aws/aws-sdk-go v1.38.36
github.com/briandowns/spinner v1.12.0
github.com/creack/pty v1.1.11 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -30,6 +30,8 @@ github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw
github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/apparentlymart/go-cidr v1.1.0 h1:2mAhrMoF+nhXqxTzSZMUzDHkLjmIHC+Zzn4tdgBZjnU=
github.com/apparentlymart/go-cidr v1.1.0/go.mod h1:EBcsNrHc3zQeuaeCeCtQruQm+n9/YjEn/vI25Lg7Gwc=
github.com/apppackio/saw v0.2.3-0.20210507180802-f6559c287e6f h1:4qSROTO6FceKFgKaoYmALA953QpYHRrQhcG1v2uqusU=
github.com/apppackio/saw v0.2.3-0.20210507180802-f6559c287e6f/go.mod h1:GjKNeaxQeBkAudVlPmb2el62OMm4rjtY7Uzz1OmByAs=
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
Expand Down

0 comments on commit 9cc31b8

Please sign in to comment.