Skip to content

Commit 54bfea1

Browse files
authored
Merge pull request #4474 from vincepri/revert-subnet-id-required
🐛 Restore subnet management functionality
2 parents b36e26e + 703ec18 commit 54bfea1

22 files changed

+386
-141
lines changed

api/v1beta1/awscluster_conversion.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package v1beta1
1818

1919
import (
20+
apiconversion "k8s.io/apimachinery/pkg/conversion"
2021
infrav2 "sigs.k8s.io/cluster-api-provider-aws/v2/api/v1beta2"
2122
utilconversion "sigs.k8s.io/cluster-api/util/conversion"
2223
"sigs.k8s.io/controller-runtime/pkg/conversion"
@@ -73,6 +74,37 @@ func (src *AWSCluster) ConvertTo(dstRaw conversion.Hub) error {
7374

7475
dst.Spec.NetworkSpec.AdditionalControlPlaneIngressRules = restored.Spec.NetworkSpec.AdditionalControlPlaneIngressRules
7576

77+
if restored.Spec.NetworkSpec.VPC.IPAMPool != nil {
78+
if dst.Spec.NetworkSpec.VPC.IPAMPool == nil {
79+
dst.Spec.NetworkSpec.VPC.IPAMPool = &infrav2.IPAMPool{}
80+
}
81+
82+
restoreIPAMPool(restored.Spec.NetworkSpec.VPC.IPAMPool, dst.Spec.NetworkSpec.VPC.IPAMPool)
83+
}
84+
85+
if restored.Spec.NetworkSpec.VPC.IsIPv6Enabled() && restored.Spec.NetworkSpec.VPC.IPv6.IPAMPool != nil {
86+
if dst.Spec.NetworkSpec.VPC.IPv6.IPAMPool == nil {
87+
dst.Spec.NetworkSpec.VPC.IPv6.IPAMPool = &infrav2.IPAMPool{}
88+
}
89+
90+
restoreIPAMPool(restored.Spec.NetworkSpec.VPC.IPv6.IPAMPool, dst.Spec.NetworkSpec.VPC.IPv6.IPAMPool)
91+
}
92+
93+
dst.Spec.NetworkSpec.AdditionalControlPlaneIngressRules = restored.Spec.NetworkSpec.AdditionalControlPlaneIngressRules
94+
95+
// Restore SubnetSpec.ResourceID field, if any.
96+
for _, subnet := range restored.Spec.NetworkSpec.Subnets {
97+
if len(subnet.ResourceID) == 0 {
98+
continue
99+
}
100+
for i, dstSubnet := range dst.Spec.NetworkSpec.Subnets {
101+
if dstSubnet.ID == subnet.ID {
102+
dstSubnet.ResourceID = subnet.ResourceID
103+
dstSubnet.DeepCopyInto(&dst.Spec.NetworkSpec.Subnets[i])
104+
}
105+
}
106+
}
107+
76108
return nil
77109
}
78110

@@ -133,3 +165,7 @@ func (r *AWSClusterList) ConvertFrom(srcRaw conversion.Hub) error {
133165

134166
return Convert_v1beta2_AWSClusterList_To_v1beta1_AWSClusterList(src, r, nil)
135167
}
168+
169+
func Convert_v1beta2_SubnetSpec_To_v1beta1_SubnetSpec(in *infrav2.SubnetSpec, out *SubnetSpec, s apiconversion.Scope) error {
170+
return autoConvert_v1beta2_SubnetSpec_To_v1beta1_SubnetSpec(in, out, s)
171+
}

api/v1beta1/zz_generated.conversion.go

Lines changed: 28 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1beta2/network_types.go

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,20 @@ func (v *VPCSpec) IsIPv6Enabled() bool {
348348
// SubnetSpec configures an AWS Subnet.
349349
type SubnetSpec struct {
350350
// ID defines a unique identifier to reference this resource.
351+
// If you're bringing your subnet, set the AWS subnet-id here, it must start with `subnet-`.
352+
//
353+
// When the VPC is managed by CAPA, and you'd like the provider to create a subnet for you,
354+
// the id can be set to any placeholder value that does not start with `subnet-`;
355+
// upon creation, the subnet AWS identifier will be populated in the `ResourceID` field and
356+
// the `id` field is going to be used as the subnet name. If you specify a tag
357+
// called `Name`, it takes precedence.
351358
ID string `json:"id"`
352359

360+
// ResourceID is the subnet identifier from AWS, READ ONLY.
361+
// This field is populated when the provider manages the subnet.
362+
// +optional
363+
ResourceID string `json:"resourceID,omitempty"`
364+
353365
// CidrBlock is the CIDR block to be used when the provider creates a managed VPC.
354366
CidrBlock string `json:"cidrBlock,omitempty"`
355367

@@ -384,9 +396,18 @@ type SubnetSpec struct {
384396
Tags Tags `json:"tags,omitempty"`
385397
}
386398

399+
// GetResourceID returns the identifier for this subnet,
400+
// if the subnet was not created or reconciled, it returns the subnet ID.
401+
func (s *SubnetSpec) GetResourceID() string {
402+
if s.ResourceID != "" {
403+
return s.ResourceID
404+
}
405+
return s.ID
406+
}
407+
387408
// String returns a string representation of the subnet.
388409
func (s *SubnetSpec) String() string {
389-
return fmt.Sprintf("id=%s/az=%s/public=%v", s.ID, s.AvailabilityZone, s.IsPublic)
410+
return fmt.Sprintf("id=%s/az=%s/public=%v", s.GetResourceID(), s.AvailabilityZone, s.IsPublic)
390411
}
391412

392413
// Subnets is a slice of Subnet.
@@ -399,7 +420,7 @@ func (s Subnets) ToMap() map[string]*SubnetSpec {
399420
res := make(map[string]*SubnetSpec)
400421
for i := range s {
401422
x := s[i]
402-
res[x.ID] = &x
423+
res[x.GetResourceID()] = &x
403424
}
404425
return res
405426
}
@@ -408,19 +429,18 @@ func (s Subnets) ToMap() map[string]*SubnetSpec {
408429
func (s Subnets) IDs() []string {
409430
res := []string{}
410431
for _, subnet := range s {
411-
res = append(res, subnet.ID)
432+
res = append(res, subnet.GetResourceID())
412433
}
413434
return res
414435
}
415436

416437
// FindByID returns a single subnet matching the given id or nil.
417438
func (s Subnets) FindByID(id string) *SubnetSpec {
418439
for _, x := range s {
419-
if x.ID == id {
440+
if x.GetResourceID() == id {
420441
return &x
421442
}
422443
}
423-
424444
return nil
425445
}
426446

@@ -429,7 +449,9 @@ func (s Subnets) FindByID(id string) *SubnetSpec {
429449
// or if they are in the same vpc and the cidr block is the same.
430450
func (s Subnets) FindEqual(spec *SubnetSpec) *SubnetSpec {
431451
for _, x := range s {
432-
if (spec.ID != "" && x.ID == spec.ID) || (spec.CidrBlock == x.CidrBlock) || (spec.IPv6CidrBlock != "" && spec.IPv6CidrBlock == x.IPv6CidrBlock) {
452+
if (spec.GetResourceID() != "" && x.GetResourceID() == spec.GetResourceID()) ||
453+
(spec.CidrBlock == x.CidrBlock) ||
454+
(spec.IPv6CidrBlock != "" && spec.IPv6CidrBlock == x.IPv6CidrBlock) {
433455
return &x
434456
}
435457
}

config/crd/bases/controlplane.cluster.x-k8s.io_awsmanagedcontrolplanes.yaml

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,15 @@ spec:
481481
the provider creates a managed VPC.
482482
type: string
483483
id:
484-
description: ID defines a unique identifier to reference
485-
this resource.
484+
description: "ID defines a unique identifier to reference
485+
this resource. If you're bringing your subnet, set the
486+
AWS subnet-id here, it must start with `subnet-`. \n When
487+
the VPC is managed by CAPA, and you'd like the provider
488+
to create a subnet for you, the id can be set to any placeholder
489+
value that does not start with `subnet-`; upon creation,
490+
the subnet AWS identifier will be populated in the `ResourceID`
491+
field and the `id` field is going to be used as the subnet
492+
name. If you specify a tag called `Name`, it takes precedence."
486493
type: string
487494
ipv6CidrBlock:
488495
description: IPv6CidrBlock is the IPv6 CIDR block to be
@@ -510,6 +517,11 @@ spec:
510517
to determine routes for private subnets in the same AZ
511518
as the public subnet.
512519
type: string
520+
resourceID:
521+
description: ResourceID is the subnet identifier from AWS,
522+
READ ONLY. This field is populated when the provider manages
523+
the subnet.
524+
type: string
513525
routeTableId:
514526
description: RouteTableID is the routing table id associated
515527
with the subnet.
@@ -2052,8 +2064,15 @@ spec:
20522064
the provider creates a managed VPC.
20532065
type: string
20542066
id:
2055-
description: ID defines a unique identifier to reference
2056-
this resource.
2067+
description: "ID defines a unique identifier to reference
2068+
this resource. If you're bringing your subnet, set the
2069+
AWS subnet-id here, it must start with `subnet-`. \n When
2070+
the VPC is managed by CAPA, and you'd like the provider
2071+
to create a subnet for you, the id can be set to any placeholder
2072+
value that does not start with `subnet-`; upon creation,
2073+
the subnet AWS identifier will be populated in the `ResourceID`
2074+
field and the `id` field is going to be used as the subnet
2075+
name. If you specify a tag called `Name`, it takes precedence."
20572076
type: string
20582077
ipv6CidrBlock:
20592078
description: IPv6CidrBlock is the IPv6 CIDR block to be
@@ -2081,6 +2100,11 @@ spec:
20812100
to determine routes for private subnets in the same AZ
20822101
as the public subnet.
20832102
type: string
2103+
resourceID:
2104+
description: ResourceID is the subnet identifier from AWS,
2105+
READ ONLY. This field is populated when the provider manages
2106+
the subnet.
2107+
type: string
20842108
routeTableId:
20852109
description: RouteTableID is the routing table id associated
20862110
with the subnet.

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,8 +1284,15 @@ spec:
12841284
the provider creates a managed VPC.
12851285
type: string
12861286
id:
1287-
description: ID defines a unique identifier to reference
1288-
this resource.
1287+
description: "ID defines a unique identifier to reference
1288+
this resource. If you're bringing your subnet, set the
1289+
AWS subnet-id here, it must start with `subnet-`. \n When
1290+
the VPC is managed by CAPA, and you'd like the provider
1291+
to create a subnet for you, the id can be set to any placeholder
1292+
value that does not start with `subnet-`; upon creation,
1293+
the subnet AWS identifier will be populated in the `ResourceID`
1294+
field and the `id` field is going to be used as the subnet
1295+
name. If you specify a tag called `Name`, it takes precedence."
12891296
type: string
12901297
ipv6CidrBlock:
12911298
description: IPv6CidrBlock is the IPv6 CIDR block to be
@@ -1313,6 +1320,11 @@ spec:
13131320
to determine routes for private subnets in the same AZ
13141321
as the public subnet.
13151322
type: string
1323+
resourceID:
1324+
description: ResourceID is the subnet identifier from AWS,
1325+
READ ONLY. This field is populated when the provider manages
1326+
the subnet.
1327+
type: string
13161328
routeTableId:
13171329
description: RouteTableID is the routing table id associated
13181330
with the subnet.

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -888,8 +888,17 @@ spec:
888888
when the provider creates a managed VPC.
889889
type: string
890890
id:
891-
description: ID defines a unique identifier to reference
892-
this resource.
891+
description: "ID defines a unique identifier to
892+
reference this resource. If you're bringing your
893+
subnet, set the AWS subnet-id here, it must start
894+
with `subnet-`. \n When the VPC is managed by
895+
CAPA, and you'd like the provider to create a
896+
subnet for you, the id can be set to any placeholder
897+
value that does not start with `subnet-`; upon
898+
creation, the subnet AWS identifier will be populated
899+
in the `ResourceID` field and the `id` field is
900+
going to be used as the subnet name. If you specify
901+
a tag called `Name`, it takes precedence."
893902
type: string
894903
ipv6CidrBlock:
895904
description: IPv6CidrBlock is the IPv6 CIDR block
@@ -920,6 +929,11 @@ spec:
920929
routes for private subnets in the same AZ as the
921930
public subnet.
922931
type: string
932+
resourceID:
933+
description: ResourceID is the subnet identifier
934+
from AWS, READ ONLY. This field is populated when
935+
the provider manages the subnet.
936+
type: string
923937
routeTableId:
924938
description: RouteTableID is the routing table id
925939
associated with the subnet.

0 commit comments

Comments
 (0)