From 0cdecca9e7473f466d193bfda54957733ef39fa7 Mon Sep 17 00:00:00 2001 From: Shilpa-Gokul Date: Fri, 24 May 2024 11:13:31 +0530 Subject: [PATCH] Use multiple zones for multiple subnets --- cloud/scope/powervs_cluster.go | 59 ++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/cloud/scope/powervs_cluster.go b/cloud/scope/powervs_cluster.go index 8c8f2905a0..4d33c8da5d 100644 --- a/cloud/scope/powervs_cluster.go +++ b/cloud/scope/powervs_cluster.go @@ -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" @@ -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 @@ -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 @@ -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, },