Skip to content
This repository has been archived by the owner on Jan 8, 2024. It is now read-only.

Commit

Permalink
imp: Use pagination func for searching EFS file systems.
Browse files Browse the repository at this point in the history
Simplify runner uninstall func for ECS by removing an `if`.

Add the copyright headers back in.
  • Loading branch information
paladin-devops committed Jun 9, 2023
1 parent c596458 commit 29c0687
Showing 1 changed file with 68 additions and 77 deletions.
145 changes: 68 additions & 77 deletions internal/runnerinstall/ecs.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package runnerinstall

import (
Expand Down Expand Up @@ -364,99 +367,87 @@ func (i *ECSRunnerInstaller) Uninstall(ctx context.Context, opts *InstallOpts) e
// uninstall fails
s = sg.Add("Deleting runner file system")
efsSvc := efs.New(sess)
marker := ""
done := false
var fileSystems []*efs.FileSystemDescription
for !done {
req := &efs.DescribeFileSystemsInput{
MaxItems: aws.Int64(100),
}
if marker != "" {
req.Marker = aws.String(marker)
}
fileSystemsResp, err := efsSvc.DescribeFileSystems(req)
if err != nil {
return err
}

if fileSystemsResp.NextMarker == nil {
done = true
} else {
marker = *fileSystemsResp.NextMarker
time.Sleep(5 * time.Second)
}
fileSystems = append(fileSystems, fileSystemsResp.FileSystems...)
err = efsSvc.DescribeFileSystemsPages(&efs.DescribeFileSystemsInput{},
func(page *efs.DescribeFileSystemsOutput, lastPage bool) bool {
fileSystems = append(fileSystems, page.FileSystems...)
return !lastPage
})
if err != nil {
return err
}

if len(fileSystems) == 0 {
s.Update("No file systems detected, skipping deletion")
} else {
var fileSystemId *string
for _, fileSystem := range fileSystems {
// Check if tags match ID, if so then delete things
for _, tag := range fileSystem.Tags {
if *tag.Key == "runner-id" && *tag.Value == opts.Id {
fileSystemId = fileSystem.FileSystemId
// This goto skips to the logic for deleting the file system -
// we know which one we need to delete now, so there's no need
// to iterate through any additional fileSystems
goto DeleteFileSystem
}
s.Done()
return nil
}

var fileSystemId *string
for _, fileSystem := range fileSystems {
// Check if tags match ID, if so then delete things
for _, tag := range fileSystem.Tags {
if *tag.Key == "runner-id" && *tag.Value == opts.Id {
fileSystemId = fileSystem.FileSystemId
// This goto skips to the logic for deleting the file system -
// we know which one we need to delete now, so there's no need
// to iterate through any additional fileSystems
goto DeleteFileSystem
}
}
}

if *fileSystemId == "" {
s.Update("File system with tag key `runner-id` and value " + opts.Id + " not detected, skipping deletion")
s.Done()
return nil
}
if *fileSystemId == "" {
s.Update("File system with tag key `runner-id` and value " + opts.Id + " not detected, skipping deletion")
s.Done()
return nil
}

DeleteFileSystem:
describeAccessPointsResp, err := efsSvc.DescribeAccessPoints(&efs.DescribeAccessPointsInput{
FileSystemId: fileSystemId,
})
DeleteFileSystem:
describeAccessPointsResp, err := efsSvc.DescribeAccessPoints(&efs.DescribeAccessPointsInput{
FileSystemId: fileSystemId,
})
if err != nil {
return err
}
for _, accessPoint := range describeAccessPointsResp.AccessPoints {
_, err = efsSvc.DeleteAccessPoint(&efs.DeleteAccessPointInput{AccessPointId: accessPoint.AccessPointId})
if err != nil {
return err
}
for _, accessPoint := range describeAccessPointsResp.AccessPoints {
_, err = efsSvc.DeleteAccessPoint(&efs.DeleteAccessPointInput{AccessPointId: accessPoint.AccessPointId})
if err != nil {
return err
}
}
}

describeMountTargetsResp, err := efsSvc.DescribeMountTargets(&efs.DescribeMountTargetsInput{
FileSystemId: fileSystemId,
})
for _, mountTarget := range describeMountTargetsResp.MountTargets {
_, err = efsSvc.DeleteMountTarget(&efs.DeleteMountTargetInput{MountTargetId: mountTarget.MountTargetId})
if err != nil {
return err
}
describeMountTargetsResp, err := efsSvc.DescribeMountTargets(&efs.DescribeMountTargetsInput{
FileSystemId: fileSystemId,
})
for _, mountTarget := range describeMountTargetsResp.MountTargets {
_, err = efsSvc.DeleteMountTarget(&efs.DeleteMountTargetInput{MountTargetId: mountTarget.MountTargetId})
if err != nil {
return err
}
}

ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
for {
select {
case <-ctx.Done():
return errors.New("after 5 minutes, the file system could" +
"not be deleted, because the mount targets weren't deleted")
default:
_, err = efsSvc.DeleteFileSystem(&efs.DeleteFileSystemInput{FileSystemId: fileSystemId})
if err != nil {
if strings.Contains(err.Error(), "because it has mount targets") {
// sleep here for 5 seconds to avoid slamming the API
time.Sleep(5 * time.Second)
continue
}
return err
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
for {
select {
case <-ctx.Done():
return errors.New("after 5 minutes, the file system could" +
"not be deleted, because the mount targets weren't deleted")
default:
_, err = efsSvc.DeleteFileSystem(&efs.DeleteFileSystemInput{FileSystemId: fileSystemId})
if err != nil {
if strings.Contains(err.Error(), "because it has mount targets") {
// sleep here for 5 seconds to avoid slamming the API
time.Sleep(5 * time.Second)
continue
}
// if we reach this point, we're done
s.Update("Runner file system deleted")
s.Done()
return nil
return err
}
// if we reach this point, we're done
s.Update("Runner file system deleted")
s.Done()
return nil
}
}
return nil
Expand Down Expand Up @@ -565,7 +556,7 @@ func launchRunner(
ExecutionRoleArn: aws.String(executionRoleArn),
Cpu: aws.String(cpu),
Memory: aws.String(memory),
Family: aws.String(installutil.DefaultRunnerName(id)),
Family: aws.String(defaultRunnerTagName),
TaskRoleArn: &taskRoleArn,
NetworkMode: aws.String("awsvpc"),
RequiresCompatibilities: []*string{aws.String(defaultTaskRuntime)},
Expand Down

0 comments on commit 29c0687

Please sign in to comment.