Files
moby/distribution/pull.go
Sebastiaan van Stijn 52774154c9 distribution: continueOnError: handle context cancellation / timeout
Before this change, it would fail to detect context errors, resulting in
pullEndpoints clobbering the context error and changing it into a fallback
error; 029933578b/distribution/pull.go (L114-L119)

While the context cancellation would still be handled, the error returned
would be wrapped, causing calling code to no longer being able to detect
it as context cancellation;
029933578b/distribution/pull.go (L125)

Context cancellation are now logged as "info" in daemon-logs, as they
are not an error from the daemon's perspective;

Before:

    DEBU[2025-01-18T14:59:10.079259676Z] pulling blob "sha256:8bb55f0677778c3027fcc4253dc452bc9c22de989a696391e739fb1cdbbdb4c2"
    ERRO[2025-01-18T14:59:10.564076135Z] Not continuing with pull after error: context canceled

After:

    DEBU[2025-01-18T15:09:56.743045420Z] pulling blob "sha256:8bb55f0677778c3027fcc4253dc452bc9c22de989a696391e739fb1cdbbdb4c2"
    INFO[2025-01-18T15:09:57.390835628Z] Not continuing with pull after error          error="context canceled"

This package needs a big cleanup for context- and error-handling, as it's
very messy, so these changes are only a short-term workaround.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2025-01-18 16:20:49 +01:00

159 lines
5.0 KiB
Go

package distribution // import "github.com/docker/docker/distribution"
import (
"context"
"fmt"
"github.com/containerd/log"
"github.com/distribution/reference"
"github.com/docker/docker/api"
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/errdefs"
refstore "github.com/docker/docker/reference"
"github.com/docker/docker/registry"
"github.com/opencontainers/go-digest"
"github.com/pkg/errors"
)
// 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.
func Pull(ctx context.Context, ref reference.Named, config *ImagePullConfig, local ContentStore) error {
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(ctx, 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)
if err != nil {
return err
}
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
}
// makes sure name is not `scratch`
if err := validateRepoName(repoInfo.Name); err != nil {
return repoInfo, err
}
endpoints, err := registryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
if err != nil {
return repoInfo, err
}
var (
lastErr error
// 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{})
)
for _, endpoint := range endpoints {
if endpoint.URL.Scheme != "https" {
if _, confirmedTLS := confirmedTLSRegistries[endpoint.URL.Host]; confirmedTLS {
log.G(ctx).Debugf("Skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL)
continue
}
}
log.G(ctx).Debugf("Trying to pull %s from %s", reference.FamiliarName(repoInfo.Name), endpoint.URL)
if err := f(ctx, *repoInfo, endpoint); err != nil {
if _, ok := err.(fallbackError); !ok && continueOnError(err, endpoint.Mirror) {
err = fallbackError{
err: err,
transportOK: true,
}
}
// Was this pull cancelled? If so, don't try to fall
// back.
fallback := false
select {
case <-ctx.Done():
default:
if fallbackErr, ok := err.(fallbackError); ok {
fallback = true
if fallbackErr.transportOK && endpoint.URL.Scheme == "https" {
confirmedTLSRegistries[endpoint.URL.Host] = struct{}{}
}
err = fallbackErr.err
}
}
if fallback {
lastErr = err
log.G(ctx).Infof("Attempting next endpoint for pull after error: %v", err)
continue
}
// FIXME(thaJeztah): cleanup error and context handling in this package, as it's really messy.
if errdefs.IsContext(err) {
log.G(ctx).WithError(err).Info("Not continuing with pull after error")
} else {
log.G(ctx).WithError(err).Error("Not continuing with pull after error")
}
return repoInfo, translatePullError(err, ref)
}
return repoInfo, nil
}
if lastErr == nil {
lastErr = fmt.Errorf("no endpoints found for %s", reference.FamiliarString(ref))
}
return repoInfo, translatePullError(lastErr, ref)
}