Skip to content

Commit

Permalink
Merge pull request #1778 from dmcgowan/reference-with-split-hostname
Browse files Browse the repository at this point in the history
Integrate docker reference changes
  • Loading branch information
aaronlehmann authored Jan 9, 2017
2 parents 2bc4a94 + 429c75f commit dbc336e
Show file tree
Hide file tree
Showing 7 changed files with 864 additions and 110 deletions.
18 changes: 18 additions & 0 deletions reference/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ func IsNameOnly(ref Named) bool {
}
return true
}

// FamiliarName returns the familiar name string
// for the given named, familiarizing if needed.
func FamiliarName(ref Named) string {
if nn, ok := ref.(NormalizedNamed); ok {
return nn.Familiar().Name()
}
return ref.Name()
}

// FamiliarString returns the familiar string representation
// for the given reference, familiarizing if needed.
func FamiliarString(ref Reference) string {
if nn, ok := ref.(NormalizedNamed); ok {
return nn.Familiar().String()
}
return ref.String()
}
147 changes: 146 additions & 1 deletion reference/normalize.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,124 @@
package reference

import (
"errors"
"fmt"
"strings"

"github.com/docker/distribution/digest"
)

var (
defaultTag = "latest"
legacyDefaultDomain = "index.docker.io"
defaultDomain = "docker.io"
defaultRepoPrefix = "library/"
defaultTag = "latest"
)

// NormalizedNamed represents a name which has been
// normalized and has a familiar form. A familiar name
// is what is used in Docker UI. An example normalized
// name is "docker.io/library/ubuntu" and corresponding
// familiar name of "ubuntu".
type NormalizedNamed interface {
Named
Familiar() Named
}

// ParseNormalizedNamed parses a string into a named reference
// transforming a familiar name from Docker UI to a fully
// qualified reference. If the value may be an identifier
// use ParseAnyReference.
func ParseNormalizedNamed(s string) (NormalizedNamed, error) {
if ok := anchoredIdentifierRegexp.MatchString(s); ok {
return nil, fmt.Errorf("invalid repository name (%s), cannot specify 64-byte hexadecimal strings", s)
}
domain, remainder := splitDockerDomain(s)
var remoteName string
if tagSep := strings.IndexRune(remainder, ':'); tagSep > -1 {
remoteName = remainder[:tagSep]
} else {
remoteName = remainder
}
if strings.ToLower(remoteName) != remoteName {
return nil, errors.New("invalid reference format: repository name must be lowercase")
}

ref, err := Parse(domain + "/" + remainder)
if err != nil {
return nil, err
}
named, isNamed := ref.(NormalizedNamed)
if !isNamed {
return nil, fmt.Errorf("reference %s has no name", ref.String())
}
return named, nil
}

// splitDockerDomain splits a repository name to domain and remotename string.
// If no valid domain is found, the default domain is used. Repository name
// needs to be already validated before.
func splitDockerDomain(name string) (domain, remainder string) {
i := strings.IndexRune(name, '/')
if i == -1 || (!strings.ContainsAny(name[:i], ".:") && name[:i] != "localhost") {
domain, remainder = defaultDomain, name
} else {
domain, remainder = name[:i], name[i+1:]
}
if domain == legacyDefaultDomain {
domain = defaultDomain
}
if domain == defaultDomain && !strings.ContainsRune(remainder, '/') {
remainder = defaultRepoPrefix + remainder
}
return
}

// familiarizeName returns a shortened version of the name familiar
// to to the Docker UI. Familiar names have the default domain
// "docker.io" and "library/" repository prefix removed.
// For example, "docker.io/library/redis" will have the familiar
// name "redis" and "docker.io/dmcgowan/myapp" will be "dmcgowan/myapp".
// Returns a familiarized named only reference.
func familiarizeName(named NamedRepository) repository {
repo := repository{
domain: named.Domain(),
path: named.Path(),
}

if repo.domain == defaultDomain {
repo.domain = ""
repo.path = strings.TrimPrefix(repo.path, defaultRepoPrefix)
}
return repo
}

func (r reference) Familiar() Named {
return reference{
NamedRepository: familiarizeName(r.NamedRepository),
tag: r.tag,
digest: r.digest,
}
}

func (r repository) Familiar() Named {
return familiarizeName(r)
}

func (t taggedReference) Familiar() Named {
return taggedReference{
NamedRepository: familiarizeName(t.NamedRepository),
tag: t.tag,
}
}

func (c canonicalReference) Familiar() Named {
return canonicalReference{
NamedRepository: familiarizeName(c.NamedRepository),
digest: c.digest,
}
}

// EnsureTagged adds the default tag "latest" to a reference if it only has
// a repo name.
func EnsureTagged(ref Named) NamedTagged {
Expand All @@ -20,3 +135,33 @@ func EnsureTagged(ref Named) NamedTagged {
}
return namedTagged
}

// ParseAnyReference parses a reference string as a possible identifier,
// full digest, or familiar name.
func ParseAnyReference(ref string) (Reference, error) {
if ok := anchoredIdentifierRegexp.MatchString(ref); ok {
return digestReference("sha256:" + ref), nil
}
if dgst, err := digest.ParseDigest(ref); err == nil {
return digestReference(dgst), nil
}

return ParseNormalizedNamed(ref)
}

// ParseAnyReferenceWithSet parses a reference string as a possible short
// identifier to be matched in a digest set, a full digest, or familiar name.
func ParseAnyReferenceWithSet(ref string, ds *digest.Set) (Reference, error) {
if ok := anchoredShortIdentifierRegexp.MatchString(ref); ok {
dgst, err := ds.Lookup(ref)
if err == nil {
return digestReference(dgst), nil
}
} else {
if dgst, err := digest.ParseDigest(ref); err == nil {
return digestReference(dgst), nil
}
}

return ParseNormalizedNamed(ref)
}
Loading

0 comments on commit dbc336e

Please sign in to comment.