Skip to content

Commit

Permalink
Merge pull request #1921 from djdongjin/remove-hashicorp-multierror
Browse files Browse the repository at this point in the history
replace harshicorp/multierror with errors.Join
  • Loading branch information
ktock authored Jan 6, 2025
2 parents 394fdcd + d81ec67 commit d8470f9
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 46 deletions.
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

0 comments on commit d8470f9

Please sign in to comment.