From 29433aa17a3bf10a7d25175cd47ab5c0c67c9a9b Mon Sep 17 00:00:00 2001 From: Justin Santa Barbara Date: Sun, 25 Mar 2018 18:26:58 -0400 Subject: [PATCH] ElasticIP deletion: Honor shared tags We previously weren't tagging as shared, so there was no reason to consider the tag. Now we are tagging, we should. --- pkg/resources/aws/BUILD.bazel | 1 + pkg/resources/aws/aws.go | 37 +++----------------------- pkg/resources/aws/elasticip.go | 48 ++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 34 deletions(-) create mode 100644 pkg/resources/aws/elasticip.go diff --git a/pkg/resources/aws/BUILD.bazel b/pkg/resources/aws/BUILD.bazel index 14d920fcfdf01..1f5ef40dba907 100644 --- a/pkg/resources/aws/BUILD.bazel +++ b/pkg/resources/aws/BUILD.bazel @@ -4,6 +4,7 @@ go_library( name = "go_default_library", srcs = [ "aws.go", + "elasticip.go", "errors.go", "filters.go", "routetable.go", diff --git a/pkg/resources/aws/aws.go b/pkg/resources/aws/aws.go index a7e2fe0bbc8ed..4156ea5be3f33 100644 --- a/pkg/resources/aws/aws.go +++ b/pkg/resources/aws/aws.go @@ -527,15 +527,7 @@ func ListVolumes(cloud fi.Cloud, clusterName string) ([]*resources.Resource, err continue } - resourceTracker := &resources.Resource{ - Name: ip, - ID: aws.StringValue(address.AllocationId), - Type: TypeElasticIp, - Deleter: DeleteElasticIP, - } - - resourceTrackers = append(resourceTrackers, resourceTracker) - + resourceTrackers = append(resourceTrackers, buildElasticIPResource(address, false, clusterName)) } } @@ -709,15 +701,7 @@ func ListSubnets(cloud fi.Cloud, clusterName string) ([]*resources.Resource, err if !elasticIPs.Has(ip) { continue } - - resourceTracker := &resources.Resource{ - Name: ip, - ID: aws.StringValue(address.AllocationId), - Type: TypeElasticIp, - Deleter: DeleteElasticIP, - Shared: !ownedElasticIPs.Has(ip), - } - resourceTrackers = append(resourceTrackers, resourceTracker) + resourceTrackers = append(resourceTrackers, buildElasticIPResource(address, ownedElasticIPs.Has(ip), clusterName)) } } @@ -744,7 +728,6 @@ func ListSubnets(cloud fi.Cloud, clusterName string) ([]*resources.Resource, err } } } - } glog.V(2).Infof("Querying Nat Gateways") @@ -1211,14 +1194,6 @@ func FindNatGateways(cloud fi.Cloud, routeTables map[string]*resources.Resource, // If we're deleting the NatGateway, we should delete the ElasticIP also for _, address := range t.NatGatewayAddresses { if address.AllocationId != nil { - name := aws.StringValue(address.PublicIp) - if name == "" { - name = aws.StringValue(address.PrivateIp) - } - if name == "" { - name = aws.StringValue(address.AllocationId) - } - request := &ec2.DescribeAddressesInput{} request.AllocationIds = []*string{address.AllocationId} response, err := c.EC2().DescribeAddresses(request) @@ -1227,13 +1202,7 @@ func FindNatGateways(cloud fi.Cloud, routeTables map[string]*resources.Resource, } for _, eip := range response.Addresses { - eipTracker := &resources.Resource{ - Name: name, - ID: aws.StringValue(address.AllocationId), - Type: TypeElasticIp, - Deleter: DeleteElasticIP, - Shared: HasSharedTag(TypeElasticIp+":"+*eip.AllocationId, eip.Tags, clusterName) || !ownedNatGatewayIds.Has(natGatewayId), - } + eipTracker := buildElasticIPResource(eip, !ownedNatGatewayIds.Has(natGatewayId), clusterName) resourceTrackers = append(resourceTrackers, eipTracker) ngwTracker.Blocks = append(ngwTracker.Blocks, eipTracker.Type+":"+eipTracker.ID) } diff --git a/pkg/resources/aws/elasticip.go b/pkg/resources/aws/elasticip.go new file mode 100644 index 0000000000000..4acba4fbce3ad --- /dev/null +++ b/pkg/resources/aws/elasticip.go @@ -0,0 +1,48 @@ +/* +Copyright 2018 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License 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 aws + +import ( + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/ec2" + + "k8s.io/kops/pkg/resources" +) + +func buildElasticIPResource(address *ec2.Address, forceShared bool, clusterName string) *resources.Resource { + name := aws.StringValue(address.PublicIp) + if name == "" { + name = aws.StringValue(address.PrivateIpAddress) + } + if name == "" { + name = aws.StringValue(address.AllocationId) + } + + r := &resources.Resource{ + Name: name, + ID: aws.StringValue(address.AllocationId), + Type: TypeElasticIp, + Deleter: DeleteElasticIP, + Shared: forceShared, + } + + if HasSharedTag(r.Type+":"+r.Name, address.Tags, clusterName) { + r.Shared = true + } + + return r +}