Skip to content

Commit 3d81a39

Browse files
Léonard Susliansynthe102
authored andcommitted
feat: create vpc objects in explicitly provided availability zones
1 parent b25eef6 commit 3d81a39

12 files changed

+277
-34
lines changed

api/v1beta1/awscluster_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
8686

8787
dst.Spec.NetworkSpec.AdditionalControlPlaneIngressRules = restored.Spec.NetworkSpec.AdditionalControlPlaneIngressRules
8888
dst.Spec.NetworkSpec.NodePortIngressRuleCidrBlocks = restored.Spec.NetworkSpec.NodePortIngressRuleCidrBlocks
89+
dst.Spec.NetworkSpec.VPC.AvailabilityZones = restored.Spec.NetworkSpec.VPC.AvailabilityZones
8990

9091
if restored.Spec.NetworkSpec.VPC.IPAMPool != nil {
9192
if dst.Spec.NetworkSpec.VPC.IPAMPool == nil {

api/v1beta1/zz_generated.conversion.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta2/awscluster_webhook.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ func (r *AWSCluster) validateNetwork() field.ErrorList {
298298
}
299299
}
300300

301+
if err := r.Spec.NetworkSpec.VPC.ValidateAvailabilityZones(); err != nil {
302+
allErrs = append(allErrs, err)
303+
}
304+
301305
return allErrs
302306
}
303307

api/v1beta2/defaults.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package v1beta2
1818

1919
import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
"k8s.io/utils/ptr"
2122

2223
clusterv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
2324
)
@@ -51,6 +52,15 @@ func SetDefaults_NetworkSpec(obj *NetworkSpec) { //nolint:golint,stylecheck
5152
},
5253
}
5354
}
55+
// If AvailabilityZones are not set, set defaults for AZ selection
56+
if obj.VPC.AvailabilityZones == nil {
57+
if obj.VPC.AvailabilityZoneUsageLimit == nil {
58+
obj.VPC.AvailabilityZoneUsageLimit = ptr.To(3)
59+
}
60+
if obj.VPC.AvailabilityZoneSelection == nil {
61+
obj.VPC.AvailabilityZoneSelection = &AZSelectionSchemeOrdered
62+
}
63+
}
5464
}
5565

5666
// SetDefaults_AWSClusterSpec is used by defaulter-gen.

api/v1beta2/network_types.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/aws/aws-sdk-go/aws"
2525
"github.com/aws/aws-sdk-go/service/ec2"
26+
"k8s.io/apimachinery/pkg/util/validation/field"
2627
"k8s.io/utils/ptr"
2728
)
2829

