Skip to content

Commit

Permalink
CORS-2895: aws/capi: decouple the zone config
Browse files Browse the repository at this point in the history
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
mtulio committed Mar 19, 2024
1 parent f810dd9 commit 0fd9ede
Show file tree
Hide file tree
Showing 2 changed files with 232 additions and 52 deletions.
69 changes: 17 additions & 52 deletions pkg/asset/manifests/aws/cluster.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package aws

import (
"context"
"fmt"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/ptr"
Expand All @@ -19,33 +17,24 @@ import (
)

// GenerateClusterAssets generates the manifests for the cluster-api.
func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID *installconfig.ClusterID) (*capiutils.GenerateClusterAssetsOutput, error) {
func GenerateClusterAssets(ic *installconfig.InstallConfig, clusterID *installconfig.ClusterID) (*capiutils.GenerateClusterAssetsOutput, error) {
manifests := []*asset.RuntimeFile{}
mainCIDR := capiutils.CIDRFromInstallConfig(installConfig)

zones, err := installConfig.AWS.AvailabilityZones(context.TODO())
if err != nil {
return nil, errors.Wrap(err, "failed to get availability zones")
}

tags, err := aws.CapaTagsFromUserTags(clusterID.InfraID, installConfig.Config.AWS.UserTags)
tags, err := aws.CapaTagsFromUserTags(clusterID.InfraID, ic.Config.AWS.UserTags)
if err != nil {
return nil, fmt.Errorf("failed to get user tags: %w", err)
}

mainCIDR := capiutils.CIDRFromInstallConfig(ic)

awsCluster := &capa.AWSCluster{
ObjectMeta: metav1.ObjectMeta{
Name: clusterID.InfraID,
Namespace: capiutils.Namespace,
},
Spec: capa.AWSClusterSpec{
Region: installConfig.Config.AWS.Region,
Region: ic.Config.AWS.Region,
NetworkSpec: capa.NetworkSpec{
VPC: capa.VPCSpec{
CidrBlock: mainCIDR.String(),
AvailabilityZoneUsageLimit: ptr.To(len(zones)),
AvailabilityZoneSelection: &capa.AZSelectionSchemeOrdered,
},
CNI: &capa.CNISpec{
CNIIngressRules: capa.CNIIngressRules{
{
Expand Down Expand Up @@ -182,7 +171,7 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
},
}

if installConfig.Config.Publish == types.ExternalPublishingStrategy {
if ic.Config.Publish == types.ExternalPublishingStrategy {
// FIXME: CAPA bug. Remove when fixed upstream
// The primary and secondary load balancers in CAPA share the same
// security group. However, specifying an ingress rule only in the
Expand Down Expand Up @@ -217,41 +206,17 @@ func GenerateClusterAssets(installConfig *installconfig.InstallConfig, clusterID
}
}

// If the install config has subnets, use them.
if len(installConfig.AWS.Subnets) > 0 {
privateSubnets, err := installConfig.AWS.PrivateSubnets(context.TODO())
if err != nil {
return nil, errors.Wrap(err, "failed to get private subnets")
}
for _, subnet := range privateSubnets {
awsCluster.Spec.NetworkSpec.Subnets = append(awsCluster.Spec.NetworkSpec.Subnets, capa.SubnetSpec{
ID: subnet.ID,
CidrBlock: subnet.CIDR,
AvailabilityZone: subnet.Zone.Name,
IsPublic: subnet.Public,
})
}
publicSubnets, err := installConfig.AWS.PublicSubnets(context.TODO())
if err != nil {
return nil, errors.Wrap(err, "failed to get public subnets")
}

for _, subnet := range publicSubnets {
awsCluster.Spec.NetworkSpec.Subnets = append(awsCluster.Spec.NetworkSpec.Subnets, capa.SubnetSpec{
ID: subnet.ID,
CidrBlock: subnet.CIDR,
AvailabilityZone: subnet.Zone.Name,
IsPublic: subnet.Public,
})
}

vpc, err := installConfig.AWS.VPC(context.TODO())
if err != nil {
return nil, errors.Wrap(err, "failed to get VPC")
}
awsCluster.Spec.NetworkSpec.VPC = capa.VPCSpec{
ID: vpc,
}
// Set the VPC and zones (managed) or subnets (BYO VPC) based in the
// install-config.yaml.
err = setZones(&zoneConfigInput{
InstallConfig: ic,
Config: ic.Config,
Meta: ic.AWS,
ClusterID: clusterID,
Cluster: awsCluster,
})
if err != nil {
return nil, fmt.Errorf("failed to set cluster zones or subnets: %w", err)
}

manifests = append(manifests, &asset.RuntimeFile{
Expand Down
215 changes: 215 additions & 0 deletions pkg/asset/manifests/aws/zones.go
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
}

0 comments on commit 0fd9ede

Please sign in to comment.