Skip to content

Commit f5dd997

Browse files
authored
Merge pull request #326 from thaJeztah/stdlib_errors
replace uses of pkg/errors with stdlib
2 parents 0b27271 + 6659fd6 commit f5dd997

File tree

16 files changed

+171
-185
lines changed

16 files changed

+171
-185
lines changed

cmd/convertor/builder/builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"crypto/tls"
2222
"crypto/x509"
2323
"encoding/json"
24+
"errors"
2425
"fmt"
2526
"io"
2627
"net"
@@ -42,7 +43,6 @@ import (
4243
"github.com/containerd/platforms"
4344
"github.com/opencontainers/go-digest"
4445
v1 "github.com/opencontainers/image-spec/specs-go/v1"
45-
"github.com/pkg/errors"
4646
"github.com/sirupsen/logrus"
4747
"golang.org/x/sync/errgroup"
4848
)
@@ -468,7 +468,7 @@ func (b *overlaybdBuilder) Build(ctx context.Context) (v1.Descriptor, error) {
468468

469469
targetDesc, err := b.engine.UploadImage(ctx)
470470
if err != nil {
471-
return v1.Descriptor{}, errors.Wrap(err, "failed to upload manifest or config")
471+
return v1.Descriptor{}, fmt.Errorf("failed to upload manifest or config: %w", err)
472472
}
473473
b.engine.StoreConvertedManifestDetails(ctx)
474474
logrus.Info("convert finished")

cmd/convertor/builder/builder_engine.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"github.com/containerd/log"
3131
"github.com/opencontainers/go-digest"
3232
specs "github.com/opencontainers/image-spec/specs-go/v1"
33-
"github.com/pkg/errors"
3433
)
3534

