Skip to content

Commit

Permalink
osbuild-service-maintenance/aws: merge errors
Browse files Browse the repository at this point in the history
Collect and merge errors, instead of
nesting errors.
In this case we want to continue execution if only one
cleanup fails.
  • Loading branch information
schuellerf committed Dec 5, 2024
1 parent 294111b commit 7684e9e
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions cmd/osbuild-service-maintenance/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"slices"
"strings"
"sync"
"time"

Expand All @@ -19,6 +20,27 @@ type ChildToParentAssociation struct {
Parent string
}

type MultiError struct {
Errors []error
}

// Append adds a new error to the MultiError if non-nil
func (m *MultiError) Append(err error) {
if err != nil {
m.Errors = append(m.Errors, err)
}
}

// Error implements the error interface for MultiError
// which joins them with semicolon
func (m MultiError) Error() string {
var messages []string
for _, err := range m.Errors {
messages = append(messages, err.Error())
}
return strings.Join(messages, "; ")
}

func AWSCleanup(maxConcurrentRequests int, dryRun bool, accessKeyID, accessKey string, cutoff time.Time) error {
const region = "us-east-1"
var a *awscloud.AWS
Expand Down Expand Up @@ -104,33 +126,29 @@ func AWSCleanup(maxConcurrentRequests int, dryRun bool, accessKeyID, accessKey s
wg.Wait()
}

// using err to collect both errors as we want to
// continue execution if one cleanup fails
err = nil
errSecureInstances := terminateOrphanedSecureInstances(a, dryRun)
// keep going with other cleanup even on error
if errSecureInstances != nil {
logrus.Errorf("Error in terminating secure instances: %v, continuing other cleanup.", errSecureInstances)
err = errSecureInstances
// using `errors` to collect all errors as we want to
// continue execution if only one cleanup fails
var errors MultiError

err = terminateOrphanedSecureInstances(a, dryRun)
if err != nil {
logrus.Errorf("Error in terminating secure instances: %v, continuing other cleanup.", err)
errors.Append(err)
}

errSecurityGroups := searchSGAndCleanup(ctx, a, dryRun)
if errSecurityGroups != nil {
logrus.Errorf("Error in cleaning up security groups: %v", errSecurityGroups)
if err != nil {
err = fmt.Errorf("Multiple errors while processing AWSCleanup: %w and %w.", err, errSecurityGroups)
}
err = searchSGAndCleanup(ctx, a, dryRun)
if err != nil {
logrus.Errorf("Error in cleaning up security groups: %v", err)
errors.Append(err)
}

errLaunchTemplates := searchLTAndCleanup(ctx, a, dryRun)
if errLaunchTemplates != nil {
logrus.Errorf("Error in cleaning up launch templates: %v", errLaunchTemplates)
if err != nil {
err = fmt.Errorf("Multiple errors while processing AWSCleanup: %w and %w.", err, errLaunchTemplates)
}
err = searchLTAndCleanup(ctx, a, dryRun)
if err != nil {
logrus.Errorf("Error in cleaning up launch templates: %v", err)
errors.Append(err)
}

return err
return errors
}

func terminateOrphanedSecureInstances(a *awscloud.AWS, dryRun bool) error {
Expand Down

0 comments on commit 7684e9e

Please sign in to comment.