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:
parent
87ef1527fc
commit
153bcadf2d
1 changed files with 18 additions and 21 deletions
|
|
@ -2,6 +2,7 @@ package main
|
|||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"slices"
|
||||
"sync"
|
||||
|
|
@ -104,33 +105,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 `errs` to collect all errors as we want to
|
||||
// continue execution if only one cleanup fails
|
||||
var errs []error
|
||||
|
||||
err = terminateOrphanedSecureInstances(a, dryRun)
|
||||
if err != nil {
|
||||
logrus.Errorf("Error in terminating secure instances: %v, continuing other cleanup.", err)
|
||||
errs = append(errs, 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)
|
||||
errs = append(errs, 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)
|
||||
errs = append(errs, err)
|
||||
}
|
||||
|
||||
return err
|
||||
return errors.Join(errs...)
|
||||
}
|
||||
|
||||
func terminateOrphanedSecureInstances(a *awscloud.AWS, dryRun bool) error {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue