Skip to content

Fix usage of errors.Join #1755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions pkg/hostagent/hostagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,9 @@ func (a *HostAgent) startHostAgentRoutines(ctx context.Context) error {
}
return nil
})
var mErr error
var errs []error
if err := a.waitForRequirements("essential", a.essentialRequirements()); err != nil {
mErr = errors.Join(mErr, err)
errs = append(errs, err)
}
if *a.y.SSH.ForwardAgent {
faScript := `#!/bin/bash
Expand All @@ -413,79 +413,79 @@ sudo chown -R "${USER}" /run/host-services`
stdout, stderr, err := ssh.ExecuteScript("127.0.0.1", a.sshLocalPort, a.sshConfig, faScript, faDesc)
logrus.Debugf("stdout=%q, stderr=%q, err=%v", stdout, stderr, err)
if err != nil {
mErr = errors.Join(mErr, fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err))
errs = append(errs, fmt.Errorf("stdout=%q, stderr=%q: %w", stdout, stderr, err))
}
}
if *a.y.MountType == limayaml.REVSSHFS {
mounts, err := a.setupMounts()
if err != nil {
mErr = errors.Join(mErr, err)
errs = append(errs, err)
}
a.onClose = append(a.onClose, func() error {
var unmountMErr error
var unmountErrs []error
for _, m := range mounts {
if unmountErr := m.close(); unmountErr != nil {
unmountMErr = errors.Join(unmountMErr, unmountErr)
unmountErrs = append(unmountErrs, unmountErr)
}
}
return unmountMErr
return errors.Join(unmountErrs...)
})
}
if len(a.y.AdditionalDisks) > 0 {
a.onClose = append(a.onClose, func() error {
var unlockMErr error
var unlockErrs []error
for _, d := range a.y.AdditionalDisks {
disk, inspectErr := store.InspectDisk(d.Name)
if inspectErr != nil {
unlockMErr = errors.Join(unlockMErr, inspectErr)
unlockErrs = append(unlockErrs, inspectErr)
continue
}
logrus.Infof("Unmounting disk %q", disk.Name)
if unlockErr := disk.Unlock(); unlockErr != nil {
unlockMErr = errors.Join(unlockMErr, unlockErr)
unlockErrs = append(unlockErrs, unlockErr)
}
}
return unlockMErr
return errors.Join(unlockErrs...)
})
}
go a.watchGuestAgentEvents(ctx)
if err := a.waitForRequirements("optional", a.optionalRequirements()); err != nil {
mErr = errors.Join(mErr, err)
errs = append(errs, err)
}
if err := a.waitForRequirements("final", a.finalRequirements()); err != nil {
mErr = errors.Join(mErr, err)
errs = append(errs, err)
}
// Copy all config files _after_ the requirements are done
for _, rule := range a.y.CopyToHost {
if err := copyToHost(ctx, a.sshConfig, a.sshLocalPort, rule.HostFile, rule.GuestFile); err != nil {
mErr = errors.Join(mErr, err)
errs = append(errs, err)
}
}
a.onClose = append(a.onClose, func() error {
var mErr error
var rmErrs []error
for _, rule := range a.y.CopyToHost {
if rule.DeleteOnStop {
logrus.Infof("Deleting %s", rule.HostFile)
if err := os.RemoveAll(rule.HostFile); err != nil {
mErr = errors.Join(mErr, err)
rmErrs = append(rmErrs, err)
}
}
}
return mErr
return errors.Join(rmErrs...)
})
return mErr
return errors.Join(errs...)
}

func (a *HostAgent) close() error {
logrus.Infof("Shutting down the host agent")
var mErr error
var errs []error
for i := len(a.onClose) - 1; i >= 0; i-- {
f := a.onClose[i]
if err := f(); err != nil {
mErr = errors.Join(mErr, err)
errs = append(errs, err)
}
}
return mErr
return errors.Join(errs...)
}

func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {
Expand All @@ -505,20 +505,20 @@ func (a *HostAgent) watchGuestAgentEvents(ctx context.Context) {

a.onClose = append(a.onClose, func() error {
logrus.Debugf("Stop forwarding unix sockets")
var mErr error
var errs []error
for _, rule := range a.y.PortForwards {
if rule.GuestSocket != "" {
local := hostAddress(rule, guestagentapi.IPPort{})
// using ctx.Background() because ctx has already been cancelled
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, local, rule.GuestSocket, verbCancel, rule.Reverse); err != nil {
mErr = errors.Join(mErr, err)
errs = append(errs, err)
}
}
}
if err := forwardSSH(context.Background(), a.sshConfig, a.sshLocalPort, localUnix, remoteUnix, verbCancel, false); err != nil {
mErr = errors.Join(mErr, err)
errs = append(errs, err)
}
return mErr
return errors.Join(errs...)
})

for {
Expand Down
6 changes: 3 additions & 3 deletions pkg/hostagent/mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ type mount struct {
func (a *HostAgent) setupMounts() ([]*mount, error) {
var (
res []*mount
mErr error
errs []error
)
for _, f := range a.y.Mounts {
m, err := a.setupMount(f)
if err != nil {
mErr = errors.Join(mErr, err)
errs = append(errs, err)
continue
}
res = append(res, m)
}
return res, mErr
return res, errors.Join(errs...)
}

func (a *HostAgent) setupMount(m limayaml.Mount) (*mount, error) {
Expand Down
9 changes: 5 additions & 4 deletions pkg/hostagent/requirements.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (a *HostAgent) waitForRequirements(label string, requirements []requirement
retries = 60
sleepDuration = 10 * time.Second
)
var mErr error
var errs []error

for i, req := range requirements {
retryLoop:
Expand All @@ -28,16 +28,17 @@ func (a *HostAgent) waitForRequirements(label string, requirements []requirement
}
if req.fatal {
logrus.Infof("No further %s requirements will be checked", label)
return errors.Join(mErr, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s; skipping further checks: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
errs = append(errs, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s; skipping further checks: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
return errors.Join(errs...)
}
if j == retries-1 {
mErr = errors.Join(mErr, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
errs = append(errs, fmt.Errorf("failed to satisfy the %s requirement %d of %d %q: %s: %w", label, i+1, len(requirements), req.description, req.debugHint, err))
break retryLoop
}
time.Sleep(10 * time.Second)
}
}
return mErr
return errors.Join(errs...)
}

func (a *HostAgent) waitForRequirement(r requirement) error {
Expand Down
6 changes: 3 additions & 3 deletions pkg/qemu/qemu_driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,14 +282,14 @@ func (l *LimaQemuDriver) removeVNCFiles() error {
}

func (l *LimaQemuDriver) killVhosts() error {
var mErr error
var errs []error
for i, vhost := range l.vhostCmds {
if err := vhost.Process.Kill(); err != nil && !errors.Is(err, os.ErrProcessDone) {
mErr = errors.Join(mErr, fmt.Errorf("Failed to kill virtiofsd instance #%d: %w", i, err))
errs = append(errs, fmt.Errorf("Failed to kill virtiofsd instance #%d: %w", i, err))
}
}

return mErr
return errors.Join(errs...)
}

func (l *LimaQemuDriver) shutdownQEMU(ctx context.Context, timeout time.Duration, qCmd *exec.Cmd, qWaitCh <-chan error) error {
Expand Down