Skip to content

Commit d9b6061

Browse files
committed
feat: create vpc objects in explicitly provided availability zones
1 parent 8d4c7f2 commit d9b6061

12 files changed

+287
-34
lines changed

api/v1beta1/awscluster_conversion.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
9090
dst.Spec.NetworkSpec.AdditionalControlPlaneIngressRules = restored.Spec.NetworkSpec.AdditionalControlPlaneIngressRules
9191
dst.Spec.NetworkSpec.AdditionalNodeIngressRules = restored.Spec.NetworkSpec.AdditionalNodeIngressRules
9292
dst.Spec.NetworkSpec.NodePortIngressRuleCidrBlocks = restored.Spec.NetworkSpec.NodePortIngressRuleCidrBlocks
93+
dst.Spec.NetworkSpec.VPC.AvailabilityZones = restored.Spec.NetworkSpec.VPC.AvailabilityZones
9394

9495
if restored.Spec.NetworkSpec.VPC.IPAMPool != nil {
9596
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
@@ -356,6 +356,10 @@ func (r *AWSCluster) validateNetwork() field.ErrorList {
356356
}
357357
}
358358

359+
if err := r.Spec.NetworkSpec.VPC.ValidateAvailabilityZones(); err != nil {
360+
allErrs = append(allErrs, err)
361+
}
362+
359363
return allErrs
360364
}
361365

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

