-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORS-2895: aws/capi: decouple the zone config
Moving the cluster subnet configuration for CAPI to a dedicated file decoupling the functions for BYOVPC and managed VPC to dedicated functions to create tests dedicated for each scenario. The subnet structure created in managed VPC by CAPA is created with SubnetSpec, without providing the valid ID, so CAPA will understand that the subnet does not exists and will created it following the zone specified/discovered in the install config.
- Loading branch information
Showing
2 changed files
with
232 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
|
||
"k8s.io/utils/ptr" | ||
capa "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2" | ||
|
||
"github.com/openshift/installer/pkg/asset/installconfig" | ||
"github.com/openshift/installer/pkg/asset/installconfig/aws" | ||
"github.com/openshift/installer/pkg/asset/manifests/capiutils" | ||
utilscidr "github.com/openshift/installer/pkg/asset/manifests/capiutils/cidr" | ||
|
||
"github.com/openshift/installer/pkg/types" | ||
) | ||
|
||
type zoneConfigInput struct { | ||
InstallConfig *installconfig.InstallConfig | ||
Config *types.InstallConfig | ||
Meta *aws.Metadata | ||
Cluster *capa.AWSCluster | ||
ClusterID *installconfig.ClusterID | ||
} | ||
|
||
// setZones creates the CAPI NetworkSpec structures for managed or | ||
// BYO VPC deployments from install-config.yaml. | ||
func setZones(in *zoneConfigInput) error { | ||
if len(in.Config.AWS.Subnets) > 0 { | ||
return setZonesBYOVPC(in) | ||
} | ||
return setZonesManagedVPC(in) | ||
} | ||
|
||
// setZonesManagedVPC creates the CAPI NetworkSpec.Subnets setting the | ||
// desired subnets from install-config.yaml in the BYO VPC deployment. | ||
func setZonesBYOVPC(in *zoneConfigInput) error { | ||
privateSubnets, err := in.Meta.PrivateSubnets(context.TODO()) | ||
if err != nil { | ||
return fmt.Errorf("failed to get private subnets: %w", err) | ||
} | ||
for _, subnet := range privateSubnets { | ||
in.Cluster.Spec.NetworkSpec.Subnets = append(in.Cluster.Spec.NetworkSpec.Subnets, capa.SubnetSpec{ | ||
ID: subnet.ID, | ||
CidrBlock: subnet.CIDR, | ||
AvailabilityZone: subnet.Zone.Name, | ||
IsPublic: subnet.Public, | ||
}) | ||
} | ||
|
||
publicSubnets, err := in.Meta.PublicSubnets(context.TODO()) | ||
if err != nil { | ||
return fmt.Errorf("failed to get public subnets: %w", err) | ||
} | ||
for _, subnet := range publicSubnets { | ||
in.Cluster.Spec.NetworkSpec.Subnets = append(in.Cluster.Spec.NetworkSpec.Subnets, capa.SubnetSpec{ | ||
ID: subnet.ID, | ||
CidrBlock: subnet.CIDR, | ||
AvailabilityZone: subnet.Zone.Name, | ||
IsPublic: subnet.Public, | ||
}) | ||
} | ||
|
||
vpc, err := in.Meta.VPC(context.TODO()) | ||
if err != nil { | ||
return fmt.Errorf("failed to get VPC: %w", err) | ||
} | ||
in.Cluster.Spec.NetworkSpec.VPC = capa.VPCSpec{ | ||
ID: vpc, | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// setZonesManagedVPC creates the CAPI NetworkSpec.VPC setting the | ||
// desired zones from install-config.yaml in the managed VPC deployment. | ||
func setZonesManagedVPC(in *zoneConfigInput) error { | ||
zones, err := extractZonesFromInstallConfig(in) | ||
if err != nil { | ||
return fmt.Errorf("failed to get availability zones: %w", err) | ||
} | ||
|
||
mainCIDR := capiutils.CIDRFromInstallConfig(in.InstallConfig) | ||
|
||
// Fallback to query available zones in the region. | ||
if len(zones) == 0 { | ||
// TODO/FIXME(mtulio): check if it is possible to reproduce the same logic of "terraform version"[1][2], | ||
// where it filter the zones based in the defaul+(machine pool), then merged it like package `tfvars`[3], | ||
// before rendering the CAPI configuration? | ||
// [1] https://github.com/openshift/installer/blob/0eafdbb77fd62f8311ba8d9abda145f3280c5f79/pkg/asset/machines/master.go#L197-L198 | ||
// [2] https://github.com/openshift/installer/blob/0eafdbb77fd62f8311ba8d9abda145f3280c5f79/pkg/asset/machines/worker.go#L373-L374 | ||
// [3] https://github.com/openshift/installer/blob/0eafdbb77fd62f8311ba8d9abda145f3280c5f79/pkg/tfvars/aws/aws.go#L111-L167 | ||
|
||
// FIXME(mtulio). Leaving CAPA to discover zones | ||
in.Cluster.Spec.NetworkSpec.VPC = capa.VPCSpec{ | ||
CidrBlock: mainCIDR.String(), | ||
AvailabilityZoneUsageLimit: ptr.To(len(zones)), | ||
AvailabilityZoneSelection: &capa.AZSelectionSchemeOrdered, | ||
} | ||
return nil | ||
} | ||
|
||
in.Cluster.Spec.NetworkSpec.VPC = capa.VPCSpec{ | ||
CidrBlock: mainCIDR.String(), | ||
} | ||
|
||
// Base subnets considering only private zones, leaving one block free to allow | ||
// future subnet expansions in Day-2. | ||
numSubnets := len(zones) + 1 | ||
|
||
// Public subnets consumes one range from base blocks. | ||
isPublishingExternal := in.Config.Publish == types.ExternalPublishingStrategy | ||
if isPublishingExternal { | ||
numSubnets++ | ||
} | ||
|
||
subnetsCIDRs, err := utilscidr.SplitIntoSubnetsIPv4(mainCIDR.String(), numSubnets) | ||
if err != nil { | ||
return fmt.Errorf("unable to retrieve CIDR blocks for all private subnets: %w", err) | ||
} | ||
var publicSubnetsCIDRs []*net.IPNet | ||
if isPublishingExternal { | ||
publicSubnetsCIDRs, err = utilscidr.SplitIntoSubnetsIPv4(subnetsCIDRs[len(zones)].String(), len(zones)) | ||
if err != nil { | ||
return fmt.Errorf("unable to retrieve CIDR blocks for all public subnets: %w", err) | ||
} | ||
} | ||
|
||
idxCIDR := 0 | ||
// Q: Can we use the standard terraform name (without 'subnet') and tell CAPA | ||
// to query it for Control Planes? | ||
subnetNamePrefix := fmt.Sprintf("%s-subnet", in.ClusterID.InfraID) | ||
for _, zone := range zones { | ||
if len(subnetsCIDRs) < idxCIDR { | ||
return fmt.Errorf("unable to define CIDR blocks for all private subnets: %w", err) | ||
} | ||
cidr := subnetsCIDRs[idxCIDR] | ||
in.Cluster.Spec.NetworkSpec.Subnets = append(in.Cluster.Spec.NetworkSpec.Subnets, capa.SubnetSpec{ | ||
AvailabilityZone: zone.Name, | ||
CidrBlock: cidr.String(), | ||
ID: fmt.Sprintf("%s-private-%s", subnetNamePrefix, zone.Name), | ||
IsPublic: false, | ||
}) | ||
if isPublishingExternal { | ||
if len(publicSubnetsCIDRs) < idxCIDR { | ||
return fmt.Errorf("unable to define CIDR blocks for all public subnets: %w", err) | ||
} | ||
cidr = publicSubnetsCIDRs[idxCIDR] | ||
in.Cluster.Spec.NetworkSpec.Subnets = append(in.Cluster.Spec.NetworkSpec.Subnets, capa.SubnetSpec{ | ||
AvailabilityZone: zone.Name, | ||
CidrBlock: cidr.String(), | ||
ID: fmt.Sprintf("%s-public-%s", subnetNamePrefix, zone.Name), | ||
IsPublic: true, | ||
}) | ||
} | ||
idxCIDR++ | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// extractZonesFromInstallConfig extracts all zones defined in the install-config, | ||
// otherwise discover it based in the AWS metadata when none is defined. | ||
func extractZonesFromInstallConfig(in *zoneConfigInput) ([]*aws.Zone, error) { | ||
var zones []*aws.Zone | ||
zonesMap := make(map[string]struct{}) | ||
|
||
if in.Config == nil { | ||
return nil, fmt.Errorf("unable to retrieve Config") | ||
} | ||
|
||
cfg := in.Config | ||
defaultZones := []string{} | ||
if cfg.AWS.DefaultMachinePlatform != nil && len(cfg.AWS.DefaultMachinePlatform.Zones) > 0 { | ||
defaultZones = cfg.AWS.DefaultMachinePlatform.Zones | ||
} | ||
|
||
if cfg.ControlPlane != nil && cfg.ControlPlane.Platform.AWS != nil && | ||
len(cfg.ControlPlane.Platform.AWS.Zones) > 0 { | ||
for _, zone := range cfg.ControlPlane.Platform.AWS.Zones { | ||
if _, ok := zonesMap[zone]; !ok { | ||
zonesMap[zone] = struct{}{} | ||
zones = append(zones, &aws.Zone{Name: zone}) | ||
} | ||
} | ||
} else if len(defaultZones) > 0 { | ||
for _, zone := range defaultZones { | ||
if _, ok := zonesMap[zone]; !ok { | ||
zonesMap[zone] = struct{}{} | ||
zones = append(zones, &aws.Zone{Name: zone}) | ||
} | ||
} | ||
} | ||
|
||
for _, compute := range cfg.Compute { | ||
if len(compute.Platform.AWS.Zones) > 0 { | ||
for _, zone := range compute.Platform.AWS.Zones { | ||
if _, ok := zonesMap[zone]; !ok { | ||
zonesMap[zone] = struct{}{} | ||
zones = append(zones, &aws.Zone{Name: zone}) | ||
} | ||
} | ||
} else if compute.Name == "worker" && len(defaultZones) > 0 { | ||
for _, zone := range defaultZones { | ||
if _, ok := zonesMap[zone]; !ok { | ||
zonesMap[zone] = struct{}{} | ||
zones = append(zones, &aws.Zone{Name: zone}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return zones, nil | ||
} |