2015-11-18 17:18:44 -05:00
|
|
|
package distribution
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-12-31 08:57:58 -05:00
|
|
|
"github.com/docker/docker/api"
|
2015-11-18 17:18:44 -05:00
|
|
|
"github.com/docker/docker/distribution/metadata"
|
2015-11-13 19:59:01 -05:00
|
|
|
"github.com/docker/docker/pkg/progress"
|
2017-01-25 19:54:18 -05:00
|
|
|
refstore "github.com/docker/docker/reference"
|
2015-11-18 17:18:44 -05:00
|
|
|
"github.com/docker/docker/registry"
|
2017-01-06 20:23:18 -05:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-07-19 10:20:13 -04:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 17:42:13 -04:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-11-13 19:59:01 -05:00
|
|
|
"golang.org/x/net/context"
|
2015-11-18 17:18:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Puller is an interface that abstracts pulling for different API versions.
|
|
|
|
type Puller interface {
|
|
|
|
// Pull tries to pull the image referenced by `tag`
|
|
|
|
// Pull returns an error if any, as well as a boolean that determines whether to retry Pull on the next configured endpoint.
|
|
|
|
//
|
2015-12-04 16:42:33 -05:00
|
|
|
Pull(ctx context.Context, ref reference.Named) error
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// newPuller returns a Puller interface that will pull from either a v1 or v2
|
|
|
|
// registry. The endpoint argument contains a Version field that determines
|
|
|
|
// whether a v1 or v2 puller will be created. The other parameters are passed
|
|
|
|
// through to the underlying puller implementation for use during the actual
|
|
|
|
// pull operation.
|
2015-11-13 19:59:01 -05:00
|
|
|
func newPuller(endpoint registry.APIEndpoint, repoInfo *registry.RepositoryInfo, imagePullConfig *ImagePullConfig) (Puller, error) {
|
2015-11-18 17:18:44 -05:00
|
|
|
switch endpoint.Version {
|
|
|
|
case registry.APIVersion2:
|
|
|
|
return &v2Puller{
|
2016-01-13 22:34:27 -05:00
|
|
|
V2MetadataService: metadata.NewV2MetadataService(imagePullConfig.MetadataStore),
|
|
|
|
endpoint: endpoint,
|
|
|
|
config: imagePullConfig,
|
|
|
|
repoInfo: repoInfo,
|
2015-11-18 17:18:44 -05:00
|
|
|
}, nil
|
|
|
|
case registry.APIVersion1:
|
|
|
|
return &v1Puller{
|
|
|
|
v1IDService: metadata.NewV1IDService(imagePullConfig.MetadataStore),
|
|
|
|
endpoint: endpoint,
|
|
|
|
config: imagePullConfig,
|
|
|
|
repoInfo: repoInfo,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown version %d for registry %s", endpoint.Version, endpoint.URL)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull initiates a pull operation. image is the repository name to pull, and
|
|
|
|
// tag may be either empty, or indicate a specific tag to pull.
|
2015-11-13 19:59:01 -05:00
|
|
|
func Pull(ctx context.Context, ref reference.Named, imagePullConfig *ImagePullConfig) error {
|
2015-11-18 17:18:44 -05:00
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
|
|
|
repoInfo, err := imagePullConfig.RegistryService.ResolveRepository(ref)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
// makes sure name is not `scratch`
|
|
|
|
if err := ValidateRepoName(repoInfo.Name); err != nil {
|
2015-11-18 17:18:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
endpoints, err := imagePullConfig.RegistryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-01-18 17:17:39 -05:00
|
|
|
lastErr error
|
2015-11-18 17:18:44 -05:00
|
|
|
|
|
|
|
// discardNoSupportErrors is used to track whether an endpoint encountered an error of type registry.ErrNoSupport
|
2016-04-28 02:08:21 -04:00
|
|
|
// By default it is false, which means that if an ErrNoSupport error is encountered, it will be saved in lastErr.
|
2015-11-18 17:18:44 -05:00
|
|
|
// As soon as another kind of error is encountered, discardNoSupportErrors is set to true, avoiding the saving of
|
2016-01-18 17:17:39 -05:00
|
|
|
// any subsequent ErrNoSupport errors in lastErr.
|
2015-11-18 17:18:44 -05:00
|
|
|
// It's needed for pull-by-digest on v1 endpoints: if there are only v1 endpoints configured, the error should be
|
|
|
|
// returned and displayed, but if there was a v2 endpoint which supports pull-by-digest, then the last relevant
|
|
|
|
// error is the ones from v2 endpoints not v1.
|
|
|
|
discardNoSupportErrors bool
|
2015-12-04 16:42:33 -05:00
|
|
|
|
|
|
|
// confirmedV2 is set to true if a pull attempt managed to
|
|
|
|
// confirm that it was talking to a v2 registry. This will
|
|
|
|
// prevent fallback to the v1 protocol.
|
|
|
|
confirmedV2 bool
|
2016-02-11 18:45:29 -05:00
|
|
|
|
|
|
|
// confirmedTLSRegistries is a map indicating which registries
|
|
|
|
// are known to be using TLS. There should never be a plaintext
|
|
|
|
// retry for any of these.
|
|
|
|
confirmedTLSRegistries = make(map[string]struct{})
|
2015-11-18 17:18:44 -05:00
|
|
|
)
|
|
|
|
for _, endpoint := range endpoints {
|
2016-12-16 14:19:05 -05:00
|
|
|
if imagePullConfig.RequireSchema2 && endpoint.Version == registry.APIVersion1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-12-04 16:42:33 -05:00
|
|
|
if confirmedV2 && endpoint.Version == registry.APIVersion1 {
|
|
|
|
logrus.Debugf("Skipping v1 endpoint %s because v2 registry was detected", endpoint.URL)
|
|
|
|
continue
|
|
|
|
}
|
2016-02-11 18:45:29 -05:00
|
|
|
|
2016-02-17 19:53:25 -05:00
|
|
|
if endpoint.URL.Scheme != "https" {
|
|
|
|
if _, confirmedTLS := confirmedTLSRegistries[endpoint.URL.Host]; confirmedTLS {
|
2016-02-11 18:45:29 -05:00
|
|
|
logrus.Debugf("Skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
logrus.Debugf("Trying to pull %s from %s %s", reference.FamiliarName(repoInfo.Name), endpoint.URL, endpoint.Version)
|
2015-11-18 17:18:44 -05:00
|
|
|
|
2015-11-13 19:59:01 -05:00
|
|
|
puller, err := newPuller(endpoint, repoInfo, imagePullConfig)
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
2016-01-18 17:17:39 -05:00
|
|
|
lastErr = err
|
2015-11-18 17:18:44 -05:00
|
|
|
continue
|
|
|
|
}
|
2015-12-04 16:42:33 -05:00
|
|
|
if err := puller.Pull(ctx, ref); err != nil {
|
2015-11-13 19:59:01 -05:00
|
|
|
// Was this pull cancelled? If so, don't try to fall
|
|
|
|
// back.
|
2015-12-04 16:42:33 -05:00
|
|
|
fallback := false
|
2015-11-13 19:59:01 -05:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
default:
|
2015-12-04 16:42:33 -05:00
|
|
|
if fallbackErr, ok := err.(fallbackError); ok {
|
|
|
|
fallback = true
|
|
|
|
confirmedV2 = confirmedV2 || fallbackErr.confirmedV2
|
2016-02-17 19:53:25 -05:00
|
|
|
if fallbackErr.transportOK && endpoint.URL.Scheme == "https" {
|
|
|
|
confirmedTLSRegistries[endpoint.URL.Host] = struct{}{}
|
2016-02-11 18:45:29 -05:00
|
|
|
}
|
2015-12-04 16:42:33 -05:00
|
|
|
err = fallbackErr.err
|
|
|
|
}
|
2015-11-13 19:59:01 -05:00
|
|
|
}
|
2015-11-18 17:18:44 -05:00
|
|
|
if fallback {
|
2016-02-11 17:08:49 -05:00
|
|
|
if _, ok := err.(ErrNoSupport); !ok {
|
2015-11-18 17:18:44 -05:00
|
|
|
// Because we found an error that's not ErrNoSupport, discard all subsequent ErrNoSupport errors.
|
|
|
|
discardNoSupportErrors = true
|
|
|
|
// append subsequent errors
|
2016-01-18 17:17:39 -05:00
|
|
|
lastErr = err
|
2015-11-18 17:18:44 -05:00
|
|
|
} else if !discardNoSupportErrors {
|
|
|
|
// Save the ErrNoSupport error, because it's either the first error or all encountered errors
|
|
|
|
// were also ErrNoSupport errors.
|
|
|
|
// append subsequent errors
|
2016-01-18 17:17:39 -05:00
|
|
|
lastErr = err
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
2017-02-20 11:50:31 -05:00
|
|
|
logrus.Infof("Attempting next endpoint for pull after error: %v", err)
|
2015-11-18 17:18:44 -05:00
|
|
|
continue
|
|
|
|
}
|
2016-02-11 17:08:49 -05:00
|
|
|
logrus.Errorf("Not continuing with pull after error: %v", err)
|
2016-11-15 18:06:48 -05:00
|
|
|
return TranslatePullError(err, ref)
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
imagePullConfig.ImageEventLogger(reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), "pull")
|
2015-11-18 17:18:44 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-18 17:17:39 -05:00
|
|
|
if lastErr == nil {
|
2017-01-25 19:54:18 -05:00
|
|
|
lastErr = fmt.Errorf("no endpoints found for %s", reference.FamiliarString(ref))
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:06:48 -05:00
|
|
|
return TranslatePullError(lastErr, ref)
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// writeStatus writes a status message to out. If layersDownloaded is true, the
|
|
|
|
// status message indicates that a newer image was downloaded. Otherwise, it
|
|
|
|
// indicates that the image is up to date. requestedTag is the tag the message
|
|
|
|
// will refer to.
|
2015-11-13 19:59:01 -05:00
|
|
|
func writeStatus(requestedTag string, out progress.Output, layersDownloaded bool) {
|
2015-11-18 17:18:44 -05:00
|
|
|
if layersDownloaded {
|
2015-11-13 19:59:01 -05:00
|
|
|
progress.Message(out, "", "Status: Downloaded newer image for "+requestedTag)
|
2015-11-18 17:18:44 -05:00
|
|
|
} else {
|
2015-11-13 19:59:01 -05:00
|
|
|
progress.Message(out, "", "Status: Image is up to date for "+requestedTag)
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 11:50:55 -04:00
|
|
|
// ValidateRepoName validates the name of a repository.
|
2017-01-25 19:54:18 -05:00
|
|
|
func ValidateRepoName(name reference.Named) error {
|
|
|
|
if reference.FamiliarName(name) == api.NoBaseImageSpecifier {
|
2017-07-19 10:20:13 -04:00
|
|
|
return errors.WithStack(reservedNameError(api.NoBaseImageSpecifier))
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-27 13:09:57 -04:00
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
func addDigestReference(store refstore.Store, ref reference.Named, dgst digest.Digest, id digest.Digest) error {
|
2016-11-10 18:59:02 -05:00
|
|
|
dgstRef, err := reference.WithDigest(reference.TrimNamed(ref), dgst)
|
2016-06-27 13:09:57 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
if oldTagID, err := store.Get(dgstRef); err == nil {
|
|
|
|
if oldTagID != id {
|
2016-06-27 13:09:57 -04:00
|
|
|
// Updating digests not supported by reference store
|
2016-09-15 19:37:32 -04:00
|
|
|
logrus.Errorf("Image ID for digest %s changed from %s to %s, cannot update", dgst.String(), oldTagID, id)
|
2016-06-27 13:09:57 -04:00
|
|
|
}
|
|
|
|
return nil
|
2017-01-25 19:54:18 -05:00
|
|
|
} else if err != refstore.ErrDoesNotExist {
|
2016-06-27 13:09:57 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-15 19:37:32 -04:00
|
|
|
return store.AddDigest(dgstRef, id, true)
|
2016-06-27 13:09:57 -04:00
|
|
|
}
|