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"
|
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2023-08-30 16:31:46 +00:00
|
|
|
"github.com/distribution/reference"
|
2015-12-31 13:57:58 +00:00
|
|
|
"github.com/docker/docker/api"
|
2023-08-26 13:24:46 +00:00
|
|
|
"github.com/docker/docker/api/types/events"
|
2017-01-26 00:54:18 +00:00
|
|
|
refstore "github.com/docker/docker/reference"
|
2023-09-20 09:47:39 +00:00
|
|
|
"github.com/docker/docker/registry"
|
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"
|
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 {
|
2023-09-20 09:47:39 +00:00
|
|
|
repoInfo, err := pullEndpoints(ctx, config.RegistryService, ref, func(ctx context.Context, repoInfo registry.RepositoryInfo, endpoint registry.APIEndpoint) error {
|
|
|
|
log.G(ctx).Debugf("Trying to pull %s from %s", reference.FamiliarName(repoInfo.Name), endpoint.URL)
|
|
|
|
puller := newPuller(endpoint, &repoInfo, config, local)
|
|
|
|
return puller.pull(ctx, ref)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
config.ImageEventLogger(reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), events.ActionPull)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tags returns available tags for the given image in the remote repository.
|
|
|
|
func Tags(ctx context.Context, ref reference.Named, config *Config) ([]string, error) {
|
|
|
|
var tags []string
|
|
|
|
_, err := pullEndpoints(ctx, config.RegistryService, ref, func(ctx context.Context, repoInfo registry.RepositoryInfo, endpoint registry.APIEndpoint) error {
|
|
|
|
repo, err := newRepository(ctx, &repoInfo, endpoint, config.MetaHeaders, config.AuthConfig, "pull")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tags, err = repo.Tags(ctx).All(ctx)
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
|
|
|
return tags, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateRepoName validates the name of a repository.
|
|
|
|
func validateRepoName(name reference.Named) error {
|
|
|
|
if reference.FamiliarName(name) == api.NoBaseImageSpecifier {
|
|
|
|
return errors.WithStack(reservedNameError(api.NoBaseImageSpecifier))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addDigestReference(store refstore.Store, ref reference.Named, dgst digest.Digest, id digest.Digest) error {
|
|
|
|
dgstRef, err := reference.WithDigest(reference.TrimNamed(ref), dgst)
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-09-20 09:47:39 +00:00
|
|
|
if oldTagID, err := store.Get(dgstRef); err == nil {
|
|
|
|
if oldTagID != id {
|
|
|
|
// Updating digests not supported by reference store
|
|
|
|
log.G(context.TODO()).Errorf("Image ID for digest %s changed from %s to %s, cannot update", dgst.String(), oldTagID, id)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} else if err != refstore.ErrDoesNotExist {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return store.AddDigest(dgstRef, id, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func pullEndpoints(ctx context.Context, registryService RegistryResolver, ref reference.Named,
|
|
|
|
f func(context.Context, registry.RepositoryInfo, registry.APIEndpoint) error,
|
|
|
|
) (*registry.RepositoryInfo, error) {
|
|
|
|
// Resolve the Repository name from fqn to RepositoryInfo
|
|
|
|
repoInfo, err := registryService.ResolveRepository(ref)
|
|
|
|
if err != nil {
|
|
|
|
return nil, 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 {
|
2023-09-20 09:47:39 +00:00
|
|
|
return repoInfo, err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 09:47:39 +00:00
|
|
|
endpoints, err := registryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
|
2015-11-18 22:18:44 +00:00
|
|
|
if err != nil {
|
2023-09-20 09:47:39 +00:00
|
|
|
return repoInfo, err
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).Debugf("Skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL)
|
2016-02-11 23:45:29 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).Debugf("Trying to pull %s from %s", reference.FamiliarName(repoInfo.Name), endpoint.URL)
|
2017-08-08 19:43:48 +00:00
|
|
|
|
2023-09-20 09:47:39 +00:00
|
|
|
if err := f(ctx, *repoInfo, endpoint); err != nil {
|
|
|
|
if _, ok := err.(fallbackError); !ok && continueOnError(err, endpoint.Mirror) {
|
|
|
|
err = fallbackError{
|
|
|
|
err: err,
|
|
|
|
transportOK: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).Infof("Attempting next endpoint for pull after error: %v", err)
|
2015-11-18 22:18:44 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(ctx).Errorf("Not continuing with pull after error: %v", err)
|
2023-09-20 09:47:39 +00:00
|
|
|
return repoInfo, translatePullError(err, ref)
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
2023-09-20 09:47:39 +00:00
|
|
|
return repoInfo, nil
|
2015-11-18 22:18:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-09-20 09:47:39 +00:00
|
|
|
return repoInfo, translatePullError(lastErr, ref)
|
2016-06-27 17:09:57 +00:00
|
|
|
}
|