sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/services/gc/cleanup.go (about) 1 /* 2 Copyright 2022 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package gc 18 19 import ( 20 "context" 21 "fmt" 22 "strconv" 23 "strings" 24 25 "github.com/aws/aws-sdk-go/aws" 26 "github.com/aws/aws-sdk-go/aws/arn" 27 rgapi "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi" 28 29 infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1" 30 expinfrav1 "sigs.k8s.io/cluster-api-provider-aws/exp/api/v1beta1" 31 "sigs.k8s.io/cluster-api-provider-aws/pkg/annotations" 32 ) 33 34 const ( 35 serviceNameTag = "kubernetes.io/service-name" 36 eksClusterNameTag = "aws:eks:cluster-name" 37 ) 38 39 // ReconcileDelete is responsible for determining if the infra cluster needs to be garbage collected. If 40 // does then it will perform garbage collection. For example, it will delete the ELB/NLBs that where created 41 // as a result of Services of type load balancer. 42 func (s *Service) ReconcileDelete(ctx context.Context) error { 43 s.scope.Info("reconciling deletion for garbage collection") 44 45 val, found := annotations.Get(s.scope.InfraCluster(), expinfrav1.ExternalResourceGCAnnotation) 46 if !found { 47 val = "true" 48 } 49 50 shouldGC, err := strconv.ParseBool(val) 51 if err != nil { 52 return fmt.Errorf("converting value %s of annotation %s to bool: %w", val, expinfrav1.ExternalResourceGCAnnotation, err) 53 } 54 55 if !shouldGC { 56 s.scope.Info("cluster opted-out of garbage collection") 57 58 return nil 59 } 60 61 return s.deleteResources(ctx) 62 } 63 64 func (s *Service) deleteResources(ctx context.Context) error { 65 s.scope.Info("deleting aws resources created by tenant cluster") 66 67 serviceTag := infrav1.ClusterAWSCloudProviderTagKey(s.scope.KubernetesClusterName()) 68 awsInput := rgapi.GetResourcesInput{ 69 ResourceTypeFilters: nil, 70 TagFilters: []*rgapi.TagFilter{ 71 { 72 Key: aws.String(serviceTag), 73 Values: []*string{aws.String(string(infrav1.ResourceLifecycleOwned))}, 74 }, 75 }, 76 } 77 78 awsOutput, err := s.resourceTaggingClient.GetResourcesWithContext(ctx, &awsInput) 79 if err != nil { 80 return fmt.Errorf("getting tagged resources: %w", err) 81 } 82 83 resources := []*AWSResource{} 84 85 for i := range awsOutput.ResourceTagMappingList { 86 mapping := awsOutput.ResourceTagMappingList[i] 87 parsedArn, err := arn.Parse(*mapping.ResourceARN) 88 if err != nil { 89 return fmt.Errorf("parsing resource arn %s: %w", *mapping.ResourceARN, err) 90 } 91 92 tags := map[string]string{} 93 for _, rgTag := range mapping.Tags { 94 tags[*rgTag.Key] = *rgTag.Value 95 } 96 97 resources = append(resources, &AWSResource{ 98 ARN: &parsedArn, 99 Tags: tags, 100 }) 101 } 102 103 if deleteErr := s.cleanupFuncs.Execute(ctx, resources); deleteErr != nil { 104 return fmt.Errorf("deleting resources: %w", deleteErr) 105 } 106 107 return nil 108 } 109 110 func (s *Service) isMatchingResource(resource *AWSResource, serviceName, resourceName string) bool { 111 if resource.ARN.Service != serviceName { 112 s.scope.V(5).Info("Resource not for service", "arn", resource.ARN.String(), "service_name", serviceName, "resource_name", resourceName) 113 return false 114 } 115 if !strings.HasPrefix(resource.ARN.Resource, resourceName+"/") { 116 s.scope.V(5).Info("Resource type does not match", "arn", resource.ARN.String(), "service_name", serviceName, "resource_name", resourceName) 117 return false 118 } 119 120 return true 121 }