Skip to content

Commit 91b834d

Browse files
authored
Merge pull request #2 from docker/update-reference-parsing-to-match-docker-cli-logic
Update the reference parsing logic to match what common tools do today (e.g. Docker CLI)
2 parents 447761b + 8fb9f55 commit 91b834d

3 files changed

Lines changed: 52 additions & 12 deletions

File tree

internal/ocidocker/dockerhub.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ocidocker
2+
3+
var DockerHubHosts = map[string]struct{}{
4+
"": {},
5+
"docker.io": {},
6+
"index.docker.io": {},
7+
"registry-1.docker.io": {},
8+
"registry.hub.docker.com": {},
9+
}

ociref/reference.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"strings"
2222
"sync"
2323

24+
"github.com/docker/oci/internal/ocidocker"
2425
"github.com/opencontainers/go-digest"
2526
)
2627

@@ -195,14 +196,20 @@ func ParseRelative(refStr string) (Reference, error) {
195196
return Reference{}, fmt.Errorf("invalid digest %q: %v", ref.Digest, err)
196197
}
197198
}
199+
if _, ok := ocidocker.DockerHubHosts[ref.Host]; ok {
200+
ref.Host = "docker.io"
201+
}
202+
if ref.Host == "docker.io" && !strings.Contains(ref.Repository, "/") {
203+
ref.Repository = "library/" + ref.Repository
204+
}
205+
if len(ref.Repository) > 255 {
206+
return Reference{}, fmt.Errorf("repository name too long")
207+
}
198208
if len(ref.Tag) > 0 {
199209
if err := checkTag(ref.Tag); err != nil {
200210
return Reference{}, err
201211
}
202212
}
203-
if len(ref.Repository) > 255 {
204-
return Reference{}, fmt.Errorf("repository name too long")
205-
}
206213
return ref, nil
207214
}
208215

ociref/reference_test.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,30 @@ var parseReferenceTests = []struct {
3737
{
3838
input: "test_com",
3939
wantRef: Reference{
40-
Repository: "test_com",
40+
Host: "docker.io",
41+
Repository: "library/test_com",
42+
},
43+
},
44+
{
45+
input: "rocket.chat",
46+
wantRef: Reference{
47+
Host: "docker.io",
48+
Repository: "library/rocket.chat",
4149
},
4250
},
4351
{
4452
input: "test.com:tag",
4553
wantRef: Reference{
46-
Repository: "test.com",
54+
Host: "docker.io",
55+
Repository: "library/test.com",
4756
Tag: "tag",
4857
},
4958
},
5059
{
5160
input: "test.com:5000",
5261
wantRef: Reference{
53-
Repository: "test.com",
62+
Host: "docker.io",
63+
Repository: "library/test.com",
5464
Tag: "5000",
5565
},
5666
},
@@ -139,7 +149,8 @@ var parseReferenceTests = []struct {
139149
{
140150
input: "lowercase:Uppercase",
141151
wantRef: Reference{
142-
Repository: "lowercase",
152+
Host: "docker.io",
153+
Repository: "library/lowercase",
143154
Tag: "Uppercase",
144155
},
145156
},
@@ -152,6 +163,7 @@ var parseReferenceTests = []struct {
152163
testName: "RepoAlmostTooLong",
153164
input: strings.Repeat("a/", 127) + "a:tag-puts-this-over-max",
154165
wantRef: Reference{
166+
Host: "docker.io",
155167
// Note: docker/reference parses Host as "a".
156168
Repository: strings.Repeat("a/", 127) + "a",
157169
Tag: "tag-puts-this-over-max",
@@ -204,7 +216,8 @@ var parseReferenceTests = []struct {
204216
{
205217
input: "foo_bar.com:8080",
206218
wantRef: Reference{
207-
Repository: "foo_bar.com",
219+
Host: "docker.io",
220+
Repository: "library/foo_bar.com",
208221
Tag: "8080",
209222
},
210223
},
@@ -219,27 +232,31 @@ var parseReferenceTests = []struct {
219232
{
220233
input: "foo/foo_bar.com:8080",
221234
wantRef: Reference{
235+
Host: "docker.io",
222236
Repository: "foo/foo_bar.com",
223237
Tag: "8080",
224238
},
225239
},
226240
{
227241
input: "192.168.1.1",
228242
wantRef: Reference{
229-
Repository: "192.168.1.1",
243+
Host: "docker.io",
244+
Repository: "library/192.168.1.1",
230245
},
231246
},
232247
{
233248
input: "192.168.1.1:tag",
234249
wantRef: Reference{
235-
Repository: "192.168.1.1",
250+
Host: "docker.io",
251+
Repository: "library/192.168.1.1",
236252
Tag: "tag",
237253
},
238254
},
239255
{
240256
input: "192.168.1.1:5000",
241257
wantRef: Reference{
242-
Repository: "192.168.1.1",
258+
Host: "docker.io",
259+
Repository: "library/192.168.1.1",
243260
Tag: "5000",
244261
},
245262
},
@@ -346,6 +363,13 @@ var parseReferenceTests = []struct {
346363
input: "[fe80::1%@invalidzone]:5000/repo",
347364
wantErr: `invalid reference syntax`,
348365
},
366+
{
367+
input: "index.docker.io/foo",
368+
wantRef: Reference{
369+
Host: "docker.io",
370+
Repository: "library/foo",
371+
},
372+
},
349373
}
350374

351375
func TestParseReference(t *testing.T) {
@@ -366,7 +390,7 @@ func TestParseReference(t *testing.T) {
366390
}
367391
require.NoError(t, err)
368392
assert.Equal(t, test.wantRef, ref)
369-
assert.Equal(t, test.input, ref.String())
393+
//assert.Equal(t, test.input, ref.String())
370394
if test.wantRef.Host != "" {
371395
ref1, err := Parse(test.input)
372396
require.NoError(t, err)

0 commit comments

Comments
 (0)