github.com/lazyboychen7/engine@v17.12.1-ce-rc2+incompatible/distribution/pull.go (about)

     1  package distribution
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  
     7  	"github.com/docker/distribution/reference"
     8  	"github.com/docker/docker/api"
     9  	"github.com/docker/docker/distribution/metadata"
    10  	"github.com/docker/docker/pkg/progress"
    11  	refstore "github.com/docker/docker/reference"
    12  	"github.com/docker/docker/registry"
    13  	"github.com/opencontainers/go-digest"
    14  	"github.com/pkg/errors"
    15  	"github.com/sirupsen/logrus"
    16  	"golang.org/x/net/context"
    17  )
    18  
    19  // Puller is an interface that abstracts pulling for different API versions.
    20  type Puller interface {
    21  	// Pull tries to pull the image referenced by `tag`
    22  	// Pull returns an error if any, as well as a boolean that determines whether to retry Pull on the next configured endpoint.
    23  	//
    24  	Pull(ctx context.Context, ref reference.Named, platform string) error
    25  }
    26  
    27  // newPuller returns a Puller interface that will pull from either a v1 or v2
    28  // registry. The endpoint argument contains a Version field that determines
    29  // whether a v1 or v2 puller will be created. The other parameters are passed
    30  // through to the underlying puller implementation for use during the actual
    31  // pull operation.
    32  func newPuller(endpoint registry.APIEndpoint, repoInfo *registry.RepositoryInfo, imagePullConfig *ImagePullConfig) (Puller, error) {
    33  	switch endpoint.Version {
    34  	case registry.APIVersion2:
    35  		return &v2Puller{
    36  			V2MetadataService: metadata.NewV2MetadataService(imagePullConfig.MetadataStore),
    37  			endpoint:          endpoint,
    38  			config:            imagePullConfig,
    39  			repoInfo:          repoInfo,
    40  		}, nil
    41  	case registry.APIVersion1:
    42  		return &v1Puller{
    43  			v1IDService: metadata.NewV1IDService(imagePullConfig.MetadataStore),
    44  			endpoint:    endpoint,
    45  			config:      imagePullConfig,
    46  			repoInfo:    repoInfo,
    47  		}, nil
    48  	}
    49  	return nil, fmt.Errorf("unknown version %d for registry %s", endpoint.Version, endpoint.URL)
    50  }
    51  
    52  // Pull initiates a pull operation. image is the repository name to pull, and
    53  // tag may be either empty, or indicate a specific tag to pull.
    54  func Pull(ctx context.Context, ref reference.Named, imagePullConfig *ImagePullConfig) error {
    55  	// Resolve the Repository name from fqn to RepositoryInfo
    56  	repoInfo, err := imagePullConfig.RegistryService.ResolveRepository(ref)
    57  	if err != nil {
    58  		return err
    59  	}
    60  
    61  	// makes sure name is not `scratch`
    62  	if err := ValidateRepoName(repoInfo.Name); err != nil {
    63  		return err
    64  	}
    65  
    66  	endpoints, err := imagePullConfig.RegistryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
    67  	if err != nil {
    68  		return err
    69  	}
    70  
    71  	var (
    72  		lastErr error
    73  
    74  		// discardNoSupportErrors is used to track whether an endpoint encountered an error of type registry.ErrNoSupport
    75  		// By default it is false, which means that if an ErrNoSupport error is encountered, it will be saved in lastErr.
    76  		// As soon as another kind of error is encountered, discardNoSupportErrors is set to true, avoiding the saving of
    77  		// any subsequent ErrNoSupport errors in lastErr.
    78  		// It's needed for pull-by-digest on v1 endpoints: if there are only v1 endpoints configured, the error should be
    79  		// returned and displayed, but if there was a v2 endpoint which supports pull-by-digest, then the last relevant
    80  		// error is the ones from v2 endpoints not v1.
    81  		discardNoSupportErrors bool
    82  
    83  		// confirmedV2 is set to true if a pull attempt managed to
    84  		// confirm that it was talking to a v2 registry. This will
    85  		// prevent fallback to the v1 protocol.
    86  		confirmedV2 bool
    87  
    88  		// confirmedTLSRegistries is a map indicating which registries
    89  		// are known to be using TLS. There should never be a plaintext
    90  		// retry for any of these.
    91  		confirmedTLSRegistries = make(map[string]struct{})
    92  	)
    93  	for _, endpoint := range endpoints {
    94  		if imagePullConfig.RequireSchema2 && endpoint.Version == registry.APIVersion1 {
    95  			continue
    96  		}
    97  
    98  		if confirmedV2 && endpoint.Version == registry.APIVersion1 {
    99  			logrus.Debugf("Skipping v1 endpoint %s because v2 registry was detected", endpoint.URL)
   100  			continue
   101  		}
   102  
   103  		if endpoint.URL.Scheme != "https" {
   104  			if _, confirmedTLS := confirmedTLSRegistries[endpoint.URL.Host]; confirmedTLS {
   105  				logrus.Debugf("Skipping non-TLS endpoint %s for host/port that appears to use TLS", endpoint.URL)
   106  				continue
   107  			}
   108  		}
   109  
   110  		logrus.Debugf("Trying to pull %s from %s %s", reference.FamiliarName(repoInfo.Name), endpoint.URL, endpoint.Version)
   111  
   112  		puller, err := newPuller(endpoint, repoInfo, imagePullConfig)
   113  		if err != nil {
   114  			lastErr = err
   115  			continue
   116  		}
   117  
   118  		// Make sure we default the platform if it hasn't been supplied
   119  		if imagePullConfig.Platform == "" {
   120  			imagePullConfig.Platform = runtime.GOOS
   121  		}
   122  
   123  		if err := puller.Pull(ctx, ref, imagePullConfig.Platform); err != nil {
   124  			// Was this pull cancelled? If so, don't try to fall
   125  			// back.
   126  			fallback := false
   127  			select {
   128  			case <-ctx.Done():
   129  			default:
   130  				if fallbackErr, ok := err.(fallbackError); ok {
   131  					fallback = true
   132  					confirmedV2 = confirmedV2 || fallbackErr.confirmedV2
   133  					if fallbackErr.transportOK && endpoint.URL.Scheme == "https" {
   134  						confirmedTLSRegistries[endpoint.URL.Host] = struct{}{}
   135  					}
   136  					err = fallbackErr.err
   137  				}
   138  			}
   139  			if fallback {
   140  				if _, ok := err.(ErrNoSupport); !ok {
   141  					// Because we found an error that's not ErrNoSupport, discard all subsequent ErrNoSupport errors.
   142  					discardNoSupportErrors = true
   143  					// append subsequent errors
   144  					lastErr = err
   145  				} else if !discardNoSupportErrors {
   146  					// Save the ErrNoSupport error, because it's either the first error or all encountered errors
   147  					// were also ErrNoSupport errors.
   148  					// append subsequent errors
   149  					lastErr = err
   150  				}
   151  				logrus.Infof("Attempting next endpoint for pull after error: %v", err)
   152  				continue
   153  			}
   154  			logrus.Errorf("Not continuing with pull after error: %v", err)
   155  			return TranslatePullError(err, ref)
   156  		}
   157  
   158  		imagePullConfig.ImageEventLogger(reference.FamiliarString(ref), reference.FamiliarName(repoInfo.Name), "pull")
   159  		return nil
   160  	}
   161  
   162  	if lastErr == nil {
   163  		lastErr = fmt.Errorf("no endpoints found for %s", reference.FamiliarString(ref))
   164  	}
   165  
   166  	return TranslatePullError(lastErr, ref)
   167  }
   168  
   169  // writeStatus writes a status message to out. If layersDownloaded is true, the
   170  // status message indicates that a newer image was downloaded. Otherwise, it
   171  // indicates that the image is up to date. requestedTag is the tag the message
   172  // will refer to.
   173  func writeStatus(requestedTag string, out progress.Output, layersDownloaded bool) {
   174  	if layersDownloaded {
   175  		progress.Message(out, "", "Status: Downloaded newer image for "+requestedTag)
   176  	} else {
   177  		progress.Message(out, "", "Status: Image is up to date for "+requestedTag)
   178  	}
   179  }
   180  
   181  // ValidateRepoName validates the name of a repository.
   182  func ValidateRepoName(name reference.Named) error {
   183  	if reference.FamiliarName(name) == api.NoBaseImageSpecifier {
   184  		return errors.WithStack(reservedNameError(api.NoBaseImageSpecifier))
   185  	}
   186  	return nil
   187  }
   188  
   189  func addDigestReference(store refstore.Store, ref reference.Named, dgst digest.Digest, id digest.Digest) error {
   190  	dgstRef, err := reference.WithDigest(reference.TrimNamed(ref), dgst)
   191  	if err != nil {
   192  		return err
   193  	}
   194  
   195  	if oldTagID, err := store.Get(dgstRef); err == nil {
   196  		if oldTagID != id {
   197  			// Updating digests not supported by reference store
   198  			logrus.Errorf("Image ID for digest %s changed from %s to %s, cannot update", dgst.String(), oldTagID, id)
   199  		}
   200  		return nil
   201  	} else if err != refstore.ErrDoesNotExist {
   202  		return err
   203  	}
   204  
   205  	return store.AddDigest(dgstRef, id, true)
   206  }