Skip to content

Commit b5c4c9f

Browse files
VPC attachment for InternetGateway (#48)
Closes aws-controllers-k8s/community#1216 Description of changes: Add support for attaching an `InternetGateway` to a `VPC`. The EC2 API exposes a list of `Attachments` when you describe `InternetGateway`, but manual testing shows that you can only attach a gateway to a single VPC - so this is treated as a list of at most one element. I had to create a new VPC for integration tests, since the default VPC we use for all of the other tests already contains an IGW, and you can have at most 1 per VPC. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 67545f7 commit b5c4c9f

22 files changed

+492
-34
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-03-30T20:36:15Z"
3-
build_hash: c6b852a8017aa73cfc5a882b1ba60c88d820e967
4-
go_version: go1.17
2+
build_date: "2022-03-30T21:05:10Z"
3+
build_hash: 8a55c536d84c0a798df5be9f1cbdebdef51f3355
4+
go_version: go1.17.8
55
version: v0.18.1
6-
api_directory_checksum: e5bf2003f6be051fdeda5059df47eb7fff0a9c86
6+
api_directory_checksum: 35ef0e4da69ded8c1fa7a4a6029510864a1069af
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.42.0
99
generator_config_info:
10-
file_checksum: e017fb539a06710c6713157e5f61e2a0c20e70ba
10+
file_checksum: 8bcbefb7a9f69e907e36562fc8d2e9c11e74cf28
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ resources:
191191
template_path: hooks/elastic_ip_address/sdk_read_many_pre_build_request.go.tpl
192192
sdk_read_many_post_build_request:
193193
template_path: hooks/elastic_ip_address/sdk_read_many_post_build_request.go.tpl
194+
InternetGateway:
195+
fields:
196+
VPC:
197+
from:
198+
operation: AttachInternetGateway
199+
path: VpcId
200+
references:
201+
resource: VPC
202+
path: Status.VPCID
203+
hooks:
204+
sdk_create_post_set_output:
205+
template_path: hooks/internet_gateway/sdk_create_post_set_output.go.tpl
206+
sdk_read_many_post_set_output:
207+
template_path: hooks/internet_gateway/sdk_read_many_post_set_output.go.tpl
208+
sdk_delete_pre_build_request:
209+
template_path: hooks/internet_gateway/sdk_delete_pre_build_request.go.tpl
210+
update_operation:
211+
custom_method_name: customUpdateInternetGateway
194212
NatGateway:
195213
fields:
196214
AllocationId:

apis/v1alpha1/internet_gateway.go

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

apis/v1alpha1/zz_generated.deepcopy.go

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

config/crd/bases/ec2.services.k8s.aws_internetgateways.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ spec:
5757
type: array
5858
type: object
5959
type: array
60+
vpc:
61+
description: The ID of the VPC.
62+
type: string
63+
vpcRef:
64+
description: 'AWSResourceReferenceWrapper provides a wrapper around
65+
*AWSResourceReference type to provide more user friendly syntax
66+
for references using ''from'' field Ex: APIIDRef: from: name:
67+
my-api'
68+
properties:
69+
from:
70+
description: AWSResourceReference provides all the values necessary
71+
to reference another k8s resource for finding the identifier(Id/ARN/Name)
72+
properties:
73+
name:
74+
type: string
75+
type: object
76+
type: object
6077
type: object
6178
status:
6279
description: InternetGatewayStatus defines the observed state of InternetGateway

generator.yaml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,24 @@ resources:
191191
template_path: hooks/elastic_ip_address/sdk_read_many_pre_build_request.go.tpl
192192
sdk_read_many_post_build_request:
193193
template_path: hooks/elastic_ip_address/sdk_read_many_post_build_request.go.tpl
194+
InternetGateway:
195+
fields:
196+
VPC:
197+
from:
198+
operation: AttachInternetGateway
199+
path: VpcId
200+
references:
201+
resource: VPC
202+
path: Status.VPCID
203+
hooks:
204+
sdk_create_post_set_output:
205+
template_path: hooks/internet_gateway/sdk_create_post_set_output.go.tpl
206+
sdk_read_many_post_set_output:
207+
template_path: hooks/internet_gateway/sdk_read_many_post_set_output.go.tpl
208+
sdk_delete_pre_build_request:
209+
template_path: hooks/internet_gateway/sdk_delete_pre_build_request.go.tpl
210+
update_operation:
211+
custom_method_name: customUpdateInternetGateway
194212
NatGateway:
195213
fields:
196214
AllocationId:

helm/crds/ec2.services.k8s.aws_internetgateways.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,23 @@ spec:
5757
type: array
5858
type: object
5959
type: array
60+
vpc:
61+
description: The ID of the VPC.
62+
type: string
63+
vpcRef:
64+
description: 'AWSResourceReferenceWrapper provides a wrapper around
65+
*AWSResourceReference type to provide more user friendly syntax
66+
for references using ''from'' field Ex: APIIDRef: from: name:
67+
my-api'
68+
properties:
69+
from:
70+
description: AWSResourceReference provides all the values necessary
71+
to reference another k8s resource for finding the identifier(Id/ARN/Name)
72+
properties:
73+
name:
74+
type: string
75+
type: object
76+
type: object
6077
type: object
6178
status:
6279
description: InternetGatewayStatus defines the observed state of InternetGateway

pkg/resource/internet_gateway/delta.go

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

pkg/resource/internet_gateway/hook.go

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License"). You may
4+
// not use this file except in compliance with the License. A copy of the
5+
// License is located at
6+
//
7+
// http://aws.amazon.com/apache2.0/
8+
//
9+
// or in the "license" file accompanying this file. This file is distributed
10+
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
11+
// express or implied. See the License for the specific language governing
12+
// permissions and limitations under the License.
13+
14+
package internet_gateway
15+
16+
import (
17+
"context"
18+
19+
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
20+
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
21+
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
22+
)
23+
24+
func (rm *resourceManager) customUpdateInternetGateway(
25+
ctx context.Context,
26+
desired *resource,
27+
latest *resource,
28+
delta *ackcompare.Delta,
29+
) (updated *resource, err error) {
30+
rlog := ackrtlog.FromContext(ctx)
31+
exit := rlog.Trace("rm.customUpdateInternetGateway")
32+
defer func(err error) {
33+
exit(err)
34+
}(err)
35+
36+
ko := desired.ko.DeepCopy()
37+
rm.setStatusDefaults(ko)
38+
39+
if delta.DifferentAt("Spec.VPC") {
40+
if latest.ko.Spec.VPC != nil {
41+
if err = rm.detachFromVPC(ctx, *latest.ko.Spec.VPC, *latest.ko.Status.InternetGatewayID); err != nil {
42+
return nil, err
43+
}
44+
}
45+
if desired.ko.Spec.VPC != nil {
46+
if err = rm.attachToVPC(ctx, desired); err != nil {
47+
return nil, err
48+
}
49+
}
50+
}
51+
52+
return &resource{ko}, nil
53+
}
54+
55+
// getAttachedVPC will attempt to find the VPCID for any VPC that the
56+
// InternetGateway is currently attached to. If it is not attached, or is
57+
// actively being detached, then it will return nil.
58+
func (rm *resourceManager) getAttachedVPC(
59+
ctx context.Context,
60+
latest *resource,
61+
) (vpcID *string, err error) {
62+
rlog := ackrtlog.FromContext(ctx)
63+
exit := rlog.Trace("rm.getAttachedVPC")
64+
defer func(err error) {
65+
exit(err)
66+
}(err)
67+
68+
// InternetGateways can only be attached to a single VPC at a time - even
69+
// though attachments is a slice. Attaching is almost instant, but if the
70+
// request returns that it is in `Attaching` status still, we can assume
71+
// that it will be attached in the near future and does not need to be
72+
// updated.
73+
for _, att := range latest.ko.Status.Attachments {
74+
// There is no `AttachmentStatusAvailable` - so we can just check by
75+
// using negative logic with the constants we have, instead
76+
if *att.State != svcsdk.AttachmentStatusDetached &&
77+
*att.State != svcsdk.AttachmentStatusDetaching {
78+
return att.VPCID, nil
79+
}
80+
}
81+
82+
return nil, nil
83+
}
84+
85+
func (rm *resourceManager) attachToVPC(
86+
ctx context.Context,
87+
desired *resource,
88+
) (err error) {
89+
rlog := ackrtlog.FromContext(ctx)
90+
exit := rlog.Trace("rm.attachToVPC")
91+
defer func(err error) {
92+
exit(err)
93+
}(err)
94+
95+
input := &svcsdk.AttachInternetGatewayInput{
96+
InternetGatewayId: desired.ko.Status.InternetGatewayID,
97+
VpcId: desired.ko.Spec.VPC,
98+
}
99+
100+
_, err = rm.sdkapi.AttachInternetGatewayWithContext(ctx, input)
101+
rm.metrics.RecordAPICall("UPDATE", "AttachInternetGateway", err)
102+
if err != nil {
103+
return err
104+
}
105+
106+
return nil
107+
}
108+
109+
func (rm *resourceManager) detachFromVPC(
110+
ctx context.Context,
111+
vpcID string,
112+
igwID string,
113+
) (err error) {
114+
rlog := ackrtlog.FromContext(ctx)
115+
exit := rlog.Trace("rm.detachFromVPC")
116+
defer func(err error) {
117+
exit(err)
118+
}(err)
119+
120+
input := &svcsdk.DetachInternetGatewayInput{
121+
InternetGatewayId: &igwID,
122+
VpcId: &vpcID,
123+
}
124+
125+
_, err = rm.sdkapi.DetachInternetGatewayWithContext(ctx, input)
126+
rm.metrics.RecordAPICall("UPDATE", "DetachInternetGateway", err)
127+
if err != nil {
128+
return err
129+
}
130+
131+
return nil
132+
}

pkg/resource/internet_gateway/references.go

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

0 commit comments

Comments
 (0)