Skip to content

Commit f0fbb06

Browse files
author
Léonard Suslian
committed
feat: create vpc objects in explicitly provided availability zones
1 parent 2815946 commit f0fbb06

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
@@ -85,6 +85,7 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
8585
}
8686

8787
dst.Spec.NetworkSpec.AdditionalControlPlaneIngressRules = restored.Spec.NetworkSpec.AdditionalControlPlaneIngressRules
88+
dst.Spec.NetworkSpec.VPC.AvailabilityZones = restored.Spec.NetworkSpec.VPC.AvailabilityZones
8889

8990
if restored.Spec.NetworkSpec.VPC.IPAMPool != nil {
9091
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
@@ -291,6 +291,10 @@ func (r *AWSCluster) validateNetwork() field.ErrorList {
291291
}
292292
}
293293

294+
if err := r.Spec.NetworkSpec.VPC.ValidateAvailabilityZones(); err != nil {
295+
allErrs = append(allErrs, err)
296+
}
297+
294298
return allErrs
295299
}
296300

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

@@ -437,7 +438,7 @@ type VPCSpec struct {
437438
// should be used in a region when automatically creating subnets. If a region has more
438439
// than this number of AZs then this number of AZs will be picked randomly when creating
439440
// default subnets. Defaults to 3
440-
// +kubebuilder:default=3
441+
// +optional
441442
// +kubebuilder:validation:Minimum=1
442443
AvailabilityZoneUsageLimit *int `json:"availabilityZoneUsageLimit,omitempty"`
443444

@@ -446,10 +447,16 @@ type VPCSpec struct {
446447
// Ordered - selects based on alphabetical order
447448
// Random - selects AZs randomly in a region
448449
// Defaults to Ordered
449-
// +kubebuilder:default=Ordered
450+
// +optional
450451
// +kubebuilder:validation:Enum=Ordered;Random
451452
AvailabilityZoneSelection *AZSelectionScheme `json:"availabilityZoneSelection,omitempty"`
452453

454+
// AvailabilityZones defines a list of Availability Zones in which to create network resources in.
455+
// Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
456+
// +optional
457+
// +kubebuilder:validation:MinItems=1
458+
AvailabilityZones []string `json:"availabilityZones,omitempty"`
459+
453460
// EmptyRoutesDefaultVPCSecurityGroup specifies whether the default VPC security group ingress
454461
// and egress rules should be removed.
455462
//
@@ -522,6 +529,15 @@ func (v *VPCSpec) GetPublicIpv4Pool() *string {
522529
return nil
523530
}
524531

532+
// ValidateAvailabilityZones returns an error if the availability zones field combination is invalid.
533+
func (v *VPCSpec) ValidateAvailabilityZones() *field.Error {
534+
if len(v.AvailabilityZones) > 0 && (v.AvailabilityZoneSelection != nil || v.AvailabilityZoneUsageLimit != nil) {
535+
availabilityZonesField := field.NewPath("spec", "network", "vpc", "availabilityZones")
536+
return field.Invalid(availabilityZonesField, v.AvailabilityZoneSelection, "availabilityZones cannot be set if availabilityZoneUsageLimit and availabilityZoneSelection are set")
537+
}
538+
return nil
539+
}
540+
525541
// SubnetSpec configures an AWS Subnet.
526542
type SubnetSpec struct {
527543
// 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
@@ -598,7 +598,6 @@ spec:
598598
description: VPC configuration.
599599
properties:
600600
availabilityZoneSelection:
601-
default: Ordered
602601
description: |-
603602
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
604603
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -610,14 +609,21 @@ spec:
610609
- Random
611610
type: string
612611
availabilityZoneUsageLimit:
613-
default: 3
614612
description: |-
615613
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
616614
should be used in a region when automatically creating subnets. If a region has more
617615
than this number of AZs then this number of AZs will be picked randomly when creating
618616
default subnets. Defaults to 3
619617
minimum: 1
620618
type: integer
619+
availabilityZones:
620+
description: |-
621+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
622+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
623+
items:
624+
type: string
625+
minItems: 1
626+
type: array
621627
carrierGatewayId:
622628
description: |-
623629
CarrierGatewayID is the id of the internet gateway associated with the VPC,
@@ -2624,7 +2630,6 @@ spec:
26242630
description: VPC configuration.
26252631
properties:
26262632
availabilityZoneSelection:
2627-
default: Ordered
26282633
description: |-
26292634
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
26302635
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -2636,14 +2641,21 @@ spec:
26362641
- Random
26372642
type: string
26382643
availabilityZoneUsageLimit:
2639-
default: 3
26402644
description: |-
26412645
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
26422646
should be used in a region when automatically creating subnets. If a region has more
26432647
than this number of AZs then this number of AZs will be picked randomly when creating
26442648
default subnets. Defaults to 3
26452649
minimum: 1
26462650
type: integer
2651+
availabilityZones:
2652+
description: |-
2653+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
2654+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
2655+
items:
2656+
type: string
2657+
minItems: 1
2658+
type: array
26472659
carrierGatewayId:
26482660
description: |-
26492661
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
@@ -1538,7 +1538,6 @@ spec:
15381538
description: VPC configuration.
15391539
properties:
15401540
availabilityZoneSelection:
1541-
default: Ordered
15421541
description: |-
15431542
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
15441543
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1550,14 +1549,21 @@ spec:
15501549
- Random
15511550
type: string
15521551
availabilityZoneUsageLimit:
1553-
default: 3
15541552
description: |-
15551553
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
15561554
should be used in a region when automatically creating subnets. If a region has more
15571555
than this number of AZs then this number of AZs will be picked randomly when creating
15581556
default subnets. Defaults to 3
15591557
minimum: 1
15601558
type: integer
1559+
availabilityZones:
1560+
description: |-
1561+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
1562+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
1563+
items:
1564+
type: string
1565+
minItems: 1
1566+
type: array
15611567
carrierGatewayId:
15621568
description: |-
15631569
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
@@ -1136,7 +1136,6 @@ spec:
11361136
description: VPC configuration.
11371137
properties:
11381138
availabilityZoneSelection:
1139-
default: Ordered
11401139
description: |-
11411140
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
11421141
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1148,14 +1147,21 @@ spec:
11481147
- Random
11491148
type: string
11501149
availabilityZoneUsageLimit:
1151-
default: 3
11521150
description: |-
11531151
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
11541152
should be used in a region when automatically creating subnets. If a region has more
11551153
than this number of AZs then this number of AZs will be picked randomly when creating
11561154
default subnets. Defaults to 3
11571155
minimum: 1
11581156
type: integer
1157+
availabilityZones:
1158+
description: |-
1159+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
1160+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
1161+
items:
1162+
type: string
1163+
minItems: 1
1164+
type: array
11591165
carrierGatewayId:
11601166
description: |-
11611167
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)