2015-11-18 22:18:44 +00:00
|
|
|
package distribution
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2016-02-11 18:30:56 +00:00
|
|
|
"net/url"
|
2015-11-18 22:18:44 +00:00
|
|
|
"os"
|
|
|
|
"runtime"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
"github.com/docker/distribution"
|
|
|
|
"github.com/docker/distribution/digest"
|
2015-12-17 03:19:22 +00:00
|
|
|
"github.com/docker/distribution/manifest/manifestlist"
|
2015-11-18 22:18:44 +00:00
|
|
|
"github.com/docker/distribution/manifest/schema1"
|
2015-12-11 23:24:12 +00:00
|
|
|
"github.com/docker/distribution/manifest/schema2"
|
2015-12-21 23:42:04 +00:00
|
|
|
"github.com/docker/distribution/registry/api/errcode"
|
2016-02-11 18:30:56 +00:00
|
|
|
"github.com/docker/distribution/registry/client/auth"
|
2016-01-26 19:19:18 +00:00
|
|
|
"github.com/docker/distribution/registry/client/transport"
|
2015-11-18 22:18:44 +00:00
|
|
|
"github.com/docker/docker/distribution/metadata"
|
2015-11-14 00:59:01 +00:00
|
|
|
"github.com/docker/docker/distribution/xfer"
|
2015-11-18 22:18:44 +00:00
|
|
|
"github.com/docker/docker/image"
|
|
|
|
"github.com/docker/docker/image/v1"
|
|
|
|
"github.com/docker/docker/layer"
|
2015-11-14 00:59:01 +00:00
|
|
|
"github.com/docker/docker/pkg/ioutils"
|
|
|
|
"github.com/docker/docker/pkg/progress"
|
2015-11-18 22:18:44 +00:00
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2015-12-04 21:55:15 +00:00
|
|
|
"github.com/docker/docker/reference"
|
2015-11-18 22:18:44 +00:00
|
|
|
"github.com/docker/docker/registry"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
2016-08-10 19:04:42 +00:00
|
|
|
var (
|
|
|
|
errRootFSMismatch = errors.New("layers from manifest don't match image configuration")
|
|
|
|
errMediaTypePlugin = errors.New("target is a plugin")
|
|
|
|
errRootFSInvalid = errors.New("invalid rootfs in image configuration")
|
|
|
|
)
|
2015-12-11 23:24:12 +00:00
|
|
|
|
2016-02-11 22:08:49 +00:00
|
|
|
// ImageConfigPullError is an error pulling the image config blob
|
|
|
|
// (only applies to schema2).
|
|
|
|
type ImageConfigPullError struct {
|
|
|
|
Err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the error string for ImageConfigPullError.
|
|
|
|
func (e ImageConfigPullError) Error() string {
|
|
|
|
return "error pulling image configuration: " + e.Err.Error()
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:18:44 +00:00
|
|
|
type v2Puller struct {
|
2016-01-14 03:34:27 +00:00
|
|
|
V2MetadataService *metadata.V2MetadataService
|
|
|
|
endpoint registry.APIEndpoint
|
|
|
|
config *ImagePullConfig
|
|
|
|
repoInfo *registry.RepositoryInfo
|
|
|
|
repo distribution.Repository
|
2015-12-04 21:42:33 +00:00
|
|
|
// confirmedV2 is set to true if we confirm we're talking to a v2
|
|
|
|
// registry. This is used to limit fallbacks to the v1 protocol.
|
|
|
|
confirmedV2 bool
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 21:42:33 +00:00
|
|
|
func (p *v2Puller) Pull(ctx context.Context, ref reference.Named) (err error) {
|
2015-11-18 22:18:44 +00:00
|
|
|
// TODO(tiborvass): was ReceiveTimeout
|
2015-12-04 21:42:33 +00:00
|
|
|
p.repo, p.confirmedV2, err = NewV2Repository(ctx, p.repoInfo, p.endpoint, p.config.MetaHeaders, p.config.AuthConfig, "pull")
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2015-11-14 00:59:01 +00:00
|
|
|
logrus.Warnf("Error getting v2 registry: %v", err)
|
2016-02-11 23:45:29 +00:00
|
|
|
return err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 21:42:33 +00:00
|
|
|
if err = p.pullV2Repository(ctx, ref); err != nil {
|
2015-12-23 23:21:43 +00:00
|
|
|
if _, ok := err.(fallbackError); ok {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-11 22:08:49 +00:00
|
|
|
if continueOnError(err) {
|
|
|
|
logrus.Errorf("Error trying v2 registry: %v", err)
|
2016-02-11 23:45:29 +00:00
|
|
|
return fallbackError{
|
|
|
|
err: err,
|
|
|
|
confirmedV2: p.confirmedV2,
|
|
|
|
transportOK: true,
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
2015-12-04 21:42:33 +00:00
|
|
|
return err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
func (p *v2Puller) pullV2Repository(ctx context.Context, ref reference.Named) (err error) {
|
2015-12-23 23:21:43 +00:00
|
|
|
var layersDownloaded bool
|
2015-12-10 19:01:34 +00:00
|
|
|
if !reference.IsNameOnly(ref) {
|
2015-12-23 23:21:43 +00:00
|
|
|
layersDownloaded, err = p.pullV2Tag(ctx, ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
} else {
|
2015-12-08 19:14:02 +00:00
|
|
|
tags, err := p.repo.Tags(ctx).All(ctx)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2015-12-23 23:21:43 +00:00
|
|
|
// If this repository doesn't exist on V2, we should
|
|
|
|
// permit a fallback to V1.
|
|
|
|
return allowV1Fallback(err)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-23 23:21:43 +00:00
|
|
|
// The v2 registry knows about this repository, so we will not
|
|
|
|
// allow fallback to the v1 protocol even if we encounter an
|
|
|
|
// error later on.
|
2015-12-04 21:42:33 +00:00
|
|
|
p.confirmedV2 = true
|
|
|
|
|
2015-11-18 22:18:44 +00:00
|
|
|
for _, tag := range tags {
|
2015-12-11 19:00:13 +00:00
|
|
|
tagRef, err := reference.WithTag(ref, tag)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-23 23:21:43 +00:00
|
|
|
pulledNew, err := p.pullV2Tag(ctx, tagRef)
|
|
|
|
if err != nil {
|
|
|
|
// Since this is the pull-all-tags case, don't
|
|
|
|
// allow an error pulling a particular tag to
|
|
|
|
// make the whole pull fall back to v1.
|
|
|
|
if fallbackErr, ok := err.(fallbackError); ok {
|
|
|
|
return fallbackErr.err
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// pulledNew is true if either new layers were downloaded OR if existing images were newly tagged
|
|
|
|
// TODO(tiborvass): should we change the name of `layersDownload`? What about message in WriteStatus?
|
|
|
|
layersDownloaded = layersDownloaded || pulledNew
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-10 19:01:34 +00:00
|
|
|
writeStatus(ref.String(), p.config.ProgressOutput, layersDownloaded)
|
2015-11-18 22:18:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
type v2LayerDescriptor struct {
|
2016-01-14 03:34:27 +00:00
|
|
|
digest digest.Digest
|
|
|
|
repoInfo *registry.RepositoryInfo
|
|
|
|
repo distribution.Repository
|
|
|
|
V2MetadataService *metadata.V2MetadataService
|
2016-01-26 02:20:18 +00:00
|
|
|
tmpFile *os.File
|
2016-01-26 19:19:18 +00:00
|
|
|
verifier digest.Verifier
|
2016-06-07 00:49:34 +00:00
|
|
|
src distribution.Descriptor
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
func (ld *v2LayerDescriptor) Key() string {
|
|
|
|
return "v2:" + ld.digest.String()
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
func (ld *v2LayerDescriptor) ID() string {
|
|
|
|
return stringid.TruncateID(ld.digest.String())
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
func (ld *v2LayerDescriptor) DiffID() (layer.DiffID, error) {
|
2016-01-14 03:34:27 +00:00
|
|
|
return ld.V2MetadataService.GetDiffID(ld.digest)
|
2015-11-14 00:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ld *v2LayerDescriptor) Download(ctx context.Context, progressOutput progress.Output) (io.ReadCloser, int64, error) {
|
|
|
|
logrus.Debugf("pulling blob %q", ld.digest)
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2016-01-26 19:19:18 +00:00
|
|
|
var (
|
|
|
|
err error
|
|
|
|
offset int64
|
|
|
|
)
|
2016-01-26 02:20:18 +00:00
|
|
|
|
|
|
|
if ld.tmpFile == nil {
|
|
|
|
ld.tmpFile, err = createDownloadFile()
|
2016-01-26 19:19:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
2016-01-26 02:20:18 +00:00
|
|
|
} else {
|
2016-01-26 19:19:18 +00:00
|
|
|
offset, err = ld.tmpFile.Seek(0, os.SEEK_END)
|
|
|
|
if err != nil {
|
|
|
|
logrus.Debugf("error seeking to end of download file: %v", err)
|
|
|
|
offset = 0
|
|
|
|
|
|
|
|
ld.tmpFile.Close()
|
|
|
|
if err := os.Remove(ld.tmpFile.Name()); err != nil {
|
|
|
|
logrus.Errorf("Failed to remove temp file: %s", ld.tmpFile.Name())
|
|
|
|
}
|
|
|
|
ld.tmpFile, err = createDownloadFile()
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
|
|
|
} else if offset != 0 {
|
|
|
|
logrus.Debugf("attempting to resume download of %q from %d bytes", ld.digest, offset)
|
|
|
|
}
|
2016-01-26 02:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tmpFile := ld.tmpFile
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2016-05-26 02:11:51 +00:00
|
|
|
layerDownload, err := ld.open(ctx)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2016-02-11 22:08:49 +00:00
|
|
|
logrus.Errorf("Error initiating layer download: %v", err)
|
2015-11-14 00:59:01 +00:00
|
|
|
if err == distribution.ErrBlobUnknown {
|
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
|
|
|
return nil, 0, retryOnError(err)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 19:19:18 +00:00
|
|
|
if offset != 0 {
|
|
|
|
_, err := layerDownload.Seek(offset, os.SEEK_SET)
|
|
|
|
if err != nil {
|
|
|
|
if err := ld.truncateDownloadFile(); err != nil {
|
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
|
|
|
return nil, 0, err
|
|
|
|
}
|
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
size, err := layerDownload.Seek(0, os.SEEK_END)
|
2015-12-04 02:23:21 +00:00
|
|
|
if err != nil {
|
|
|
|
// Seek failed, perhaps because there was no Content-Length
|
|
|
|
// header. This shouldn't fail the download, because we can
|
|
|
|
// still continue without a progress bar.
|
2015-11-14 00:59:01 +00:00
|
|
|
size = 0
|
2015-12-04 02:23:21 +00:00
|
|
|
} else {
|
2016-01-26 19:19:18 +00:00
|
|
|
if size != 0 && offset > size {
|
2016-06-11 20:16:55 +00:00
|
|
|
logrus.Debug("Partial download is larger than full blob. Starting over")
|
2016-01-26 19:19:18 +00:00
|
|
|
offset = 0
|
|
|
|
if err := ld.truncateDownloadFile(); err != nil {
|
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Restore the seek offset either at the beginning of the
|
|
|
|
// stream, or just after the last byte we have from previous
|
|
|
|
// attempts.
|
|
|
|
_, err = layerDownload.Seek(offset, os.SEEK_SET)
|
2015-12-04 02:23:21 +00:00
|
|
|
if err != nil {
|
2015-11-14 00:59:01 +00:00
|
|
|
return nil, 0, err
|
2015-12-04 02:23:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-26 19:19:18 +00:00
|
|
|
reader := progress.NewProgressReader(ioutils.NewCancelReadCloser(ctx, layerDownload), progressOutput, size-offset, ld.ID(), "Downloading")
|
2015-11-14 00:59:01 +00:00
|
|
|
defer reader.Close()
|
|
|
|
|
2016-01-26 19:19:18 +00:00
|
|
|
if ld.verifier == nil {
|
|
|
|
ld.verifier, err = digest.NewDigestVerifier(ld.digest)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2016-01-26 19:19:18 +00:00
|
|
|
_, err = io.Copy(tmpFile, io.TeeReader(reader, ld.verifier))
|
2015-11-14 00:59:01 +00:00
|
|
|
if err != nil {
|
2016-01-26 19:19:18 +00:00
|
|
|
if err == transport.ErrWrongCodeForByteRange {
|
|
|
|
if err := ld.truncateDownloadFile(); err != nil {
|
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
|
|
|
return nil, 0, err
|
2016-01-26 02:20:18 +00:00
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
return nil, 0, retryOnError(err)
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
progress.Update(progressOutput, ld.ID(), "Verifying Checksum")
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2016-01-26 19:19:18 +00:00
|
|
|
if !ld.verifier.Verified() {
|
2015-11-14 00:59:01 +00:00
|
|
|
err = fmt.Errorf("filesystem layer verification failed for digest %s", ld.digest)
|
2015-11-18 22:18:44 +00:00
|
|
|
logrus.Error(err)
|
2016-01-26 02:20:18 +00:00
|
|
|
|
2016-01-26 19:19:18 +00:00
|
|
|
// Allow a retry if this digest verification error happened
|
|
|
|
// after a resumed download.
|
|
|
|
if offset != 0 {
|
|
|
|
if err := ld.truncateDownloadFile(); err != nil {
|
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
|
2016-01-26 19:19:18 +00:00
|
|
|
return nil, 0, err
|
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
progress.Update(progressOutput, ld.ID(), "Download complete")
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
logrus.Debugf("Downloaded %s to tempfile %s", ld.ID(), tmpFile.Name())
|
|
|
|
|
2016-01-26 02:20:18 +00:00
|
|
|
_, err = tmpFile.Seek(0, os.SEEK_SET)
|
|
|
|
if err != nil {
|
|
|
|
tmpFile.Close()
|
|
|
|
if err := os.Remove(tmpFile.Name()); err != nil {
|
|
|
|
logrus.Errorf("Failed to remove temp file: %s", tmpFile.Name())
|
|
|
|
}
|
2016-01-26 02:20:18 +00:00
|
|
|
ld.tmpFile = nil
|
2016-01-26 19:19:18 +00:00
|
|
|
ld.verifier = nil
|
2016-01-26 02:20:18 +00:00
|
|
|
return nil, 0, xfer.DoNotRetry{Err: err}
|
|
|
|
}
|
2016-03-31 02:26:12 +00:00
|
|
|
|
|
|
|
// hand off the temporary file to the download manager, so it will only
|
|
|
|
// be closed once
|
|
|
|
ld.tmpFile = nil
|
|
|
|
|
|
|
|
return ioutils.NewReadCloserWrapper(tmpFile, func() error {
|
|
|
|
tmpFile.Close()
|
|
|
|
err := os.RemoveAll(tmpFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
logrus.Errorf("Failed to remove temp file: %s", tmpFile.Name())
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}), size, nil
|
2016-01-26 02:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ld *v2LayerDescriptor) Close() {
|
|
|
|
if ld.tmpFile != nil {
|
|
|
|
ld.tmpFile.Close()
|
|
|
|
if err := os.RemoveAll(ld.tmpFile.Name()); err != nil {
|
|
|
|
logrus.Errorf("Failed to remove temp file: %s", ld.tmpFile.Name())
|
|
|
|
}
|
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2016-01-26 19:19:18 +00:00
|
|
|
func (ld *v2LayerDescriptor) truncateDownloadFile() error {
|
|
|
|
// Need a new hash context since we will be redoing the download
|
|
|
|
ld.verifier = nil
|
|
|
|
|
|
|
|
if _, err := ld.tmpFile.Seek(0, os.SEEK_SET); err != nil {
|
2016-02-11 22:08:49 +00:00
|
|
|
logrus.Errorf("error seeking to beginning of download file: %v", err)
|
2016-01-26 19:19:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ld.tmpFile.Truncate(0); err != nil {
|
2016-02-11 22:08:49 +00:00
|
|
|
logrus.Errorf("error truncating download file: %v", err)
|
2016-01-26 19:19:18 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
func (ld *v2LayerDescriptor) Registered(diffID layer.DiffID) {
|
|
|
|
// Cache mapping from this layer's DiffID to the blobsum
|
2016-01-14 03:34:27 +00:00
|
|
|
ld.V2MetadataService.Add(diffID, metadata.V2Metadata{Digest: ld.digest, SourceRepository: ld.repoInfo.FullName()})
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
func (p *v2Puller) pullV2Tag(ctx context.Context, ref reference.Named) (tagUpdated bool, err error) {
|
2015-12-08 19:14:02 +00:00
|
|
|
manSvc, err := p.repo.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
manifest distribution.Manifest
|
|
|
|
tagOrDigest string // Used for logging/progress only
|
|
|
|
)
|
2015-12-04 21:55:15 +00:00
|
|
|
if tagged, isTagged := ref.(reference.NamedTagged); isTagged {
|
2015-12-08 19:14:02 +00:00
|
|
|
// NOTE: not using TagService.Get, since it uses HEAD requests
|
|
|
|
// against the manifests endpoint, which are not supported by
|
|
|
|
// all registry versions.
|
2016-04-12 00:29:17 +00:00
|
|
|
manifest, err = manSvc.Get(ctx, "", distribution.WithTag(tagged.Tag()))
|
2015-12-08 19:14:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, allowV1Fallback(err)
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
tagOrDigest = tagged.Tag()
|
2015-12-08 19:14:02 +00:00
|
|
|
} else if digested, isDigested := ref.(reference.Canonical); isDigested {
|
|
|
|
manifest, err = manSvc.Get(ctx, digested.Digest())
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
tagOrDigest = digested.Digest().String()
|
|
|
|
} else {
|
|
|
|
return false, fmt.Errorf("internal error: reference has neither a tag nor a digest: %s", ref.String())
|
|
|
|
}
|
|
|
|
|
2015-12-08 19:14:02 +00:00
|
|
|
if manifest == nil {
|
|
|
|
return false, fmt.Errorf("image manifest does not exist for tag or digest %q", tagOrDigest)
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2016-08-10 19:04:42 +00:00
|
|
|
if m, ok := manifest.(*schema2.DeserializedManifest); ok {
|
|
|
|
if m.Manifest.Config.MediaType == schema2.MediaTypePluginConfig {
|
|
|
|
return false, errMediaTypePlugin
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-08 19:14:02 +00:00
|
|
|
// If manSvc.Get succeeded, we can be confident that the registry on
|
|
|
|
// the other side speaks the v2 protocol.
|
|
|
|
p.confirmedV2 = true
|
|
|
|
|
|
|
|
logrus.Debugf("Pulling ref from V2 registry: %s", ref.String())
|
2016-01-26 19:20:14 +00:00
|
|
|
progress.Message(p.config.ProgressOutput, tagOrDigest, "Pulling from "+p.repo.Named().Name())
|
2015-12-08 19:14:02 +00:00
|
|
|
|
2015-12-11 23:24:12 +00:00
|
|
|
var (
|
|
|
|
imageID image.ID
|
|
|
|
manifestDigest digest.Digest
|
|
|
|
)
|
2015-12-08 19:14:02 +00:00
|
|
|
|
|
|
|
switch v := manifest.(type) {
|
|
|
|
case *schema1.SignedManifest:
|
2015-12-11 23:24:12 +00:00
|
|
|
imageID, manifestDigest, err = p.pullSchema1(ctx, ref, v)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
case *schema2.DeserializedManifest:
|
|
|
|
imageID, manifestDigest, err = p.pullSchema2(ctx, ref, v)
|
2015-12-08 19:14:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-12-17 03:19:22 +00:00
|
|
|
case *manifestlist.DeserializedManifestList:
|
|
|
|
imageID, manifestDigest, err = p.pullManifestList(ctx, ref, v)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-12-08 19:14:02 +00:00
|
|
|
default:
|
|
|
|
return false, errors.New("unsupported manifest format")
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 23:24:12 +00:00
|
|
|
progress.Message(p.config.ProgressOutput, "", "Digest: "+manifestDigest.String())
|
|
|
|
|
2015-12-08 19:14:02 +00:00
|
|
|
oldTagImageID, err := p.config.ReferenceStore.Get(ref)
|
2015-12-11 23:24:12 +00:00
|
|
|
if err == nil {
|
|
|
|
if oldTagImageID == imageID {
|
2016-06-27 17:09:57 +00:00
|
|
|
return false, addDigestReference(p.config.ReferenceStore, ref, manifestDigest, imageID)
|
2015-12-11 23:24:12 +00:00
|
|
|
}
|
|
|
|
} else if err != reference.ErrDoesNotExist {
|
|
|
|
return false, err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
2015-12-08 19:14:02 +00:00
|
|
|
|
|
|
|
if canonical, ok := ref.(reference.Canonical); ok {
|
|
|
|
if err = p.config.ReferenceStore.AddDigest(canonical, imageID, true); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2016-06-27 17:09:57 +00:00
|
|
|
} else {
|
|
|
|
if err = addDigestReference(p.config.ReferenceStore, ref, manifestDigest, imageID); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err = p.config.ReferenceStore.AddTag(ref, imageID, true); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
2015-12-08 19:14:02 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
2015-12-04 21:42:33 +00:00
|
|
|
|
2015-12-11 23:24:12 +00:00
|
|
|
func (p *v2Puller) pullSchema1(ctx context.Context, ref reference.Named, unverifiedManifest *schema1.SignedManifest) (imageID image.ID, manifestDigest digest.Digest, err error) {
|
2015-11-18 22:18:44 +00:00
|
|
|
var verifiedManifest *schema1.Manifest
|
2015-12-11 23:24:12 +00:00
|
|
|
verifiedManifest, err = verifySchema1Manifest(unverifiedManifest, ref)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2015-12-11 23:24:12 +00:00
|
|
|
return "", "", err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rootFS := image.NewRootFS()
|
|
|
|
|
|
|
|
// remove duplicate layers and check parent chain validity
|
|
|
|
err = fixManifestLayers(verifiedManifest)
|
|
|
|
if err != nil {
|
2015-12-11 23:24:12 +00:00
|
|
|
return "", "", err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
var descriptors []xfer.DownloadDescriptor
|
2015-11-18 22:18:44 +00:00
|
|
|
|
|
|
|
// Image history converted to the new format
|
|
|
|
var history []image.History
|
|
|
|
|
|
|
|
// Note that the order of this loop is in the direction of bottom-most
|
|
|
|
// to top-most, so that the downloads slice gets ordered correctly.
|
|
|
|
for i := len(verifiedManifest.FSLayers) - 1; i >= 0; i-- {
|
|
|
|
blobSum := verifiedManifest.FSLayers[i].BlobSum
|
|
|
|
|
|
|
|
var throwAway struct {
|
|
|
|
ThrowAway bool `json:"throwaway,omitempty"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal([]byte(verifiedManifest.History[i].V1Compatibility), &throwAway); err != nil {
|
2015-12-11 23:24:12 +00:00
|
|
|
return "", "", err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h, err := v1.HistoryFromConfig([]byte(verifiedManifest.History[i].V1Compatibility), throwAway.ThrowAway)
|
|
|
|
if err != nil {
|
2015-12-11 23:24:12 +00:00
|
|
|
return "", "", err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
history = append(history, h)
|
|
|
|
|
|
|
|
if throwAway.ThrowAway {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
layerDescriptor := &v2LayerDescriptor{
|
2016-01-14 03:34:27 +00:00
|
|
|
digest: blobSum,
|
|
|
|
repoInfo: p.repoInfo,
|
|
|
|
repo: p.repo,
|
|
|
|
V2MetadataService: p.V2MetadataService,
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
descriptors = append(descriptors, layerDescriptor)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
resultRootFS, release, err := p.config.DownloadManager.Download(ctx, *rootFS, descriptors, p.config.ProgressOutput)
|
|
|
|
if err != nil {
|
2015-12-11 23:24:12 +00:00
|
|
|
return "", "", err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
defer release()
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2015-11-14 00:59:01 +00:00
|
|
|
config, err := v1.MakeConfigFromV1Config([]byte(verifiedManifest.History[0].V1Compatibility), &resultRootFS, history)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2015-12-11 23:24:12 +00:00
|
|
|
return "", "", err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-08 19:14:02 +00:00
|
|
|
imageID, err = p.config.ImageStore.Create(config)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2015-12-11 23:24:12 +00:00
|
|
|
return "", "", err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 23:24:12 +00:00
|
|
|
manifestDigest = digest.FromBytes(unverifiedManifest.Canonical)
|
|
|
|
|
|
|
|
return imageID, manifestDigest, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *v2Puller) pullSchema2(ctx context.Context, ref reference.Named, mfst *schema2.DeserializedManifest) (imageID image.ID, manifestDigest digest.Digest, err error) {
|
2015-12-17 03:19:22 +00:00
|
|
|
manifestDigest, err = schema2ManifestDigest(ref, mfst)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2015-12-11 23:24:12 +00:00
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
target := mfst.Target()
|
|
|
|
imageID = image.ID(target.Digest)
|
|
|
|
if _, err := p.config.ImageStore.Get(imageID); err == nil {
|
|
|
|
// If the image already exists locally, no need to pull
|
|
|
|
// anything.
|
|
|
|
return imageID, manifestDigest, nil
|
|
|
|
}
|
|
|
|
|
2016-05-26 02:11:51 +00:00
|
|
|
var descriptors []xfer.DownloadDescriptor
|
|
|
|
|
|
|
|
// Note that the order of this loop is in the direction of bottom-most
|
|
|
|
// to top-most, so that the downloads slice gets ordered correctly.
|
|
|
|
for _, d := range mfst.Layers {
|
|
|
|
layerDescriptor := &v2LayerDescriptor{
|
|
|
|
digest: d.Digest,
|
|
|
|
repo: p.repo,
|
|
|
|
repoInfo: p.repoInfo,
|
|
|
|
V2MetadataService: p.V2MetadataService,
|
2016-06-07 00:49:34 +00:00
|
|
|
src: d,
|
2016-05-26 02:11:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
descriptors = append(descriptors, layerDescriptor)
|
|
|
|
}
|
|
|
|
|
2015-12-11 23:24:12 +00:00
|
|
|
configChan := make(chan []byte, 1)
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
var cancel func()
|
|
|
|
ctx, cancel = context.WithCancel(ctx)
|
|
|
|
|
|
|
|
// Pull the image config
|
|
|
|
go func() {
|
|
|
|
configJSON, err := p.pullSchema2ImageConfig(ctx, target.Digest)
|
|
|
|
if err != nil {
|
2016-02-11 22:08:49 +00:00
|
|
|
errChan <- ImageConfigPullError{Err: err}
|
2015-12-11 23:24:12 +00:00
|
|
|
cancel()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
configChan <- configJSON
|
|
|
|
}()
|
|
|
|
|
2016-01-08 23:38:55 +00:00
|
|
|
var (
|
|
|
|
configJSON []byte // raw serialized image config
|
|
|
|
unmarshalledConfig image.Image // deserialized image config
|
|
|
|
downloadRootFS image.RootFS // rootFS to use for registering layers.
|
|
|
|
)
|
2016-07-22 22:29:21 +00:00
|
|
|
|
|
|
|
// https://github.com/docker/docker/issues/24766 - Err on the side of caution,
|
|
|
|
// explicitly blocking images intended for linux from the Windows daemon
|
|
|
|
if runtime.GOOS == "windows" && unmarshalledConfig.OS == "linux" {
|
|
|
|
return "", "", fmt.Errorf("image operating system %q cannot be used on this platform", unmarshalledConfig.OS)
|
2016-01-08 23:38:55 +00:00
|
|
|
}
|
|
|
|
|
2016-07-22 22:29:21 +00:00
|
|
|
downloadRootFS = *image.NewRootFS()
|
|
|
|
|
2016-01-08 23:38:55 +00:00
|
|
|
rootFS, release, err := p.config.DownloadManager.Download(ctx, downloadRootFS, descriptors, p.config.ProgressOutput)
|
2015-12-11 23:24:12 +00:00
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case err = <-errChan:
|
|
|
|
return "", "", err
|
|
|
|
default:
|
|
|
|
cancel()
|
|
|
|
select {
|
|
|
|
case <-configChan:
|
|
|
|
case <-errChan:
|
|
|
|
}
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
defer release()
|
|
|
|
|
2016-09-05 07:02:14 +00:00
|
|
|
configJSON, unmarshalledConfig, err = receiveConfig(configChan, errChan)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
2015-12-11 23:24:12 +00:00
|
|
|
}
|
|
|
|
|
2016-08-10 19:04:42 +00:00
|
|
|
if unmarshalledConfig.RootFS == nil {
|
|
|
|
return "", "", errRootFSInvalid
|
|
|
|
}
|
|
|
|
|
2015-12-11 23:24:12 +00:00
|
|
|
// The DiffIDs returned in rootFS MUST match those in the config.
|
|
|
|
// Otherwise the image config could be referencing layers that aren't
|
|
|
|
// included in the manifest.
|
|
|
|
if len(rootFS.DiffIDs) != len(unmarshalledConfig.RootFS.DiffIDs) {
|
|
|
|
return "", "", errRootFSMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := range rootFS.DiffIDs {
|
|
|
|
if rootFS.DiffIDs[i] != unmarshalledConfig.RootFS.DiffIDs[i] {
|
|
|
|
return "", "", errRootFSMismatch
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
imageID, err = p.config.ImageStore.Create(configJSON)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageID, manifestDigest, nil
|
|
|
|
}
|
|
|
|
|
2016-01-08 23:38:55 +00:00
|
|
|
func receiveConfig(configChan <-chan []byte, errChan <-chan error) ([]byte, image.Image, error) {
|
|
|
|
select {
|
|
|
|
case configJSON := <-configChan:
|
|
|
|
var unmarshalledConfig image.Image
|
|
|
|
if err := json.Unmarshal(configJSON, &unmarshalledConfig); err != nil {
|
|
|
|
return nil, image.Image{}, err
|
|
|
|
}
|
|
|
|
return configJSON, unmarshalledConfig, nil
|
|
|
|
case err := <-errChan:
|
|
|
|
return nil, image.Image{}, err
|
|
|
|
// Don't need a case for ctx.Done in the select because cancellation
|
|
|
|
// will trigger an error in p.pullSchema2ImageConfig.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-17 03:19:22 +00:00
|
|
|
// pullManifestList handles "manifest lists" which point to various
|
|
|
|
// platform-specifc manifests.
|
|
|
|
func (p *v2Puller) pullManifestList(ctx context.Context, ref reference.Named, mfstList *manifestlist.DeserializedManifestList) (imageID image.ID, manifestListDigest digest.Digest, err error) {
|
|
|
|
manifestListDigest, err = schema2ManifestDigest(ref, mfstList)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var manifestDigest digest.Digest
|
|
|
|
for _, manifestDescriptor := range mfstList.Manifests {
|
|
|
|
// TODO(aaronl): The manifest list spec supports optional
|
|
|
|
// "features" and "variant" fields. These are not yet used.
|
|
|
|
// Once they are, their values should be interpreted here.
|
2016-04-06 20:39:30 +00:00
|
|
|
if manifestDescriptor.Platform.Architecture == runtime.GOARCH && manifestDescriptor.Platform.OS == runtime.GOOS {
|
2015-12-17 03:19:22 +00:00
|
|
|
manifestDigest = manifestDescriptor.Digest
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if manifestDigest == "" {
|
|
|
|
return "", "", errors.New("no supported platform found in manifest list")
|
|
|
|
}
|
|
|
|
|
|
|
|
manSvc, err := p.repo.Manifests(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest, err := manSvc.Get(ctx, manifestDigest)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
manifestRef, err := reference.WithDigest(ref, manifestDigest)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v := manifest.(type) {
|
|
|
|
case *schema1.SignedManifest:
|
|
|
|
imageID, _, err = p.pullSchema1(ctx, manifestRef, v)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
case *schema2.DeserializedManifest:
|
|
|
|
imageID, _, err = p.pullSchema2(ctx, manifestRef, v)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return "", "", errors.New("unsupported manifest format")
|
|
|
|
}
|
|
|
|
|
|
|
|
return imageID, manifestListDigest, err
|
|
|
|
}
|
|
|
|
|
2015-12-11 23:24:12 +00:00
|
|
|
func (p *v2Puller) pullSchema2ImageConfig(ctx context.Context, dgst digest.Digest) (configJSON []byte, err error) {
|
|
|
|
blobs := p.repo.Blobs(ctx)
|
|
|
|
configJSON, err = blobs.Get(ctx, dgst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify image config digest
|
|
|
|
verifier, err := digest.NewDigestVerifier(dgst)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err := verifier.Write(configJSON); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !verifier.Verified() {
|
|
|
|
err := fmt.Errorf("image config verification failed for digest %s", dgst)
|
|
|
|
logrus.Error(err)
|
|
|
|
return nil, err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-11 23:24:12 +00:00
|
|
|
return configJSON, nil
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2015-12-17 03:19:22 +00:00
|
|
|
// schema2ManifestDigest computes the manifest digest, and, if pulling by
|
|
|
|
// digest, ensures that it matches the requested digest.
|
|
|
|
func schema2ManifestDigest(ref reference.Named, mfst distribution.Manifest) (digest.Digest, error) {
|
|
|
|
_, canonical, err := mfst.Payload()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If pull by digest, then verify the manifest digest.
|
|
|
|
if digested, isDigested := ref.(reference.Canonical); isDigested {
|
|
|
|
verifier, err := digest.NewDigestVerifier(digested.Digest())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if _, err := verifier.Write(canonical); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if !verifier.Verified() {
|
|
|
|
err := fmt.Errorf("manifest verification failed for digest %s", digested.Digest())
|
|
|
|
logrus.Error(err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return digested.Digest(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return digest.FromBytes(canonical), nil
|
|
|
|
}
|
|
|
|
|
2015-12-23 23:21:43 +00:00
|
|
|
// allowV1Fallback checks if the error is a possible reason to fallback to v1
|
|
|
|
// (even if confirmedV2 has been set already), and if so, wraps the error in
|
|
|
|
// a fallbackError with confirmedV2 set to false. Otherwise, it returns the
|
|
|
|
// error unmodified.
|
|
|
|
func allowV1Fallback(err error) error {
|
|
|
|
switch v := err.(type) {
|
|
|
|
case errcode.Errors:
|
|
|
|
if len(v) != 0 {
|
2016-02-11 22:08:49 +00:00
|
|
|
if v0, ok := v[0].(errcode.Error); ok && shouldV2Fallback(v0) {
|
2016-02-11 23:45:29 +00:00
|
|
|
return fallbackError{
|
|
|
|
err: err,
|
|
|
|
confirmedV2: false,
|
|
|
|
transportOK: true,
|
|
|
|
}
|
2015-12-23 23:21:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case errcode.Error:
|
2016-02-11 22:08:49 +00:00
|
|
|
if shouldV2Fallback(v) {
|
2016-02-11 23:45:29 +00:00
|
|
|
return fallbackError{
|
|
|
|
err: err,
|
|
|
|
confirmedV2: false,
|
|
|
|
transportOK: true,
|
|
|
|
}
|
2015-12-23 23:21:43 +00:00
|
|
|
}
|
2016-02-11 18:30:56 +00:00
|
|
|
case *url.Error:
|
|
|
|
if v.Err == auth.ErrNoBasicAuthCredentials {
|
|
|
|
return fallbackError{err: err, confirmedV2: false}
|
|
|
|
}
|
2015-12-23 23:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-12-11 23:24:12 +00:00
|
|
|
func verifySchema1Manifest(signedManifest *schema1.SignedManifest, ref reference.Named) (m *schema1.Manifest, err error) {
|
2015-11-18 22:18:44 +00:00
|
|
|
// If pull by digest, then verify the manifest digest. NOTE: It is
|
|
|
|
// important to do this first, before any other content validation. If the
|
|
|
|
// digest cannot be verified, don't even bother with those other things.
|
2015-12-04 21:55:15 +00:00
|
|
|
if digested, isCanonical := ref.(reference.Canonical); isCanonical {
|
2015-11-18 22:18:44 +00:00
|
|
|
verifier, err := digest.NewDigestVerifier(digested.Digest())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-12-08 19:14:02 +00:00
|
|
|
if _, err := verifier.Write(signedManifest.Canonical); err != nil {
|
2015-11-18 22:18:44 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !verifier.Verified() {
|
|
|
|
err := fmt.Errorf("image verification failed for digest %s", digested.Digest())
|
|
|
|
logrus.Error(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2015-12-08 19:14:02 +00:00
|
|
|
m = &signedManifest.Manifest
|
2015-11-18 22:18:44 +00:00
|
|
|
|
|
|
|
if m.SchemaVersion != 1 {
|
|
|
|
return nil, fmt.Errorf("unsupported schema version %d for %q", m.SchemaVersion, ref.String())
|
|
|
|
}
|
|
|
|
if len(m.FSLayers) != len(m.History) {
|
|
|
|
return nil, fmt.Errorf("length of history not equal to number of layers for %q", ref.String())
|
|
|
|
}
|
|
|
|
if len(m.FSLayers) == 0 {
|
|
|
|
return nil, fmt.Errorf("no FSLayers in manifest for %q", ref.String())
|
|
|
|
}
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// fixManifestLayers removes repeated layers from the manifest and checks the
|
|
|
|
// correctness of the parent chain.
|
|
|
|
func fixManifestLayers(m *schema1.Manifest) error {
|
|
|
|
imgs := make([]*image.V1Image, len(m.FSLayers))
|
|
|
|
for i := range m.FSLayers {
|
|
|
|
img := &image.V1Image{}
|
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(m.History[i].V1Compatibility), img); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
imgs[i] = img
|
|
|
|
if err := v1.ValidateID(img.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if imgs[len(imgs)-1].Parent != "" && runtime.GOOS != "windows" {
|
|
|
|
// Windows base layer can point to a base layer parent that is not in manifest.
|
|
|
|
return errors.New("Invalid parent ID in the base layer of the image.")
|
|
|
|
}
|
|
|
|
|
|
|
|
// check general duplicates to error instead of a deadlock
|
|
|
|
idmap := make(map[string]struct{})
|
|
|
|
|
|
|
|
var lastID string
|
|
|
|
for _, img := range imgs {
|
|
|
|
// skip IDs that appear after each other, we handle those later
|
|
|
|
if _, exists := idmap[img.ID]; img.ID != lastID && exists {
|
|
|
|
return fmt.Errorf("ID %+v appears multiple times in manifest", img.ID)
|
|
|
|
}
|
|
|
|
lastID = img.ID
|
|
|
|
idmap[lastID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// backwards loop so that we keep the remaining indexes after removing items
|
|
|
|
for i := len(imgs) - 2; i >= 0; i-- {
|
|
|
|
if imgs[i].ID == imgs[i+1].ID { // repeated ID. remove and continue
|
|
|
|
m.FSLayers = append(m.FSLayers[:i], m.FSLayers[i+1:]...)
|
|
|
|
m.History = append(m.History[:i], m.History[i+1:]...)
|
|
|
|
} else if imgs[i].Parent != imgs[i+1].ID {
|
|
|
|
return fmt.Errorf("Invalid parent ID. Expected %v, got %v.", imgs[i+1].ID, imgs[i].Parent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-26 02:20:18 +00:00
|
|
|
|
|
|
|
func createDownloadFile() (*os.File, error) {
|
|
|
|
return ioutil.TempFile("", "GetImageBlob")
|
|
|
|
}
|