@@ -442,7 +443,7 @@ type VPCSpec struct {
442443
// should be used in a region when automatically creating subnets. If a region has more
443444
// than this number of AZs then this number of AZs will be picked randomly when creating
444445
// default subnets. Defaults to 3
445-
// +kubebuilder:default=3
446+
// +optional
446447
// +kubebuilder:validation:Minimum=1
447448
AvailabilityZoneUsageLimit *int `json:"availabilityZoneUsageLimit,omitempty"`
448449

@@ -451,10 +452,16 @@ type VPCSpec struct {
451452
// Ordered - selects based on alphabetical order
452453
// Random - selects AZs randomly in a region
453454
// Defaults to Ordered
454-
// +kubebuilder:default=Ordered
455+
// +optional
455456
// +kubebuilder:validation:Enum=Ordered;Random
456457
AvailabilityZoneSelection *AZSelectionScheme `json:"availabilityZoneSelection,omitempty"`
457458

459+
// AvailabilityZones defines a list of Availability Zones in which to create network resources in.
460+
// Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
461+
// +optional
462+
// +kubebuilder:validation:MinItems=1
463+
AvailabilityZones []string `json:"availabilityZones,omitempty"`
464+
458465
// EmptyRoutesDefaultVPCSecurityGroup specifies whether the default VPC security group ingress
459466
// and egress rules should be removed.
460467
//
@@ -527,6 +534,15 @@ func (v *VPCSpec) GetPublicIpv4Pool() *string {
527534
return nil
528535
}
529536

537+
// ValidateAvailabilityZones returns an error if the availability zones field combination is invalid.
538+
func (v *VPCSpec) ValidateAvailabilityZones() *field.Error {
539+
if len(v.AvailabilityZones) > 0 && (v.AvailabilityZoneSelection != nil || v.AvailabilityZoneUsageLimit != nil) {
540+
availabilityZonesField := field.NewPath("spec", "network", "vpc", "availabilityZones")
541+
return field.Invalid(availabilityZonesField, v.AvailabilityZoneSelection, "availabilityZones cannot be set if availabilityZoneUsageLimit and availabilityZoneSelection are set")
542+
}
543+
return nil
544+
}
545+
530546
// SubnetSpec configures an AWS Subnet.
531547
type SubnetSpec struct {
532548
// ID defines a unique identifier to reference this resource.

api/v1beta2/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/controlplane.cluster.x-k8s.io_awsmanagedcontrolplanes.yaml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,6 @@ spec:
605605
description: VPC configuration.
606606
properties:
607607
availabilityZoneSelection:
608-
default: Ordered
609608
description: |-
610609
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
611610
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -617,14 +616,21 @@ spec:
617616
- Random
618617
type: string
619618
availabilityZoneUsageLimit:
620-
default: 3
621619
description: |-
622620
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
623621
should be used in a region when automatically creating subnets. If a region has more
624622
than this number of AZs then this number of AZs will be picked randomly when creating
625623
default subnets. Defaults to 3
626624
minimum: 1
627625
type: integer
626+
availabilityZones:
627+
description: |-
628+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
629+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
630+
items:
631+
type: string
632+
minItems: 1
633+
type: array
628634
carrierGatewayId:
629635
description: |-
630636
CarrierGatewayID is the id of the internet gateway associated with the VPC,
@@ -2648,7 +2654,6 @@ spec:
26482654
description: VPC configuration.
26492655
properties:
26502656
availabilityZoneSelection:
2651-
default: Ordered
26522657
description: |-
26532658
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
26542659
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -2660,14 +2665,21 @@ spec:
26602665
- Random
26612666
type: string
26622667
availabilityZoneUsageLimit:
2663-
default: 3
26642668
description: |-
26652669
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
26662670
should be used in a region when automatically creating subnets. If a region has more
26672671
than this number of AZs then this number of AZs will be picked randomly when creating
26682672
default subnets. Defaults to 3
26692673
minimum: 1
26702674
type: integer
2675+
availabilityZones:
2676+
description: |-
2677+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
2678+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
2679+
items:
2680+
type: string
2681+
minItems: 1
2682+
type: array
26712683
carrierGatewayId:
26722684
description: |-
26732685
CarrierGatewayID is the id of the internet gateway associated with the VPC,

config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusters.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,6 @@ spec:
15451545
description: VPC configuration.
15461546
properties:
15471547
availabilityZoneSelection:
1548-
default: Ordered
15491548
description: |-
15501549
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
15511550
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1557,14 +1556,21 @@ spec:
15571556
- Random
15581557
type: string
15591558
availabilityZoneUsageLimit:
1560-
default: 3
15611559
description: |-
15621560
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
15631561
should be used in a region when automatically creating subnets. If a region has more
15641562
than this number of AZs then this number of AZs will be picked randomly when creating
15651563
default subnets. Defaults to 3
15661564
minimum: 1
15671565
type: integer
1566+
availabilityZones:
1567+
description: |-
1568+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
1569+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
1570+
items:
1571+
type: string
1572+
minItems: 1
1573+
type: array
15681574
carrierGatewayId:
15691575
description: |-
15701576
CarrierGatewayID is the id of the internet gateway associated with the VPC,

config/crd/bases/infrastructure.cluster.x-k8s.io_awsclustertemplates.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,6 @@ spec:
11431143
description: VPC configuration.
11441144
properties:
11451145
availabilityZoneSelection:
1146-
default: Ordered
11471146
description: |-
11481147
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
11491148
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1155,14 +1154,21 @@ spec:
11551154
- Random
11561155
type: string
11571156
availabilityZoneUsageLimit:
1158-
default: 3
11591157
description: |-
11601158
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
11611159
should be used in a region when automatically creating subnets. If a region has more
11621160
than this number of AZs then this number of AZs will be picked randomly when creating
11631161
default subnets. Defaults to 3
11641162
minimum: 1
11651163
type: integer
1164+
availabilityZones:
1165+
description: |-
1166+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
1167+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
1168+
items:
1169+
type: string
1170+
minItems: 1
1171+
type: array
11661172
carrierGatewayId:
11671173
description: |-
11681174
CarrierGatewayID is the id of the internet gateway associated with the VPC,

controlplane/eks/api/v1beta2/awsmanagedcontrolplane_webhook.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"k8s.io/apimachinery/pkg/util/validation/field"
2828
"k8s.io/apimachinery/pkg/util/version"
2929
"k8s.io/klog/v2"
30+
"k8s.io/utils/ptr"
3031
ctrl "sigs.k8s.io/controller-runtime"
3132
"sigs.k8s.io/controller-runtime/pkg/webhook"
3233
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -474,6 +475,10 @@ func (r *AWSManagedControlPlane) validateNetwork() field.ErrorList {
474475
allErrs = append(allErrs, field.Invalid(ipamPoolField, r.Spec.NetworkSpec.VPC.IPv6.IPAMPool, "ipamPool must have either id or name"))
475476
}
476477

478+
if err := r.Spec.NetworkSpec.VPC.ValidateAvailabilityZones(); err != nil {
479+
allErrs = append(allErrs, err)
480+
}
481+
477482
return allErrs
478483
}
479484

@@ -500,6 +505,16 @@ func (r *AWSManagedControlPlane) Default() {
500505
}
501506
}
502507

508+
// If AvailabilityZones are not set, set defaults for AZ selection
509+
if r.Spec.NetworkSpec.VPC.AvailabilityZones == nil {
510+
if r.Spec.NetworkSpec.VPC.AvailabilityZoneUsageLimit == nil {
511+
r.Spec.NetworkSpec.VPC.AvailabilityZoneUsageLimit = ptr.To(3)
512+
}
513+
if r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection == nil {
514+
r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection = &infrav1.AZSelectionSchemeOrdered
515+
}
516+
}
517+
503518
infrav1.SetDefaults_Bastion(&r.Spec.Bastion)
504519
infrav1.SetDefaults_NetworkSpec(&r.Spec.NetworkSpec)
505520
}

0 commit comments

Comments
 (0)