Skip to content

Add DNS attributes to VPC spec #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions apis/v1alpha1/generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ resources:
terminal_codes:
- InvalidVpcID.Malformed
- InvalidVpcID.NotFound
Vpc:
update_operation:
custom_method_name: customUpdate
exceptions:
terminal_codes:
- InvalidParameterCombination
fields:
EnableDNSSupport:
from:
operation: ModifyVpcAttribute
path: EnableDnsSupport.Value
EnableDNSHostnames:
from:
operation: ModifyVpcAttribute
path: EnableDnsHostnames.Value
hooks:
sdk_create_post_set_output:
template_path: hooks/vpc/sdk_create_post_set_output.go.tpl
sdk_read_many_post_set_output:
template_path: hooks/vpc/sdk_read_many_post_set_output.go.tpl
VpcEndpoint:
fields:
VpcId:
Expand Down
4 changes: 4 additions & 0 deletions apis/v1alpha1/vpc.go

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

10 changes: 10 additions & 0 deletions apis/v1alpha1/zz_generated.deepcopy.go

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

6 changes: 6 additions & 0 deletions config/crd/bases/ec2.services.k8s.aws_vpcs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ spec:
its canonical form; for example, if you specify 100.68.0.18/18,
we modify it to 100.68.0.0/18.
type: string
enableDNSHostnames:
description: The attribute value. The valid values are true or false.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL. Great description :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating an issue for custom documentation rn

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:) I didn't expect you to do that.

Copy link
Contributor Author

@RedbackThomson RedbackThomson Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been really bugging me. Especially that now we are creating resource references, they should have proper documentation - rather than being the GoDoc for the ACKResourceReference type

type: boolean
enableDNSSupport:
description: The attribute value. The valid values are true or false.
type: boolean
instanceTenancy:
description: "The tenancy options for instances launched into the
VPC. For default, instances are launched with shared tenancy by
Expand Down
20 changes: 20 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,26 @@ resources:
terminal_codes:
- InvalidVpcID.Malformed
- InvalidVpcID.NotFound
Vpc:
update_operation:
custom_method_name: customUpdate
exceptions:
terminal_codes:
- InvalidParameterCombination
fields:
EnableDNSSupport:
from:
operation: ModifyVpcAttribute
path: EnableDnsSupport.Value
EnableDNSHostnames:
from:
operation: ModifyVpcAttribute
path: EnableDnsHostnames.Value
hooks:
sdk_create_post_set_output:
template_path: hooks/vpc/sdk_create_post_set_output.go.tpl
sdk_read_many_post_set_output:
template_path: hooks/vpc/sdk_read_many_post_set_output.go.tpl
VpcEndpoint:
fields:
VpcId:
Expand Down
6 changes: 6 additions & 0 deletions helm/crds/ec2.services.k8s.aws_vpcs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ spec:
its canonical form; for example, if you specify 100.68.0.18/18,
we modify it to 100.68.0.0/18.
type: string
enableDNSHostnames:
description: The attribute value. The valid values are true or false.
type: boolean
enableDNSSupport:
description: The attribute value. The valid values are true or false.
type: boolean
instanceTenancy:
description: "The tenancy options for instances launched into the
VPC. For default, instances are launched with shared tenancy by
Expand Down
14 changes: 14 additions & 0 deletions pkg/resource/vpc/delta.go

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

178 changes: 178 additions & 0 deletions pkg/resource/vpc/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package vpc

import (
"context"

ackcompare "github.com/aws-controllers-k8s/runtime/pkg/compare"
ackrtlog "github.com/aws-controllers-k8s/runtime/pkg/runtime/log"
svcsdk "github.com/aws/aws-sdk-go/service/ec2"
)

type DNSAttrs struct {
EnableSupport *bool
EnableHostnames *bool
}

func newDescribeVpcAttributePayload(
vpcID string,
attribute string,
) *svcsdk.DescribeVpcAttributeInput {
res := &svcsdk.DescribeVpcAttributeInput{}
res.SetVpcId(vpcID)
res.SetAttribute(attribute)
return res
}

func (rm *resourceManager) getDNSAttributes(
ctx context.Context,
vpcID string,
) (res *DNSAttrs, err error) {
res = &DNSAttrs{}
dnsSupport, err := rm.sdkapi.DescribeVpcAttributeWithContext(
ctx,
newDescribeVpcAttributePayload(vpcID, svcsdk.VpcAttributeNameEnableDnsSupport),
)
if err != nil {
return nil, err
}
res.EnableSupport = dnsSupport.EnableDnsSupport.Value

dnsHostnames, err := rm.sdkapi.DescribeVpcAttributeWithContext(
ctx,
newDescribeVpcAttributePayload(vpcID, svcsdk.VpcAttributeNameEnableDnsHostnames),
)
if err != nil {
return nil, err
}
res.EnableHostnames = dnsHostnames.EnableDnsHostnames.Value

return res, nil
}

