Skip to content

Commit

Permalink
Abort Each/ParallelEach early on context errors
Browse files Browse the repository at this point in the history
Signed-off-by: Kimmo Lehto <klehto@mirantis.com>
  • Loading branch information
kke committed Feb 3, 2025
1 parent d204510 commit b4715fd
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions pkg/apis/k0sctl.k0sproject.io/v1beta1/cluster/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ func (hosts Hosts) Workers() Hosts {
func (hosts Hosts) Each(ctx context.Context, filters ...func(context.Context, *Host) error) error {
for _, filter := range filters {
for _, h := range hosts {
if err := ctx.Err(); err != nil {
return fmt.Errorf("error from context: %w", err)
}
if err := filter(ctx, h); err != nil {
return err
}
Expand All @@ -122,6 +125,12 @@ func (hosts Hosts) ParallelEach(ctx context.Context, filters ...func(context.Con
wg.Add(1)
go func(h *Host) {
defer wg.Done()
if err := ctx.Err(); err != nil {
mu.Lock()
errors = append(errors, fmt.Sprintf("error from context: %v", err))
mu.Unlock()
return
}
if err := filter(ctx, h); err != nil {
mu.Lock()
errors = append(errors, fmt.Sprintf("%s: %s", h.String(), err.Error()))
Expand All @@ -146,6 +155,9 @@ func (hosts Hosts) BatchedParallelEach(ctx context.Context, batchSize int, filte
if end > len(hosts) {
end = len(hosts)
}
if err := ctx.Err(); err != nil {
return fmt.Errorf("error from context: %w", err)
}
if err := hosts[i:end].ParallelEach(ctx, filter...); err != nil {
return err
}
Expand Down

0 comments on commit b4715fd

Please sign in to comment.