@@ -18,6 +18,7 @@ package scope
18
18
19
19
import (
20
20
"context"
21
+ "fmt"
21
22
22
23
"github.com/go-logr/logr"
23
24
"github.com/pkg/errors"
@@ -213,14 +214,9 @@ func (s *ClusterScope) CreateSubnet() (*vpcv1.Subnet, error) {
213
214
}
214
215
215
216
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
224
220
}
225
221
subnetName = s .IBMVPCCluster .Name + "-subnet"
226
222
options .SetSubnetPrototype (& vpcv1.SubnetPrototype {
@@ -248,6 +244,24 @@ func (s *ClusterScope) CreateSubnet() (*vpcv1.Subnet, error) {
248
244
return subnet , err
249
245
}
250
246
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
+
251
265
func (s * ClusterScope ) ensureSubnetUnique (subnetName string ) (* vpcv1.Subnet , error ) {
252
266
options := & vpcv1.ListSubnetsOptions {}
253
267
subnets , _ , err := s .IBMVPCClients .VPCService .ListSubnets (options )
0 commit comments