2018-02-05 21:05:59 +00:00
|
|
|
package distribution // import "github.com/docker/docker/distribution"
|
2015-11-18 22:18:44 +00:00
|
|
|
|
|
|
|
import (
|
2018-04-19 22:30:59 +00:00
|
|
|
"context"
|
2015-11-18 22:18:44 +00:00
|
|
|
"fmt"
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-12-31 13:57:58 +00:00
|
|
|
"github.com/docker/docker/api"
|
2017-01-26 00:54:18 +00:00
|
|
|
refstore "github.com/docker/docker/reference"
|
2022-03-04 13:49:42 +00:00
|
|
|
"github.com/opencontainers/go-digest"
|
2017-07-19 14:20:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-26 21:42:13 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2015-11-18 22:18:44 +00: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 19:57:07 +00:00
|
|
|
func Pull(ctx context.Context, ref reference.Named, config *ImagePullConfig, local ContentStore) error {
|
2015-11-18 22:18:44 +00:00
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
2022-02-27 19:57:07 +00:00
|
|
|
repoInfo, err := config.RegistryService.ResolveRepository(ref)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
// makes sure name is not `scratch`
|
2022-02-27 21:11:13 +00:00
|
|
|
if err := validateRepoName(repoInfo.Name); err != nil {
|
2015-11-18 22:18:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-27 19:57:07 +00:00
|
|
|
endpoints, err := config.RegistryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-01-18 22:17:39 +00:00
|
|
|
lastErr error
|
2015-11-18 22:18:44 +00:00
|
|
|
|
2016-02-11 23:45:29 +00: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 22:18:44 +00:00
|
|
|
)
|
|
|
|
for _, endpoint := range endpoints {
|
2016-02-18 00:53:25 +00:00
|
|
|
if endpoint.URL.Scheme != "https" {
|
|
|
|
if _, confirmedTLS := confirmedTLSRegistries[endpoint.URL.Host]; confirmedTLS {
|
2016-02-11 23:45:29 +00:00
|
|
|
logrus.Debugf("Skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 19:57:07 +00:00
|
|
|
logrus.Debugf("Trying to pull %s from %s", reference.FamiliarName(repoInfo.Name), endpoint.URL)
|
2017-08-08 19:43:48 +00:00
|
|
|
|
2022-02-27 19:57:07 +00:00
|
|
|
if err := newPuller(endpoint, repoInfo, config, local).pull(ctx, ref); err != nil {
|
2015-11-14 00:59:01 +00:00
|
|
|
// Was this pull cancelled? If so, don't try to fall
|
|
|
|
// back.
|
2015-12-04 21:42:33 +00:00
|
|
|
fallback := false
|
2015-11-14 00:59:01 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
default:
|
2015-12-04 21:42:33 +00:00
|
|
|
if fallbackErr, ok := err.(fallbackError); ok {
|
|
|
|
fallback = true
|
2016-02-18 00:53:25 +00:00
|
|
|
if fallbackErr.transportOK && endpoint.URL.Scheme == "https" {
|
|
|
|
confirmedTLSRegistries[endpoint.URL.Host] = struct{}{}
|
2016-02-11 23:45:29 +00:00
|
|
|
}
|
2015-12-04 21:42:33 +00:00
|
|
|
err = fallbackErr.err
|
|
|
|
}
|
2015-11-14 00:59:01 +00:00
|
|
|
}
|
2015-11-18 22:18:44 +00:00
|
|
|
if fallback {
|
2022-02-27 19:46:24 +00:00
|
|
|
lastErr = err
|
2017-02-20 16:50:31 +00:00
|
|
|
logrus.Infof("Attempting next endpoint for pull after error: %v", err)
|
2015-11-18 22:18:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2016-02-11 22:08:49 +00:00
|
|
|
logrus.Errorf("Not continuing with pull after error: %v", err)
|
2022-02-27 19:46:24 +00:00
|
|
|
return translatePullError(err, ref)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 19:57:07 +00:00
|
|
|
config.ImageEventLogger(reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), "pull")
|
2015-11-18 22:18:44 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-01-18 22:17:39 +00:00
|
|
|
if lastErr == nil {
|
2017-01-26 00:54:18 +00:00
|
|
|
lastErr = fmt.Errorf("no endpoints found for %s", reference.FamiliarString(ref))
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 19:46:24 +00:00
|
|
|
return translatePullError(lastErr, ref)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2022-02-27 21:11:13 +00:00
|
|
|
// validateRepoName validates the name of a repository.
|
|
|
|
func validateRepoName(name reference.Named) error {
|
2017-01-26 00:54:18 +00:00
|
|
|
if reference.FamiliarName(name) == api.NoBaseImageSpecifier {
|
2017-07-19 14:20:13 +00:00
|
|
|
return errors.WithStack(reservedNameError(api.NoBaseImageSpecifier))
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-27 17:09:57 +00:00
|
|
|
|
2017-01-26 00:54:18 +00:00
|
|
|
func addDigestReference(store refstore.Store, ref reference.Named, dgst digest.Digest, id digest.Digest) error {
|
2016-11-10 23:59:02 +00:00
|
|
|
dgstRef, err := reference.WithDigest(reference.TrimNamed(ref), dgst)
|
2016-06-27 17:09:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-15 23:37:32 +00:00
|
|
|
if oldTagID, err := store.Get(dgstRef); err == nil {
|
|
|
|
if oldTagID != id {
|
2016-06-27 17:09:57 +00:00
|
|
|
// Updating digests not supported by reference store
|
2016-09-15 23:37:32 +00:00
|
|
|
logrus.Errorf("Image ID for digest %s changed from %s to %s, cannot update", dgst.String(), oldTagID, id)
|
2016-06-27 17:09:57 +00:00
|
|
|
}
|
|
|
|
return nil
|
2017-01-26 00:54:18 +00:00
|
|
|
} else if err != refstore.ErrDoesNotExist {
|
2016-06-27 17:09:57 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-09-15 23:37:32 +00:00
|
|
|
return store.AddDigest(dgstRef, id, true)
|
2016-06-27 17:09:57 +00:00
|
|
|
}
|