ImageService.GetImage(): pass context

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
Signed-off-by: Djordje Lukic <djordje.lukic@docker.com>
This commit is contained in:
Sebastiaan van Stijn 2022-08-09 14:42:50 +02:00 committed by Djordje Lukic
parent 9dab00a76e
commit 779a5b3029
18 changed files with 64 additions and 32 deletions

View file

@ -24,7 +24,7 @@ type imageBackend interface {
ImageDelete(ctx context.Context, imageRef string, force, prune bool) ([]types.ImageDeleteResponseItem, error)
ImageHistory(imageName string) ([]*image.HistoryResponseItem, error)
Images(ctx context.Context, opts types.ImageListOptions) ([]*types.ImageSummary, error)
GetImage(refOrID string, options image.GetImageOpts) (*dockerimage.Image, error)
GetImage(ctx context.Context, refOrID string, options image.GetImageOpts) (*dockerimage.Image, error)
TagImage(imageName, repository, tag string) (string, error)
ImagesPrune(ctx context.Context, pruneFilters filters.Args) (*types.ImagesPruneReport, error)
}

View file

@ -193,7 +193,7 @@ func (ir *imageRouter) deleteImages(ctx context.Context, w http.ResponseWriter,
}
func (ir *imageRouter) getImagesByName(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
img, err := ir.backend.GetImage(vars["name"], opts.GetImageOpts{})
img, err := ir.backend.GetImage(ctx, vars["name"], opts.GetImageOpts{})
if err != nil {
return err
}

View file

@ -77,5 +77,5 @@ type VolumeBackend interface {
type ImageBackend interface {
PullImage(ctx context.Context, image, tag string, platform *specs.Platform, metaHeaders map[string][]string, authConfig *registry.AuthConfig, outStream io.Writer) error
GetRepository(context.Context, reference.Named, *registry.AuthConfig) (distribution.Repository, error)
GetImage(refOrID string, options opts.GetImageOpts) (*image.Image, error)
GetImage(ctx context.Context, refOrID string, options opts.GetImageOpts) (*image.Image, error)
}

View file

@ -76,7 +76,10 @@ func (c *containerAdapter) pullImage(ctx context.Context) error {
named, err := reference.ParseNormalizedNamed(spec.Image)
if err == nil {
if _, ok := named.(reference.Canonical); ok {
_, err := c.imageBackend.GetImage(spec.Image, imagetypes.GetImageOpts{})
_, err := c.imageBackend.GetImage(ctx, spec.Image, imagetypes.GetImageOpts{})
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return err
}
if err == nil {
return nil
}

View file

@ -1,11 +1,13 @@
package containerd
import (
"context"
imagetype "github.com/docker/docker/api/types/image"
"github.com/docker/docker/image"
)
// GetImage returns an image corresponding to the image referred to by refOrID.
func (i *ImageService) GetImage(refOrID string, options imagetype.GetImageOpts) (retImg *image.Image, retErr error) {
func (i *ImageService) GetImage(ctx context.Context, refOrID string, options imagetype.GetImageOpts) (retImg *image.Image, retErr error) {
panic("not implemented")
}

View file

@ -1,6 +1,7 @@
package daemon // import "github.com/docker/docker/daemon"
import (
"context"
"fmt"
"net"
"runtime"
@ -58,6 +59,7 @@ func (daemon *Daemon) ContainerCreateIgnoreImagesArgsEscaped(params types.Contai
}
func (daemon *Daemon) containerCreate(opts createOpts) (containertypes.CreateResponse, error) {
ctx := context.TODO()
start := time.Now()
if opts.params.Config == nil {
return containertypes.CreateResponse{}, errdefs.InvalidParameter(errors.New("Config cannot be empty in order to create a container"))
@ -69,7 +71,11 @@ func (daemon *Daemon) containerCreate(opts createOpts) (containertypes.CreateRes
}
if opts.params.Platform == nil && opts.params.Config.Image != "" {
if img, _ := daemon.imageService.GetImage(opts.params.Config.Image, imagetypes.GetImageOpts{Platform: opts.params.Platform}); img != nil {
img, err := daemon.imageService.GetImage(ctx, opts.params.Config.Image, imagetypes.GetImageOpts{Platform: opts.params.Platform})
if err != nil {
return containertypes.CreateResponse{}, err
}
if img != nil {
p := maximumSpec()
imgPlat := v1.Platform{
OS: img.OS,
@ -111,6 +117,7 @@ func (daemon *Daemon) containerCreate(opts createOpts) (containertypes.CreateRes
// Create creates a new container from the given configuration with a given name.
func (daemon *Daemon) create(opts createOpts) (retC *container.Container, retErr error) {
ctx := context.TODO()
var (
ctr *container.Container
img *image.Image
@ -120,7 +127,7 @@ func (daemon *Daemon) create(opts createOpts) (retC *container.Container, retErr
)
if opts.params.Config.Image != "" {
img, err = daemon.imageService.GetImage(opts.params.Config.Image, imagetypes.GetImageOpts{Platform: opts.params.Platform})
img, err = daemon.imageService.GetImage(ctx, opts.params.Config.Image, imagetypes.GetImageOpts{Platform: opts.params.Platform})
if err != nil {
return nil, err
}

View file

@ -40,7 +40,7 @@ type ImageService interface {
ImportImage(src string, repository string, platform *v1.Platform, tag string, msg string, inConfig io.ReadCloser, outStream io.Writer, changes []string) error
TagImage(imageName, repository, tag string) (string, error)
TagImageWithReference(imageID image.ID, newTag reference.Named) error
GetImage(refOrID string, options imagetype.GetImageOpts) (*image.Image, error)
GetImage(ctx context.Context, refOrID string, options imagetype.GetImageOpts) (*image.Image, error)
ImageHistory(name string) ([]*imagetype.HistoryResponseItem, error)
CommitImage(c backend.CommitConfig) (image.ID, error)
SquashImage(id, parent string) (string, error)

View file

@ -1,6 +1,8 @@
package images // import "github.com/docker/docker/daemon/images"
import (
"context"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/builder"
"github.com/docker/docker/image/cache"
@ -9,6 +11,7 @@ import (
// MakeImageCache creates a stateful image cache.
func (i *ImageService) MakeImageCache(sourceRefs []string) builder.ImageCache {
ctx := context.TODO()
if len(sourceRefs) == 0 {
return cache.NewLocal(i.imageStore)
}
@ -16,7 +19,7 @@ func (i *ImageService) MakeImageCache(sourceRefs []string) builder.ImageCache {
cache := cache.New(i.imageStore)
for _, ref := range sourceRefs {
img, err := i.GetImage(ref, imagetypes.GetImageOpts{})
img, err := i.GetImage(ctx, ref, imagetypes.GetImageOpts{})
if err != nil {
logrus.Warnf("Could not look up %s for cache resolution, skipping: %+v", ref, err)
continue

View file

@ -45,13 +45,13 @@ type manifest struct {
Config specs.Descriptor `json:"config"`
}
func (i *ImageService) manifestMatchesPlatform(ctx context.Context, img *image.Image, platform specs.Platform) bool {
func (i *ImageService) manifestMatchesPlatform(ctx context.Context, img *image.Image, platform specs.Platform) (bool, error) {
logger := logrus.WithField("image", img.ID).WithField("desiredPlatform", platforms.Format(platform))
ls, leaseErr := i.leases.ListResources(ctx, leases.Lease{ID: imageKey(img.ID().Digest())})
if leaseErr != nil {
logger.WithError(leaseErr).Error("Error looking up image leases")
return false
return false, leaseErr
}
// Note we are comparing against manifest lists here, which we expect to always have a CPU variant set (where applicable).
@ -137,19 +137,18 @@ func (i *ImageService) manifestMatchesPlatform(ctx context.Context, img *image.I
if m.Config.Digest == img.ID().Digest() {
logger.WithField("manifestDigest", md.Digest).Debug("Found matching manifest for image")
return true
return true, nil
}
logger.WithField("otherDigest", md.Digest).Debug("Skipping non-matching manifest")
}
}
return false
return false, nil
}
// GetImage returns an image corresponding to the image referred to by refOrID.
func (i *ImageService) GetImage(refOrID string, options imagetypes.GetImageOpts) (retImg *image.Image, retErr error) {
ctx := context.TODO()
func (i *ImageService) GetImage(ctx context.Context, refOrID string, options imagetypes.GetImageOpts) (retImg *image.Image, retErr error) {
defer func() {
if retErr != nil || retImg == nil || options.Platform == nil {
return
@ -168,7 +167,9 @@ func (i *ImageService) GetImage(refOrID string, options imagetypes.GetImageOpts)
}
// In some cases the image config can actually be wrong (e.g. classic `docker build` may not handle `--platform` correctly)
// So we'll look up the manifest list that coresponds to this imaage to check if at least the manifest list says it is the correct image.
if i.manifestMatchesPlatform(ctx, retImg, p) {
var matches bool
matches, retErr = i.manifestMatchesPlatform(ctx, retImg, p)
if matches || retErr != nil {
return
}

View file

@ -168,7 +168,7 @@ func (i *ImageService) pullForBuilder(ctx context.Context, name string, authConf
return nil, err
}
img, err := i.GetImage(name, imagetypes.GetImageOpts{Platform: platform})
img, err := i.GetImage(ctx, name, imagetypes.GetImageOpts{Platform: platform})
if errdefs.IsNotFound(err) && img != nil && platform != nil {
imgPlat := specs.Platform{
OS: img.OS,
@ -212,11 +212,13 @@ func (i *ImageService) GetImageAndReleasableLayer(ctx context.Context, refOrID s
}
if opts.PullOption != backend.PullOptionForcePull {
img, err := i.GetImage(refOrID, imagetypes.GetImageOpts{Platform: opts.Platform})
img, err := i.GetImage(ctx, refOrID, imagetypes.GetImageOpts{Platform: opts.Platform})
if err != nil && opts.PullOption == backend.PullOptionNoPull {
return nil, nil, err
}
// TODO: shouldn't we error out if error is different from "not found" ?
if err != nil && !errdefs.IsNotFound(err) {
return nil, nil, err
}
if img != nil {
if !system.IsOSSupported(img.OperatingSystem()) {
return nil, nil, system.ErrNotSupportedOperatingSystem

View file

@ -64,7 +64,7 @@ func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force,
start := time.Now()
records := []types.ImageDeleteResponseItem{}
img, err := i.GetImage(imageRef, imagetypes.GetImageOpts{})
img, err := i.GetImage(ctx, imageRef, imagetypes.GetImageOpts{})
if err != nil {
return nil, err
}

View file

@ -1,6 +1,8 @@
package images // import "github.com/docker/docker/daemon/images"
import (
"context"
"github.com/docker/docker/api/types/events"
imagetypes "github.com/docker/docker/api/types/image"
)
@ -12,7 +14,8 @@ func (i *ImageService) LogImageEvent(imageID, refName, action string) {
// LogImageEventWithAttributes generates an event related to an image with specific given attributes.
func (i *ImageService) LogImageEventWithAttributes(imageID, refName, action string, attributes map[string]string) {
img, err := i.GetImage(imageID, imagetypes.GetImageOpts{})
ctx := context.TODO()
img, err := i.GetImage(ctx, imageID, imagetypes.GetImageOpts{})
if err == nil && img.Config != nil {
// image has not been removed yet.
// it could be missing if the event is `delete`.

View file

@ -1,6 +1,7 @@
package images // import "github.com/docker/docker/daemon/images"
import (
"context"
"fmt"
"time"
@ -12,8 +13,9 @@ import (
// ImageHistory returns a slice of ImageHistory structures for the specified image
// name by walking the image lineage.
func (i *ImageService) ImageHistory(name string) ([]*image.HistoryResponseItem, error) {
ctx := context.TODO()
start := time.Now()
img, err := i.GetImage(name, image.GetImageOpts{})
img, err := i.GetImage(ctx, name, image.GetImageOpts{})
if err != nil {
return nil, err
}
@ -69,7 +71,7 @@ func (i *ImageService) ImageHistory(name string) ([]*image.HistoryResponseItem,
if id == "" {
break
}
histImg, err = i.GetImage(id.String(), image.GetImageOpts{})
histImg, err = i.GetImage(ctx, id.String(), image.GetImageOpts{})
if err != nil {
break
}

View file

@ -31,7 +31,7 @@ func (r byCreated) Swap(i, j int) { r[i], r[j] = r[j], r[i] }
func (r byCreated) Less(i, j int) bool { return r[i].Created < r[j].Created }
// Images returns a filtered list of images.
func (i *ImageService) Images(_ context.Context, opts types.ImageListOptions) ([]*types.ImageSummary, error) {
func (i *ImageService) Images(ctx context.Context, opts types.ImageListOptions) ([]*types.ImageSummary, error) {
if err := opts.Filters.Validate(acceptedImageFilterTags); err != nil {
return nil, err
}
@ -50,7 +50,7 @@ func (i *ImageService) Images(_ context.Context, opts types.ImageListOptions) ([
err error
)
err = opts.Filters.WalkValues("before", func(value string) error {
beforeFilter, err = i.GetImage(value, imagetypes.GetImageOpts{})
beforeFilter, err = i.GetImage(ctx, value, imagetypes.GetImageOpts{})
return err
})
if err != nil {
@ -58,7 +58,7 @@ func (i *ImageService) Images(_ context.Context, opts types.ImageListOptions) ([
}
err = opts.Filters.WalkValues("since", func(value string) error {
sinceFilter, err = i.GetImage(value, imagetypes.GetImageOpts{})
sinceFilter, err = i.GetImage(ctx, value, imagetypes.GetImageOpts{})
return err
})
if err != nil {

View file

@ -64,7 +64,7 @@ func (i *ImageService) PullImage(ctx context.Context, image, tag string, platfor
// we allow the image to have a non-matching architecture. The code
// below checks for this situation, and returns a warning to the client,
// as well as logging it to the daemon logs.
img, err := i.GetImage(image, imagetypes.GetImageOpts{Platform: platform})
img, err := i.GetImage(ctx, image, imagetypes.GetImageOpts{Platform: platform})
// Note that this is a special case where GetImage returns both an image
// and an error: https://github.com/docker/docker/blob/v20.10.7/daemon/images/image.go#L175-L183
@ -72,6 +72,8 @@ func (i *ImageService) PullImage(ctx context.Context, image, tag string, platfor
po := streamformatter.NewJSONProgressOutput(outStream, false)
progress.Messagef(po, "", `WARNING: %s`, err.Error())
logrus.WithError(err).WithField("image", image).Warn("ignoring platform mismatch on single-arch image")
} else if err != nil {
return err
}
}

View file

@ -1,6 +1,8 @@
package images // import "github.com/docker/docker/daemon/images"
import (
"context"
"github.com/docker/distribution/reference"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/image"
@ -9,7 +11,8 @@ import (
// TagImage creates the tag specified by newTag, pointing to the image named
// imageName (alternatively, imageName can also be an image ID).
func (i *ImageService) TagImage(imageName, repository, tag string) (string, error) {
img, err := i.GetImage(imageName, imagetypes.GetImageOpts{})
ctx := context.TODO()
img, err := i.GetImage(ctx, imageName, imagetypes.GetImageOpts{})
if err != nil {
return "", err
}

View file

@ -1,6 +1,7 @@
package daemon // import "github.com/docker/docker/daemon"
import (
"context"
"fmt"
"sort"
"strconv"
@ -243,6 +244,7 @@ func (daemon *Daemon) reducePsContainer(container *container.Snapshot, filter *l
// foldFilter generates the container filter based on the user's filtering options.
func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerListOptions) (*listContext, error) {
ctx := context.TODO()
psFilters := config.Filters
var filtExited []int
@ -318,7 +320,7 @@ func (daemon *Daemon) foldFilter(view container.View, config *types.ContainerLis
if psFilters.Contains("ancestor") {
ancestorFilter = true
psFilters.WalkValues("ancestor", func(ancestor string) error {
img, err := daemon.imageService.GetImage(ancestor, imagetypes.GetImageOpts{})
img, err := daemon.imageService.GetImage(ctx, ancestor, imagetypes.GetImageOpts{})
if err != nil {
logrus.Warnf("Error while looking up for image %v", ancestor)
return nil
@ -579,10 +581,11 @@ func includeContainerInList(container *container.Snapshot, filter *listContext)
// refreshImage checks if the Image ref still points to the correct ID, and updates the ref to the actual ID when it doesn't
func (daemon *Daemon) refreshImage(s *container.Snapshot, filter *listContext) (*types.Container, error) {
ctx := context.TODO()
c := s.Container
tmpImage := s.Image // keep the original ref if still valid (hasn't changed)
if tmpImage != s.ImageID {
img, err := daemon.imageService.GetImage(tmpImage, imagetypes.GetImageOpts{})
img, err := daemon.imageService.GetImage(ctx, tmpImage, imagetypes.GetImageOpts{})
if _, isDNE := err.(images.ErrImageDoesNotExist); err != nil && !isDNE {
return nil, err
}

View file

@ -1,6 +1,7 @@
package daemon // import "github.com/docker/docker/daemon"
import (
"context"
"encoding/json"
"fmt"
"os"
@ -26,8 +27,8 @@ const (
)
func (daemon *Daemon) createSpec(c *container.Container) (*specs.Spec, error) {
img, err := daemon.imageService.GetImage(string(c.ImageID), imagetypes.GetImageOpts{})
ctx := context.TODO()
img, err := daemon.imageService.GetImage(ctx, string(c.ImageID), imagetypes.GetImageOpts{})
if err != nil {
return nil, err
}