Skip to content

Commit 4526048

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

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
@@ -424,7 +424,6 @@ type VPCSpec struct {
424424
// should be used in a region when automatically creating subnets. If a region has more
425425
// than this number of AZs then this number of AZs will be picked randomly when creating
426426
// default subnets. Defaults to 3
427-
// +kubebuilder:default=3
428427
// +kubebuilder:validation:Minimum=1
429428
AvailabilityZoneUsageLimit *int `json:"availabilityZoneUsageLimit,omitempty"`
430429

@@ -433,10 +432,14 @@ type VPCSpec struct {
433432
// Ordered - selects based on alphabetical order
434433
// Random - selects AZs randomly in a region
435434
// Defaults to Ordered
436-
// +kubebuilder:default=Ordered
437435
// +kubebuilder:validation:Enum=Ordered;Random
438436
AvailabilityZoneSelection *AZSelectionScheme `json:"availabilityZoneSelection,omitempty"`
439437

438+
// AvailabilityZones defines a list of Availability Zones in which to create network resources in.
439+
// If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
440+
// +optional
441+
AvailabilityZones []string `json:"availabilityZones,omitempty"`
442+
440443
// EmptyRoutesDefaultVPCSecurityGroup specifies whether the default VPC security group ingress
441444
// and egress rules should be removed.
442445
//

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
@@ -594,7 +594,6 @@ spec:
594594
description: VPC configuration.
595595
properties:
596596
availabilityZoneSelection:
597-
default: Ordered
598597
description: |-
599598
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
600599
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -606,7 +605,6 @@ spec:
606605
- Random
607606
type: string
608607
availabilityZoneUsageLimit:
609-
default: 3
610608
description: |-
611609
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
612610
should be used in a region when automatically creating subnets. If a region has more
@@ -622,6 +620,13 @@ spec:
622620
x-kubernetes-validations:
623621
- message: Carrier Gateway ID must start with 'cagw-'
624622
rule: self.startsWith('cagw-')
623+
availabilityZones:
624+
description: |-
625+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
626+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
627+
items:
628+
type: string
629+
type: array
625630
cidrBlock:
626631
description: |-
627632
CidrBlock is the CIDR block to be used when the provider creates a managed VPC.
@@ -2544,7 +2549,6 @@ spec:
25442549
description: VPC configuration.
25452550
properties:
25462551
availabilityZoneSelection:
2547-
default: Ordered
25482552
description: |-
25492553
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
25502554
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -2556,7 +2560,6 @@ spec:
25562560
- Random
25572561
type: string
25582562
availabilityZoneUsageLimit:
2559-
default: 3
25602563
description: |-
25612564
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
25622565
should be used in a region when automatically creating subnets. If a region has more
@@ -2572,6 +2575,13 @@ spec:
25722575
x-kubernetes-validations:
25732576
- message: Carrier Gateway ID must start with 'cagw-'
25742577
rule: self.startsWith('cagw-')
2578+
availabilityZones:
2579+
description: |-
2580+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
2581+
If defined, both AvailabilityZoneUsageLimit and AvailabilityZoneSelection are ignored.
2582+
items:
2583+
type: string
2584+
type: array
25752585
cidrBlock:
25762586
description: |-
25772587
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
@@ -1530,7 +1530,6 @@ spec:
15301530
description: VPC configuration.
15311531
properties:
15321532
availabilityZoneSelection:
1533-
default: Ordered
15341533
description: |-
15351534
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
15361535
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1542,7 +1541,6 @@ spec:
15421541
- Random
15431542
type: string
15441543
availabilityZoneUsageLimit:
1545-
default: 3
15461544
description: |-
15471545
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
15481546
should be used in a region when automatically creating subnets. If a region has more
@@ -1558,6 +1556,13 @@ spec:
15581556
x-kubernetes-validations:
15591557
- message: Carrier Gateway ID must start with 'cagw-'
15601558
rule: self.startsWith('cagw-')
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+
type: array
15611566
cidrBlock:
15621567
description: |-
15631568
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
@@ -1128,7 +1128,6 @@ spec:
11281128
description: VPC configuration.
11291129
properties:
11301130
availabilityZoneSelection:
1131-
default: Ordered
11321131
description: |-
11331132
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
11341133
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1140,7 +1139,6 @@ spec:
11401139
- Random
11411140
type: string
11421141
availabilityZoneUsageLimit:
1143-
default: 3
11441142
description: |-
11451143
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
11461144
should be used in a region when automatically creating subnets. If a region has more
@@ -1156,6 +1154,13 @@ spec:
11561154
x-kubernetes-validations:
11571155
- message: Carrier Gateway ID must start with 'cagw-'
11581156
rule: self.startsWith('cagw-')
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+
type: array
11591164
cidrBlock:
11601165
description: |-
11611166
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)