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 2d53312
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions cloud/scope/powervs_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ import (
"fmt"
"reflect"
"strings"

"github.com/go-logr/logr"

"github.com/IBM-Cloud/power-go-client/ibmpisession"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM/go-sdk-core/v5/core"
Expand All @@ -38,7 +37,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,7 +1069,25 @@ func (s *PowerVSClusterScope) ReconcileVPCSubnets() (bool, error) {
}
subnets = append(subnets, subnet)
}
for _, subnet := range subnets {
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
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 subnet", "subnet", subnet)
var subnetID *string
if subnet.ID != nil {
Expand Down Expand Up @@ -1106,9 +1122,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,27 +1155,13 @@ 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()
Expand Down

0 comments on commit 2d53312

Please sign in to comment.