Skip to content

Commit

Permalink
fix: maintain cleanup order
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Salas <carlos.salas@suse.com>
  • Loading branch information
salasberryfin committed Mar 1, 2024
1 parent 7c02ae4 commit 3d0873d
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,25 @@ type action struct {
commit bool
}

type Cleaner struct {
Service string
Run CleanupFunc
}

func (a *action) Cleanup(ctx context.Context, input *Input) error {

//NOTE: ordering matters here!
cleanupFuncs := map[string]CleanupFunc{
eks.ServiceName: a.cleanEKSClusters,
autoscaling.ServiceName: a.cleanASGs,
elb.ServiceName: a.cleanLoadBalancers,
ec2.ServiceName: a.cleanSecurityGroups,
cloudformation.ServiceName: a.cleanCfStacks,
// use []Cleaner to keep the order
cleaners := []Cleaner{
{Service: eks.ServiceName, Run: a.cleanEKSClusters},
{Service: autoscaling.ServiceName, Run: a.cleanASGs},
{Service: elb.ServiceName, Run: a.cleanLoadBalancers},
{Service: ec2.ServiceName, Run: a.cleanSecurityGroups},
{Service: cloudformation.ServiceName, Run: a.cleanCfStacks},
}
inputRegions := strings.Split(input.Regions, ",")

for service, cleanupFunc := range cleanupFuncs {
regions := getServiceRegions(service, inputRegions)
for _, cleaner := range cleaners {
regions := getServiceRegions(cleaner.Service, inputRegions)

for _, region := range regions {
sess, err := session.NewSession(&aws.Config{
Expand All @@ -59,9 +64,9 @@ func (a *action) Cleanup(ctx context.Context, input *Input) error {
IgnoreTag: input.IgnoreTag,
}

Log("Cleaning up resources for service %s in region %s", service, region)
if err := cleanupFunc(ctx, scope); err != nil {
return fmt.Errorf("failed running cleanup for service %s: %w", service, err)
Log("Cleaning up resources for service %s in region %s", cleaner.Service, region)
if err := cleaner.Run(ctx, scope); err != nil {
return fmt.Errorf("failed running cleanup for service %s: %w", cleaner.Service, err)
}
}
}
Expand Down

0 comments on commit 3d0873d

Please sign in to comment.