sigs.k8s.io/cluster-api-provider-aws@v1.5.5/pkg/cloud/services/gc/service.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  
    22  	"github.com/aws/aws-sdk-go/aws/arn"
    23  	"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
    24  	"github.com/aws/aws-sdk-go/service/elb/elbiface"
    25  	"github.com/aws/aws-sdk-go/service/elbv2/elbv2iface"
    26  	"github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi/resourcegroupstaggingapiiface"
    27  
    28  	"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud"
    29  	"sigs.k8s.io/cluster-api-provider-aws/pkg/cloud/scope"
    30  )
    31  
    32  // Service is used to perform operations against a tenant/workload/child cluster.
    33  type Service struct {
    34  	scope                 cloud.ClusterScoper
    35  	elbClient             elbiface.ELBAPI
    36  	elbv2Client           elbv2iface.ELBV2API
    37  	resourceTaggingClient resourcegroupstaggingapiiface.ResourceGroupsTaggingAPIAPI
    38  	ec2Client             ec2iface.EC2API
    39  	cleanupFuncs          ResourceCleanupFuncs
    40  }
    41  
    42  // NewService creates a new Service.
    43  func NewService(clusterScope cloud.ClusterScoper, opts ...ServiceOption) *Service {
    44  	svc := &Service{
    45  		scope:                 clusterScope,
    46  		elbClient:             scope.NewELBClient(clusterScope, clusterScope, clusterScope, clusterScope.InfraCluster()),
    47  		elbv2Client:           scope.NewELBv2Client(clusterScope, clusterScope, clusterScope, clusterScope.InfraCluster()),
    48  		resourceTaggingClient: scope.NewResourgeTaggingClient(clusterScope, clusterScope, clusterScope, clusterScope.InfraCluster()),
    49  		ec2Client:             scope.NewEC2Client(clusterScope, clusterScope, clusterScope, clusterScope.InfraCluster()),
    50  		cleanupFuncs:          ResourceCleanupFuncs{},
    51  	}
    52  	addDefaultCleanupFuncs(svc)
    53  
    54  	for _, opt := range opts {
    55  		opt(svc)
    56  	}
    57  
    58  	return svc
    59  }
    60  
    61  func addDefaultCleanupFuncs(s *Service) {
    62  	s.cleanupFuncs = []ResourceCleanupFunc{
    63  		s.deleteLoadBalancers,
    64  		s.deleteTargetGroups,
    65  		s.deleteSecurityGroups,
    66  	}
    67  }
    68  
    69  // AWSResource represents a resource in AWS.
    70  type AWSResource struct {
    71  	ARN  *arn.ARN
    72  	Tags map[string]string
    73  }
    74  
    75  // ResourceCleanupFunc is a function type to cleaning up resources for a specific AWS service type.
    76  type ResourceCleanupFunc func(ctx context.Context, resources []*AWSResource) error
    77  
    78  // ResourceCleanupFuncs is a collection of ResourceCleanupFunc.
    79  type ResourceCleanupFuncs []ResourceCleanupFunc
    80  
    81  // Execute will execute all the defined clean up functions against the aws resources.
    82  func (fn *ResourceCleanupFuncs) Execute(ctx context.Context, resources []*AWSResource) error {
    83  	for _, f := range *fn {
    84  		if err := f(ctx, resources); err != nil {
    85  			return err
    86  		}
    87  	}
    88  
    89  	return nil
    90  }