2018-02-07 15:52:47 -05:00
|
|
|
package images // import "github.com/docker/docker/daemon/images"
|
2016-04-07 17:29:18 -04:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2016-04-07 17:29:18 -04:00
|
|
|
"io"
|
|
|
|
"strings"
|
2018-06-07 18:26:07 -04:00
|
|
|
"time"
|
2016-04-07 17:29:18 -04:00
|
|
|
|
2020-10-30 15:47:06 -04:00
|
|
|
"github.com/containerd/containerd/leases"
|
|
|
|
"github.com/containerd/containerd/namespaces"
|
2016-11-08 12:32:29 -05:00
|
|
|
dist "github.com/docker/distribution"
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2022-07-25 11:24:03 -04:00
|
|
|
imagetypes "github.com/docker/docker/api/types/image"
|
2022-03-03 04:28:39 -05:00
|
|
|
"github.com/docker/docker/api/types/registry"
|
2016-04-07 17:29:18 -04:00
|
|
|
"github.com/docker/docker/distribution"
|
2016-12-12 18:05:53 -05:00
|
|
|
progressutils "github.com/docker/docker/distribution/utils"
|
2018-01-11 14:53:06 -05:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-04-07 17:29:18 -04:00
|
|
|
"github.com/docker/docker/pkg/progress"
|
2021-04-26 06:41:40 -04:00
|
|
|
"github.com/docker/docker/pkg/streamformatter"
|
2022-03-04 08:49:42 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2018-06-26 17:49:33 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
2020-10-30 15:47:06 -04:00
|
|
|
"github.com/pkg/errors"
|
2021-04-26 06:41:40 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2016-04-07 17:29:18 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// PullImage initiates a pull operation. image is the repository name to pull, and
|
|
|
|
// tag may be either empty, or indicate a specific tag to pull.
|
2022-03-03 04:28:39 -05:00
|
|
|
func (i *ImageService) PullImage(ctx context.Context, image, tag string, platform *specs.Platform, metaHeaders map[string][]string, authConfig *registry.AuthConfig, outStream io.Writer) error {
|
2018-06-07 18:26:07 -04:00
|
|
|
start := time.Now()
|
2016-04-07 17:29:18 -04:00
|
|
|
// Special case: "pull -a" may send an image name with a
|
|
|
|
// trailing :. This is ugly, but let's not break API
|
|
|
|
// compatibility.
|
|
|
|
image = strings.TrimSuffix(image, ":")
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
ref, err := reference.ParseNormalizedNamed(image)
|
2016-04-07 17:29:18 -04:00
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.InvalidParameter(err)
|
2016-04-07 17:29:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if tag != "" {
|
|
|
|
// The "tag" could actually be a digest.
|
|
|
|
var dgst digest.Digest
|
2017-01-06 20:23:18 -05:00
|
|
|
dgst, err = digest.Parse(tag)
|
2016-04-07 17:29:18 -04:00
|
|
|
if err == nil {
|
2016-11-10 18:59:02 -05:00
|
|
|
ref, err = reference.WithDigest(reference.TrimNamed(ref), dgst)
|
2016-04-07 17:29:18 -04:00
|
|
|
} else {
|
|
|
|
ref, err = reference.WithTag(ref, tag)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return errdefs.InvalidParameter(err)
|
2016-04-07 17:29:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 17:49:33 -04:00
|
|
|
err = i.pullImageWithReference(ctx, ref, platform, metaHeaders, authConfig, outStream)
|
2018-06-07 18:26:07 -04:00
|
|
|
imageActions.WithValues("pull").UpdateSince(start)
|
2021-04-26 06:41:40 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if platform != nil {
|
|
|
|
// If --platform was specified, check that the image we pulled matches
|
|
|
|
// the expected platform. This check is for situations where the image
|
|
|
|
// is a single-arch image, in which case (for backward compatibility),
|
|
|
|
// we allow the image to have a non-matching architecture. The code
|
|
|
|
// below checks for this situation, and returns a warning to the client,
|
2021-07-24 12:32:52 -04:00
|
|
|
// as well as logging it to the daemon logs.
|
2022-08-09 08:42:50 -04:00
|
|
|
img, err := i.GetImage(ctx, image, imagetypes.GetImageOpts{Platform: platform})
|
2021-04-26 06:41:40 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
if errdefs.IsNotFound(err) && img != nil {
|
|
|
|
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")
|
2022-08-09 08:42:50 -04:00
|
|
|
} else if err != nil {
|
|
|
|
return err
|
2021-04-26 06:41:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2016-04-07 17:29:18 -04:00
|
|
|
}
|
|
|
|
|
2022-03-03 04:28:39 -05:00
|
|
|
func (i *ImageService) pullImageWithReference(ctx context.Context, ref reference.Named, platform *specs.Platform, metaHeaders map[string][]string, authConfig *registry.AuthConfig, outStream io.Writer) error {
|
2016-04-07 17:29:18 -04:00
|
|
|
// Include a buffer so that slow client connections don't affect
|
|
|
|
// transfer performance.
|
|
|
|
progressChan := make(chan progress.Progress, 100)
|
|
|
|
|
|
|
|
writesDone := make(chan struct{})
|
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
go func() {
|
2016-12-12 18:05:53 -05:00
|
|
|
progressutils.WriteDistributionProgress(cancelFunc, outStream, progressChan)
|
2016-04-07 17:29:18 -04:00
|
|
|
close(writesDone)
|
|
|
|
}()
|
|
|
|
|
2020-10-30 15:47:06 -04:00
|
|
|
ctx = namespaces.WithNamespace(ctx, i.contentNamespace)
|
|
|
|
// Take out a temporary lease for everything that gets persisted to the content store.
|
|
|
|
// Before the lease is cancelled, any content we want to keep should have it's own lease applied.
|
|
|
|
ctx, done, err := tempLease(ctx, i.leases)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer done(ctx)
|
|
|
|
|
|
|
|
cs := &contentStoreForPull{
|
|
|
|
ContentStore: i.content,
|
|
|
|
leases: i.leases,
|
|
|
|
}
|
|
|
|
imageStore := &imageStoreForPull{
|
|
|
|
ImageConfigStore: distribution.NewImageConfigStoreFromStore(i.imageStore),
|
|
|
|
ingested: cs,
|
|
|
|
leases: i.leases,
|
|
|
|
}
|
|
|
|
|
2016-04-07 17:29:18 -04:00
|
|
|
imagePullConfig := &distribution.ImagePullConfig{
|
2016-12-16 14:19:05 -05:00
|
|
|
Config: distribution.Config{
|
|
|
|
MetaHeaders: metaHeaders,
|
|
|
|
AuthConfig: authConfig,
|
|
|
|
ProgressOutput: progress.ChanOutput(progressChan),
|
2018-02-02 17:18:46 -05:00
|
|
|
RegistryService: i.registryService,
|
|
|
|
ImageEventLogger: i.LogImageEvent,
|
|
|
|
MetadataStore: i.distributionMetadataStore,
|
2020-10-30 15:47:06 -04:00
|
|
|
ImageStore: imageStore,
|
2018-02-02 17:18:46 -05:00
|
|
|
ReferenceStore: i.referenceStore,
|
2016-12-16 14:19:05 -05:00
|
|
|
},
|
2018-02-02 17:18:46 -05:00
|
|
|
DownloadManager: i.downloadManager,
|
2018-06-26 17:49:33 -04:00
|
|
|
Platform: platform,
|
2016-04-07 17:29:18 -04:00
|
|
|
}
|
|
|
|
|
2020-10-30 15:47:06 -04:00
|
|
|
err = distribution.Pull(ctx, ref, imagePullConfig, cs)
|
2016-04-07 17:29:18 -04:00
|
|
|
close(progressChan)
|
|
|
|
<-writesDone
|
|
|
|
return err
|
|
|
|
}
|
2016-11-08 02:59:55 -05:00
|
|
|
|
2016-11-18 18:57:11 -05:00
|
|
|
// GetRepository returns a repository from the registry.
|
2022-03-03 04:28:39 -05:00
|
|
|
func (i *ImageService) GetRepository(ctx context.Context, ref reference.Named, authConfig *registry.AuthConfig) (dist.Repository, error) {
|
2022-02-27 16:11:13 -05:00
|
|
|
return distribution.GetRepository(ctx, ref, &distribution.ImagePullConfig{
|
|
|
|
Config: distribution.Config{
|
|
|
|
AuthConfig: authConfig,
|
|
|
|
RegistryService: i.registryService,
|
|
|
|
},
|
|
|
|
})
|
2016-11-08 02:59:55 -05:00
|
|
|
}
|
2020-10-30 15:47:06 -04:00
|
|
|
|
|
|
|
func tempLease(ctx context.Context, mgr leases.Manager) (context.Context, func(context.Context) error, error) {
|
|
|
|
nop := func(context.Context) error { return nil }
|
|
|
|
_, ok := leases.FromContext(ctx)
|
|
|
|
if ok {
|
|
|
|
return ctx, nop, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use an expiration that ensures the lease is cleaned up at some point if there is a crash, SIGKILL, etc.
|
|
|
|
opts := []leases.Opt{
|
|
|
|
leases.WithRandomID(),
|
|
|
|
leases.WithExpiration(24 * time.Hour),
|
|
|
|
leases.WithLabels(map[string]string{
|
|
|
|
"moby.lease/temporary": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
l, err := mgr.Create(ctx, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return ctx, nop, errors.Wrap(err, "error creating temporary lease")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = leases.WithLease(ctx, l.ID)
|
|
|
|
return ctx, func(ctx context.Context) error {
|
|
|
|
return mgr.Delete(ctx, l)
|
|
|
|
}, nil
|
|
|
|
}
|