-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor splitDockerDomain to include more documentation #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,20 +123,51 @@ func ParseDockerRef(ref string) (Named, error) { | |
// splitDockerDomain splits a repository name to domain and remote-name. | ||
// 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 && strings.ToLower(name[:i]) == name[:i]) { | ||
domain, remainder = defaultDomain, name | ||
} else { | ||
domain, remainder = name[:i], name[i+1:] | ||
} | ||
if domain == legacyDefaultDomain { | ||
domain = defaultDomain | ||
} | ||
if domain == defaultDomain && !strings.ContainsRune(remainder, '/') { | ||
remainder = officialRepoPrefix + remainder | ||
} | ||
return | ||
func splitDockerDomain(name string) (domain, remoteName string) { | ||
maybeDomain, maybeRemoteName, ok := strings.Cut(name, "/") | ||
if !ok { | ||
// Fast-path for single element ("familiar" names), such as "ubuntu" | ||
// or "ubuntu:latest". Familiar names must be handled separately, to | ||
// prevent them from being handled as "hostname:port". | ||
// | ||
// Canonicalize them as "docker.io/library/name[:tag]" | ||
|
||
// FIXME(thaJeztah): account for bare "localhost" or "example.com" names, which SHOULD be considered a domain. | ||
return defaultDomain, officialRepoPrefix + name | ||
} | ||
|
||
switch { | ||
case maybeDomain == localhost: | ||
// localhost is a reserved namespace and always considered a domain. | ||
domain, remoteName = maybeDomain, maybeRemoteName | ||
Jamstah marked this conversation as resolved.
Show resolved
Hide resolved
|
||
case maybeDomain == legacyDefaultDomain: | ||
// canonicalize the Docker Hub and legacy "Docker Index" domains. | ||
domain, remoteName = defaultDomain, maybeRemoteName | ||
case strings.ContainsAny(maybeDomain, ".:"): | ||
// Likely a domain or IP-address: | ||
// | ||
// - contains a "." (e.g., "example.com" or "127.0.0.1") | ||
// - contains a ":" (e.g., "example:5000", "::1", or "[::1]:5000") | ||
domain, remoteName = maybeDomain, maybeRemoteName | ||
case strings.ToLower(maybeDomain) != maybeDomain: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to ignore this comment. This sent me down the rabbit hole of RFCs for domain names; if the unicode cars are allowed in domain names (I'm not entirely clear on some of the mini-research I've undertaken 🙃) then we could use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, my follow-up would keep the original case, but would compare all domain name cases (localhost and the "index.docker.io" domain with strings.EqualFold There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My initial intent here was to keep the existing behavior (warts and all), but just make the code more readable/explained, as it was a lot of logic condensed into a single line |
||
// Uppercase namespaces are not allowed, so if the first element | ||
// is not lowercase, we assume it to be a domain-name. | ||
domain, remoteName = maybeDomain, maybeRemoteName | ||
default: | ||
// None of the above: it's not a domain, so use the default, and | ||
// use the name input the remote-name. | ||
domain, remoteName = defaultDomain, name | ||
} | ||
|
||
if domain == defaultDomain && !strings.ContainsRune(remoteName, '/') { | ||
// Canonicalize "familiar" names, but only on Docker Hub, not | ||
// on other domains: | ||
// | ||
// "docker.io/ubuntu[:tag]" => "docker.io/library/ubuntu[:tag]" | ||
remoteName = officialRepoPrefix + remoteName | ||
} | ||
|
||
return domain, remoteName | ||
} | ||
|
||
// familiarizeName returns a shortened version of the name familiar | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is the worry in this
FIXME
with someone doing a pull ondocker pull localhost:foobar
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's not a real worry, other than a correctness fix; currently,
docker.io
as name would get converted todocker.io/library/docker.io
(and similarly,localhost
becomesdocker.io/library/localhost
)both would probably not be useful without a fix, and rejected by the registry, so it's not a big concern