Skip to content

Commit

Permalink
Use multiple zones for multiple subnets
Browse files Browse the repository at this point in the history
  • Loading branch information
Shilpa-Gokul committed Jun 4, 2024
1 parent ff78637 commit 0cdecca
Showing 1 changed file with 32 additions and 27 deletions.
59 changes: 32 additions & 27 deletions cloud/scope/powervs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
"github.com/IBM/vpc-go-sdk/vpcv1"

kerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/klog/v2"
"k8s.io/utils/ptr"

"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -1071,8 +1070,27 @@ func (s *PowerVSClusterScope) ReconcileVPCSubnets() (bool, error) {
}
subnets = append(subnets, subnet)
}
for _, subnet := range subnets {
s.Info("Reconciling VPC subnet", "subnet", subnet)
powerVSZone := s.Zone()
if powerVSZone == nil {
return false, fmt.Errorf("powervs zone is not set")
}
region := endpoints.ConstructRegionFromZone(*powerVSZone)
vpcZones, err := genUtil.VPCZonesForPowerVSRegion(region)
if err != nil {
return false, err
}
// TODO(karthik-k-n): Decide on using all zones or using one zone
if len(vpcZones) == 0 {
return false, fmt.Errorf("error getting vpc zones error: %v", err)
}
// TODO: Solution- use different CIDR from available zones
s.Info("New- Length of subnets and zones", "subnet", len(subnets), "zones", len(vpcZones))
if len(subnets) < len(vpcZones) {
return false, fmt.Errorf("no enough vpcZones to use from PowerVS region please set zone with each subnet")
}

for index, subnet := range subnets {
s.Info("Reconciling VPC subnets-new", "subnet", subnet)
var subnetID *string
if subnet.ID != nil {
subnetID = subnet.ID
Expand Down Expand Up @@ -1106,9 +1124,14 @@ func (s *PowerVSClusterScope) ReconcileVPCSubnets() (bool, error) {
// check for next subnet
continue
}

var zone string
if subnet.Zone != nil {
zone = *subnet.Zone
} else {
zone = vpcZones[index]
}
s.V(3).Info("Creating VPC subnet")
subnetID, err = s.createVPCSubnet(subnet)
subnetID, err = s.createVPCSubnet(subnet, zone)
if err != nil {
s.Error(err, "failed to create VPC subnet")
return false, err
Expand All @@ -1134,44 +1157,26 @@ func (s *PowerVSClusterScope) checkVPCSubnet(subnetName string) (string, error)
}

// createVPCSubnet creates a VPC subnet.
func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet) (*string, error) {
func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet, zone string) (*string, error) {
// TODO(karthik-k-n): consider moving to clusterscope
// fetch resource group id
resourceGroupID := s.GetResourceGroupID()
if resourceGroupID == "" {
return nil, fmt.Errorf("failed to fetch resource group ID for resource group %v, ID is empty", s.ResourceGroup())
}
var zone string
if subnet.Zone != nil {
zone = *subnet.Zone
} else {
vpcZones, err := genUtil.VPCZonesForVPCRegion(*s.VPC().Region)
if err != nil {
return nil, err
}
// TODO(karthik-k-n): Decide on using all zones or using one zone
if len(vpcZones) == 0 {
return nil, fmt.Errorf("failed to fetch VPC zones, error: %v", err)
}
zone = vpcZones[0]
}

// create subnet
vpcID := s.GetVPCID()
if vpcID == nil {
return nil, fmt.Errorf("VPC ID is empty")
}
cidrBlock, err := s.IBMVPCClient.GetSubnetAddrPrefix(*vpcID, zone)
if err != nil {
return nil, err
}
ipVersion := "ipv4"

options := &vpcv1.CreateSubnetOptions{}
options.SetSubnetPrototype(&vpcv1.SubnetPrototype{
IPVersion: &ipVersion,
Ipv4CIDRBlock: &cidrBlock,
Name: subnet.Name,
IPVersion: &ipVersion,
//Ipv4CIDRBlock: &cidrBlock,
Name: subnet.Name,
VPC: &vpcv1.VPCIdentity{
ID: vpcID,
},
Expand Down

0 comments on commit 0cdecca

Please sign in to comment.