Merge pull request #43968 from thaJeztah/implement_GetImageOpts

introduce GetImageOpts to manage image inspect data in backend
This commit is contained in:
Sebastiaan van Stijn 2022-08-24 19:07:30 +02:00 committed by GitHub
commit ce550fa9c2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 48 additions and 26 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, platform *specs.Platform) (retImg *dockerimage.Image, retErr error)
GetImage(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

@ -12,6 +12,7 @@ import (
"github.com/docker/docker/api/server/httputils"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
opts "github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/api/types/versions"
"github.com/docker/docker/errdefs"
@ -192,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"], nil)
img, err := ir.backend.GetImage(vars["name"], opts.GetImageOpts{})
if err != nil {
return err
}

8
api/types/image/opts.go Normal file
View file

@ -0,0 +1,8 @@
package image
import specs "github.com/opencontainers/image-spec/specs-go/v1"
// GetImageOpts holds parameters to inspect an image.
type GetImageOpts struct {
Platform *specs.Platform
}

View file

@ -12,6 +12,7 @@ import (
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/api/types/filters"
opts "github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/network"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/api/types/swarm"
@ -76,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, platform *specs.Platform) (retImg *image.Image, retErr error)
GetImage(refOrID string, options opts.GetImageOpts) (*image.Image, error)
}

View file

@ -16,6 +16,7 @@ import (
"github.com/docker/docker/api/types/backend"
containertypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/events"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
containerpkg "github.com/docker/docker/container"
"github.com/docker/docker/daemon"
@ -75,7 +76,7 @@ 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, nil)
_, err := c.imageBackend.GetImage(spec.Image, imagetypes.GetImageOpts{})
if err == nil {
return nil
}

View file

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

View file

@ -10,6 +10,7 @@ import (
"github.com/containerd/containerd/platforms"
"github.com/docker/docker/api/types"
containertypes "github.com/docker/docker/api/types/container"
imagetypes "github.com/docker/docker/api/types/image"
networktypes "github.com/docker/docker/api/types/network"
"github.com/docker/docker/container"
"github.com/docker/docker/daemon/images"
@ -68,7 +69,7 @@ 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, opts.params.Platform); img != nil {
if img, _ := daemon.imageService.GetImage(opts.params.Config.Image, imagetypes.GetImageOpts{Platform: opts.params.Platform}); img != nil {
p := maximumSpec()
imgPlat := v1.Platform{
OS: img.OS,
@ -119,7 +120,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, opts.params.Platform)
img, err = daemon.imageService.GetImage(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, platform *v1.Platform) (retImg *image.Image, retErr error)
GetImage(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,7 @@
package images // import "github.com/docker/docker/daemon/images"
import (
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/builder"
"github.com/docker/docker/image/cache"
"github.com/sirupsen/logrus"
@ -15,7 +16,7 @@ func (i *ImageService) MakeImageCache(sourceRefs []string) builder.ImageCache {
cache := cache.New(i.imageStore)
for _, ref := range sourceRefs {
img, err := i.GetImage(ref, nil)
img, err := i.GetImage(ref, imagetypes.GetImageOpts{})
if err != nil {
logrus.Warnf("Could not look up %s for cache resolution, skipping: %+v", ref, err)
continue

View file

@ -12,6 +12,7 @@ import (
"github.com/containerd/containerd/leases"
"github.com/containerd/containerd/platforms"
"github.com/docker/distribution/reference"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/image"
"github.com/opencontainers/go-digest"
@ -148,9 +149,9 @@ func (i *ImageService) manifestMatchesPlatform(img *image.Image, platform specs.
}
// GetImage returns an image corresponding to the image referred to by refOrID.
func (i *ImageService) GetImage(refOrID string, platform *specs.Platform) (retImg *image.Image, retErr error) {
func (i *ImageService) GetImage(refOrID string, options imagetypes.GetImageOpts) (retImg *image.Image, retErr error) {
defer func() {
if retErr != nil || retImg == nil || platform == nil {
if retErr != nil || retImg == nil || options.Platform == nil {
return
}
@ -159,7 +160,7 @@ func (i *ImageService) GetImage(refOrID string, platform *specs.Platform) (retIm
Architecture: retImg.Architecture,
Variant: retImg.Variant,
}
p := *platform
p := *options.Platform
// Note that `platforms.Only` will fuzzy match this for us
// For example: an armv6 image will run just fine an an armv7 CPU, without emulation or anything.
if OnlyPlatformWithFallback(p).Match(imgPlat) {

View file

@ -8,6 +8,7 @@ import (
"github.com/containerd/containerd/platforms"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types/backend"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/builder"
"github.com/docker/docker/errdefs"
@ -167,7 +168,7 @@ func (i *ImageService) pullForBuilder(ctx context.Context, name string, authConf
return nil, err
}
img, err := i.GetImage(name, platform)
img, err := i.GetImage(name, imagetypes.GetImageOpts{Platform: platform})
if errdefs.IsNotFound(err) && img != nil && platform != nil {
imgPlat := specs.Platform{
OS: img.OS,
@ -211,7 +212,7 @@ func (i *ImageService) GetImageAndReleasableLayer(ctx context.Context, refOrID s
}
if opts.PullOption != backend.PullOptionForcePull {
img, err := i.GetImage(refOrID, opts.Platform)
img, err := i.GetImage(refOrID, imagetypes.GetImageOpts{Platform: opts.Platform})
if err != nil && opts.PullOption == backend.PullOptionNoPull {
return nil, nil, err
}

View file

@ -8,6 +8,7 @@ import (
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/container"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/image"
@ -63,7 +64,7 @@ func (i *ImageService) ImageDelete(ctx context.Context, imageRef string, force,
start := time.Now()
records := []types.ImageDeleteResponseItem{}
img, err := i.GetImage(imageRef, nil)
img, err := i.GetImage(imageRef, imagetypes.GetImageOpts{})
if err != nil {
return nil, err
}

View file

@ -2,6 +2,7 @@ package images // import "github.com/docker/docker/daemon/images"
import (
"github.com/docker/docker/api/types/events"
imagetypes "github.com/docker/docker/api/types/image"
)
// LogImageEvent generates an event related to an image with only the default attributes.
@ -11,7 +12,7 @@ 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, nil)
img, err := i.GetImage(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

@ -13,7 +13,7 @@ import (
// name by walking the image lineage.
func (i *ImageService) ImageHistory(name string) ([]*image.HistoryResponseItem, error) {
start := time.Now()
img, err := i.GetImage(name, nil)
img, err := i.GetImage(name, image.GetImageOpts{})
if err != nil {
return nil, err
}
@ -69,7 +69,7 @@ func (i *ImageService) ImageHistory(name string) ([]*image.HistoryResponseItem,
if id == "" {
break
}
histImg, err = i.GetImage(id.String(), nil)
histImg, err = i.GetImage(id.String(), image.GetImageOpts{})
if err != nil {
break
}

View file

@ -7,6 +7,7 @@ import (
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/container"
"github.com/docker/docker/image"
"github.com/docker/docker/layer"
@ -49,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, nil)
beforeFilter, err = i.GetImage(value, imagetypes.GetImageOpts{})
return err
})
if err != nil {
@ -57,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, nil)
sinceFilter, err = i.GetImage(value, imagetypes.GetImageOpts{})
return err
})
if err != nil {

View file

@ -10,6 +10,7 @@ import (
"github.com/containerd/containerd/namespaces"
dist "github.com/docker/distribution"
"github.com/docker/distribution/reference"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/distribution"
progressutils "github.com/docker/docker/distribution/utils"
@ -63,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, platform)
img, err := i.GetImage(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

View file

@ -2,13 +2,14 @@ package images // import "github.com/docker/docker/daemon/images"
import (
"github.com/docker/distribution/reference"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/image"
)
// 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, nil)
img, err := i.GetImage(imageName, imagetypes.GetImageOpts{})
if err != nil {
return "", err
}

View file

@ -8,6 +8,7 @@ import (
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/container"
"github.com/docker/docker/daemon/images"
"github.com/docker/docker/errdefs"
@ -317,7 +318,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, nil)
img, err := daemon.imageService.GetImage(ancestor, imagetypes.GetImageOpts{})
if err != nil {
logrus.Warnf("Error while looking up for image %v", ancestor)
return nil
@ -581,7 +582,7 @@ func (daemon *Daemon) refreshImage(s *container.Snapshot, filter *listContext) (
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, nil)
img, err := daemon.imageService.GetImage(tmpImage, imagetypes.GetImageOpts{})
if _, isDNE := err.(images.ErrImageDoesNotExist); err != nil && !isDNE {
return nil, err
}

View file

@ -8,6 +8,7 @@ import (
"strings"
containertypes "github.com/docker/docker/api/types/container"
imagetypes "github.com/docker/docker/api/types/image"
"github.com/docker/docker/container"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/oci"
@ -26,7 +27,7 @@ const (
func (daemon *Daemon) createSpec(c *container.Container) (*specs.Spec, error) {
img, err := daemon.imageService.GetImage(string(c.ImageID), nil)
img, err := daemon.imageService.GetImage(string(c.ImageID), imagetypes.GetImageOpts{})
if err != nil {
return nil, err
}