Skip to content

replace harshicorp/multierror with errors.Join #1921

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
Jan 6, 2025
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
11 changes: 6 additions & 5 deletions analyzer/fanotify/fanotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
package fanotify

import (
"errors"
"fmt"
"os/exec"
"sync"
"syscall"
"time"

"github.com/containerd/stargz-snapshotter/analyzer/fanotify/conn"
"github.com/hashicorp/go-multierror"
)

// Fanotifier monitors "/" mountpoint of a new mount namespace and notifies all
Expand Down Expand Up @@ -59,14 +59,15 @@ func SpawnFanotifier(fanotifierBin string) (*Fanotifier, error) {

// Connect to the spawned fanotifier over stdio
conn: conn.NewClient(notifyR, notifyW, cmd.Process.Pid, 5*time.Second),
closeFunc: func() (allErr error) {
closeFunc: func() error {
var errs []error
if err := notifyR.Close(); err != nil {
allErr = multierror.Append(allErr, err)
errs = append(errs, err)
}
if err := notifyW.Close(); err != nil {
allErr = multierror.Append(allErr, err)
errs = append(errs, err)
}
return
return errors.Join(errs...)
},
}, nil
}
Expand Down
10 changes: 5 additions & 5 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package cache

import (
"bytes"
"errors"
"fmt"
"io"
"os"
Expand All @@ -26,7 +27,6 @@ import (

"github.com/containerd/stargz-snapshotter/util/cacheutil"
"github.com/containerd/stargz-snapshotter/util/namedmutex"
"github.com/hashicorp/go-multierror"
)

const (
Expand Down Expand Up @@ -294,12 +294,12 @@ func (dc *directoryCache) Add(key string, opts ...Option) (Writer, error) {
// Commit the cache contents
c := dc.cachePath(key)
if err := os.MkdirAll(filepath.Dir(c), os.ModePerm); err != nil {
var allErr error
var errs []error
if err := os.Remove(wip.Name()); err != nil {
allErr = multierror.Append(allErr, err)
errs = append(errs, err)
}
return multierror.Append(allErr,
fmt.Errorf("failed to create cache directory %q: %w", c, err))
errs = append(errs, fmt.Errorf("failed to create cache directory %q: %w", c, err))
return errors.Join(errs...)
}
return os.Rename(wip.Name(), c)
},
Expand Down
21 changes: 12 additions & 9 deletions cmd/containerd-stargz-grpc/db/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import (
"github.com/containerd/stargz-snapshotter/estargz"
"github.com/containerd/stargz-snapshotter/metadata"
"github.com/goccy/go-json"
"github.com/hashicorp/go-multierror"
digest "github.com/opencontainers/go-digest"
"github.com/rs/xid"
bolt "go.etcd.io/bbolt"
Expand Down Expand Up @@ -99,7 +98,7 @@ func NewReader(db *bolt.DB, sr *io.SectionReader, opts ...metadata.Option) (meta
rOpts.Telemetry.GetFooterLatency(start)
}

var allErr error
var errs []error
var tocR io.ReadCloser
var decompressor metadata.Decompressor
for _, d := range decompressors {
Expand All @@ -108,7 +107,7 @@ func NewReader(db *bolt.DB, sr *io.SectionReader, opts ...metadata.Option) (meta
maybeTocBytes := footer[:fOffset]
_, tocOffset, tocSize, err := d.ParseFooter(footer[fOffset:])
if err != nil {
allErr = multierror.Append(allErr, err)
errs = append(errs, err)
continue
}
if tocOffset >= 0 && tocSize <= 0 {
Expand All @@ -119,12 +118,14 @@ func NewReader(db *bolt.DB, sr *io.SectionReader, opts ...metadata.Option) (meta
}
tocR, err = decompressTOC(d, sr, tocOffset, tocSize, maybeTocBytes, rOpts)
if err != nil {
allErr = multierror.Append(allErr, err)
errs = append(errs, err)
continue
}
decompressor = d
break
}

allErr := errors.Join(errs...)
if tocR == nil {
if allErr == nil {
return nil, fmt.Errorf("failed to get the reader of TOC: unknown")
Expand Down Expand Up @@ -238,20 +239,22 @@ func (r *reader) init(decompressedR io.Reader, rOpts metadata.Options) (retErr e
if err != nil {
return err
}
closeFunc := func() (closeErr error) {
closeFunc := func() error {
name := f.Name()
var errs []error
if err := f.Close(); err != nil {
closeErr = multierror.Append(closeErr, err)
errs = append(errs, err)
}
if err := os.Remove(name); err != nil {
closeErr = multierror.Append(closeErr, err)
errs = append(errs, err)
}
return
return errors.Join(errs...)
}
defer func() {
if retErr != nil {
if err := closeFunc(); err != nil {
retErr = multierror.Append(retErr, err)
retErr = errors.Join(retErr, err)
return
}
}
}()
Expand Down
16 changes: 9 additions & 7 deletions cmd/ctr-remote/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
gocontext "context"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -36,7 +37,6 @@ import (
"github.com/containerd/containerd/v2/pkg/oci"
gocni "github.com/containerd/go-cni"
"github.com/containerd/log"
"github.com/hashicorp/go-multierror"
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
"github.com/rs/xid"
Expand Down Expand Up @@ -129,13 +129,14 @@ var samplerFlags = []cli.Flag{
func getSpecOpts(clicontext *cli.Context) func(image containerd.Image, rootfs string) (opts []oci.SpecOpts, done func() error, rErr error) {
return func(image containerd.Image, rootfs string) (opts []oci.SpecOpts, done func() error, rErr error) {
var cleanups []func() error
done = func() (allErr error) {
done = func() error {
var errs []error
for i := len(cleanups) - 1; i >= 0; i-- {
if err := cleanups[i](); err != nil {
allErr = multierror.Append(allErr, err)
errs = append(errs, err)
}
}
return
return errors.Join(errs...)
}
defer func() {
if rErr != nil {
Expand Down Expand Up @@ -265,13 +266,14 @@ func withEntrypointArgs(clicontext *cli.Context, image containerd.Image) (oci.Sp

func withCNI(clicontext *cli.Context) (specOpt oci.SpecOpts, done func() error, rErr error) {
var cleanups []func() error
done = func() (allErr error) {
done = func() error {
var errs []error
for i := len(cleanups) - 1; i >= 0; i-- {
if err := cleanups[i](); err != nil {
allErr = multierror.Append(allErr, err)
errs = append(errs, err)
}
}
return
return errors.Join(errs...)
}
defer func() {
if rErr != nil {
Expand Down
2 changes: 0 additions & 2 deletions cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/coreos/go-systemd/v22 v22.5.0
github.com/docker/go-metrics v0.0.1
github.com/goccy/go-json v0.10.4
github.com/hashicorp/go-multierror v1.1.1
github.com/klauspost/compress v1.17.11
github.com/opencontainers/go-digest v1.0.0
github.com/opencontainers/image-spec v1.1.0
Expand Down Expand Up @@ -72,7 +71,6 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hanwen/go-fuse/v2 v2.7.2 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
github.com/imdario/mergo v0.3.13 // indirect
Expand Down
11 changes: 6 additions & 5 deletions fs/reader/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"bytes"
"context"
"crypto/sha256"
"errors"
"fmt"
"io"
"os"
Expand All @@ -38,7 +39,6 @@ import (
"github.com/containerd/stargz-snapshotter/estargz"
commonmetrics "github.com/containerd/stargz-snapshotter/fs/metrics/common"
"github.com/containerd/stargz-snapshotter/metadata"
"github.com/hashicorp/go-multierror"
digest "github.com/opencontainers/go-digest"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
Expand Down Expand Up @@ -390,20 +390,21 @@ func (gr *reader) OpenFile(id uint32) (io.ReaderAt, error) {
}, nil
}

func (gr *reader) Close() (retErr error) {
func (gr *reader) Close() error {
gr.closedMu.Lock()
defer gr.closedMu.Unlock()
if gr.closed {
return nil
}
gr.closed = true
var errs []error
if err := gr.cache.Close(); err != nil {
retErr = multierror.Append(retErr, err)
errs = append(errs, err)
}
if err := gr.r.Close(); err != nil {
retErr = multierror.Append(retErr, err)
errs = append(errs, err)
}
return
return errors.Join(errs...)
}

func (gr *reader) isClosed() bool {
Expand Down
8 changes: 5 additions & 3 deletions fs/remote/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"context"
"crypto/rand"
"crypto/sha256"
"errors"
"fmt"
"io"
"math/big"
Expand All @@ -46,7 +47,6 @@ import (
"github.com/containerd/stargz-snapshotter/fs/config"
commonmetrics "github.com/containerd/stargz-snapshotter/fs/metrics/common"
"github.com/containerd/stargz-snapshotter/fs/source"
"github.com/hashicorp/go-multierror"
rhttp "github.com/hashicorp/go-retryablehttp"
digest "github.com/opencontainers/go-digest"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
Expand Down Expand Up @@ -129,19 +129,21 @@ func (r *Resolver) resolveFetcher(ctx context.Context, hosts source.RegistryHost
minWaitMSec: time.Duration(blobConfig.MinWaitMSec) * time.Millisecond,
maxWaitMSec: time.Duration(blobConfig.MaxWaitMSec) * time.Millisecond,
}
var handlersErr error
var errs []error
for name, p := range r.handlers {
// TODO: allow to configure the selection of readers based on the hostname in refspec
r, size, err := p.Handle(ctx, desc)
if err != nil {
handlersErr = multierror.Append(handlersErr, err)
errs = append(errs, err)
continue
}
log.G(ctx).WithField("handler name", name).WithField("ref", refspec.String()).WithField("digest", desc.Digest).
Debugf("contents is provided by a handler")
return &remoteFetcher{r}, size, nil
}

handlersErr := errors.Join(errs...)

log.G(ctx).WithError(handlersErr).WithField("ref", refspec.String()).WithField("digest", desc.Digest).Debugf("using default handler")
hf, size, err := newHTTPFetcher(ctx, fc)
if err != nil {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da
github.com/hanwen/go-fuse/v2 v2.7.2
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/go-retryablehttp v0.7.7
github.com/klauspost/compress v1.17.11
github.com/moby/sys/mountinfo v0.7.2
Expand Down
12 changes: 6 additions & 6 deletions metadata/testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package testutil

import (
"compress/gzip"
"errors"
"fmt"
"io"
"os"
Expand All @@ -31,7 +32,6 @@ import (
"github.com/containerd/stargz-snapshotter/estargz"
"github.com/containerd/stargz-snapshotter/metadata"
tutil "github.com/containerd/stargz-snapshotter/util/testutil"
"github.com/hashicorp/go-multierror"
"github.com/klauspost/compress/zstd"
digest "github.com/opencontainers/go-digest"
)
Expand Down Expand Up @@ -412,17 +412,17 @@ func newCalledTelemetry() (telemetry *metadata.Telemetry, check func() error) {
GetTocLatency: func(time.Time) { getTocLatencyCalled = true },
DeserializeTocLatency: func(time.Time) { deserializeTocLatencyCalled = true },
}, func() error {
var allErr error
var errs []error
if !getFooterLatencyCalled {
allErr = multierror.Append(allErr, fmt.Errorf("metrics GetFooterLatency isn't called"))
errs = append(errs, fmt.Errorf("metrics GetFooterLatency isn't called"))
}
if !getTocLatencyCalled {
allErr = multierror.Append(allErr, fmt.Errorf("metrics GetTocLatency isn't called"))
errs = append(errs, fmt.Errorf("metrics GetTocLatency isn't called"))
}
if !deserializeTocLatencyCalled {
allErr = multierror.Append(allErr, fmt.Errorf("metrics DeserializeTocLatency isn't called"))
errs = append(errs, fmt.Errorf("metrics DeserializeTocLatency isn't called"))
}
return allErr
return errors.Join(errs...)
}
}

Expand Down
7 changes: 4 additions & 3 deletions service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package service

import (
"context"
"errors"
"fmt"
"path/filepath"

Expand All @@ -33,7 +34,6 @@ import (
"github.com/containerd/stargz-snapshotter/service/resolver"
"github.com/containerd/stargz-snapshotter/snapshot"
snbase "github.com/containerd/stargz-snapshotter/snapshot"
"github.com/hashicorp/go-multierror"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
)

Expand Down Expand Up @@ -136,14 +136,15 @@ func fsRoot(root string) string {

func sources(ps ...source.GetSources) source.GetSources {
return func(labels map[string]string) (source []source.Source, allErr error) {
var errs []error
for _, p := range ps {
src, err := p(labels)
if err == nil {
return src, nil
}
allErr = multierror.Append(allErr, err)
errs = append(errs, err)
}
return
return nil, errors.Join(errs...)
}
}

Expand Down