Skip to content

Commit ca10c55

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

12 files changed

+298
-34
lines changed

api/v1beta1/awscluster_conversion.go

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

8686
dst.Spec.NetworkSpec.AdditionalControlPlaneIngressRules = restored.Spec.NetworkSpec.AdditionalControlPlaneIngressRules
87+
dst.Spec.NetworkSpec.VPC.AvailabilityZones = restored.Spec.NetworkSpec.VPC.AvailabilityZones
8788

8889
if restored.Spec.NetworkSpec.VPC.IPAMPool != nil {
8990
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
@@ -283,6 +283,10 @@ func (r *AWSCluster) validateNetwork() field.ErrorList {
283283
}
284284
}
285285

286+
if err := r.Spec.NetworkSpec.VPC.ValidateAvailabilityZones(); err != nil {
287+
allErrs = append(allErrs, err)
288+
}
289+
286290
return allErrs
287291
}
288292

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

@@ -424,7 +425,7 @@ type VPCSpec struct {
424425
// should be used in a region when automatically creating subnets. If a region has more
425426
// than this number of AZs then this number of AZs will be picked randomly when creating
426427
// default subnets. Defaults to 3
427-
// +kubebuilder:default=3
428+
// +optional
428429
// +kubebuilder:validation:Minimum=1
429430
AvailabilityZoneUsageLimit *int `json:"availabilityZoneUsageLimit,omitempty"`
430431

@@ -433,10 +434,16 @@ type VPCSpec struct {
433434
// Ordered - selects based on alphabetical order
434435
// Random - selects AZs randomly in a region
435436
// Defaults to Ordered
436-
// +kubebuilder:default=Ordered
437+
// +optional
437438
// +kubebuilder:validation:Enum=Ordered;Random
438439
AvailabilityZoneSelection *AZSelectionScheme `json:"availabilityZoneSelection,omitempty"`
439440

441+
// AvailabilityZones defines a list of Availability Zones in which to create network resources in.
442+
// If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
443+
// +optional
444+
// +kubebuilder:validation:MinItems=1
445+
AvailabilityZones []string `json:"availabilityZones,omitempty"`
446+
440447
// EmptyRoutesDefaultVPCSecurityGroup specifies whether the default VPC security group ingress
441448
// and egress rules should be removed.
442449
//
@@ -499,6 +506,15 @@ func (v *VPCSpec) GetPublicIpv4Pool() *string {
499506
return nil
500507
}
501508

509+
// ValidateAvailabilityZones returns an error if the availability zones field combination is invalid.
510+
func (v *VPCSpec) ValidateAvailabilityZones() *field.Error {
511+
if len(v.AvailabilityZones) > 0 && (v.AvailabilityZoneSelection != nil || v.AvailabilityZoneUsageLimit != nil) {
512+
availabilityZonesField := field.NewPath("spec", "network", "vpc", "availabilityZones")
513+
return field.Invalid(availabilityZonesField, v.AvailabilityZoneSelection, "availabilityZones cannot be set if availabilityZoneUsageLimit and availabilityZoneSelection are set")
514+
}
515+
return nil
516+
}
517+
502518
// SubnetSpec configures an AWS Subnet.
503519
type SubnetSpec struct {
504520
// 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+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
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,
@@ -2589,7 +2595,6 @@ spec:
25892595
description: VPC configuration.
25902596
properties:
25912597
availabilityZoneSelection:
2592-
default: Ordered
25932598
description: |-
25942599
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
25952600
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -2601,14 +2606,21 @@ spec:
26012606
- Random
26022607
type: string
26032608
availabilityZoneUsageLimit:
2604-
default: 3
26052609
description: |-
26062610
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
26072611
should be used in a region when automatically creating subnets. If a region has more
26082612
than this number of AZs then this number of AZs will be picked randomly when creating
26092613
default subnets. Defaults to 3
26102614
minimum: 1
26112615
type: integer
2616+
availabilityZones:
2617+
description: |-
2618+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
2619+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
2620+
items:
2621+
type: string
2622+
minItems: 1
2623+
type: array
26122624
carrierGatewayId:
26132625
description: |-
26142626
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+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
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+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
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"
@@ -426,6 +427,10 @@ func (r *AWSManagedControlPlane) validateNetwork() field.ErrorList {
426427
allErrs = append(allErrs, field.Invalid(ipamPoolField, r.Spec.NetworkSpec.VPC.IPv6.IPAMPool, "ipamPool must have either id or name"))
427428
}
428429

430+
if err := r.Spec.NetworkSpec.VPC.ValidateAvailabilityZones(); err != nil {
431+
allErrs = append(allErrs, err)
432+
}
433+
429434
return allErrs
430435
}
431436

@@ -452,6 +457,16 @@ func (r *AWSManagedControlPlane) Default() {
452457
}
453458
}
454459

460+
// If AvailabilityZones are not set, set defaults for AZ selection
461+
if r.Spec.NetworkSpec.VPC.AvailabilityZones == nil {
462+
if r.Spec.NetworkSpec.VPC.AvailabilityZoneUsageLimit == nil {
463+
r.Spec.NetworkSpec.VPC.AvailabilityZoneUsageLimit = ptr.To(3)
464+
}
465+
if r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection == nil {
466+
r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection = &infrav1.AZSelectionSchemeOrdered
467+
}
468+
}
469+
455470
infrav1.SetDefaults_Bastion(&r.Spec.Bastion)
456471
infrav1.SetDefaults_NetworkSpec(&r.Spec.NetworkSpec)
457472
}

0 commit comments

Comments
 (0)