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
|
|
|
|
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"
|
2016-09-06 14:18:12 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
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"
|
|
|
|
"github.com/docker/docker/registry"
|
2019-08-05 10:37:47 -04:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2018-06-26 17:49:33 -04:00
|
|
|
specs "github.com/opencontainers/image-spec/specs-go/v1"
|
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.
|
2018-06-26 17:49:33 -04:00
|
|
|
func (i *ImageService) PullImage(ctx context.Context, image, tag string, platform *specs.Platform, metaHeaders map[string][]string, authConfig *types.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)
|
|
|
|
return err
|
2016-04-07 17:29:18 -04:00
|
|
|
}
|
|
|
|
|
2018-06-26 17:49:33 -04:00
|
|
|
func (i *ImageService) pullImageWithReference(ctx context.Context, ref reference.Named, platform *specs.Platform, metaHeaders map[string][]string, authConfig *types.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)
|
|
|
|
}()
|
|
|
|
|
|
|
|
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,
|
|
|
|
ImageStore: distribution.NewImageConfigStoreFromStore(i.imageStore),
|
|
|
|
ReferenceStore: i.referenceStore,
|
2016-12-16 14:19:05 -05:00
|
|
|
},
|
2018-02-02 17:18:46 -05:00
|
|
|
DownloadManager: i.downloadManager,
|
2016-12-16 14:19:05 -05:00
|
|
|
Schema2Types: distribution.ImageTypes,
|
2018-06-26 17:49:33 -04:00
|
|
|
Platform: platform,
|
2016-04-07 17:29:18 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
err := distribution.Pull(ctx, ref, imagePullConfig)
|
|
|
|
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.
|
2018-02-07 15:52:47 -05:00
|
|
|
func (i *ImageService) GetRepository(ctx context.Context, ref reference.Named, authConfig *types.AuthConfig) (dist.Repository, bool, error) {
|
2016-11-08 02:59:55 -05:00
|
|
|
// get repository info
|
2018-02-02 17:18:46 -05:00
|
|
|
repoInfo, err := i.registryService.ResolveRepository(ref)
|
2016-11-08 02:59:55 -05:00
|
|
|
if err != nil {
|
2018-08-03 10:36:43 -04:00
|
|
|
return nil, false, errdefs.InvalidParameter(err)
|
2016-11-08 02:59:55 -05:00
|
|
|
}
|
|
|
|
// makes sure name is not empty or `scratch`
|
2017-01-25 19:54:18 -05:00
|
|
|
if err := distribution.ValidateRepoName(repoInfo.Name); err != nil {
|
2017-11-28 23:09:37 -05:00
|
|
|
return nil, false, errdefs.InvalidParameter(err)
|
2016-11-08 02:59:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// get endpoints
|
2018-02-02 17:18:46 -05:00
|
|
|
endpoints, err := i.registryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
|
2016-11-08 02:59:55 -05:00
|
|
|
if err != nil {
|
2016-11-08 12:32:29 -05:00
|
|
|
return nil, false, err
|
2016-11-08 02:59:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// retrieve repository
|
2016-11-08 12:32:29 -05:00
|
|
|
var (
|
|
|
|
confirmedV2 bool
|
|
|
|
repository dist.Repository
|
|
|
|
lastError error
|
|
|
|
)
|
|
|
|
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
if endpoint.Version == registry.APIVersion1 {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-08 02:59:55 -05:00
|
|
|
|
2016-11-08 12:32:29 -05:00
|
|
|
repository, confirmedV2, lastError = distribution.NewV2Repository(ctx, repoInfo, endpoint, nil, authConfig, "pull")
|
|
|
|
if lastError == nil && confirmedV2 {
|
|
|
|
break
|
2016-11-08 02:59:55 -05:00
|
|
|
}
|
|
|
|
}
|
2016-11-08 12:32:29 -05:00
|
|
|
return repository, confirmedV2, lastError
|
2016-11-08 02:59:55 -05:00
|
|
|
}
|