github.com/rawahars/moby@v24.0.4+incompatible/distribution/pull.go (about)

     1  package distribution // import "github.com/docker/docker/distribution"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/distribution/reference"
     8  	"github.com/docker/docker/api"
     9  	refstore "github.com/docker/docker/reference"
    10  	"github.com/opencontainers/go-digest"
    11  	"github.com/pkg/errors"
    12  	"github.com/sirupsen/logrus"
    13  )
    14  
    15  // Pull initiates a pull operation. image is the repository name to pull, and
    16  // tag may be either empty, or indicate a specific tag to pull.
    17  func Pull(ctx context.Context, ref reference.Named, config *ImagePullConfig, local ContentStore) error {
    18  	// Resolve the Repository name from fqn to RepositoryInfo
    19  	repoInfo, err := config.RegistryService.ResolveRepository(ref)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	// makes sure name is not `scratch`
    25  	if err := validateRepoName(repoInfo.Name); err != nil {
    26  		return err
    27  	}
    28  
    29  	endpoints, err := config.RegistryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	var (
    35  		lastErr error
    36  
    37  		// confirmedTLSRegistries is a map indicating which registries
    38  		// are known to be using TLS. There should never be a plaintext
    39  		// retry for any of these.
    40  		confirmedTLSRegistries = make(map[string]struct{})
    41  	)
    42  	for _, endpoint := range endpoints {
    43  		if endpoint.URL.Scheme != "https" {
    44  			if _, confirmedTLS := confirmedTLSRegistries[endpoint.URL.Host]; confirmedTLS {
    45  				logrus.Debugf("Skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL)
    46  				continue
    47  			}
    48  		}
    49  
    50  		logrus.Debugf("Trying to pull %s from %s", reference.FamiliarName(repoInfo.Name), endpoint.URL)
    51  
    52  		if err := newPuller(endpoint, repoInfo, config, local).pull(ctx, ref); err != nil {
    53  			// Was this pull cancelled? If so, don't try to fall
    54  			// back.
    55  			fallback := false
    56  			select {
    57  			case <-ctx.Done():
    58  			default:
    59  				if fallbackErr, ok := err.(fallbackError); ok {
    60  					fallback = true
    61  					if fallbackErr.transportOK && endpoint.URL.Scheme == "https" {
    62  						confirmedTLSRegistries[endpoint.URL.Host] = struct{}{}
    63  					}
    64  					err = fallbackErr.err
    65  				}
    66  			}
    67  			if fallback {
    68  				lastErr = err
    69  				logrus.Infof("Attempting next endpoint for pull after error: %v", err)
    70  				continue
    71  			}
    72  			logrus.Errorf("Not continuing with pull after error: %v", err)
    73  			return translatePullError(err, ref)
    74  		}
    75  
    76  		config.ImageEventLogger(reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), "pull")
    77  		return nil
    78  	}
    79  
    80  	if lastErr == nil {
    81  		lastErr = fmt.Errorf("no endpoints found for %s", reference.FamiliarString(ref))
    82  	}
    83  
    84  	return translatePullError(lastErr, ref)
    85  }
    86  
    87  // validateRepoName validates the name of a repository.
    88  func validateRepoName(name reference.Named) error {
    89  	if reference.FamiliarName(name) == api.NoBaseImageSpecifier {
    90  		return errors.WithStack(reservedNameError(api.NoBaseImageSpecifier))
    91  	}
    92  	return nil
    93  }
    94  
    95  func addDigestReference(store refstore.Store, ref reference.Named, dgst digest.Digest, id digest.Digest) error {
    96  	dgstRef, err := reference.WithDigest(reference.TrimNamed(ref), dgst)
    97  	if err != nil {
    98  		return err
    99  	}
   100  
   101  	if oldTagID, err := store.Get(dgstRef); err == nil {
   102  		if oldTagID != id {
   103  			// Updating digests not supported by reference store
   104  			logrus.Errorf("Image ID for digest %s changed from %s to %s, cannot update", dgst.String(), oldTagID, id)
   105  		}
   106  		return nil
   107  	} else if err != refstore.ErrDoesNotExist {
   108  		return err
   109  	}
   110  
   111  	return store.AddDigest(dgstRef, id, true)
   112  }