Skip to content

Commit 0b56951

Browse files
author
Nicholas Thomson
committed
Add hooks for attaching VPC
1 parent 474ceeb commit 0b56951

File tree

8 files changed

+171
-4
lines changed

8 files changed

+171
-4
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2022-03-30T20:20:28Z"
2+
build_date: "2022-03-30T21:05:10Z"
33
build_hash: 8a55c536d84c0a798df5be9f1cbdebdef51f3355
44
go_version: go1.17.8
55
version: v0.18.1
66
api_directory_checksum: 35ef0e4da69ded8c1fa7a4a6029510864a1069af
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.42.0
99
generator_config_info:
10-
file_checksum: c3e88eb4352e0de787dbd97dd9ac48762b633798
10+
file_checksum: 8bcbefb7a9f69e907e36562fc8d2e9c11e74cf28
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/generator.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ resources:
200200
references:
201201
resource: VPC
202202
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
203212
NatGateway:
204213
fields:
205214
AllocationId:

generator.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ resources:
200200
references:
201201
resource: VPC
202202
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
203212
NatGateway:
204213
fields:
205214
AllocationId:

pkg/resource/internet_gateway/hook.go

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,121 @@
1212
// permissions and limitations under the License.
1313

1414
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/sdk.go

Lines changed: 17 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if ko.Spec.VPC != nil {
2+
if err = rm.attachToVPC(ctx, &resource{ko}); err != nil {
3+
return nil, err
4+
}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if r.ko.Spec.VPC != nil && r.ko.Status.InternetGatewayID != nil {
2+
if err = rm.detachFromVPC(ctx, *r.ko.Spec.VPC, *r.ko.Status.InternetGatewayID); err != nil {
3+
return nil, err
4+
}
5+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vpcID, err := rm.getAttachedVPC(ctx, &resource{ko})
2+
if err != nil {
3+
return nil, err
4+
} else {
5+
ko.Spec.VPC = vpcID
6+
}

0 commit comments

Comments
 (0)