Skip to content

Commit b870c0d

Browse files
committed
try to catch err no left space
Signed-off-by: Daniil Antoshin <[email protected]> fix Signed-off-by: Daniil Antoshin <[email protected]>
1 parent ec8bceb commit b870c0d

File tree

2 files changed

+74
-2
lines changed

2 files changed

+74
-2
lines changed

images/dvcr-artifact/pkg/errors/errors.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@ limitations under the License.
1616

1717
package errors
1818

19-
import "fmt"
19+
import (
20+
"errors"
21+
"fmt"
22+
"os"
23+
"syscall"
24+
)
2025

2126
type ReasonError interface {
2227
error
@@ -44,3 +49,52 @@ func (e BadImageChecksumError) Reason() string {
4449
func (e BadImageChecksumError) Error() string {
4550
return fmt.Sprintf("%s sum mismatch: %s != %s", e.algorithm, e.expected, e.actual)
4651
}
52+
53+
func NewNoSpaceLeftError(err error) NoSpaceLeftError {
54+
return NoSpaceLeftError{
55+
err: err,
56+
}
57+
}
58+
59+
type NoSpaceLeftError struct {
60+
err error
61+
}
62+
63+
func (e NoSpaceLeftError) Reason() string {
64+
return "NoSpaceLeft"
65+
}
66+
67+
func (e NoSpaceLeftError) Error() string {
68+
return fmt.Sprintf("no space left on device: %v", e.err)
69+
}
70+
71+
func (e NoSpaceLeftError) Unwrap() error {
72+
return e.err
73+
}
74+
75+
func IsNoSpaceLeftError(err error) bool {
76+
if err == nil {
77+
return false
78+
}
79+
80+
var noSpaceErr NoSpaceLeftError
81+
if errors.As(err, &noSpaceErr) {
82+
return true
83+
}
84+
85+
var pathErr *os.PathError
86+
if errors.As(err, &pathErr) {
87+
if pathErr.Err == syscall.ENOSPC {
88+
return true
89+
}
90+
}
91+
92+
var errno syscall.Errno
93+
if errors.As(err, &errno) {
94+
if errno == syscall.ENOSPC {
95+
return true
96+
}
97+
}
98+
99+
return false
100+
}

images/dvcr-artifact/pkg/registry/registry.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import (
3535

3636
"github.com/google/go-containerregistry/pkg/authn"
3737
"github.com/google/go-containerregistry/pkg/name"
38-
"github.com/google/go-containerregistry/pkg/v1"
38+
v1 "github.com/google/go-containerregistry/pkg/v1"
3939
"github.com/google/go-containerregistry/pkg/v1/empty"
4040
"github.com/google/go-containerregistry/pkg/v1/mutate"
4141
"github.com/google/go-containerregistry/pkg/v1/remote"
@@ -261,6 +261,9 @@ func (p DataProcessor) inspectAndStreamSourceImage(
261261
klog.Infoln("Streaming from the source")
262262
doneSize, err := io.Copy(streamWriter, io.TeeReader(sourceImageReader, imageInfoWriter))
263263
if err != nil {
264+
if importerrs.IsNoSpaceLeftError(err) {
265+
return importerrs.NewNoSpaceLeftError(err)
266+
}
264267
return fmt.Errorf("error copying from the source: %w", err)
265268
}
266269

@@ -325,6 +328,9 @@ func (p DataProcessor) uploadLayersAndImage(
325328

326329
klog.Infoln("Uploading layer to registry")
327330
if err := remote.WriteLayer(repo, layer, remoteOpts...); err != nil {
331+
if importerrs.IsNoSpaceLeftError(err) {
332+
return importerrs.NewNoSpaceLeftError(err)
333+
}
328334
return fmt.Errorf("error uploading layer: %w", err)
329335
}
330336
klog.Infoln("Layer uploaded")
@@ -356,6 +362,9 @@ func (p DataProcessor) uploadLayersAndImage(
356362

357363
klog.Infof("Uploading image %q to registry", p.destImageName)
358364
if err = remote.Write(ref, image, remoteOpts...); err != nil {
365+
if importerrs.IsNoSpaceLeftError(err) {
366+
return importerrs.NewNoSpaceLeftError(err)
367+
}
359368
return fmt.Errorf("error uploading image: %w", err)
360369
}
361370

@@ -403,6 +412,9 @@ func getImageInfo(ctx context.Context, sourceReader io.ReadCloser) (ImageInfo, e
403412

404413
uncompressedN, err = io.CopyN(tempImageInfoFile, formatSourceReaders.TopReader(), imageInfoSize)
405414
if err != nil && !errors.Is(err, io.EOF) {
415+
if importerrs.IsNoSpaceLeftError(err) {
416+
return ImageInfo{}, importerrs.NewNoSpaceLeftError(err)
417+
}
406418
return ImageInfo{}, fmt.Errorf("error writing to temp file: %w", err)
407419
}
408420

@@ -430,6 +442,9 @@ func getImageInfo(ctx context.Context, sourceReader io.ReadCloser) (ImageInfo, e
430442
// It's necessary to read everything from the original image to avoid blocking.
431443
_, err = io.Copy(&EmptyWriter{}, sourceReader)
432444
if err != nil {
445+
if importerrs.IsNoSpaceLeftError(err) {
446+
return ImageInfo{}, importerrs.NewNoSpaceLeftError(err)
447+
}
433448
return ImageInfo{}, fmt.Errorf("error copying to nowhere: %w", err)
434449
}
435450

@@ -458,6 +473,9 @@ func getImageInfo(ctx context.Context, sourceReader io.ReadCloser) (ImageInfo, e
458473
// Count uncompressed size of source image.
459474
n, err := io.Copy(&EmptyWriter{}, formatSourceReaders.TopReader())
460475
if err != nil {
476+
if importerrs.IsNoSpaceLeftError(err) {
477+
return ImageInfo{}, importerrs.NewNoSpaceLeftError(err)
478+
}
461479
return ImageInfo{}, fmt.Errorf("error copying to nowhere: %w", err)
462480
}
463481

0 commit comments

Comments
 (0)