Skip to content

Commit

Permalink
Fix SSA support by adding Subnet.ResourceID field
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Oct 6, 2023
1 parent 63f22ec commit 703ec18
Show file tree
Hide file tree
Showing 19 changed files with 332 additions and 142 deletions.
36 changes: 36 additions & 0 deletions api/v1beta1/awscluster_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package v1beta1

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

dst.Spec.NetworkSpec.AdditionalControlPlaneIngressRules = restored.Spec.NetworkSpec.AdditionalControlPlaneIngressRules

if restored.Spec.NetworkSpec.VPC.IPAMPool != nil {
if dst.Spec.NetworkSpec.VPC.IPAMPool == nil {
dst.Spec.NetworkSpec.VPC.IPAMPool = &infrav2.IPAMPool{}
}

restoreIPAMPool(restored.Spec.NetworkSpec.VPC.IPAMPool, dst.Spec.NetworkSpec.VPC.IPAMPool)
}

if restored.Spec.NetworkSpec.VPC.IsIPv6Enabled() && restored.Spec.NetworkSpec.VPC.IPv6.IPAMPool != nil {
if dst.Spec.NetworkSpec.VPC.IPv6.IPAMPool == nil {
dst.Spec.NetworkSpec.VPC.IPv6.IPAMPool = &infrav2.IPAMPool{}
}

restoreIPAMPool(restored.Spec.NetworkSpec.VPC.IPv6.IPAMPool, dst.Spec.NetworkSpec.VPC.IPv6.IPAMPool)
}

dst.Spec.NetworkSpec.AdditionalControlPlaneIngressRules = restored.Spec.NetworkSpec.AdditionalControlPlaneIngressRules

// Restore SubnetSpec.ResourceID field, if any.
for _, subnet := range restored.Spec.NetworkSpec.Subnets {
if len(subnet.ResourceID) == 0 {
continue
}
for i, dstSubnet := range dst.Spec.NetworkSpec.Subnets {
if dstSubnet.ID == subnet.ID {
dstSubnet.ResourceID = subnet.ResourceID
dstSubnet.DeepCopyInto(&dst.Spec.NetworkSpec.Subnets[i])
}
}
}

return nil
}

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

return Convert_v1beta2_AWSClusterList_To_v1beta1_AWSClusterList(src, r, nil)
}

