reference.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package reference
  2. import (
  3. "fmt"
  4. "strings"
  5. "github.com/docker/distribution/digest"
  6. distreference "github.com/docker/distribution/reference"
  7. "github.com/docker/docker/image/v1"
  8. )
  9. const (
  10. // DefaultTag defines the default tag used when performing images related actions and no tag or digest is specified
  11. DefaultTag = "latest"
  12. // DefaultHostname is the default built-in hostname
  13. DefaultHostname = "docker.io"
  14. // LegacyDefaultHostname is automatically converted to DefaultHostname
  15. LegacyDefaultHostname = "index.docker.io"
  16. // DefaultRepoPrefix is the prefix used for default repositories in default host
  17. DefaultRepoPrefix = "library/"
  18. )
  19. // Named is an object with a full name
  20. type Named interface {
  21. // Name returns normalized repository name, like "ubuntu".
  22. Name() string
  23. // String returns full reference, like "ubuntu@sha256:abcdef..."
  24. String() string
  25. // FullName returns full repository name with hostname, like "docker.io/library/ubuntu"
  26. FullName() string
  27. // Hostname returns hostname for the reference, like "docker.io"
  28. Hostname() string
  29. // RemoteName returns the repository component of the full name, like "library/ubuntu"
  30. RemoteName() string
  31. }
  32. // NamedTagged is an object including a name and tag.
  33. type NamedTagged interface {
  34. Named
  35. Tag() string
  36. }
  37. // Canonical reference is an object with a fully unique
  38. // name including a name with hostname and digest
  39. type Canonical interface {
  40. Named
  41. Digest() digest.Digest
  42. }
  43. // ParseNamed parses s and returns a syntactically valid reference implementing
  44. // the Named interface. The reference must have a name, otherwise an error is
  45. // returned.
  46. // If an error was encountered it is returned, along with a nil Reference.
  47. func ParseNamed(s string) (Named, error) {
  48. named, err := distreference.ParseNamed(s)
  49. if err != nil {
  50. return nil, fmt.Errorf("Error parsing reference: %q is not a valid repository/tag", s)
  51. }
  52. r, err := WithName(named.Name())
  53. if err != nil {
  54. return nil, err
  55. }
  56. if canonical, isCanonical := named.(distreference.Canonical); isCanonical {
  57. return WithDigest(r, canonical.Digest())
  58. }
  59. if tagged, isTagged := named.(distreference.NamedTagged); isTagged {
  60. return WithTag(r, tagged.Tag())
  61. }
  62. return r, nil
  63. }
  64. // WithName returns a named object representing the given string. If the input
  65. // is invalid ErrReferenceInvalidFormat will be returned.
  66. func WithName(name string) (Named, error) {
  67. name = normalize(name)
  68. if err := validateName(name); err != nil {
  69. return nil, err
  70. }
  71. r, err := distreference.WithName(name)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return &namedRef{r}, nil
  76. }
  77. // WithTag combines the name from "name" and the tag from "tag" to form a
  78. // reference incorporating both the name and the tag.
  79. func WithTag(name Named, tag string) (NamedTagged, error) {
  80. r, err := distreference.WithTag(name, tag)
  81. if err != nil {
  82. return nil, err
  83. }
  84. return &taggedRef{namedRef{r}}, nil
  85. }
  86. // WithDigest combines the name from "name" and the digest from "digest" to form
  87. // a reference incorporating both the name and the digest.
  88. func WithDigest(name Named, digest digest.Digest) (Canonical, error) {
  89. r, err := distreference.WithDigest(name, digest)
  90. if err != nil {
  91. return nil, err
  92. }
  93. return &canonicalRef{namedRef{r}}, nil
  94. }
  95. type namedRef struct {
  96. distreference.Named
  97. }
  98. type taggedRef struct {
  99. namedRef
  100. }
  101. type canonicalRef struct {
  102. namedRef
  103. }
  104. func (r *namedRef) FullName() string {
  105. hostname, remoteName := splitHostname(r.Name())
  106. return hostname + "/" + remoteName
  107. }
  108. func (r *namedRef) Hostname() string {
  109. hostname, _ := splitHostname(r.Name())
  110. return hostname
  111. }
  112. func (r *namedRef) RemoteName() string {
  113. _, remoteName := splitHostname(r.Name())
  114. return remoteName
  115. }
  116. func (r *taggedRef) Tag() string {
  117. return r.namedRef.Named.(distreference.NamedTagged).Tag()
  118. }
  119. func (r *canonicalRef) Digest() digest.Digest {
  120. return r.namedRef.Named.(distreference.Canonical).Digest()
  121. }
  122. // WithDefaultTag adds a default tag to a reference if it only has a repo name.
  123. func WithDefaultTag(ref Named) Named {
  124. if IsNameOnly(ref) {
  125. ref, _ = WithTag(ref, DefaultTag)
  126. }
  127. return ref
  128. }
  129. // IsNameOnly returns true if reference only contains a repo name.
  130. func IsNameOnly(ref Named) bool {
  131. if _, ok := ref.(NamedTagged); ok {
  132. return false
  133. }
  134. if _, ok := ref.(Canonical); ok {
  135. return false
  136. }
  137. return true
  138. }
  139. // splitHostname splits a repository name to hostname and remotename string.
  140. // If no valid hostname is found, the default hostname is used. Repository name
  141. // needs to be already validated before.
  142. func splitHostname(name string) (hostname, remoteName string) {
  143. i := strings.IndexRune(name, '/')
  144. if i == -1 || (!strings.ContainsAny(name[:i], ".:") && name[:i] != "localhost") {
  145. hostname, remoteName = DefaultHostname, name
  146. } else {
  147. hostname, remoteName = name[:i], name[i+1:]
  148. }
  149. if hostname == LegacyDefaultHostname {
  150. hostname = DefaultHostname
  151. }
  152. if hostname == DefaultHostname && !strings.ContainsRune(remoteName, '/') {
  153. remoteName = DefaultRepoPrefix + remoteName
  154. }
  155. return
  156. }
  157. // normalize returns a repository name in its normalized form, meaning it
  158. // will not contain default hostname nor library/ prefix for official images.
  159. func normalize(name string) string {
  160. host, remoteName := splitHostname(name)
  161. if host == DefaultHostname {
  162. if strings.HasPrefix(remoteName, DefaultRepoPrefix) {
  163. return strings.TrimPrefix(remoteName, DefaultRepoPrefix)
  164. }
  165. return remoteName
  166. }
  167. return name
  168. }
  169. func validateName(name string) error {
  170. if err := v1.ValidateID(name); err == nil {
  171. return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", name)
  172. }
  173. return nil
  174. }