Skip to content

Commit 703ec18

Browse files
committed
Fix SSA support by adding Subnet.ResourceID field
Signed-off-by: Vince Prignano <[email protected]>
1 parent 63f22ec commit 703ec18

File tree

19 files changed

+332
-142
lines changed

19 files changed

+332
-142
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: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,19 @@ 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-
ID string `json:"id,omitempty"`
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.
358+
ID string `json:"id"`
359+
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"`
352364

353365
// CidrBlock is the CIDR block to be used when the provider creates a managed VPC.
354366
CidrBlock string `json:"cidrBlock,omitempty"`
@@ -384,20 +396,31 @@ 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.
414+
// +listType=map
415+
// +listMapKey=id
393416
type Subnets []SubnetSpec
394417

395418
// ToMap returns a map from id to subnet.
396419
func (s Subnets) ToMap() map[string]*SubnetSpec {
397420
res := make(map[string]*SubnetSpec)
398421
for i := range s {
399422
x := s[i]
400-
res[x.ID] = &x
423+
res[x.GetResourceID()] = &x
401424
}
402425
return res
403426
}
@@ -406,19 +429,18 @@ func (s Subnets) ToMap() map[string]*SubnetSpec {
406429
func (s Subnets) IDs() []string {
407430
res := []string{}
408431
for _, subnet := range s {
409-
res = append(res, subnet.ID)
432+
res = append(res, subnet.GetResourceID())
410433
}
411434
return res
412435
}
413436

414437
// FindByID returns a single subnet matching the given id or nil.
415438
func (s Subnets) FindByID(id string) *SubnetSpec {
416439
for _, x := range s {
417-
if x.ID == id {
440+
if x.GetResourceID() == id {
418441
return &x
419442
}
420443
}
421-
422444
return nil
423445
}
424446

@@ -427,7 +449,9 @@ func (s Subnets) FindByID(id string) *SubnetSpec {
427449
// or if they are in the same vpc and the cidr block is the same.
428450
func (s Subnets) FindEqual(spec *SubnetSpec) *SubnetSpec {
429451
for _, x := range s {
430-
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) {
431455
return &x
432456
}
433457
}

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

Lines changed: 38 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.
@@ -520,8 +532,13 @@ spec:
520532
description: Tags is a collection of tags describing the
521533
resource.
522534
type: object
535+
required:
536+
- id
523537
type: object
524538
type: array
539+
x-kubernetes-list-map-keys:
540+
- id
541+
x-kubernetes-list-type: map
525542
vpc:
526543
description: VPC configuration.
527544
properties:
@@ -2047,8 +2064,15 @@ spec:
20472064
the provider creates a managed VPC.
20482065
type: string
20492066
id:
2050-
description: ID defines a unique identifier to reference
2051-
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."
20522076
type: string
20532077
ipv6CidrBlock:
20542078
description: IPv6CidrBlock is the IPv6 CIDR block to be
@@ -2076,6 +2100,11 @@ spec:
20762100
to determine routes for private subnets in the same AZ
20772101
as the public subnet.
20782102
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
20792108
routeTableId:
20802109
description: RouteTableID is the routing table id associated
20812110
with the subnet.
@@ -2086,8 +2115,13 @@ spec:
20862115
description: Tags is a collection of tags describing the
20872116
resource.
20882117
type: object
2118+
required:
2119+
- id
20892120
type: object
20902121
type: array
2122+
x-kubernetes-list-map-keys:
2123+
- id
2124+
x-kubernetes-list-type: map
20912125
vpc:
20922126
description: VPC configuration.
20932127
properties:

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

Lines changed: 19 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.
@@ -1323,8 +1335,13 @@ spec:
13231335
description: Tags is a collection of tags describing the
13241336
resource.
13251337
type: object
1338+
required:
1339+
- id
13261340
type: object
13271341
type: array
1342+
x-kubernetes-list-map-keys:
1343+
- id
1344+
x-kubernetes-list-type: map
13281345
vpc:
13291346
description: VPC configuration.
13301347
properties:

0 commit comments

Comments
 (0)