Skip to content

Commit

Permalink
set subnet CIDR block automatically by getting VPC AddrPrefix
Browse files Browse the repository at this point in the history
  • Loading branch information
emmayang committed Oct 13, 2021
1 parent 5574fce commit fb7863e
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions cloud/scope/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package scope

import (
"context"
"fmt"

"github.com/IBM/go-sdk-core/core"
"github.com/IBM/vpc-go-sdk/vpcv1"
Expand Down Expand Up @@ -195,13 +196,9 @@ func (s *ClusterScope) CreateSubnet() (*vpcv1.Subnet, error) {

options := &vpcv1.CreateSubnetOptions{}
var cidrBlock string
switch s.IBMVPCCluster.Spec.Zone {
case "us-south-1":
cidrBlock = "10.240.0.0/24"
case "us-south-2":
cidrBlock = "10.240.64.0/24"
case "us-south-3":
cidrBlock = "10.240.128.0/24"
cidrBlock, err = s.getSubnetAddrPrefix(s.IBMVPCCluster.Status.VPC.ID, s.IBMVPCCluster.Spec.Zone)
if err != nil {
return nil, err
}
subnetName = s.IBMVPCCluster.Name + "-subnet"
options.SetSubnetPrototype(&vpcv1.SubnetPrototype{
Expand Down Expand Up @@ -229,6 +226,23 @@ func (s *ClusterScope) CreateSubnet() (*vpcv1.Subnet, error) {
return subnet, err
}

func (s *ClusterScope) getSubnetAddrPrefix(vpcID, zone string) (string, error) {
options := &vpcv1.ListVPCAddressPrefixesOptions{}
options.VPCID = &vpcID
addrCollection, _, err := s.IBMVPCClients.VPCService.ListVPCAddressPrefixes(options)

if err != nil {
return "", err
} else {
for _, addrPrefix := range addrCollection.AddressPrefixes {
if *addrPrefix.Zone.Name == zone {
return *addrPrefix.CIDR, nil
}
}
return "", fmt.Errorf("Error when getting subnet addr prefix for VPC %s in zone %s", vpcID, zone)
}
}

func (s *ClusterScope) ensureSubnetUnique(subnetName string) (*vpcv1.Subnet, error) {
options := &vpcv1.ListSubnetsOptions{}
subnets, _, err := s.IBMVPCClients.VPCService.ListSubnets(options)
Expand Down

0 comments on commit fb7863e

Please sign in to comment.