func Convert_v1beta2_SubnetSpec_To_v1beta1_SubnetSpec(in *infrav2.SubnetSpec, out *SubnetSpec, s apiconversion.Scope) error {
return autoConvert_v1beta2_SubnetSpec_To_v1beta1_SubnetSpec(in, out, s)
}
40 changes: 28 additions & 12 deletions api/v1beta1/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 31 additions & 7 deletions api/v1beta2/network_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,19 @@ func (v *VPCSpec) IsIPv6Enabled() bool {
// SubnetSpec configures an AWS Subnet.
type SubnetSpec struct {
// ID defines a unique identifier to reference this resource.
ID string `json:"id,omitempty"`
// If you're bringing your subnet, set the AWS subnet-id here, it must start with `subnet-`.
//
// When the VPC is managed by CAPA, and you'd like the provider to create a subnet for you,
// the id can be set to any placeholder value that does not start with `subnet-`;
// upon creation, the subnet AWS identifier will be populated in the `ResourceID` field and
// the `id` field is going to be used as the subnet name. If you specify a tag
// called `Name`, it takes precedence.
ID string `json:"id"`

// ResourceID is the subnet identifier from AWS, READ ONLY.
// This field is populated when the provider manages the subnet.
// +optional
ResourceID string `json:"resourceID,omitempty"`

// CidrBlock is the CIDR block to be used when the provider creates a managed VPC.
CidrBlock string `json:"cidrBlock,omitempty"`
Expand Down Expand Up @@ -384,20 +396,31 @@ type SubnetSpec struct {
Tags Tags `json:"tags,omitempty"`
}

// GetResourceID returns the identifier for this subnet,
// if the subnet was not created or reconciled, it returns the subnet ID.
func (s *SubnetSpec) GetResourceID() string {
if s.ResourceID != "" {
return s.ResourceID
}
return s.ID
}

// String returns a string representation of the subnet.
func (s *SubnetSpec) String() string {
return fmt.Sprintf("id=%s/az=%s/public=%v", s.ID, s.AvailabilityZone, s.IsPublic)
return fmt.Sprintf("id=%s/az=%s/public=%v", s.GetResourceID(), s.AvailabilityZone, s.IsPublic)
}

// Subnets is a slice of Subnet.
// +listType=map
// +listMapKey=id
type Subnets []SubnetSpec

// ToMap returns a map from id to subnet.
func (s Subnets) ToMap() map[string]*SubnetSpec {
res := make(map[string]*SubnetSpec)
for i := range s {
x := s[i]
res[x.ID] = &x
res[x.GetResourceID()] = &x
}
return res
}
Expand All @@ -406,19 +429,18 @@ func (s Subnets) ToMap() map[string]*SubnetSpec {
func (s Subnets) IDs() []string {
res := []string{}
for _, subnet := range s {
res = append(res, subnet.ID)
res = append(res, subnet.GetResourceID())
}
return res
}

// FindByID returns a single subnet matching the given id or nil.
func (s Subnets) FindByID(id string) *SubnetSpec {
for _, x := range s {
if x.ID == id {
if x.GetResourceID() == id {
return &x
}
}

return nil
}

Expand All @@ -427,7 +449,9 @@ func (s Subnets) FindByID(id string) *SubnetSpec {
// or if they are in the same vpc and the cidr block is the same.
func (s Subnets) FindEqual(spec *SubnetSpec) *SubnetSpec {
for _, x := range s {
if (spec.ID != "" && x.ID == spec.ID) || (spec.CidrBlock == x.CidrBlock) || (spec.IPv6CidrBlock != "" && spec.IPv6CidrBlock == x.IPv6CidrBlock) {
if (spec.GetResourceID() != "" && x.GetResourceID() == spec.GetResourceID()) ||
(spec.CidrBlock == x.CidrBlock) ||
(spec.IPv6CidrBlock != "" && spec.IPv6CidrBlock == x.IPv6CidrBlock) {
return &x
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,8 +481,15 @@ spec:
the provider creates a managed VPC.
type: string
id:
description: ID defines a unique identifier to reference
this resource.
description: "ID defines a unique identifier to reference
this resource. If you're bringing your subnet, set the
AWS subnet-id here, it must start with `subnet-`. \n When
the VPC is managed by CAPA, and you'd like the provider
to create a subnet for you, the id can be set to any placeholder
value that does not start with `subnet-`; upon creation,
the subnet AWS identifier will be populated in the `ResourceID`
field and the `id` field is going to be used as the subnet
name. If you specify a tag called `Name`, it takes precedence."
type: string
ipv6CidrBlock:
description: IPv6CidrBlock is the IPv6 CIDR block to be
Expand Down Expand Up @@ -510,6 +517,11 @@ spec:
to determine routes for private subnets in the same AZ
as the public subnet.
type: string
resourceID:
description: ResourceID is the subnet identifier from AWS,
READ ONLY. This field is populated when the provider manages
the subnet.
type: string
routeTableId:
description: RouteTableID is the routing table id associated
with the subnet.
Expand All @@ -520,8 +532,13 @@ spec:
description: Tags is a collection of tags describing the
resource.
type: object
required:
- id
type: object
type: array
x-kubernetes-list-map-keys:
- id
x-kubernetes-list-type: map
vpc:
description: VPC configuration.
properties:
Expand Down Expand Up @@ -2047,8 +2064,15 @@ spec:
the provider creates a managed VPC.
type: string
id:
description: ID defines a unique identifier to reference
this resource.
description: "ID defines a unique identifier to reference
this resource. If you're bringing your subnet, set the
AWS subnet-id here, it must start with `subnet-`. \n When
the VPC is managed by CAPA, and you'd like the provider
to create a subnet for you, the id can be set to any placeholder
value that does not start with `subnet-`; upon creation,
the subnet AWS identifier will be populated in the `ResourceID`
field and the `id` field is going to be used as the subnet
name. If you specify a tag called `Name`, it takes precedence."
type: string
ipv6CidrBlock:
description: IPv6CidrBlock is the IPv6 CIDR block to be
Expand Down Expand Up @@ -2076,6 +2100,11 @@ spec:
to determine routes for private subnets in the same AZ
as the public subnet.
type: string
resourceID:
description: ResourceID is the subnet identifier from AWS,
READ ONLY. This field is populated when the provider manages
the subnet.
type: string
routeTableId:
description: RouteTableID is the routing table id associated
with the subnet.
Expand All @@ -2086,8 +2115,13 @@ spec:
description: Tags is a collection of tags describing the
resource.
type: object
required:
- id
type: object
type: array
x-kubernetes-list-map-keys:
- id
x-kubernetes-list-type: map
vpc:
description: VPC configuration.
properties:
Expand Down
21 changes: 19 additions & 2 deletions config/crd/bases/infrastructure.cluster.x-k8s.io_awsclusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1284,8 +1284,15 @@ spec:
the provider creates a managed VPC.
type: string
id:
description: ID defines a unique identifier to reference
this resource.
description: "ID defines a unique identifier to reference
this resource. If you're bringing your subnet, set the
AWS subnet-id here, it must start with `subnet-`. \n When
the VPC is managed by CAPA, and you'd like the provider
to create a subnet for you, the id can be set to any placeholder
value that does not start with `subnet-`; upon creation,
the subnet AWS identifier will be populated in the `ResourceID`
field and the `id` field is going to be used as the subnet
name. If you specify a tag called `Name`, it takes precedence."
type: string
ipv6CidrBlock:
description: IPv6CidrBlock is the IPv6 CIDR block to be
Expand Down Expand Up @@ -1313,6 +1320,11 @@ spec:
to determine routes for private subnets in the same AZ
as the public subnet.
type: string
resourceID:
description: ResourceID is the subnet identifier from AWS,
READ ONLY. This field is populated when the provider manages
the subnet.
type: string
routeTableId:
description: RouteTableID is the routing table id associated
with the subnet.
Expand All @@ -1323,8 +1335,13 @@ spec:
description: Tags is a collection of tags describing the
resource.
type: object
required:
- id
type: object
type: array
x-kubernetes-list-map-keys:
- id
x-kubernetes-list-type: map
vpc:
description: VPC configuration.
properties:
Expand Down
Loading

0 comments on commit 703ec18

Please sign in to comment.