Skip to content

Commit 293fe13

Browse files
authored
set subnet CIDR block automatically by getting VPC AddrPrefix (#344)
address review comments
1 parent 1341904 commit 293fe13

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

cloud/scope/cluster.go

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package scope
1818

1919
import (
2020
"context"
21+
"fmt"
2122

2223
"github.com/go-logr/logr"
2324
"github.com/pkg/errors"
@@ -213,14 +214,9 @@ func (s *ClusterScope) CreateSubnet() (*vpcv1.Subnet, error) {
213214
}
214215

215216
options := &vpcv1.CreateSubnetOptions{}
216-
var cidrBlock string
217-
switch s.IBMVPCCluster.Spec.Zone {
218-
case "us-south-1":
219-
cidrBlock = "10.240.0.0/24"
220-
case "us-south-2":
221-
cidrBlock = "10.240.64.0/24"
222-
case "us-south-3":
223-
cidrBlock = "10.240.128.0/24"
217+
cidrBlock, err := s.getSubnetAddrPrefix(s.IBMVPCCluster.Status.VPC.ID, s.IBMVPCCluster.Spec.Zone)
218+
if err != nil {
219+
return nil, err
224220
}
225221
subnetName = s.IBMVPCCluster.Name + "-subnet"
226222
options.SetSubnetPrototype(&vpcv1.SubnetPrototype{
@@ -248,6 +244,24 @@ func (s *ClusterScope) CreateSubnet() (*vpcv1.Subnet, error) {
248244
return subnet, err
249245
}
250246

247+
func (s *ClusterScope) getSubnetAddrPrefix(vpcID, zone string) (string, error) {
248+
options := &vpcv1.ListVPCAddressPrefixesOptions{
249+
VPCID: &vpcID,
250+
}
251+
addrCollection, _, err := s.IBMVPCClients.VPCService.ListVPCAddressPrefixes(options)
252+
253+
if err != nil {
254+
return "", err
255+
} else {
256+
for _, addrPrefix := range addrCollection.AddressPrefixes {
257+
if *addrPrefix.Zone.Name == zone {
258+
return *addrPrefix.CIDR, nil
259+
}
260+
}
261+
return "", fmt.Errorf("not found a valid CIDR for VPC %s in zone %s", vpcID, zone)
262+
}
263+
}
264+
251265
func (s *ClusterScope) ensureSubnetUnique(subnetName string) (*vpcv1.Subnet, error) {
252266
options := &vpcv1.ListSubnetsOptions{}
253267
subnets, _, err := s.IBMVPCClients.VPCService.ListSubnets(options)

0 commit comments

Comments
 (0)