func newModifyDNSSupportAttributeInputPayload(
r *resource,
) *svcsdk.ModifyVpcAttributeInput {
res := &svcsdk.ModifyVpcAttributeInput{}
res.SetVpcId(*r.ko.Status.VPCID)

if r.ko.Spec.EnableDNSSupport != nil {
res.SetEnableDnsSupport(&svcsdk.AttributeBooleanValue{
Value: r.ko.Spec.EnableDNSSupport,
})
}

return res
}

func newModifyDNSHostnamesAttributeInputPayload(
r *resource,
) *svcsdk.ModifyVpcAttributeInput {
res := &svcsdk.ModifyVpcAttributeInput{}
res.SetVpcId(*r.ko.Status.VPCID)

if r.ko.Spec.EnableDNSHostnames != nil {
res.SetEnableDnsHostnames(&svcsdk.AttributeBooleanValue{
Value: r.ko.Spec.EnableDNSHostnames,
})
}

return res
}

func (rm *resourceManager) syncDNSSupportAttribute(
ctx context.Context,
r *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.syncDNSSupportAttribute")
defer exit(err)
input := newModifyDNSSupportAttributeInputPayload(r)

_, err = rm.sdkapi.ModifyVpcAttributeWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "ModifyVpcAttribute", err)
if err != nil {
return err
}

return nil
}

func (rm *resourceManager) syncDNSHostnamesAttribute(
ctx context.Context,
r *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.syncDNSHostnamesAttribute")
defer exit(err)
input := newModifyDNSHostnamesAttributeInputPayload(r)

_, err = rm.sdkapi.ModifyVpcAttributeWithContext(ctx, input)
rm.metrics.RecordAPICall("UPDATE", "ModifyVpcAttribute", err)
if err != nil {
return err
}

return nil
}

func (rm *resourceManager) createAttributes(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently this method makes two modify calls, can this be optimized to make single AWS API call?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree with @vijtrip2 here (though I'm confused why Vijay is reviewing code during his PTO...).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I share the concern over api calls (now and long-term), but I think if we consolidate the calls the api returns an error:

Both properties are set using the ModifyVpcAttribute method, but can only be updated one at a time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm so glad you asked!

ModifyVpcAttribute can only modify ONE attribute at a time. DescribeVpcAttribute can only describe ONE attribute at a time. Really great stuff.

ctx context.Context,
r *resource,
) (err error) {
if r.ko.Spec.EnableDNSHostnames != nil {
if err = rm.syncDNSHostnamesAttribute(ctx, r); err != nil {
return err
}
}

if r.ko.Spec.EnableDNSSupport != nil {
if err = rm.syncDNSSupportAttribute(ctx, r); err != nil {
return err
}
}

return nil
}

func (rm *resourceManager) customUpdate(
ctx context.Context,
desired *resource,
latest *resource,
delta *ackcompare.Delta,
) (updated *resource, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.customUpdateVPC")
defer exit(err)

// Merge in the information we read from the API call above to the copy of
// the original Kubernetes object we passed to the function
ko := desired.ko.DeepCopy()

if delta.DifferentAt("Spec.EnableDNSSupport") {
if err := rm.syncDNSSupportAttribute(ctx, desired); err != nil {
return nil, err
}
}

if delta.DifferentAt("Spec.EnableDNSHostnames") {
if err := rm.syncDNSHostnamesAttribute(ctx, desired); err != nil {
return nil, err
}
}

rm.setStatusDefaults(ko)
return &resource{ko}, nil
}
28 changes: 24 additions & 4 deletions pkg/resource/vpc/sdk.go

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

4 changes: 4 additions & 0 deletions templates/hooks/vpc/sdk_create_post_set_output.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
err = rm.createAttributes(ctx, &resource{ko})
if err != nil {
return nil, err
}
6 changes: 6 additions & 0 deletions templates/hooks/vpc/sdk_read_many_post_set_output.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
if dnsAttrs, err := rm.getDNSAttributes(ctx, *ko.Status.VPCID); err != nil {
return nil, err
} else {
ko.Spec.EnableDNSSupport = dnsAttrs.EnableSupport
ko.Spec.EnableDNSHostnames = dnsAttrs.EnableHostnames
}
3 changes: 3 additions & 0 deletions test/e2e/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__pycache__/
*.py[cod]
**/bootstrap.pkl
3 changes: 2 additions & 1 deletion test/e2e/replacement_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@
"""

REPLACEMENT_VALUES = {

"ENABLE_DNS_SUPPORT": "False",
"ENABLE_DNS_HOSTNAMES": "False",
}
4 changes: 3 additions & 1 deletion test/e2e/resources/vpc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ kind: VPC
metadata:
name: $VPC_NAME
spec:
cidrBlock: $CIDR_BLOCK
cidrBlock: $CIDR_BLOCK
enableDNSSupport: $ENABLE_DNS_SUPPORT
enableDNSHostnames: $ENABLE_DNS_HOSTNAMES
Loading