Skip to content

Commit d72c90c

Browse files
authored
Merge pull request #105 from thin-edge/fix-image-detection
fix: docker image detection compatibility for default repos
2 parents 3a90b0a + f96f5ca commit d72c90c

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

pkg/container/container.go

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -529,27 +529,16 @@ func NormalizeImageRef(imageRef string) string {
529529
// Use credentials function to generate initial credentials
530530
// and call again if the credentials fail which gives the credentials
531531
// helper to invalid its own cache
532-
func (c *ContainerClient) ImagePullWithRetries(ctx context.Context, imageRef string, alwaysPull bool, opts ImagePullOptions) (*image.Summary, error) {
533-
// Check if an image
534-
filterArgs := filters.NewArgs(filters.Arg("reference", imageRef))
535-
536-
// Include full image name of docker.io image as the user can
537-
// provide the short form (docker.io/<image>) which results in the post-pull image check
538-
// to fail. Add the fully qualified docker.io image to the image list filter options to find both
539-
if fullImageRef, ok := ResolveDockerIOImage(imageRef); ok {
540-
filterArgs.Add("reference", fullImageRef)
541-
}
542-
543-
images, err := c.Client.ImageList(ctx, image.ListOptions{
544-
Filters: filterArgs,
545-
})
546-
if err != nil {
547-
return nil, err
548-
}
549-
550-
if len(images) > 0 && !alwaysPull {
551-
slog.Info("Image already exists.", "ref", imageRef, "id", images[0].ID, "tags", images[0].RepoTags)
552-
return &images[0], nil
532+
func (c *ContainerClient) ImagePullWithRetries(ctx context.Context, imageRef string, alwaysPull bool, opts ImagePullOptions) (*types.ImageInspect, error) {
533+
// Check if image exists
534+
// Use ImageInspectWithRaw over ImageList as inspect is able to look up images either with or without
535+
// the repository details making it more compatible between docker and podman
536+
if imageInspect, _, err := c.Client.ImageInspectWithRaw(ctx, imageRef); err != nil {
537+
// Don't fail, just log it and continue
538+
slog.Info("Image does not already exist, trying to pull image.", "response", err)
539+
} else if !alwaysPull {
540+
slog.Info("Image already exists.", "ref", imageRef, "id", imageInspect.ID, "tags", imageInspect.RepoTags)
541+
return &imageInspect, nil
553542
}
554543

555544
result, err := utils.Retry(opts.MaxAttempts, opts.Wait, func(attempt int) (any, error) {
@@ -576,24 +565,19 @@ func (c *ContainerClient) ImagePullWithRetries(ctx context.Context, imageRef str
576565

577566
//
578567
// Check if image is not present
579-
imageList, imageErr := c.Client.ImageList(ctx, image.ListOptions{
580-
Filters: filterArgs,
581-
})
568+
imageInspect, _, imageErr := c.Client.ImageInspectWithRaw(ctx, imageRef)
582569
if imageErr != nil {
570+
slog.Error("No image found after pulling.", "err", imageErr)
583571
return nil, imageErr
584572
}
585-
if len(imageList) == 0 {
586-
slog.Info("No image found after pulling")
587-
return nil, ErrNoImage
588-
}
589-
slog.Info("Image found after pull.", "count", len(imageList))
590-
return &imageList[0], nil
573+
slog.Info("Image found after pull.", "id", imageInspect.ID, "name", imageInspect.RepoTags)
574+
return &imageInspect, nil
591575
})
592576

593577
if err != nil {
594578
return nil, err
595579
}
596-
return result.(*image.Summary), err
580+
return result.(*types.ImageInspect), err
597581
}
598582

599583
//nolint:all

0 commit comments

Comments
 (0)