From cad57ad77ee0da5c966d9187e54322c433089dfc Mon Sep 17 00:00:00 2001 From: michaelhtm <98621731+michaelhtm@users.noreply.github.com> Date: Tue, 1 Apr 2025 17:22:53 -0700 Subject: [PATCH] feat: Recreate TransitGateway and NatGateway when deleted Currently if a TransitGateway is deleted on accident by calling the API or console, it still dangles with a `deleted` state. With this change, the controller will recreate the TransitGateway if the resource has `deleted` state NatGateway getting the same changes --- apis/v1alpha1/ack-generate-metadata.yaml | 4 ++-- apis/v1alpha1/generator.yaml | 4 ++++ generator.yaml | 4 ++++ pkg/resource/nat_gateway/hooks.go | 17 +++++++++++++++++ pkg/resource/nat_gateway/sdk.go | 7 +++++++ pkg/resource/transit_gateway/hooks.go | 16 ++++++++++++++++ pkg/resource/transit_gateway/sdk.go | 7 +++++++ .../sdk_read_many_post_set_output.go.tpl | 6 ++++++ .../sdk_read_many_post_set_output.go.tpl | 6 ++++++ 9 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 templates/hooks/nat_gateway/sdk_read_many_post_set_output.go.tpl create mode 100644 templates/hooks/transit_gateway/sdk_read_many_post_set_output.go.tpl diff --git a/apis/v1alpha1/ack-generate-metadata.yaml b/apis/v1alpha1/ack-generate-metadata.yaml index 6397f345..bd2326c1 100755 --- a/apis/v1alpha1/ack-generate-metadata.yaml +++ b/apis/v1alpha1/ack-generate-metadata.yaml @@ -1,5 +1,5 @@ ack_generate_info: - build_date: "2025-04-02T00:53:09Z" + build_date: "2025-04-02T20:13:02Z" build_hash: 980cb1e4734f673d16101cf55206b84ca639ec01 go_version: go1.24.1 version: v0.44.0 @@ -7,7 +7,7 @@ api_directory_checksum: 5e4731f8ab6fa4bafdb863edf0e678e604697103 api_version: v1alpha1 aws_sdk_go_version: v1.32.6 generator_config_info: - file_checksum: e698ea1f155660431146b92e305118d58ee80a76 + file_checksum: 91537a111710acf9c1f9c7c6c451ca017568ecb7 original_file_name: generator.yaml last_modification: reason: API generation diff --git a/apis/v1alpha1/generator.yaml b/apis/v1alpha1/generator.yaml index 4a1f8213..daa0c7dc 100644 --- a/apis/v1alpha1/generator.yaml +++ b/apis/v1alpha1/generator.yaml @@ -598,6 +598,8 @@ resources: hooks: sdk_create_post_build_request: template_path: hooks/nat_gateway/sdk_create_post_build_request.go.tpl + sdk_read_many_post_set_output: + template_path: hooks/nat_gateway/sdk_read_many_post_set_output.go.tpl sdk_file_end: template_path: hooks/nat_gateway/sdk_file_end.go.tpl update_operation: @@ -877,6 +879,8 @@ resources: hooks: sdk_create_post_build_request: template_path: hooks/transit_gateway/sdk_create_post_build_request.go.tpl + sdk_read_many_post_set_output: + template_path: hooks/transit_gateway/sdk_read_many_post_set_output.go.tpl sdk_file_end: template_path: hooks/transit_gateway/sdk_file_end.go.tpl update_operation: diff --git a/generator.yaml b/generator.yaml index 4a1f8213..daa0c7dc 100644 --- a/generator.yaml +++ b/generator.yaml @@ -598,6 +598,8 @@ resources: hooks: sdk_create_post_build_request: template_path: hooks/nat_gateway/sdk_create_post_build_request.go.tpl + sdk_read_many_post_set_output: + template_path: hooks/nat_gateway/sdk_read_many_post_set_output.go.tpl sdk_file_end: template_path: hooks/nat_gateway/sdk_file_end.go.tpl update_operation: @@ -877,6 +879,8 @@ resources: hooks: sdk_create_post_build_request: template_path: hooks/transit_gateway/sdk_create_post_build_request.go.tpl + sdk_read_many_post_set_output: + template_path: hooks/transit_gateway/sdk_read_many_post_set_output.go.tpl sdk_file_end: template_path: hooks/transit_gateway/sdk_file_end.go.tpl update_operation: diff --git a/pkg/resource/nat_gateway/hooks.go b/pkg/resource/nat_gateway/hooks.go index d4dc3154..89df3030 100644 --- a/pkg/resource/nat_gateway/hooks.go +++ b/pkg/resource/nat_gateway/hooks.go @@ -24,6 +24,22 @@ import ( "github.com/aws-controllers-k8s/ec2-controller/pkg/tags" ) +func isResourceDeleted(r *resource) bool { + if r.ko.Status.State == nil { + return true + } + status := *r.ko.Status.State + return status == string(svcsdktypes.NatGatewayStateDeleted) +} + +func isResourcePending(r *resource) bool { + if r.ko.Status.State == nil { + return false + } + status := *r.ko.Status.State + return status == string(svcsdktypes.NatGatewayStatePending) +} + func (rm *resourceManager) customUpdateNATGateway( ctx context.Context, desired *resource, @@ -40,6 +56,7 @@ func (rm *resourceManager) customUpdateNATGateway( // an error, then the update was successful and desired.Spec // (now updated.Spec) reflects the latest resource state. updated = rm.concreteResource(desired.DeepCopy()) + updated.ko.Status = latest.ko.Status if delta.DifferentAt("Spec.Tags") { if err := tags.Sync( diff --git a/pkg/resource/nat_gateway/sdk.go b/pkg/resource/nat_gateway/sdk.go index d71ecfcf..0223e349 100644 --- a/pkg/resource/nat_gateway/sdk.go +++ b/pkg/resource/nat_gateway/sdk.go @@ -203,6 +203,13 @@ func (rm *resourceManager) sdkFind( } rm.setStatusDefaults(ko) + if isResourceDeleted(&resource{ko}) { + return nil, ackerr.NotFound + } + if isResourcePending(&resource{ko}) { + return nil, ackrequeue.Needed(fmt.Errorf("resource is pending")) + } + return &resource{ko}, nil } diff --git a/pkg/resource/transit_gateway/hooks.go b/pkg/resource/transit_gateway/hooks.go index 89a94e63..3b3e7e90 100644 --- a/pkg/resource/transit_gateway/hooks.go +++ b/pkg/resource/transit_gateway/hooks.go @@ -24,6 +24,22 @@ import ( "github.com/aws-controllers-k8s/ec2-controller/pkg/tags" ) +func isResourceDeleted(r *resource) bool { + if r.ko.Status.State == nil { + return true + } + status := *r.ko.Status.State + return status == string(svcsdktypes.TransitGatewayStateDeleted) +} + +func isResourcePending(r *resource) bool { + if r.ko.Status.State == nil { + return false + } + status := *r.ko.Status.State + return status == string(svcsdktypes.TransitGatewayStatePending) +} + func (rm *resourceManager) customUpdateTransitGateway( ctx context.Context, desired *resource, diff --git a/pkg/resource/transit_gateway/sdk.go b/pkg/resource/transit_gateway/sdk.go index 72b2d806..1f0c7772 100644 --- a/pkg/resource/transit_gateway/sdk.go +++ b/pkg/resource/transit_gateway/sdk.go @@ -177,6 +177,13 @@ func (rm *resourceManager) sdkFind( } rm.setStatusDefaults(ko) + if isResourceDeleted(&resource{ko}) { + return nil, ackerr.NotFound + } + if isResourcePending(&resource{ko}) { + return nil, ackrequeue.Needed(fmt.Errorf("resource is pending")) + } + return &resource{ko}, nil } diff --git a/templates/hooks/nat_gateway/sdk_read_many_post_set_output.go.tpl b/templates/hooks/nat_gateway/sdk_read_many_post_set_output.go.tpl new file mode 100644 index 00000000..af95fb63 --- /dev/null +++ b/templates/hooks/nat_gateway/sdk_read_many_post_set_output.go.tpl @@ -0,0 +1,6 @@ + if isResourceDeleted(&resource{ko}) { + return nil, ackerr.NotFound + } + if isResourcePending(&resource{ko}) { + return nil, ackrequeue.Needed(fmt.Errorf("resource is pending")) + } diff --git a/templates/hooks/transit_gateway/sdk_read_many_post_set_output.go.tpl b/templates/hooks/transit_gateway/sdk_read_many_post_set_output.go.tpl new file mode 100644 index 00000000..af95fb63 --- /dev/null +++ b/templates/hooks/transit_gateway/sdk_read_many_post_set_output.go.tpl @@ -0,0 +1,6 @@ + if isResourceDeleted(&resource{ko}) { + return nil, ackerr.NotFound + } + if isResourcePending(&resource{ko}) { + return nil, ackrequeue.Needed(fmt.Errorf("resource is pending")) + }