@@ -454,7 +455,7 @@ type VPCSpec struct {
454455
// should be used in a region when automatically creating subnets. If a region has more
455456
// than this number of AZs then this number of AZs will be picked randomly when creating
456457
// default subnets. Defaults to 3
457-
// +kubebuilder:default=3
458+
// +optional
458459
// +kubebuilder:validation:Minimum=1
459460
AvailabilityZoneUsageLimit *int `json:"availabilityZoneUsageLimit,omitempty"`
460461

@@ -463,10 +464,16 @@ type VPCSpec struct {
463464
// Ordered - selects based on alphabetical order
464465
// Random - selects AZs randomly in a region
465466
// Defaults to Ordered
466-
// +kubebuilder:default=Ordered
467+
// +optional
467468
// +kubebuilder:validation:Enum=Ordered;Random
468469
AvailabilityZoneSelection *AZSelectionScheme `json:"availabilityZoneSelection,omitempty"`
469470

471+
// AvailabilityZones defines a list of Availability Zones in which to create network resources in.
472+
// Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
473+
// +optional
474+
// +kubebuilder:validation:MinItems=1
475+
AvailabilityZones []string `json:"availabilityZones,omitempty"`
476+
470477
// EmptyRoutesDefaultVPCSecurityGroup specifies whether the default VPC security group ingress
471478
// and egress rules should be removed.
472479
//
@@ -539,6 +546,15 @@ func (v *VPCSpec) GetPublicIpv4Pool() *string {
539546
return nil
540547
}
541548

549+
// ValidateAvailabilityZones returns an error if the availability zones field combination is invalid.
550+
func (v *VPCSpec) ValidateAvailabilityZones() *field.Error {
551+
if len(v.AvailabilityZones) > 0 && (v.AvailabilityZoneSelection != nil || v.AvailabilityZoneUsageLimit != nil) {
552+
availabilityZonesField := field.NewPath("spec", "network", "vpc", "availabilityZones")
553+
return field.Invalid(availabilityZonesField, v.AvailabilityZoneSelection, "availabilityZones cannot be set if availabilityZoneUsageLimit and availabilityZoneSelection are set")
554+
}
555+
return nil
556+
}
557+
542558
// SubnetSpec configures an AWS Subnet.
543559
type SubnetSpec struct {
544560
// 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
@@ -674,7 +674,6 @@ spec:
674674
description: VPC configuration.
675675
properties:
676676
availabilityZoneSelection:
677-
default: Ordered
678677
description: |-
679678
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
680679
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -686,14 +685,21 @@ spec:
686685
- Random
687686
type: string
688687
availabilityZoneUsageLimit:
689-
default: 3
690688
description: |-
691689
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
692690
should be used in a region when automatically creating subnets. If a region has more
693691
than this number of AZs then this number of AZs will be picked randomly when creating
694692
default subnets. Defaults to 3
695693
minimum: 1
696694
type: integer
695+
availabilityZones:
696+
description: |-
697+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
698+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
699+
items:
700+
type: string
701+
minItems: 1
702+
type: array
697703
carrierGatewayId:
698704
description: |-
699705
CarrierGatewayID is the id of the internet gateway associated with the VPC,
@@ -2813,7 +2819,6 @@ spec:
28132819
description: VPC configuration.
28142820
properties:
28152821
availabilityZoneSelection:
2816-
default: Ordered
28172822
description: |-
28182823
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
28192824
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -2825,14 +2830,21 @@ spec:
28252830
- Random
28262831
type: string
28272832
availabilityZoneUsageLimit:
2828-
default: 3
28292833
description: |-
28302834
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
28312835
should be used in a region when automatically creating subnets. If a region has more
28322836
than this number of AZs then this number of AZs will be picked randomly when creating
28332837
default subnets. Defaults to 3
28342838
minimum: 1
28352839
type: integer
2840+
availabilityZones:
2841+
description: |-
2842+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
2843+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
2844+
items:
2845+
type: string
2846+
minItems: 1
2847+
type: array
28362848
carrierGatewayId:
28372849
description: |-
28382850
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
@@ -1626,7 +1626,6 @@ spec:
16261626
description: VPC configuration.
16271627
properties:
16281628
availabilityZoneSelection:
1629-
default: Ordered
16301629
description: |-
16311630
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
16321631
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1638,14 +1637,21 @@ spec:
16381637
- Random
16391638
type: string
16401639
availabilityZoneUsageLimit:
1641-
default: 3
16421640
description: |-
16431641
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
16441642
should be used in a region when automatically creating subnets. If a region has more
16451643
than this number of AZs then this number of AZs will be picked randomly when creating
16461644
default subnets. Defaults to 3
16471645
minimum: 1
16481646
type: integer
1647+
availabilityZones:
1648+
description: |-
1649+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
1650+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
1651+
items:
1652+
type: string
1653+
minItems: 1
1654+
type: array
16491655
carrierGatewayId:
16501656
description: |-
16511657
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
@@ -1214,7 +1214,6 @@ spec:
12141214
description: VPC configuration.
12151215
properties:
12161216
availabilityZoneSelection:
1217-
default: Ordered
12181217
description: |-
12191218
AvailabilityZoneSelection specifies how AZs should be selected if there are more AZs
12201219
in a region than specified by AvailabilityZoneUsageLimit. There are 2 selection schemes:
@@ -1226,14 +1225,21 @@ spec:
12261225
- Random
12271226
type: string
12281227
availabilityZoneUsageLimit:
1229-
default: 3
12301228
description: |-
12311229
AvailabilityZoneUsageLimit specifies the maximum number of availability zones (AZ) that
12321230
should be used in a region when automatically creating subnets. If a region has more
12331231
than this number of AZs then this number of AZs will be picked randomly when creating
12341232
default subnets. Defaults to 3
12351233
minimum: 1
12361234
type: integer
1235+
availabilityZones:
1236+
description: |-
1237+
AvailabilityZones defines a list of Availability Zones in which to create network resources in.
1238+
Cannot be defined at the same time as AvailabilityZoneSelection and AvailabilityZoneUsageLimit.
1239+
items:
1240+
type: string
1241+
minItems: 1
1242+
type: array
12371243
carrierGatewayId:
12381244
description: |-
12391245
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
@@ -28,6 +28,7 @@ import (
2828
"k8s.io/apimachinery/pkg/util/validation/field"
2929
"k8s.io/apimachinery/pkg/util/version"
3030
"k8s.io/klog/v2"
31+
"k8s.io/utils/ptr"
3132
ctrl "sigs.k8s.io/controller-runtime"
3233
"sigs.k8s.io/controller-runtime/pkg/webhook"
3334
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
@@ -489,6 +490,10 @@ func (r *AWSManagedControlPlane) validateNetwork() field.ErrorList {
489490
allErrs = append(allErrs, field.Invalid(ipamPoolField, r.Spec.NetworkSpec.VPC.IPv6.IPAMPool, "ipamPool must have either id or name"))
490491
}
491492

493+
if err := r.Spec.NetworkSpec.VPC.ValidateAvailabilityZones(); err != nil {
494+
allErrs = append(allErrs, err)
495+
}
496+
492497
return allErrs
493498
}
494499

@@ -520,6 +525,16 @@ func (*awsManagedControlPlaneWebhook) Default(_ context.Context, obj runtime.Obj
520525
}
521526
}
522527

528+
// If AvailabilityZones are not set, set defaults for AZ selection
529+
if r.Spec.NetworkSpec.VPC.AvailabilityZones == nil {
530+
if r.Spec.NetworkSpec.VPC.AvailabilityZoneUsageLimit == nil {
531+
r.Spec.NetworkSpec.VPC.AvailabilityZoneUsageLimit = ptr.To(3)
532+
}
533+
if r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection == nil {
534+
r.Spec.NetworkSpec.VPC.AvailabilityZoneSelection = &infrav1.AZSelectionSchemeOrdered
535+
}
536+
}
537+
523538
infrav1.SetDefaults_Bastion(&r.Spec.Bastion)
524539
infrav1.SetDefaults_NetworkSpec(&r.Spec.NetworkSpec)
525540

0 commit comments

Comments
 (0)