Skip to content

Commit aaafa24

Browse files
Add DNS attributes to VPC spec (#38)
Issue #, if available: aws-controllers-k8s/community#1215 Description of changes: Adds two new fields to the `VPC` spec: `EnableDNSSupport` and `EnableDNSHostnames`. Both properties are set using the `ModifyVpcAttribute` method, but can only be updated one at a time. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
1 parent 9b8a789 commit aaafa24

15 files changed

+383
-6
lines changed

apis/v1alpha1/generator.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,26 @@ resources:
171171
terminal_codes:
172172
- InvalidVpcID.Malformed
173173
- InvalidVpcID.NotFound
174+
Vpc:
175+
update_operation:
176+
custom_method_name: customUpdate
177+
exceptions:
178+
terminal_codes:
179+
- InvalidParameterCombination
180+
fields:
181+
EnableDNSSupport:
182+
from:
183+
operation: ModifyVpcAttribute
184+
path: EnableDnsSupport.Value
185+
EnableDNSHostnames:
186+
from:
187+
operation: ModifyVpcAttribute
188+
path: EnableDnsHostnames.Value
189+
hooks:
190+
sdk_create_post_set_output:
191+
template_path: hooks/vpc/sdk_create_post_set_output.go.tpl
192+
sdk_read_many_post_set_output:
193+
template_path: hooks/vpc/sdk_read_many_post_set_output.go.tpl
174194
VpcEndpoint:
175195
fields:
176196
VpcId:

apis/v1alpha1/vpc.go

Lines changed: 4 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_vpcs.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ spec:
4848
its canonical form; for example, if you specify 100.68.0.18/18,
4949
we modify it to 100.68.0.0/18.
5050
type: string
51+
enableDNSHostnames:
52+
description: The attribute value. The valid values are true or false.
53+
type: boolean
54+
enableDNSSupport:
55+
description: The attribute value. The valid values are true or false.
56+
type: boolean
5157
instanceTenancy:
5258
description: "The tenancy options for instances launched into the
5359
VPC. For default, instances are launched with shared tenancy by

generator.yaml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,26 @@ resources:
171171
terminal_codes:
172172
- InvalidVpcID.Malformed
173173
- InvalidVpcID.NotFound
174+
Vpc:
175+
update_operation:
176+
custom_method_name: customUpdate
177+
exceptions:
178+
terminal_codes:
179+
- InvalidParameterCombination
180+
fields:
181+
EnableDNSSupport:
182+
from:
183+
operation: ModifyVpcAttribute
184+
path: EnableDnsSupport.Value
185+
EnableDNSHostnames:
186+
from:
187+
operation: ModifyVpcAttribute
188+
path: EnableDnsHostnames.Value
189+
hooks:
190+
sdk_create_post_set_output:
191+
template_path: hooks/vpc/sdk_create_post_set_output.go.tpl
192+
sdk_read_many_post_set_output:
193+
template_path: hooks/vpc/sdk_read_many_post_set_output.go.tpl
174194
VpcEndpoint:
175195
fields:
176196
VpcId:

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ spec:
4848
its canonical form; for example, if you specify 100.68.0.18/18,
4949
we modify it to 100.68.0.0/18.
5050
type: string
51+
enableDNSHostnames:
52+
description: The attribute value. The valid values are true or false.
53+
type: boolean
54+
enableDNSSupport:
55+
description: The attribute value. The valid values are true or false.
56+
type: boolean
5157
instanceTenancy:
5258
description: "The tenancy options for instances launched into the
5359
VPC. For default, instances are launched with shared tenancy by

pkg/resource/vpc/delta.go

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

pkg/resource/vpc/hook.go

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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 vpc
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+
type DNSAttrs struct {
25+
EnableSupport *bool
26+
EnableHostnames *bool
27+
}
28+
29+
func newDescribeVpcAttributePayload(
30+
vpcID string,
31+
attribute string,
32+
) *svcsdk.DescribeVpcAttributeInput {
33+
res := &svcsdk.DescribeVpcAttributeInput{}
34+
res.SetVpcId(vpcID)
35+
res.SetAttribute(attribute)
36+
return res
37+
}
38+
39+
func (rm *resourceManager) getDNSAttributes(
40+
ctx context.Context,
41+
vpcID string,
42+
) (res *DNSAttrs, err error) {
43+
res = &DNSAttrs{}
44+
dnsSupport, err := rm.sdkapi.DescribeVpcAttributeWithContext(
45+
ctx,
46+
newDescribeVpcAttributePayload(vpcID, svcsdk.VpcAttributeNameEnableDnsSupport),
47+
)
48+
if err != nil {
49+
return nil, err
50+
}
51+
res.EnableSupport = dnsSupport.EnableDnsSupport.Value
52+
53+
dnsHostnames, err := rm.sdkapi.DescribeVpcAttributeWithContext(
54+
ctx,
55+
newDescribeVpcAttributePayload(vpcID, svcsdk.VpcAttributeNameEnableDnsHostnames),
56+
)
57+
if err != nil {
58+
return nil, err
59+
}
60+
res.EnableHostnames = dnsHostnames.EnableDnsHostnames.Value
61+
62+
return res, nil
63+
}
64+
65+
func newModifyDNSSupportAttributeInputPayload(
66+
r *resource,
67+
) *svcsdk.ModifyVpcAttributeInput {
68+
res := &svcsdk.ModifyVpcAttributeInput{}
69+
res.SetVpcId(*r.ko.Status.VPCID)
70+
71+
if r.ko.Spec.EnableDNSSupport != nil {
72+
res.SetEnableDnsSupport(&svcsdk.AttributeBooleanValue{
73+
Value: r.ko.Spec.EnableDNSSupport,
74+
})
75+
}
76+
77+
return res
78+
}
79+
80+
func newModifyDNSHostnamesAttributeInputPayload(
81+
r *resource,
82+
) *svcsdk.ModifyVpcAttributeInput {
83+
res := &svcsdk.ModifyVpcAttributeInput{}
84+
res.SetVpcId(*r.ko.Status.VPCID)
85+
86+
if r.ko.Spec.EnableDNSHostnames != nil {
87+
res.SetEnableDnsHostnames(&svcsdk.AttributeBooleanValue{
88+
Value: r.ko.Spec.EnableDNSHostnames,
89+
})
90+
}
91+
92+
return res
93+
}
94+
95+
func (rm *resourceManager) syncDNSSupportAttribute(
96+
ctx context.Context,
97+
r *resource,
98+
) (err error) {
99+
rlog := ackrtlog.FromContext(ctx)
100+
exit := rlog.Trace("rm.syncDNSSupportAttribute")
101+
defer exit(err)
102+
input := newModifyDNSSupportAttributeInputPayload(r)
103+
104+
_, err = rm.sdkapi.ModifyVpcAttributeWithContext(ctx, input)
105+
rm.metrics.RecordAPICall("UPDATE", "ModifyVpcAttribute", err)
106+
if err != nil {
107+
return err
108+
}
109+
110+
return nil
111+
}
112+
113+
func (rm *resourceManager) syncDNSHostnamesAttribute(
114+
ctx context.Context,
115+
r *resource,
116+
) (err error) {
117+
rlog := ackrtlog.FromContext(ctx)
118+
exit := rlog.Trace("rm.syncDNSHostnamesAttribute")
119+
defer exit(err)
120+
input := newModifyDNSHostnamesAttributeInputPayload(r)
121+
122+
_, err = rm.sdkapi.ModifyVpcAttributeWithContext(ctx, input)
123+
rm.metrics.RecordAPICall("UPDATE", "ModifyVpcAttribute", err)
124+
if err != nil {
125+
return err
126+
}
127+
128+
return nil
129+
}
130+
131+
func (rm *resourceManager) createAttributes(
132+
ctx context.Context,
133+
r *resource,
134+
) (err error) {
135+
if r.ko.Spec.EnableDNSHostnames != nil {
136+
if err = rm.syncDNSHostnamesAttribute(ctx, r); err != nil {
137+
return err
138+
}
139+
}
140+
141+
if r.ko.Spec.EnableDNSSupport != nil {
142+
if err = rm.syncDNSSupportAttribute(ctx, r); err != nil {
143+
return err
144+
}
145+
}
146+
147+
return nil
148+
}
149+
150+
func (rm *resourceManager) customUpdate(
151+
ctx context.Context,
152+
desired *resource,
153+
latest *resource,
154+
delta *ackcompare.Delta,
155+
) (updated *resource, err error) {
156+
rlog := ackrtlog.FromContext(ctx)
157+
exit := rlog.Trace("rm.customUpdateVPC")
158+
defer exit(err)
159+
160+
// Merge in the information we read from the API call above to the copy of
161+
// the original Kubernetes object we passed to the function
162+
ko := desired.ko.DeepCopy()
163+
164+
if delta.DifferentAt("Spec.EnableDNSSupport") {
165+
if err := rm.syncDNSSupportAttribute(ctx, desired); err != nil {
166+
return nil, err
167+
}
168+
}
169+
170+
if delta.DifferentAt("Spec.EnableDNSHostnames") {
171+
if err := rm.syncDNSHostnamesAttribute(ctx, desired); err != nil {
172+
return nil, err
173+
}
174+
}
175+
176+
rm.setStatusDefaults(ko)
177+
return &resource{ko}, nil
178+
}

pkg/resource/vpc/sdk.go

Lines changed: 24 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
err = rm.createAttributes(ctx, &resource{ko})
2+
if err != nil {
3+
return nil, err
4+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
if dnsAttrs, err := rm.getDNSAttributes(ctx, *ko.Status.VPCID); err != nil {
2+
return nil, err
3+
} else {
4+
ko.Spec.EnableDNSSupport = dnsAttrs.EnableSupport
5+
ko.Spec.EnableDNSHostnames = dnsAttrs.EnableHostnames
6+
}

test/e2e/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
__pycache__/
2+
*.py[cod]
3+
**/bootstrap.pkl

test/e2e/replacement_values.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@
1515
"""
1616

1717
REPLACEMENT_VALUES = {
18-
18+
"ENABLE_DNS_SUPPORT": "False",
19+
"ENABLE_DNS_HOSTNAMES": "False",
1920
}

test/e2e/resources/vpc.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ kind: VPC
33
metadata:
44
name: $VPC_NAME
55
spec:
6-
cidrBlock: $CIDR_BLOCK
6+
cidrBlock: $CIDR_BLOCK
7+
enableDNSSupport: $ENABLE_DNS_SUPPORT
8+
enableDNSHostnames: $ENABLE_DNS_HOSTNAMES

0 commit comments

Comments
 (0)