3635
type BuilderEngineType int
@@ -121,11 +120,11 @@ type builderEngineBase struct {
121120
func (e *builderEngineBase) isGzipLayer(ctx context.Context, idx int) (bool, error) {
122121
rc, err := e.fetcher.Fetch(ctx, e.manifest.Layers[idx])
123122
if err != nil {
124-
return false, errors.Wrapf(err, "isGzipLayer: failed to open layer %d", idx)
123+
return false, fmt.Errorf("isGzipLayer: failed to open layer %d: %w", idx, err)
125124
}
126125
drc, err := compression.DecompressStream(rc)
127126
if err != nil {
128-
return false, errors.Wrapf(err, "isGzipLayer: failed to open decompress stream for layer %d", idx)
127+
return false, fmt.Errorf("isGzipLayer: failed to open decompress stream for layer %d: %w", idx, err)
129128
}
130129
compress := drc.GetCompression()
131130
switch compress {
@@ -182,7 +181,7 @@ func (e *builderEngineBase) uploadManifestAndConfig(ctx context.Context) (specs.
182181
}
183182
if !e.noUpload {
184183
if err = uploadBytes(ctx, e.pusher, e.manifest.Config, cbuf); err != nil {
185-
return specs.Descriptor{}, errors.Wrapf(err, "failed to upload config")
184+
return specs.Descriptor{}, fmt.Errorf("failed to upload config: %w", err)
186185
}
187186
log.G(ctx).Infof("config uploaded")
188187
}
@@ -206,7 +205,7 @@ func (e *builderEngineBase) uploadManifestAndConfig(ctx context.Context) (specs.
206205
}
207206
if !e.noUpload {
208207
if err = uploadBytes(ctx, e.pusher, manifestDesc, cbuf); err != nil {
209-
return specs.Descriptor{}, errors.Wrapf(err, "failed to upload manifest")
208+
return specs.Descriptor{}, fmt.Errorf("failed to upload manifest: %w", err)
210209
}
211210
e.outputDesc = manifestDesc
212211
log.G(ctx).Infof("manifest uploaded, %s", manifestDesc.Digest)
@@ -224,19 +223,19 @@ func (e *builderEngineBase) uploadManifestAndConfig(ctx context.Context) (specs.
224223
func getBuilderEngineBase(ctx context.Context, resolver remotes.Resolver, ref, targetRef string) (*builderEngineBase, error) {
225224
_, desc, err := resolver.Resolve(ctx, ref)
226225
if err != nil {
227-
return nil, errors.Wrapf(err, "failed to resolve reference %q", ref)
226+
return nil, fmt.Errorf("failed to resolve reference %q: %w", ref, err)
228227
}
229228
fetcher, err := resolver.Fetcher(ctx, ref)
230229
if err != nil {
231-
return nil, errors.Wrapf(err, "failed to get fetcher for %q", ref)
230+
return nil, fmt.Errorf("failed to get fetcher for %q: %w", ref, err)
232231
}
233232
pusher, err := resolver.Pusher(ctx, targetRef)
234233
if err != nil {
235-
return nil, errors.Wrapf(err, "failed to get pusher for %q", targetRef)
234+
return nil, fmt.Errorf("failed to get pusher for %q: %w", targetRef, err)
236235
}
237236
manifest, config, err := fetchManifestAndConfig(ctx, fetcher, desc)
238237
if err != nil {
239-
return nil, errors.Wrap(err, "failed to fetch manifest and config")
238+
return nil, fmt.Errorf("failed to fetch manifest and config: %w", err)
240239
}
241240
return &builderEngineBase{
242241
resolver: resolver,

cmd/convertor/builder/builder_utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"context"
2323
"crypto/sha256"
2424
"encoding/json"
25+
"errors"
2526
"fmt"
2627
"io"
2728
"os"
@@ -37,7 +38,6 @@ import (
3738
"github.com/containerd/platforms"
3839
"github.com/opencontainers/go-digest"
3940
specs "github.com/opencontainers/image-spec/specs-go/v1"
40-
"github.com/pkg/errors"
4141
"github.com/sirupsen/logrus"
4242

4343
t "github.com/containerd/accelerated-container-image/pkg/types"
@@ -252,19 +252,19 @@ func tagPreviouslyConvertedManifest(ctx context.Context, pusher remotes.Pusher,
252252
func buildArchiveFromFiles(ctx context.Context, target string, compress compression.Compression, files ...string) error {
253253
archive, err := os.Create(target)
254254
if err != nil {
255-
return errors.Wrapf(err, "failed to create tgz file: %q", target)
255+
return fmt.Errorf("failed to create tgz file: %q: %w", target, err)
256256
}
257257
defer archive.Close()
258258
fzip, err := compression.CompressStream(archive, compress)
259259
if err != nil {
260-
return errors.Wrapf(err, "failed to create compression %v", compress)
260+
return fmt.Errorf("failed to create compression %v: %w", compress, err)
261261
}
262262
defer fzip.Close()
263263
ftar := tar.NewWriter(fzip)
264264
defer ftar.Close()
265265
for _, file := range files {
266266
if err := addFileToArchive(ctx, ftar, file); err != nil {
267-
return errors.Wrapf(err, "failed to add file %q to archive %q", file, target)
267+
return fmt.Errorf("failed to add file %q to archive %q: %w", file, target, err)
268268
}
269269
}
270270
return nil
@@ -276,7 +276,7 @@ func addFileToArchive(ctx context.Context, ftar *tar.Writer, filepath string) er
276276
if errors.Is(err, os.ErrNotExist) {
277277
return nil
278278
}
279-
return errors.Wrapf(err, "failed to open file: %q", filepath)
279+
return fmt.Errorf("failed to open file: %q: %w", filepath, err)
280280
}
281281
defer file.Close()
282282
info, err := file.Stat()

cmd/convertor/builder/overlaybd_builder.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"bufio"
2222
"context"
2323
"encoding/json"
24+
"errors"
2425
"fmt"
2526
"io"
2627
"os"
@@ -34,7 +35,6 @@ import (
3435
"github.com/opencontainers/go-digest"
3536
"github.com/opencontainers/image-spec/identity"
3637
specs "github.com/opencontainers/image-spec/specs-go/v1"
37-
"github.com/pkg/errors"
3838
"github.com/sirupsen/logrus"
3939
)
4040

@@ -162,7 +162,7 @@ func (e *overlaybdBuilderEngine) UploadLayer(ctx context.Context, idx int) error
162162
layerDir := e.getLayerDir(idx)
163163
desc, err := getFileDesc(path.Join(layerDir, commitFile), false)
164164
if err != nil {
165-
return errors.Wrapf(err, "failed to get descriptor for layer %d", idx)
165+
return fmt.Errorf("failed to get descriptor for layer %d: %w", idx, err)
166166
}
167167
if e.overlaybdLayers[idx].fromDedup {
168168
// validate that the layer digests match if the layer is from dedup
@@ -178,7 +178,7 @@ func (e *overlaybdBuilderEngine) UploadLayer(ctx context.Context, idx int) error
178178
}
179179
if !e.noUpload {
180180
if err := uploadBlob(ctx, e.pusher, path.Join(layerDir, commitFile), desc); err != nil {
181-
return errors.Wrapf(err, "failed to upload layer %d", idx)
181+
return fmt.Errorf("failed to upload layer %d: %w", idx, err)
182182
}
183183
}
184184
e.overlaybdLayers[idx].desc = desc
@@ -406,7 +406,7 @@ func (e *overlaybdBuilderEngine) DownloadConvertedLayer(ctx context.Context, idx
406406
if err != nil {
407407
// We should remove the commit file if the download failed to allow for fallback conversion
408408
os.Remove(targetFile) // Remove any file that may have failed to download
409-
return errors.Wrapf(err, "failed to download layer %d", idx)
409+
return fmt.Errorf("failed to download layer %d: %w", idx, err)
410410
}
411411
// Mark that this layer is from dedup
412412
e.overlaybdLayers[idx].fromDedup = true
@@ -425,19 +425,19 @@ func (e *overlaybdBuilderEngine) uploadBaseLayer(ctx context.Context) (specs.Des
425425
tarFile := path.Join(e.workDir, "ext4_64.tar")
426426
fdes, err := os.Create(tarFile)
427427
if err != nil {
428-
return specs.Descriptor{}, errors.Wrapf(err, "failed to create file %s", tarFile)
428+
return specs.Descriptor{}, fmt.Errorf("failed to create file %s: %w", tarFile, err)
429429
}
430430
digester := digest.Canonical.Digester()
431431
countWriter := &writeCountWrapper{w: io.MultiWriter(fdes, digester.Hash())}
432432
tarWriter := tar.NewWriter(countWriter)
433433

434434
fsrc, err := os.Open(overlaybdBaseLayer)
435435
if err != nil {
436-
return specs.Descriptor{}, errors.Wrapf(err, "failed to open %s", overlaybdBaseLayer)
436+
return specs.Descriptor{}, fmt.Errorf("failed to open %s: %w", overlaybdBaseLayer, err)
437437
}
438438
fstat, err := os.Stat(overlaybdBaseLayer)
439439
if err != nil {
440-
return specs.Descriptor{}, errors.Wrapf(err, "failed to get info of %s", overlaybdBaseLayer)
440+
return specs.Descriptor{}, fmt.Errorf("failed to get info of %s: %w", overlaybdBaseLayer, err)
441441
}
442442

443443
if err := tarWriter.WriteHeader(&tar.Header{
@@ -446,13 +446,13 @@ func (e *overlaybdBuilderEngine) uploadBaseLayer(ctx context.Context) (specs.Des
446446
Size: fstat.Size(),
447447
Typeflag: tar.TypeReg,
448448
}); err != nil {
449-
return specs.Descriptor{}, errors.Wrapf(err, "failed to write tar header")
449+
return specs.Descriptor{}, fmt.Errorf("failed to write tar header: %w", err)
450450
}
451451
if _, err := io.Copy(tarWriter, bufio.NewReader(fsrc)); err != nil {
452-
return specs.Descriptor{}, errors.Wrapf(err, "failed to copy IO")
452+
return specs.Descriptor{}, fmt.Errorf("failed to copy IO: %w", err)
453453
}
454454
if err = tarWriter.Close(); err != nil {
455-
return specs.Descriptor{}, errors.Wrapf(err, "failed to close tar file")
455+
return specs.Descriptor{}, fmt.Errorf("failed to close tar file: %w", err)
456456
}
457457

458458
baseDesc := specs.Descriptor{
@@ -467,7 +467,7 @@ func (e *overlaybdBuilderEngine) uploadBaseLayer(ctx context.Context) (specs.Des
467467
}
468468
if !e.noUpload {
469469
if err = uploadBlob(ctx, e.pusher, tarFile, baseDesc); err != nil {
470-
return specs.Descriptor{}, errors.Wrapf(err, "failed to upload baselayer")
470+
return specs.Descriptor{}, fmt.Errorf("failed to upload baselayer: %w", err)
471471
}
472472
logrus.Infof("baselayer uploaded")
473473
}

cmd/convertor/builder/turboOCI_builder.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import (
3232
"github.com/opencontainers/go-digest"
3333
specs "github.com/opencontainers/image-spec/specs-go/v1"
3434

35-
"github.com/pkg/errors"
3635
"github.com/sirupsen/logrus"
3736
)
3837

@@ -115,7 +114,7 @@ func (e *turboOCIBuilderEngine) BuildLayer(ctx context.Context, idx int) error {
115114
return err
116115
}
117116
if err := e.createIdentifier(idx); err != nil {
118-
return errors.Wrapf(err, "failed to create identifier %q", tociIdentifier)
117+
return fmt.Errorf("failed to create identifier %q: %w", tociIdentifier, err)
119118
}
120119
files := []string{
121120
path.Join(layerDir, fsMetaFile),
@@ -127,7 +126,7 @@ func (e *turboOCIBuilderEngine) BuildLayer(ctx context.Context, idx int) error {
127126
files = append(files, gzipIndexPath)
128127
}
129128
if err := buildArchiveFromFiles(ctx, path.Join(layerDir, tociLayerTar), compression.Gzip, files...); err != nil {
130-
return errors.Wrapf(err, "failed to create turboOCIv1 archive for layer %d", idx)
129+
return fmt.Errorf("failed to create turboOCIv1 archive for layer %d: %w", idx, err)
131130
}
132131
e.overlaybdConfig.Lowers = append(e.overlaybdConfig.Lowers, sn.OverlayBDBSConfigLower{
133132
TargetFile: path.Join(layerDir, "layer.tar"),
@@ -144,7 +143,7 @@ func (e *turboOCIBuilderEngine) UploadLayer(ctx context.Context, idx int) error
144143
layerDir := e.getLayerDir(idx)
145144
desc, err := getFileDesc(path.Join(layerDir, tociLayerTar), false)
146145
if err != nil {
147-
return errors.Wrapf(err, "failed to get descriptor for layer %d", idx)
146+
return fmt.Errorf("failed to get descriptor for layer %d: %w", idx, err)
148147
}
149148
desc.MediaType = e.mediaTypeImageLayerGzip()
150149
desc.Annotations = map[string]string{
@@ -169,7 +168,7 @@ func (e *turboOCIBuilderEngine) UploadLayer(ctx context.Context, idx int) error
169168
}
170169
desc.Annotations[label.TurboOCIMediaType] = targetMediaType
171170
if err := uploadBlob(ctx, e.pusher, path.Join(layerDir, tociLayerTar), desc); err != nil {
172-
return errors.Wrapf(err, "failed to upload layer %d", idx)
171+
return fmt.Errorf("failed to upload layer %d: %w", idx, err)
173172
}
174173
e.tociLayers[idx] = desc
175174
return nil
@@ -180,7 +179,7 @@ func (e *turboOCIBuilderEngine) UploadImage(ctx context.Context) (specs.Descript
180179
layerDir := e.getLayerDir(idx)
181180
uncompress, err := getFileDesc(path.Join(layerDir, tociLayerTar), true)
182181
if err != nil {
183-
return specs.Descriptor{}, errors.Wrapf(err, "failed to get uncompressed descriptor for layer %d", idx)
182+
return specs.Descriptor{}, fmt.Errorf("failed to get uncompressed descriptor for layer %d: %w", idx, err)
184183
}
185184
e.manifest.Layers[idx] = e.tociLayers[idx]
186185
e.config.RootFS.DiffIDs[idx] = uncompress.Digest
@@ -197,7 +196,7 @@ func (e *turboOCIBuilderEngine) UploadImage(ctx context.Context) (specs.Descript
197196
}
198197
if !e.mkfs {
199198
if err := uploadBlob(ctx, e.pusher, overlaybdBaseLayer, baseDesc); err != nil {
200-
return specs.Descriptor{}, errors.Wrapf(err, "failed to upload baselayer %q", overlaybdBaseLayer)
199+
return specs.Descriptor{}, fmt.Errorf("failed to upload baselayer %q: %w", overlaybdBaseLayer, err)
201200
}
202201
e.manifest.Layers = append([]specs.Descriptor{baseDesc}, e.manifest.Layers...)
203202
e.config.RootFS.DiffIDs = append([]digest.Digest{baseDesc.Digest}, e.config.RootFS.DiffIDs...)
@@ -260,7 +259,7 @@ func (e *turboOCIBuilderEngine) createIdentifier(idx int) error {
260259
targetFile := path.Join(e.getLayerDir(idx), tociIdentifier)
261260
file, err := os.Create(targetFile)
262261
if err != nil {
263-
return errors.Wrapf(err, "failed to create identifier file %q", tociIdentifier)
262+
return fmt.Errorf("failed to create identifier file %q: %w", tociIdentifier, err)
264263
}
265264
defer file.Close()
266265
return nil

cmd/ctr/overlaybd_conv.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package main
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"time"
2223

@@ -26,7 +27,6 @@ import (
2627
"github.com/containerd/containerd/v2/core/images/converter"
2728
"github.com/containerd/containerd/v2/core/leases"
2829
_ "github.com/go-sql-driver/mysql"
29-
"github.com/pkg/errors"
3030
"github.com/urfave/cli/v2"
3131
)
3232

@@ -83,7 +83,7 @@ var convertCommand = &cli.Command{
8383
leases.WithExpiration(1*time.Hour),
8484
)
8585
if err != nil {
86-
return errors.Wrap(err, "failed to create lease")
86+
return fmt.Errorf("failed to create lease: %w", err)
8787
}
8888
defer done(ctx)
8989

0 commit comments

Comments
 (0)