diff --git a/cmd/osbuild-service-maintenance/aws.go b/cmd/osbuild-service-maintenance/aws.go index fe8692b6c5..41df900249 100644 --- a/cmd/osbuild-service-maintenance/aws.go +++ b/cmd/osbuild-service-maintenance/aws.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "slices" + "strings" "sync" "time" @@ -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 @@ -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 {