2018-02-05 16:05:59 -05:00
|
|
|
package distribution // import "github.com/docker/docker/distribution"
|
2015-11-18 17:18:44 -05:00
|
|
|
|
|
|
|
import (
|
2018-04-19 18:30:59 -04:00
|
|
|
"context"
|
2015-11-18 17:18:44 -05:00
|
|
|
"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"
|
2017-01-25 19:54:18 -05:00
|
|
|
refstore "github.com/docker/docker/reference"
|
2022-03-04 08:49:42 -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-18 17:18:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
2022-02-27 14:57:07 -05:00
|
|
|
func Pull(ctx context.Context, ref reference.Named, config *ImagePullConfig, local ContentStore) error {
|
2015-11-18 17:18:44 -05:00
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
2022-02-27 14:57:07 -05:00
|
|
|
repoInfo, err := config.RegistryService.ResolveRepository(ref)
|
2015-11-18 17:18:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-25 19:54:18 -05:00
|
|
|
// makes sure name is not `scratch`
|
2022-02-27 16:11:13 -05:00
|
|
|
if err := validateRepoName(repoInfo.Name); err != nil {
|
2015-11-18 17:18:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-27 14:57:07 -05:00
|
|
|
endpoints, err := config.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
|
|
|
|
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-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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 14:57:07 -05:00
|
|
|
logrus.Debugf("Trying to pull %s from %s", reference.FamiliarName(repoInfo.Name), endpoint.URL)
|
2017-08-08 15:43:48 -04:00
|
|
|
|
2022-02-27 14:57:07 -05:00
|
|
|
if err := newPuller(endpoint, repoInfo, config, local).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
|
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 {
|
2022-02-27 14:46:24 -05:00
|
|
|
lastErr = err
|
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)
|
2022-02-27 14:46:24 -05:00
|
|
|
return translatePullError(err, ref)
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
2022-02-27 14:57:07 -05:00
|
|
|
config.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
|
|
|
}
|
|
|
|
|
2022-02-27 14:46:24 -05:00
|
|
|
return translatePullError(lastErr, ref)
|
2015-11-18 17:18:44 -05:00
|
|
|
}
|
|
|
|
|
2022-02-27 16:11:13 -05:00
|
|
|
// validateRepoName validates the name of a repository.
|
|
|
|
func validateRepoName(name reference.Named) error {
|
2017-01-25 19:54:18 -05:00
|
|
|
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
|
|
|
}
|