Skip to content

VPC attachment for InternetGateway #48

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 4 commits into from
Mar 31, 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
10 changes: 5 additions & 5 deletions apis/v1alpha1/ack-generate-metadata.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
ack_generate_info:
build_date: "2022-03-30T20:36:15Z"
build_hash: c6b852a8017aa73cfc5a882b1ba60c88d820e967
go_version: go1.17
build_date: "2022-03-30T21:05:10Z"
build_hash: 8a55c536d84c0a798df5be9f1cbdebdef51f3355
go_version: go1.17.8
version: v0.18.1
api_directory_checksum: e5bf2003f6be051fdeda5059df47eb7fff0a9c86
api_directory_checksum: 35ef0e4da69ded8c1fa7a4a6029510864a1069af
api_version: v1alpha1
aws_sdk_go_version: v1.42.0
generator_config_info:
file_checksum: e017fb539a06710c6713157e5f61e2a0c20e70ba
file_checksum: 8bcbefb7a9f69e907e36562fc8d2e9c11e74cf28
original_file_name: generator.yaml
last_modification:
reason: API generation
18 changes: 18 additions & 0 deletions apis/v1alpha1/generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ resources:
template_path: hooks/elastic_ip_address/sdk_read_many_pre_build_request.go.tpl
sdk_read_many_post_build_request:
template_path: hooks/elastic_ip_address/sdk_read_many_post_build_request.go.tpl
InternetGateway:
fields:
VPC:
from:
operation: AttachInternetGateway
path: VpcId
references:
resource: VPC
path: Status.VPCID
hooks:
sdk_create_post_set_output:
template_path: hooks/internet_gateway/sdk_create_post_set_output.go.tpl
sdk_read_many_post_set_output:
template_path: hooks/internet_gateway/sdk_read_many_post_set_output.go.tpl
sdk_delete_pre_build_request:
template_path: hooks/internet_gateway/sdk_delete_pre_build_request.go.tpl
update_operation:
custom_method_name: customUpdateInternetGateway
NatGateway:
fields:
AllocationId:
Expand Down
3 changes: 3 additions & 0 deletions apis/v1alpha1/internet_gateway.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.

17 changes: 17 additions & 0 deletions config/crd/bases/ec2.services.k8s.aws_internetgateways.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ spec:
type: array
type: object
type: array
vpc:
description: The ID of the VPC.
type: string
vpcRef:
description: 'AWSResourceReferenceWrapper provides a wrapper around
*AWSResourceReference type to provide more user friendly syntax
for references using ''from'' field Ex: APIIDRef: from: name:
my-api'
properties:
from:
description: AWSResourceReference provides all the values necessary
to reference another k8s resource for finding the identifier(Id/ARN/Name)
properties:
name:
type: string
type: object
type: object
type: object
status:
description: InternetGatewayStatus defines the observed state of InternetGateway
Expand Down
18 changes: 18 additions & 0 deletions generator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,24 @@ resources:
template_path: hooks/elastic_ip_address/sdk_read_many_pre_build_request.go.tpl
sdk_read_many_post_build_request:
template_path: hooks/elastic_ip_address/sdk_read_many_post_build_request.go.tpl
InternetGateway:
fields:
VPC:
from:
operation: AttachInternetGateway
path: VpcId
references:
resource: VPC
path: Status.VPCID
hooks:
sdk_create_post_set_output:
template_path: hooks/internet_gateway/sdk_create_post_set_output.go.tpl
sdk_read_many_post_set_output:
template_path: hooks/internet_gateway/sdk_read_many_post_set_output.go.tpl
sdk_delete_pre_build_request:
template_path: hooks/internet_gateway/sdk_delete_pre_build_request.go.tpl
update_operation:
custom_method_name: customUpdateInternetGateway
NatGateway:
fields:
AllocationId:
Expand Down
17 changes: 17 additions & 0 deletions helm/crds/ec2.services.k8s.aws_internetgateways.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,23 @@ spec:
type: array
type: object
type: array
vpc:
description: The ID of the VPC.
type: string
vpcRef:
description: 'AWSResourceReferenceWrapper provides a wrapper around
*AWSResourceReference type to provide more user friendly syntax
for references using ''from'' field Ex: APIIDRef: from: name:
my-api'
properties:
from:
description: AWSResourceReference provides all the values necessary
to reference another k8s resource for finding the identifier(Id/ARN/Name)
properties:
name:
type: string
type: object
type: object
type: object
status:
description: InternetGatewayStatus defines the observed state of InternetGateway
Expand Down
10 changes: 10 additions & 0 deletions pkg/resource/internet_gateway/delta.go

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

132 changes: 132 additions & 0 deletions pkg/resource/internet_gateway/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// 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 internet_gateway

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"
)

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

ko := desired.ko.DeepCopy()
rm.setStatusDefaults(ko)

if delta.DifferentAt("Spec.VPC") {
if latest.ko.Spec.VPC != nil {
if err = rm.detachFromVPC(ctx, *latest.ko.Spec.VPC, *latest.ko.Status.InternetGatewayID); err != nil {
return nil, err
}
}
if desired.ko.Spec.VPC != nil {
if err = rm.attachToVPC(ctx, desired); err != nil {
return nil, err
}
}
}

return &resource{ko}, nil
}

// getAttachedVPC will attempt to find the VPCID for any VPC that the
// InternetGateway is currently attached to. If it is not attached, or is
// actively being detached, then it will return nil.
func (rm *resourceManager) getAttachedVPC(
ctx context.Context,
latest *resource,
) (vpcID *string, err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.getAttachedVPC")
defer func(err error) {
exit(err)
}(err)

// InternetGateways can only be attached to a single VPC at a time - even
// though attachments is a slice. Attaching is almost instant, but if the
// request returns that it is in `Attaching` status still, we can assume
// that it will be attached in the near future and does not need to be
// updated.
for _, att := range latest.ko.Status.Attachments {
// There is no `AttachmentStatusAvailable` - so we can just check by
// using negative logic with the constants we have, instead
if *att.State != svcsdk.AttachmentStatusDetached &&
*att.State != svcsdk.AttachmentStatusDetaching {
return att.VPCID, nil
}
}

return nil, nil
}

func (rm *resourceManager) attachToVPC(
ctx context.Context,
desired *resource,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.attachToVPC")
defer func(err error) {
exit(err)
}(err)

input := &svcsdk.AttachInternetGatewayInput{
InternetGatewayId: desired.ko.Status.InternetGatewayID,
VpcId: desired.ko.Spec.VPC,
}

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

return nil
}

func (rm *resourceManager) detachFromVPC(
ctx context.Context,
vpcID string,
igwID string,
) (err error) {
rlog := ackrtlog.FromContext(ctx)
exit := rlog.Trace("rm.detachFromVPC")
defer func(err error) {
exit(err)
}(err)

input := &svcsdk.DetachInternetGatewayInput{
InternetGatewayId: &igwID,
VpcId: &vpcID,
}

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

return nil
}
81 changes: 79 additions & 2 deletions pkg/resource/internet_gateway/references.go

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

Loading