2014-10-07 01:54:52 +00:00
|
|
|
package registry
|
|
|
|
|
|
|
|
import (
|
2015-01-07 23:42:01 +00:00
|
|
|
"errors"
|
2014-10-07 01:54:52 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/url"
|
2015-01-07 23:42:01 +00:00
|
|
|
"strings"
|
2014-10-07 01:54:52 +00:00
|
|
|
|
2015-10-31 00:46:25 +00:00
|
|
|
"github.com/docker/distribution/reference"
|
2015-12-12 02:14:52 +00:00
|
|
|
registrytypes "github.com/docker/docker/api/types/registry"
|
2015-11-18 22:20:54 +00:00
|
|
|
"github.com/docker/docker/image/v1"
|
2014-10-07 01:54:52 +00:00
|
|
|
"github.com/docker/docker/opts"
|
|
|
|
flag "github.com/docker/docker/pkg/mflag"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Options holds command line options.
|
|
|
|
type Options struct {
|
|
|
|
Mirrors opts.ListOpts
|
|
|
|
InsecureRegistries opts.ListOpts
|
|
|
|
}
|
|
|
|
|
2015-08-04 23:30:00 +00:00
|
|
|
const (
|
|
|
|
// DefaultNamespace is the default namespace
|
|
|
|
DefaultNamespace = "docker.io"
|
|
|
|
// DefaultRegistryVersionHeader is the name of the default HTTP header
|
|
|
|
// that carries Registry version info
|
|
|
|
DefaultRegistryVersionHeader = "Docker-Distribution-Api-Version"
|
|
|
|
|
|
|
|
// IndexServer is the v1 registry server used for user auth + account creation
|
|
|
|
IndexServer = DefaultV1Registry + "/v1/"
|
|
|
|
// IndexName is the name of the index
|
|
|
|
IndexName = "docker.io"
|
|
|
|
|
|
|
|
// NotaryServer is the endpoint serving the Notary trust server
|
|
|
|
NotaryServer = "https://notary.docker.io"
|
|
|
|
|
|
|
|
// IndexServer = "https://registry-stage.hub.docker.com/v1/"
|
|
|
|
)
|
|
|
|
|
2015-01-07 23:42:01 +00:00
|
|
|
var (
|
2015-07-21 19:40:36 +00:00
|
|
|
// ErrInvalidRepositoryName is an error returned if the repository name did
|
|
|
|
// not have the correct form
|
2015-01-07 23:42:01 +00:00
|
|
|
ErrInvalidRepositoryName = errors.New("Invalid repository name (ex: \"registry.domain.tld/myrepos\")")
|
2015-07-21 19:40:36 +00:00
|
|
|
|
|
|
|
emptyServiceConfig = NewServiceConfig(nil)
|
2015-09-16 17:42:17 +00:00
|
|
|
|
|
|
|
// V2Only controls access to legacy registries. If it is set to true via the
|
|
|
|
// command line flag the daemon will not attempt to contact v1 legacy registries
|
|
|
|
V2Only = false
|
2015-01-07 23:42:01 +00:00
|
|
|
)
|
|
|
|
|
2014-10-07 01:54:52 +00:00
|
|
|
// InstallFlags adds command-line options to the top-level flag parser for
|
|
|
|
// the current process.
|
2015-05-05 04:18:28 +00:00
|
|
|
func (options *Options) InstallFlags(cmd *flag.FlagSet, usageFn func(string) string) {
|
2014-10-07 01:54:52 +00:00
|
|
|
options.Mirrors = opts.NewListOpts(ValidateMirror)
|
2015-05-05 04:18:28 +00:00
|
|
|
cmd.Var(&options.Mirrors, []string{"-registry-mirror"}, usageFn("Preferred Docker registry mirror"))
|
2014-10-07 01:54:52 +00:00
|
|
|
options.InsecureRegistries = opts.NewListOpts(ValidateIndexName)
|
2015-05-05 04:18:28 +00:00
|
|
|
cmd.Var(&options.InsecureRegistries, []string{"-insecure-registry"}, usageFn("Enable insecure registry communication"))
|
2015-10-06 22:45:32 +00:00
|
|
|
cmd.BoolVar(&V2Only, []string{"-disable-legacy-registry"}, false, "Do not contact legacy registries")
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewServiceConfig returns a new instance of ServiceConfig
|
2015-12-12 02:14:52 +00:00
|
|
|
func NewServiceConfig(options *Options) *registrytypes.ServiceConfig {
|
2014-10-07 01:54:52 +00:00
|
|
|
if options == nil {
|
|
|
|
options = &Options{
|
|
|
|
Mirrors: opts.NewListOpts(nil),
|
|
|
|
InsecureRegistries: opts.NewListOpts(nil),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Localhost is by default considered as an insecure registry
|
|
|
|
// This is a stop-gap for people who are running a private registry on localhost (especially on Boot2docker).
|
|
|
|
//
|
|
|
|
// TODO: should we deprecate this once it is easier for people to set up a TLS registry or change
|
|
|
|
// daemon flags on boot2docker?
|
|
|
|
options.InsecureRegistries.Set("127.0.0.0/8")
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
config := ®istrytypes.ServiceConfig{
|
|
|
|
InsecureRegistryCIDRs: make([]*registrytypes.NetIPNet, 0),
|
|
|
|
IndexConfigs: make(map[string]*registrytypes.IndexInfo, 0),
|
2015-02-12 18:23:22 +00:00
|
|
|
// Hack: Bypass setting the mirrors to IndexConfigs since they are going away
|
|
|
|
// and Mirrors are only for the official registry anyways.
|
|
|
|
Mirrors: options.Mirrors.GetAll(),
|
2014-10-07 01:54:52 +00:00
|
|
|
}
|
|
|
|
// Split --insecure-registry into CIDR and registry-specific settings.
|
|
|
|
for _, r := range options.InsecureRegistries.GetAll() {
|
|
|
|
// Check if CIDR was passed to --insecure-registry
|
|
|
|
_, ipnet, err := net.ParseCIDR(r)
|
|
|
|
if err == nil {
|
|
|
|
// Valid CIDR.
|
2015-12-12 02:14:52 +00:00
|
|
|
config.InsecureRegistryCIDRs = append(config.InsecureRegistryCIDRs, (*registrytypes.NetIPNet)(ipnet))
|
2014-10-07 01:54:52 +00:00
|
|
|
} else {
|
|
|
|
// Assume `host:port` if not CIDR.
|
2015-12-12 02:14:52 +00:00
|
|
|
config.IndexConfigs[r] = ®istrytypes.IndexInfo{
|
2014-10-07 01:54:52 +00:00
|
|
|
Name: r,
|
|
|
|
Mirrors: make([]string, 0),
|
|
|
|
Secure: false,
|
|
|
|
Official: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure public registry.
|
2015-12-12 02:14:52 +00:00
|
|
|
config.IndexConfigs[IndexName] = ®istrytypes.IndexInfo{
|
2015-07-21 19:40:36 +00:00
|
|
|
Name: IndexName,
|
2015-02-12 18:23:22 +00:00
|
|
|
Mirrors: config.Mirrors,
|
2014-10-07 01:54:52 +00:00
|
|
|
Secure: true,
|
|
|
|
Official: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return config
|
|
|
|
}
|
2015-01-07 23:42:01 +00:00
|
|
|
|
|
|
|
// isSecureIndex returns false if the provided indexName is part of the list of insecure registries
|
|
|
|
// Insecure registries accept HTTP and/or accept HTTPS with certificates from unknown CAs.
|
|
|
|
//
|
|
|
|
// The list of insecure registries can contain an element with CIDR notation to specify a whole subnet.
|
|
|
|
// If the subnet contains one of the IPs of the registry specified by indexName, the latter is considered
|
|
|
|
// insecure.
|
|
|
|
//
|
|
|
|
// indexName should be a URL.Host (`host:port` or `host`) where the `host` part can be either a domain name
|
|
|
|
// or an IP address. If it is a domain name, then it will be resolved in order to check if the IP is contained
|
|
|
|
// in a subnet. If the resolving is not successful, isSecureIndex will only try to match hostname to any element
|
|
|
|
// of insecureRegistries.
|
2015-12-12 02:14:52 +00:00
|
|
|
func isSecureIndex(config *registrytypes.ServiceConfig, indexName string) bool {
|
2015-01-07 23:42:01 +00:00
|
|
|
// Check for configured index, first. This is needed in case isSecureIndex
|
2015-12-12 02:14:52 +00:00
|
|
|
// is called from anything besides newIndexInfo, in order to honor per-index configurations.
|
2015-01-07 23:42:01 +00:00
|
|
|
if index, ok := config.IndexConfigs[indexName]; ok {
|
|
|
|
return index.Secure
|
|
|
|
}
|
|
|
|
|
|
|
|
host, _, err := net.SplitHostPort(indexName)
|
|
|
|
if err != nil {
|
|
|
|
// assume indexName is of the form `host` without the port and go on.
|
|
|
|
host = indexName
|
|
|
|
}
|
|
|
|
|
|
|
|
addrs, err := lookupIP(host)
|
|
|
|
if err != nil {
|
|
|
|
ip := net.ParseIP(host)
|
|
|
|
if ip != nil {
|
|
|
|
addrs = []net.IP{ip}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if ip == nil, then `host` is neither an IP nor it could be looked up,
|
|
|
|
// either because the index is unreachable, or because the index is behind an HTTP proxy.
|
|
|
|
// So, len(addrs) == 0 and we're not aborting.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try CIDR notation only if addrs has any elements, i.e. if `host`'s IP could be determined.
|
|
|
|
for _, addr := range addrs {
|
|
|
|
for _, ipnet := range config.InsecureRegistryCIDRs {
|
|
|
|
// check if the addr falls in the subnet
|
|
|
|
if (*net.IPNet)(ipnet).Contains(addr) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateMirror validates an HTTP(S) registry mirror
|
|
|
|
func ValidateMirror(val string) (string, error) {
|
|
|
|
uri, err := url.Parse(val)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("%s is not a valid URI", val)
|
|
|
|
}
|
|
|
|
|
|
|
|
if uri.Scheme != "http" && uri.Scheme != "https" {
|
|
|
|
return "", fmt.Errorf("Unsupported scheme %s", uri.Scheme)
|
|
|
|
}
|
|
|
|
|
|
|
|
if uri.Path != "" || uri.RawQuery != "" || uri.Fragment != "" {
|
|
|
|
return "", fmt.Errorf("Unsupported path/query/fragment at end of the URI")
|
|
|
|
}
|
|
|
|
|
2015-05-16 00:48:20 +00:00
|
|
|
return fmt.Sprintf("%s://%s/", uri.Scheme, uri.Host), nil
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateIndexName validates an index name.
|
|
|
|
func ValidateIndexName(val string) (string, error) {
|
|
|
|
// 'index.docker.io' => 'docker.io'
|
2015-07-21 19:40:36 +00:00
|
|
|
if val == "index."+IndexName {
|
|
|
|
val = IndexName
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
2015-05-13 06:23:13 +00:00
|
|
|
if strings.HasPrefix(val, "-") || strings.HasSuffix(val, "-") {
|
|
|
|
return "", fmt.Errorf("Invalid index name (%s). Cannot begin or end with a hyphen.", val)
|
|
|
|
}
|
2015-01-07 23:42:01 +00:00
|
|
|
// *TODO: Check if valid hostname[:port]/ip[:port]?
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:20:54 +00:00
|
|
|
func validateRemoteName(remoteName reference.Named) error {
|
|
|
|
remoteNameStr := remoteName.Name()
|
|
|
|
if !strings.Contains(remoteNameStr, "/") {
|
2015-01-07 23:42:01 +00:00
|
|
|
// the repository name must not be a valid image ID
|
2015-11-18 22:20:54 +00:00
|
|
|
if err := v1.ValidateID(remoteNameStr); err == nil {
|
2015-05-29 14:22:33 +00:00
|
|
|
return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", remoteName)
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
return nil
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func validateNoSchema(reposName string) error {
|
|
|
|
if strings.Contains(reposName, "://") {
|
|
|
|
// It cannot contain a scheme!
|
|
|
|
return ErrInvalidRepositoryName
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ValidateRepositoryName validates a repository name
|
2015-11-18 22:20:54 +00:00
|
|
|
func ValidateRepositoryName(reposName reference.Named) error {
|
|
|
|
_, _, err := loadRepositoryName(reposName)
|
2015-11-16 15:27:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// loadRepositoryName returns the repo name splitted into index name
|
|
|
|
// and remote repo name. It returns an error if the name is not valid.
|
2015-11-18 22:20:54 +00:00
|
|
|
func loadRepositoryName(reposName reference.Named) (string, reference.Named, error) {
|
|
|
|
if err := validateNoSchema(reposName.Name()); err != nil {
|
|
|
|
return "", nil, err
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
indexName, remoteName, err := splitReposName(reposName)
|
2015-11-16 15:27:22 +00:00
|
|
|
|
|
|
|
if indexName, err = ValidateIndexName(indexName); err != nil {
|
2015-11-18 22:20:54 +00:00
|
|
|
return "", nil, err
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
if err = validateRemoteName(remoteName); err != nil {
|
|
|
|
return "", nil, err
|
2015-11-16 15:27:22 +00:00
|
|
|
}
|
|
|
|
return indexName, remoteName, nil
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
// newIndexInfo returns IndexInfo configuration from indexName
|
|
|
|
func newIndexInfo(config *registrytypes.ServiceConfig, indexName string) (*registrytypes.IndexInfo, error) {
|
2015-01-07 23:42:01 +00:00
|
|
|
var err error
|
|
|
|
indexName, err = ValidateIndexName(indexName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return any configured index info, first.
|
|
|
|
if index, ok := config.IndexConfigs[indexName]; ok {
|
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct a non-configured index info.
|
2015-12-12 02:14:52 +00:00
|
|
|
index := ®istrytypes.IndexInfo{
|
2015-01-07 23:42:01 +00:00
|
|
|
Name: indexName,
|
|
|
|
Mirrors: make([]string, 0),
|
|
|
|
Official: false,
|
|
|
|
}
|
2015-12-12 02:14:52 +00:00
|
|
|
index.Secure = isSecureIndex(config, indexName)
|
2015-01-07 23:42:01 +00:00
|
|
|
return index, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetAuthConfigKey special-cases using the full index address of the official
|
|
|
|
// index as the AuthConfig key, and uses the (host)name[:port] for private indexes.
|
2015-12-12 02:14:52 +00:00
|
|
|
func GetAuthConfigKey(index *registrytypes.IndexInfo) string {
|
2015-01-07 23:42:01 +00:00
|
|
|
if index.Official {
|
2015-07-21 19:40:36 +00:00
|
|
|
return IndexServer
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
return index.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// splitReposName breaks a reposName into an index name and remote name
|
2015-11-18 22:20:54 +00:00
|
|
|
func splitReposName(reposName reference.Named) (indexName string, remoteName reference.Named, err error) {
|
|
|
|
var remoteNameStr string
|
|
|
|
indexName, remoteNameStr = reference.SplitHostname(reposName)
|
|
|
|
if indexName == "" || (!strings.Contains(indexName, ".") &&
|
|
|
|
!strings.Contains(indexName, ":") && indexName != "localhost") {
|
2015-01-07 23:42:01 +00:00
|
|
|
// This is a Docker Index repos (ex: samalba/hipache or ubuntu)
|
|
|
|
// 'docker.io'
|
2015-07-21 19:40:36 +00:00
|
|
|
indexName = IndexName
|
2015-01-07 23:42:01 +00:00
|
|
|
remoteName = reposName
|
|
|
|
} else {
|
2015-11-18 22:20:54 +00:00
|
|
|
remoteName, err = reference.WithName(remoteNameStr)
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
return
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
// newRepositoryInfo validates and breaks down a repository name into a RepositoryInfo
|
|
|
|
func newRepositoryInfo(config *registrytypes.ServiceConfig, reposName reference.Named) (*RepositoryInfo, error) {
|
2015-11-18 22:20:54 +00:00
|
|
|
if err := validateNoSchema(reposName.Name()); err != nil {
|
2015-01-07 23:42:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-18 22:20:54 +00:00
|
|
|
repoInfo := &RepositoryInfo{}
|
|
|
|
var (
|
|
|
|
indexName string
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
indexName, repoInfo.RemoteName, err = loadRepositoryName(reposName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
repoInfo.Index, err = newIndexInfo(config, indexName)
|
2015-01-07 23:42:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if repoInfo.Index.Official {
|
2015-11-18 22:20:54 +00:00
|
|
|
repoInfo.LocalName, err = normalizeLibraryRepoName(repoInfo.RemoteName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
repoInfo.RemoteName = repoInfo.LocalName
|
2015-01-07 23:42:01 +00:00
|
|
|
|
|
|
|
// If the normalized name does not contain a '/' (e.g. "foo")
|
|
|
|
// then it is an official repo.
|
2015-11-18 22:20:54 +00:00
|
|
|
if strings.IndexRune(repoInfo.RemoteName.Name(), '/') == -1 {
|
2015-01-07 23:42:01 +00:00
|
|
|
repoInfo.Official = true
|
|
|
|
// Fix up remote name for official repos.
|
2015-11-18 22:20:54 +00:00
|
|
|
repoInfo.RemoteName, err = reference.WithName("library/" + repoInfo.RemoteName.Name())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 22:20:54 +00:00
|
|
|
repoInfo.CanonicalName, err = reference.WithName("docker.io/" + repoInfo.RemoteName.Name())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-07 23:42:01 +00:00
|
|
|
} else {
|
2015-11-18 22:20:54 +00:00
|
|
|
repoInfo.LocalName, err = localNameFromRemote(repoInfo.Index.Name, repoInfo.RemoteName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-01-07 23:42:01 +00:00
|
|
|
repoInfo.CanonicalName = repoInfo.LocalName
|
|
|
|
}
|
2015-05-16 00:48:20 +00:00
|
|
|
|
2015-01-07 23:42:01 +00:00
|
|
|
return repoInfo, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseRepositoryInfo performs the breakdown of a repository name into a RepositoryInfo, but
|
|
|
|
// lacks registry configuration.
|
2015-11-18 22:20:54 +00:00
|
|
|
func ParseRepositoryInfo(reposName reference.Named) (*RepositoryInfo, error) {
|
2015-12-12 02:14:52 +00:00
|
|
|
return newRepositoryInfo(emptyServiceConfig, reposName)
|
2015-09-22 11:44:40 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 22:20:54 +00:00
|
|
|
// ParseSearchIndexInfo will use repository name to get back an indexInfo.
|
2015-12-12 02:14:52 +00:00
|
|
|
func ParseSearchIndexInfo(reposName string) (*registrytypes.IndexInfo, error) {
|
2015-11-18 22:20:54 +00:00
|
|
|
indexName, _ := splitReposSearchTerm(reposName)
|
2015-09-22 11:44:40 +00:00
|
|
|
|
2015-12-12 02:14:52 +00:00
|
|
|
indexInfo, err := newIndexInfo(emptyServiceConfig, indexName)
|
2015-09-22 11:44:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return indexInfo, nil
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|
|
|
|
|
2015-11-18 22:20:54 +00:00
|
|
|
// NormalizeLocalName transforms a repository name into a normalized LocalName
|
2015-01-07 23:42:01 +00:00
|
|
|
// Passes through the name without transformation on error (image id, etc)
|
2015-11-16 15:27:22 +00:00
|
|
|
// It does not use the repository info because we don't want to load
|
|
|
|
// the repository index and do request over the network.
|
2015-11-18 22:20:54 +00:00
|
|
|
func NormalizeLocalName(name reference.Named) reference.Named {
|
|
|
|
indexName, remoteName, err := loadRepositoryName(name)
|
2015-01-07 23:42:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return name
|
|
|
|
}
|
2015-11-16 15:27:22 +00:00
|
|
|
|
|
|
|
var officialIndex bool
|
|
|
|
// Return any configured index info, first.
|
|
|
|
if index, ok := emptyServiceConfig.IndexConfigs[indexName]; ok {
|
|
|
|
officialIndex = index.Official
|
|
|
|
}
|
|
|
|
|
|
|
|
if officialIndex {
|
2015-11-18 22:20:54 +00:00
|
|
|
localName, err := normalizeLibraryRepoName(remoteName)
|
|
|
|
if err != nil {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return localName
|
2015-11-16 15:27:22 +00:00
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
localName, err := localNameFromRemote(indexName, remoteName)
|
|
|
|
if err != nil {
|
|
|
|
return name
|
|
|
|
}
|
|
|
|
return localName
|
2015-11-16 15:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// normalizeLibraryRepoName removes the library prefix from
|
|
|
|
// the repository name for official repos.
|
2015-11-18 22:20:54 +00:00
|
|
|
func normalizeLibraryRepoName(name reference.Named) (reference.Named, error) {
|
|
|
|
if strings.HasPrefix(name.Name(), "library/") {
|
2015-11-16 15:27:22 +00:00
|
|
|
// If pull "library/foo", it's stored locally under "foo"
|
2015-11-18 22:20:54 +00:00
|
|
|
return reference.WithName(strings.SplitN(name.Name(), "/", 2)[1])
|
2015-11-16 15:27:22 +00:00
|
|
|
}
|
2015-11-18 22:20:54 +00:00
|
|
|
return name, nil
|
2015-11-16 15:27:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// localNameFromRemote combines the index name and the repo remote name
|
|
|
|
// to generate a repo local name.
|
2015-11-18 22:20:54 +00:00
|
|
|
func localNameFromRemote(indexName string, remoteName reference.Named) (reference.Named, error) {
|
|
|
|
return reference.WithName(indexName + "/" + remoteName.Name())
|
|
|
|
}
|
|
|
|
|
|
|
|
// NormalizeLocalReference transforms a reference to use a normalized LocalName
|
|
|
|
// for the name poriton. Passes through the reference without transformation on
|
|
|
|
// error.
|
|
|
|
func NormalizeLocalReference(ref reference.Named) reference.Named {
|
|
|
|
localName := NormalizeLocalName(ref)
|
|
|
|
if tagged, isTagged := ref.(reference.Tagged); isTagged {
|
|
|
|
newRef, err := reference.WithTag(localName, tagged.Tag())
|
|
|
|
if err != nil {
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
return newRef
|
|
|
|
} else if digested, isDigested := ref.(reference.Digested); isDigested {
|
|
|
|
newRef, err := reference.WithDigest(localName, digested.Digest())
|
|
|
|
if err != nil {
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
return newRef
|
|
|
|
}
|
|
|
|
return localName
|
2015-01-07 23:42:01 +00:00
|
|
|
}
|