osbuild-service-maintenance/aws: merge errors

Collect and merge errors, instead of
nesting errors.
In this case we want to continue execution if only one
cleanup fails.
This commit is contained in:
Florian Schüller 2024-12-05 17:01:55 +01:00 committed by Florian Schüller
parent 87ef1527fc
commit 153bcadf2d

View file

@ -2,6 +2,7 @@ package main
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"slices" "slices"
"sync" "sync"
@ -104,33 +105,29 @@ func AWSCleanup(maxConcurrentRequests int, dryRun bool, accessKeyID, accessKey s
wg.Wait() wg.Wait()
} }
// using err to collect both errors as we want to // using `errs` to collect all errors as we want to
// continue execution if one cleanup fails // continue execution if only one cleanup fails
err = nil var errs []error
errSecureInstances := terminateOrphanedSecureInstances(a, dryRun)
// keep going with other cleanup even on error err = terminateOrphanedSecureInstances(a, dryRun)
if errSecureInstances != nil { if err != nil {
logrus.Errorf("Error in terminating secure instances: %v, continuing other cleanup.", errSecureInstances) logrus.Errorf("Error in terminating secure instances: %v, continuing other cleanup.", err)
err = errSecureInstances errs = append(errs, err)
} }
errSecurityGroups := searchSGAndCleanup(ctx, a, dryRun) err = searchSGAndCleanup(ctx, a, dryRun)
if errSecurityGroups != nil { if err != nil {
logrus.Errorf("Error in cleaning up security groups: %v", errSecurityGroups) logrus.Errorf("Error in cleaning up security groups: %v", err)
if err != nil { errs = append(errs, err)
err = fmt.Errorf("Multiple errors while processing AWSCleanup: %w and %w.", err, errSecurityGroups)
}
} }
errLaunchTemplates := searchLTAndCleanup(ctx, a, dryRun) err = searchLTAndCleanup(ctx, a, dryRun)
if errLaunchTemplates != nil { if err != nil {
logrus.Errorf("Error in cleaning up launch templates: %v", errLaunchTemplates) logrus.Errorf("Error in cleaning up launch templates: %v", err)
if err != nil { errs = append(errs, err)
err = fmt.Errorf("Multiple errors while processing AWSCleanup: %w and %w.", err, errLaunchTemplates)
}
} }
return err return errors.Join(errs...)
} }
func terminateOrphanedSecureInstances(a *awscloud.AWS, dryRun bool) error { func terminateOrphanedSecureInstances(a *awscloud.AWS, dryRun bool) error {