Skip to content

Commit fab5efd

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

12 files changed

+284
-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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ func (r *AWSCluster) validateNetwork() field.ErrorList {
269269
}
270270
}
271271

272+
if r.Spec.NetworkSpec.VPC.AvailabilityZones != nil && (r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection != nil || r.Spec.NetworkSpec.VPC.AvailabilityZoneUsageLimit != nil) {
273+
availabilityZonesField := field.NewPath("spec", "networkSpec", "vpc", "availabilityZones")
274+
allErrs = append(allErrs, field.Invalid(availabilityZonesField, r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection, "availabilityZones cannot be set if availabilityZoneUsageLimit and availabilityZoneSelection are set"))
275+
}
276+
272277
return allErrs
273278
}
274279

api/v1beta2/defaults.go

Lines changed: 11 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,16 @@ 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+
}
64+
5465
}
5566

5667
// SetDefaults_AWSClusterSpec is used by defaulter-gen.

api/v1beta2/network_types.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,6 @@ type VPCSpec struct {
415415
// should be used in a region when automatically creating subnets. If a region has more
416416
// than this number of AZs then this number of AZs will be picked randomly when creating
417417
// default subnets. Defaults to 3
418-
// +kubebuilder:default=3
419418
// +kubebuilder:validation:Minimum=1
420419
AvailabilityZoneUsageLimit *int `json:"availabilityZoneUsageLimit,omitempty"`
421420

@@ -424,10 +423,14 @@ type VPCSpec struct {
424423
// Ordered - selects based on alphabetical order
425424
// Random - selects AZs randomly in a region
426425
// Defaults to Ordered
427-
// +kubebuilder:default=Ordered
428426
// +kubebuilder:validation:Enum=Ordered;Random
429427
AvailabilityZoneSelection *AZSelectionScheme `json:"availabilityZoneSelection,omitempty"`
430428

429+
// AvailabilityZones defines a list of Availability Zones in which to create network resources in.
430+
// If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
431+
// +optional
432+
AvailabilityZones []string `json:"availabilityZones,omitempty"`
433+
431434
// EmptyRoutesDefaultVPCSecurityGroup specifies whether the default VPC security group ingress
432435
// and egress rules should be removed.
433436
//

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: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,6 @@ spec:
589589
description: VPC configuration.
590590
properties:
591591
availabilityZoneSelection:
592-
default: Ordered
593592
description: |-
594593
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
595594
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -601,14 +600,20 @@ spec:
601600
- Random
602601
type: string
603602
availabilityZoneUsageLimit:
604-
default: 3
605603
description: |-
606604
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
607605
should be used in a region when automatically creating subnets. If a region has more
608606
than this number of AZs then this number of AZs will be picked randomly when creating
609607
default subnets. Defaults to 3
610608
minimum: 1
611609
type: integer
610+
availabilityZones:
611+
description: |-
612+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
613+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
614+
items:
615+
type: string
616+
type: array
612617
cidrBlock:
613618
description: |-
614619
CidrBlock is the CIDR block to be used when the provider creates a managed VPC.
@@ -2524,7 +2529,6 @@ spec:
25242529
description: VPC configuration.
25252530
properties:
25262531
availabilityZoneSelection:
2527-
default: Ordered
25282532
description: |-
25292533
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
25302534
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -2536,14 +2540,20 @@ spec:
25362540
- Random
25372541
type: string
25382542
availabilityZoneUsageLimit:
2539-
default: 3
25402543
description: |-
25412544
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
25422545
should be used in a region when automatically creating subnets. If a region has more
25432546
than this number of AZs then this number of AZs will be picked randomly when creating
25442547
default subnets. Defaults to 3
25452548
minimum: 1
25462549
type: integer
2550+
availabilityZones:
2551+
description: |-
2552+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
2553+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
2554+
items:
2555+
type: string
2556+
type: array
25472557
cidrBlock:
25482558
description: |-
25492559
CidrBlock is the CIDR block to be used when the provider creates a managed VPC.

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,7 +1525,6 @@ spec:
15251525
description: VPC configuration.
15261526
properties:
15271527
availabilityZoneSelection:
1528-
default: Ordered
15291528
description: |-
15301529
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
15311530
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1537,14 +1536,20 @@ spec:
15371536
- Random
15381537
type: string
15391538
availabilityZoneUsageLimit:
1540-
default: 3
15411539
description: |-
15421540
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
15431541
should be used in a region when automatically creating subnets. If a region has more
15441542
than this number of AZs then this number of AZs will be picked randomly when creating
15451543
default subnets. Defaults to 3
15461544
minimum: 1
15471545
type: integer
1546+
availabilityZones:
1547+
description: |-
1548+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
1549+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
1550+
items:
1551+
type: string
1552+
type: array
15481553
cidrBlock:
15491554
description: |-
15501555
CidrBlock is the CIDR block to be used when the provider creates a managed VPC.

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,6 @@ spec:
11231123
description: VPC configuration.
11241124
properties:
11251125
availabilityZoneSelection:
1126-
default: Ordered
11271126
description: |-
11281127
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
11291128
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1135,14 +1134,20 @@ spec:
11351134
- Random
11361135
type: string
11371136
availabilityZoneUsageLimit:
1138-
default: 3
11391137
description: |-
11401138
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
11411139
should be used in a region when automatically creating subnets. If a region has more
11421140
than this number of AZs then this number of AZs will be picked randomly when creating
11431141
default subnets. Defaults to 3
11441142
minimum: 1
11451143
type: integer
1144+
availabilityZones:
1145+
description: |-
1146+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
1147+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
1148+
items:
1149+
type: string
1150+
type: array
11461151
cidrBlock:
11471152
description: |-
11481153
CidrBlock is the CIDR block to be used when the provider creates a managed VPC.

controlplane/eks/api/v1beta2/awsmanagedcontrolplane_webhook.go

Lines changed: 16 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,11 @@ 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 r.Spec.NetworkSpec.VPC.AvailabilityZones != nil && (r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection != nil || r.Spec.NetworkSpec.VPC.AvailabilityZoneUsageLimit != nil) {
431+
availabilityZonesField := field.NewPath("spec", "networkSpec", "vpc", "availabilityZones")
432+
allErrs = append(allErrs, field.Invalid(availabilityZonesField, r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection, "availabilityZones cannot be set if availabilityZoneUsageLimit and availabilityZoneSelection are set"))
433+
}
434+
429435
return allErrs
430436
}
431437

@@ -452,6 +458,16 @@ func (r *AWSManagedControlPlane) Default() {
452458
}
453459
}
454460

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

0 commit comments

Comments
 (0)