diff --git a/cloud/scope/powervs_cluster.go b/cloud/scope/powervs_cluster.go index 68f5902c7..ab55239b9 100644 --- a/cloud/scope/powervs_cluster.go +++ b/cloud/scope/powervs_cluster.go @@ -1050,17 +1050,12 @@ func (s *PowerVSClusterScope) ReconcileVPCSubnets() (bool, error) { // check whether user has set the vpc subnets if len(s.IBMPowerVSCluster.Spec.VPCSubnets) == 0 { // if the user did not set any subnet, we try to create subnet in all the zones. - powerVSZone := s.Zone() - if powerVSZone == nil { - return false, fmt.Errorf("PowerVS zone is not set") - } - region := endpoints.ConstructRegionFromZone(*powerVSZone) - vpcZones, err := genUtil.VPCZonesForPowerVSRegion(region) + vpcZones, err := genUtil.VPCZonesForVPCRegion(*s.VPC().Region) if err != nil { return false, err } if len(vpcZones) == 0 { - return false, fmt.Errorf("failed to fetch VPC zones, no zone found for region %s", region) + return false, fmt.Errorf("failed to fetch VPC zones, no zone found for region %s", *s.VPC().Region) } for _, zone := range vpcZones { subnet := infrav1beta2.Subnet{ @@ -1150,12 +1145,7 @@ func (s *PowerVSClusterScope) createVPCSubnet(subnet infrav1beta2.Subnet) (*stri if subnet.Zone != nil { zone = *subnet.Zone } else { - powerVSZone := s.Zone() - if powerVSZone == nil { - return nil, fmt.Errorf("PowerVS zone is not set") - } - region := endpoints.ConstructRegionFromZone(*powerVSZone) - vpcZones, err := genUtil.VPCZonesForPowerVSRegion(region) + vpcZones, err := genUtil.VPCZonesForVPCRegion(*s.VPC().Region) if err != nil { return nil, err } diff --git a/util/util.go b/util/util.go index 289abb598..f4a64ae47 100644 --- a/util/util.go +++ b/util/util.go @@ -230,3 +230,13 @@ func GetTransitGatewayLocationAndRouting(powerVSZone *string, vpcRegion *string) // since VPC region is not set and used PowerVS region to calculate the transit gateway location, hence returning local routing as default. return &location, ptr.To(false), nil } + +// VPCZonesForVPCRegion returns the VPC zones associated with the VPC region. +func VPCZonesForVPCRegion(region string) ([]string, error) { + for _, regionDetails := range Regions { + if regionDetails.VPCRegion == region { + return regionDetails.VPCZones, nil + } + } + return nil, fmt.Errorf("VPC zones corresponding to the VPC region %s is not found", region) +}