Skip to content

Commit 0436bbe

Browse files
authored
Adding support for VPC association in DHCPOptions (#143)
Issue #, if available: Description of changes: Adding support in EC2 controller to associate VPC in DHCPOptions resource. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 55cbd30 commit 0436bbe

18 files changed

+434
-9
lines changed
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
ack_generate_info:
2-
build_date: "2023-07-07T07:03:45Z"
2+
build_date: "2023-07-17T14:36:56Z"
33
build_hash: e9b68590da73ce9143ba1e4361cebdc1d876c81e
44
go_version: go1.19
55
version: v0.26.1-7-ge9b6859
6-
api_directory_checksum: 9af0bb4ef3924278a9490af8b0b6956a41cda601
6+
api_directory_checksum: cd75b9b3537b16abdbf981cc60ab948e978ad939
77
api_version: v1alpha1
88
aws_sdk_go_version: v1.44.93
99
generator_config_info:
10-
file_checksum: 2630331c335136e1f45c564750f1d20b61a51b12
10+
file_checksum: e3d618bcc0a018d73959e27cc0d74c7abfa0502d
1111
original_file_name: generator.yaml
1212
last_modification:
1313
reason: API generation

apis/v1alpha1/dhcp_options.go

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

apis/v1alpha1/generator.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ resources:
197197
print:
198198
path: Status.dhcpOptionsID
199199
name: ID
200+
VPC:
201+
# from:
202+
# operation: AssociateDhcpOptions
203+
# path: VpcId
204+
custom_field:
205+
list_of: String
206+
references:
207+
resource: VPC
208+
path: Status.VPCID
200209
Tags:
201210
from:
202211
operation: CreateTags
@@ -208,6 +217,12 @@ resources:
208217
code: compareTags(delta, a, b)
209218
sdk_create_post_build_request:
210219
template_path: hooks/dhcp_options/sdk_create_post_build_request.go.tpl
220+
sdk_create_post_set_output:
221+
template_path: hooks/dhcp_options/sdk_create_post_set_output.go.tpl
222+
sdk_delete_pre_build_request:
223+
template_path: hooks/dhcp_options/sdk_delete_pre_build_request.go.tpl
224+
sdk_read_many_post_set_output:
225+
template_path: hooks/dhcp_options/sdk_read_many_post_set_output.go.tpl
211226
sdk_file_end:
212227
template_path: hooks/dhcp_options/sdk_file_end.go.tpl
213228
update_operation:

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 22 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_dhcpoptions.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ spec:
6565
type: string
6666
type: object
6767
type: array
68+
vpc:
69+
items:
70+
type: string
71+
type: array
72+
vpcRefs:
73+
items:
74+
description: "AWSResourceReferenceWrapper provides a wrapper around
75+
*AWSResourceReference type to provide more user friendly syntax
76+
for references using 'from' field Ex: APIIDRef: \n from: name:
77+
my-api"
78+
properties:
79+
from:
80+
description: AWSResourceReference provides all the values necessary
81+
to reference another k8s resource for finding the identifier(Id/ARN/Name)
82+
properties:
83+
name:
84+
type: string
85+
type: object
86+
type: object
87+
type: array
6888
required:
6989
- dhcpConfigurations
7090
type: object

generator.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,15 @@ resources:
197197
print:
198198
path: Status.dhcpOptionsID
199199
name: ID
200+
VPC:
201+
# from:
202+
# operation: AssociateDhcpOptions
203+
# path: VpcId
204+
custom_field:
205+
list_of: String
206+
references:
207+
resource: VPC
208+
path: Status.VPCID
200209
Tags:
201210
from:
202211
operation: CreateTags
@@ -208,6 +217,12 @@ resources:
208217
code: compareTags(delta, a, b)
209218
sdk_create_post_build_request:
210219
template_path: hooks/dhcp_options/sdk_create_post_build_request.go.tpl
220+
sdk_create_post_set_output:
221+
template_path: hooks/dhcp_options/sdk_create_post_set_output.go.tpl
222+
sdk_delete_pre_build_request:
223+
template_path: hooks/dhcp_options/sdk_delete_pre_build_request.go.tpl
224+
sdk_read_many_post_set_output:
225+
template_path: hooks/dhcp_options/sdk_read_many_post_set_output.go.tpl
211226
sdk_file_end:
212227
template_path: hooks/dhcp_options/sdk_file_end.go.tpl
213228
update_operation:

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ spec:
6565
type: string
6666
type: object
6767
type: array
68+
vpc:
69+
items:
70+
type: string
71+
type: array
72+
vpcRefs:
73+
items:
74+
description: "AWSResourceReferenceWrapper provides a wrapper around
75+
*AWSResourceReference type to provide more user friendly syntax
76+
for references using 'from' field Ex: APIIDRef: \n from: name:
77+
my-api"
78+
properties:
79+
from:
80+
description: AWSResourceReference provides all the values necessary
81+
to reference another k8s resource for finding the identifier(Id/ARN/Name)
82+
properties:
83+
name:
84+
type: string
85+
type: object
86+
type: object
87+
type: array
6888
required:
6989
- dhcpConfigurations
7090
type: object

pkg/resource/dhcp_options/delta.go

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

pkg/resource/dhcp_options/hooks.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
2121
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
2222
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
23+
"github.com/samber/lo"
2324
)
2425

