reference.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package reference
  2. import (
  3. "fmt"
  4. distreference "github.com/docker/distribution/reference"
  5. "github.com/docker/docker/pkg/stringid"
  6. "github.com/opencontainers/go-digest"
  7. "github.com/pkg/errors"
  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.ParseNormalizedNamed(s)
  49. if err != nil {
  50. return nil, errors.Wrapf(err, "failed to parse reference %q", s)
  51. }
  52. if err := validateName(distreference.FamiliarName(named)); err != nil {
  53. return nil, err
  54. }
  55. // Ensure returned reference cannot have tag and digest
  56. if canonical, isCanonical := named.(distreference.Canonical); isCanonical {
  57. r, err := distreference.WithDigest(distreference.TrimNamed(named), canonical.Digest())
  58. if err != nil {
  59. return nil, err
  60. }
  61. return &canonicalRef{namedRef{r}}, nil
  62. }
  63. if tagged, isTagged := named.(distreference.NamedTagged); isTagged {
  64. r, err := distreference.WithTag(distreference.TrimNamed(named), tagged.Tag())
  65. if err != nil {
  66. return nil, err
  67. }
  68. return &taggedRef{namedRef{r}}, nil
  69. }
  70. return &namedRef{named}, nil
  71. }
  72. // TrimNamed removes any tag or digest from the named reference
  73. func TrimNamed(ref Named) Named {
  74. return &namedRef{distreference.TrimNamed(ref)}
  75. }
  76. // WithName returns a named object representing the given string. If the input
  77. // is invalid ErrReferenceInvalidFormat will be returned.
  78. func WithName(name string) (Named, error) {
  79. r, err := distreference.ParseNormalizedNamed(name)
  80. if err != nil {
  81. return nil, err
  82. }
  83. if err := validateName(distreference.FamiliarName(r)); err != nil {
  84. return nil, err
  85. }
  86. if !distreference.IsNameOnly(r) {
  87. return nil, distreference.ErrReferenceInvalidFormat
  88. }
  89. return &namedRef{r}, nil
  90. }
  91. // WithTag combines the name from "name" and the tag from "tag" to form a
  92. // reference incorporating both the name and the tag.
  93. func WithTag(name Named, tag string) (NamedTagged, error) {
  94. r, err := distreference.WithTag(name, tag)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return &taggedRef{namedRef{r}}, nil
  99. }
  100. // WithDigest combines the name from "name" and the digest from "digest" to form
  101. // a reference incorporating both the name and the digest.
  102. func WithDigest(name Named, digest digest.Digest) (Canonical, error) {
  103. r, err := distreference.WithDigest(name, digest)
  104. if err != nil {
  105. return nil, err
  106. }
  107. return &canonicalRef{namedRef{r}}, nil
  108. }
  109. type namedRef struct {
  110. distreference.Named
  111. }
  112. type taggedRef struct {
  113. namedRef
  114. }
  115. type canonicalRef struct {
  116. namedRef
  117. }
  118. func (r *namedRef) Name() string {
  119. return distreference.FamiliarName(r.Named)
  120. }
  121. func (r *namedRef) String() string {
  122. return distreference.FamiliarString(r.Named)
  123. }
  124. func (r *namedRef) FullName() string {
  125. return r.Named.Name()
  126. }
  127. func (r *namedRef) Hostname() string {
  128. return distreference.Domain(r.Named)
  129. }
  130. func (r *namedRef) RemoteName() string {
  131. return distreference.Path(r.Named)
  132. }
  133. func (r *taggedRef) Tag() string {
  134. return r.namedRef.Named.(distreference.NamedTagged).Tag()
  135. }
  136. func (r *canonicalRef) Digest() digest.Digest {
  137. return r.namedRef.Named.(distreference.Canonical).Digest()
  138. }
  139. // WithDefaultTag adds a default tag to a reference if it only has a repo name.
  140. func WithDefaultTag(ref Named) Named {
  141. if IsNameOnly(ref) {
  142. ref, _ = WithTag(ref, DefaultTag)
  143. }
  144. return ref
  145. }
  146. // IsNameOnly returns true if reference only contains a repo name.
  147. func IsNameOnly(ref Named) bool {
  148. if _, ok := ref.(NamedTagged); ok {
  149. return false
  150. }
  151. if _, ok := ref.(Canonical); ok {
  152. return false
  153. }
  154. return true
  155. }
  156. // ParseIDOrReference parses string for an image ID or a reference. ID can be
  157. // without a default prefix.
  158. func ParseIDOrReference(idOrRef string) (digest.Digest, Named, error) {
  159. if err := stringid.ValidateID(idOrRef); err == nil {
  160. idOrRef = "sha256:" + idOrRef
  161. }
  162. if dgst, err := digest.Parse(idOrRef); err == nil {
  163. return dgst, nil, nil
  164. }
  165. ref, err := ParseNamed(idOrRef)
  166. return "", ref, err
  167. }
  168. func validateName(name string) error {
  169. if err := stringid.ValidateID(name); err == nil {
  170. return fmt.Errorf("Invalid repository name (%s), cannot specify 64-byte hexadecimal strings", name)
  171. }
  172. return nil
  173. }