Skip to content

Commit ac406f3

Browse files
committed
fixes ImagePruner error wrapping
This removes ErrImage wrapping / detection from the acceptable error path in favor of directly inspecting the errors returned. However, we keep ErrImage wrapping in place so that we can ensure that the image name is associated with the error.
1 parent db39958 commit ac406f3

File tree

2 files changed

+25
-49
lines changed

2 files changed

+25
-49
lines changed

pkg/controller/build/imagepruner/errors.go

Lines changed: 12 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,6 @@ func IsImageNotFoundErr(err error) bool {
4545
return false
4646
}
4747

48-
if !isErrImage(err) {
49-
return false
50-
}
51-
5248
if isMaskedHTTP404(err) {
5349
return true
5450
}
@@ -68,10 +64,6 @@ func IsAccessDeniedErr(err error) bool {
6864
return false
6965
}
7066

71-
if !isErrImage(err) {
72-
return false
73-
}
74-
7567
if isAccessDeniedErrorCode(err) {
7668
return true
7769
}
@@ -225,61 +217,35 @@ func isTolerableUnexpectedHTTPStatusError(err error) bool {
225217

226218
// ErrImage holds and wraps an error related to a specific image.
227219
type ErrImage struct {
228-
msg string
229-
img string
230-
err error
231-
}
232-
233-
// newErrImageWithMessage constructs a new ErrImage instance with a custom message,
234-
// image pullspec, and wrapped error.
235-
func newErrImageWithMessage(msg, img string, err error) error {
236-
return &ErrImage{msg: msg, img: img, err: err}
220+
image string
221+
err error
237222
}
238223

239224
// newErrImage constructs a new ErrImage instance with an image pullspec and
240-
// wrapped error, without a custom message.
225+
// wrapped error.
241226
func newErrImage(img string, err error) error {
242-
return &ErrImage{img: img, err: err}
227+
return &ErrImage{image: img, err: err}
243228
}
244229

245-
// Image returns the image pullspec that caused the error.
230+
// Returns the image embedded within the ErrImage struct.
246231
func (e *ErrImage) Image() string {
247-
return e.img
232+
return e.image
248233
}
249234

250235
// Error implements the error interface, providing a formatted error string
251-
// including the message (if present), image (if present), and the wrapped error's string.
236+
// including the image (if present), and the wrapped error's string.
252237
func (e *ErrImage) Error() string {
253-
if e.msg != "" && e.img != "" {
254-
// If both the message and image are not empty, include both.
255-
return fmt.Sprintf("%s: image %q: %s", e.msg, e.img, e.err.Error())
238+
// If the image is defined and not contained within the underlying error, inject it here.
239+
if e.image != "" && !strings.Contains(e.err.Error(), e.image) {
240+
return fmt.Sprintf("error occurred with image %q: %s", e.image, e.err.Error())
256241
}
257242

258-
if e.msg == "" && e.img != "" {
259-
// If the message is empty and the image is not, only include the image.
260-
return fmt.Sprintf("image %q: %s", e.img, e.err.Error())
261-
}
262-
263-
// If neither the message nor the image is populated, just return the error
264-
// string as-is.
243+
// If the image is undefined or is already present in the error, return the
244+
// error as-is.
265245
return e.err.Error()
266246
}
267247

268248
// Unwrap implements the Unwrap interface, allowing the nested error to be surfaced.
269249
func (e *ErrImage) Unwrap() error {
270250
return e.err
271251
}
272-
273-
// isErrImage determines whether the given error is an instance of the ErrImage
274-
// type defined above.
275-
func isErrImage(err error) bool {
276-
if err == nil {
277-
return false
278-
}
279-
280-
// Any errors related to the actual image registry query are wrapped in an
281-
// ErrImage instance. This allows us to easily identify intolerable errors
282-
// such as not being able to write the authfile or certs, etc.
283-
var errImage *ErrImage
284-
return errors.As(err, &errImage)
285-
}

pkg/controller/build/imagepruner/imageinspect.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,21 @@ func NewImageInspectorDeleter() ImageInspectorDeleter {
4848

4949
// ImageInspect uses the provided system context to inspect the provided image pullspec.
5050
func (i *imageInspectorImpl) ImageInspect(ctx context.Context, sysCtx *types.SystemContext, image string) (*types.ImageInspectInfo, *digest.Digest, error) {
51-
return imageInspect(ctx, sysCtx, image)
51+
info, digest, err := imageInspect(ctx, sysCtx, image)
52+
if err == nil {
53+
return info, digest, nil
54+
}
55+
56+
return nil, nil, newErrImage(image, err)
5257
}
5358

5459
// DeleteImage uses the provided system context to delete the provided image pullspec.
5560
func (i *imageInspectorImpl) DeleteImage(ctx context.Context, sysCtx *types.SystemContext, image string) error {
56-
return deleteImage(ctx, sysCtx, image)
61+
if err := deleteImage(ctx, sysCtx, image); err != nil {
62+
return newErrImage(image, err)
63+
}
64+
65+
return nil
5766
}
5867

5968
// deleteImage attempts to delete the specified image with retries,
@@ -73,8 +82,9 @@ func deleteImage(ctx context.Context, sysCtx *types.SystemContext, imageName str
7382
if err := retry.IfNecessary(ctx, func() error {
7483
return ref.DeleteImage(ctx, sysCtx)
7584
}, &retryOpts); err != nil {
76-
return newErrImage(imageName, err)
85+
return err
7786
}
87+
7888
return nil
7989
}
8090

0 commit comments

Comments
 (0)