github.com/tonistiigi/docker@v0.10.1-0.20240229224939-974013b0dc6a/distribution/repository.go (about)

     1  package distribution
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/containerd/log"
     7  	"github.com/distribution/reference"
     8  	"github.com/docker/distribution"
     9  	"github.com/docker/docker/errdefs"
    10  )
    11  
    12  // GetRepositories returns a list of repositories configured for the given
    13  // reference. Multiple repositories can be returned if the reference is for
    14  // the default (Docker Hub) registry and a mirror is configured, but it omits
    15  // registries that were not reachable (pinging the /v2/ endpoint failed).
    16  //
    17  // It returns an error if it was unable to reach any of the registries for
    18  // the given reference, or if the provided reference is invalid.
    19  func GetRepositories(ctx context.Context, ref reference.Named, config *ImagePullConfig) ([]distribution.Repository, error) {
    20  	repoInfo, err := config.RegistryService.ResolveRepository(ref)
    21  	if err != nil {
    22  		return nil, errdefs.InvalidParameter(err)
    23  	}
    24  	// makes sure name is not empty or `scratch`
    25  	if err := validateRepoName(repoInfo.Name); err != nil {
    26  		return nil, errdefs.InvalidParameter(err)
    27  	}
    28  
    29  	endpoints, err := config.RegistryService.LookupPullEndpoints(reference.Domain(repoInfo.Name))
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	var (
    35  		repositories []distribution.Repository
    36  		lastError    error
    37  	)
    38  	for _, endpoint := range endpoints {
    39  		repo, err := newRepository(ctx, repoInfo, endpoint, nil, config.AuthConfig, "pull")
    40  		if err != nil {
    41  			log.G(ctx).WithFields(log.Fields{"endpoint": endpoint.URL.String(), "error": err}).Info("endpoint")
    42  			lastError = err
    43  			continue
    44  		}
    45  		repositories = append(repositories, repo)
    46  	}
    47  	if len(repositories) == 0 {
    48  		return nil, lastError
    49  	}
    50  	return repositories, nil
    51  }