2526
func (rm *resourceManager) customUpdateDHCPOptions(
@@ -39,6 +40,16 @@ func (rm *resourceManager) customUpdateDHCPOptions(
3940
// (now updated.Spec) reflects the latest resource state.
4041
updated = rm.concreteResource(desired.DeepCopy())
4142

43+
if delta.DifferentAt("Spec.VPC") {
44+
if err = rm.syncVPCs(ctx, desired, latest); err != nil {
45+
return nil, err
46+
}
47+
updated, err = rm.sdkFind(ctx, desired)
48+
if err != nil {
49+
return nil, err
50+
}
51+
}
52+
4253
if delta.DifferentAt("Spec.Tags") {
4354
if err := rm.syncTags(ctx, desired, latest); err != nil {
4455
return nil, err
@@ -48,6 +59,72 @@ func (rm *resourceManager) customUpdateDHCPOptions(
4859
return updated, nil
4960
}
5061

62+
func (rm *resourceManager) syncVPCs(
63+
ctx context.Context,
64+
desired *resource,
65+
latest *resource,
66+
) (err error) {
67+
rlog := ackrtlog.FromContext(ctx)
68+
exit := rlog.Trace("rm.syncEntries")
69+
defer exit(err)
70+
71+
latestVPC := []string{}
72+
desiredVPC := []string{}
73+
74+
if latest != nil {
75+
for _, vpc := range latest.ko.Spec.VPC {
76+
latestVPC = append(latestVPC, *vpc)
77+
}
78+
}
79+
for _, vpc := range desired.ko.Spec.VPC {
80+
desiredVPC = append(desiredVPC, *vpc)
81+
}
82+
83+
toAdd, toDelete := lo.Difference(desiredVPC, latestVPC)
84+
85+
for _, vpc := range toAdd {
86+
rm.attachToVPC(ctx, desired, vpc)
87+
88+
}
89+
for _, vpc := range toDelete {
90+
rm.detachFromVPC(ctx, desired, vpc)
91+
92+
}
93+
return nil
94+
}
95+
96+
func (rm *resourceManager) getAttachedVPC(
97+
ctx context.Context,
98+
latest *resource,
99+
) (vpcID []*string, err error) {
100+
rlog := ackrtlog.FromContext(ctx)
101+
exit := rlog.Trace("rm.getAttachedVPC")
102+
defer func(err error) {
103+
exit(err)
104+
}(err)
105+
106+
input := &svcsdk.DescribeVpcsInput{
107+
Filters: []*svcsdk.Filter{
108+
{
109+
Name: lo.ToPtr("dhcp-options-id"),
110+
Values: []*string{latest.ko.Status.DHCPOptionsID},
111+
},
112+
},
113+
}
114+
115+
var resp *svcsdk.DescribeVpcsOutput
116+
resp, err = rm.sdkapi.DescribeVpcsWithContext(ctx, input)
117+
rm.metrics.RecordAPICall("READ_MANY", "DescribeVpcs", err)
118+
if err != nil {
119+
return nil, err
120+
}
121+
for _, vpc := range resp.Vpcs {
122+
vpcID = append(vpcID, vpc.VpcId)
123+
}
124+
125+
return vpcID, nil
126+
}
127+
51128
// syncTags used to keep tags in sync by calling Create and Delete API's
52129
func (rm *resourceManager) syncTags(
53130
ctx context.Context,
@@ -187,3 +264,53 @@ func updateTagSpecificationsInCreateRequest(r *resource,
187264
input.TagSpecifications = []*svcsdk.TagSpecification{&desiredTagSpecs}
188265
}
189266
}
267+
268+
func (rm *resourceManager) attachToVPC(
269+
ctx context.Context,
270+
desired *resource,
271+
vpc string,
272+
) (err error) {
273+
rlog := ackrtlog.FromContext(ctx)
274+
exit := rlog.Trace("rm.attachToVPC")
275+
defer func(err error) {
276+
exit(err)
277+
}(err)
278+
279+
if vpc == "" {
280+
return nil
281+
}
282+
input := &svcsdk.AssociateDhcpOptionsInput{
283+
DhcpOptionsId: desired.ko.Status.DHCPOptionsID,
284+
VpcId: &vpc,
285+
}
286+
_, err = rm.sdkapi.AssociateDhcpOptionsWithContext(ctx, input)
287+
rm.metrics.RecordAPICall("UPDATE", "AssociateDhcpOptions", err)
288+
if err != nil {
289+
return err
290+
}
291+
292+
return nil
293+
}
294+
295+
func (rm *resourceManager) detachFromVPC(
296+
ctx context.Context,
297+
desired *resource,
298+
vpc string,
299+
) (err error) {
300+
rlog := ackrtlog.FromContext(ctx)
301+
exit := rlog.Trace("rm.detachFromVPC")
302+
defer func(err error) {
303+
exit(err)
304+
}(err)
305+
306+
input := &svcsdk.AssociateDhcpOptionsInput{
307+
DhcpOptionsId: lo.ToPtr("default"),
308+
VpcId: &vpc,
309+
}
310+
_, err = rm.sdkapi.AssociateDhcpOptionsWithContext(ctx, input)
311+
rm.metrics.RecordAPICall("UPDATE", "AssociateDhcpOptions", err)
312+
if err != nil {
313+
return err
314+
}
315+
return nil
316+
}

0 commit comments

Comments
 (0)