Skip to content

Commit f5551cc

Browse files
authored
Merge pull request moby#3427 from JordanGoasdoue/chore-allow-mirror-path
feat: handle mirror url with path
2 parents 402e402 + 7f67670 commit f5551cc

File tree

4 files changed

+112
-9
lines changed

4 files changed

+112
-9
lines changed

docs/buildkitd.toml.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ insecure-entitlements = [ "network.host", "security.insecure" ]
9595

9696
# registry configures a new Docker register used for cache import or output.
9797
[registry."docker.io"]
98-
mirrors = ["yourmirror.local:5000"]
98+
# mirror configuration to handle path in case a mirror registry requires a /project path rather than just a host:port
99+
mirrors = ["yourmirror.local:5000", "core.harbor.domain/proxy.docker.io"]
99100
http = true
100101
insecure = true
101102
ca=["/etc/config/myca.pem"]

util/resolver/resolver.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net"
77
"net/http"
88
"os"
9+
"path"
910
"path/filepath"
1011
"runtime"
1112
"strings"
@@ -17,6 +18,10 @@ import (
1718
"github.com/pkg/errors"
1819
)
1920

21+
const (
22+
defaultPath = "/v2"
23+
)
24+
2025
func fillInsecureOpts(host string, c config.RegistryConfig, h docker.RegistryHost) ([]docker.RegistryHost, error) {
2126
var hosts []docker.RegistryHost
2227

@@ -126,14 +131,7 @@ func NewRegistryConfig(m map[string]config.RegistryConfig) docker.RegistryHosts
126131
var out []docker.RegistryHost
127132

128133
for _, mirror := range c.Mirrors {
129-
h := docker.RegistryHost{
130-
Scheme: "https",
131-
Client: newDefaultClient(),
132-
Host: mirror,
133-
Path: "/v2",
134-
Capabilities: docker.HostCapabilityPull | docker.HostCapabilityResolve,
135-
}
136-
134+
h := newMirrorRegistryHost(mirror)
137135
hosts, err := fillInsecureOpts(mirror, m[mirror], h)
138136
if err != nil {
139137
return nil, err
@@ -169,6 +167,20 @@ func NewRegistryConfig(m map[string]config.RegistryConfig) docker.RegistryHosts
169167
)
170168
}
171169

170+
func newMirrorRegistryHost(mirror string) docker.RegistryHost {
171+
mirrorHost, mirrorPath := extractMirrorHostAndPath(mirror)
172+
path := path.Join(defaultPath, mirrorPath)
173+
h := docker.RegistryHost{
174+
Scheme: "https",
175+
Client: newDefaultClient(),
176+
Host: mirrorHost,
177+
Path: path,
178+
Capabilities: docker.HostCapabilityPull | docker.HostCapabilityResolve,
179+
}
180+
181+
return h
182+
}
183+
172184
func newDefaultClient() *http.Client {
173185
return &http.Client{
174186
Transport: tracing.NewTransport(newDefaultTransport()),

util/resolver/resolver_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package resolver
2+
3+
import (
4+
"bytes"
5+
"path"
6+
"testing"
7+
8+
"github.com/moby/buildkit/cmd/buildkitd/config"
9+
"github.com/stretchr/testify/require"
10+
)
11+
12+
func TestNewMirrorRegistryHost(t *testing.T) {
13+
const testConfig = `
14+
[registry."docker.io"]
15+
mirrors = ["hub.docker.io", "yourmirror.local:5000/proxy.docker.io"]
16+
[registry."quay.io"]
17+
mirrors = ["yourmirror.local:5000/proxy.quay.io"]
18+
[registry."fake.io"]
19+
mirrors = ["https://url/", "https://url/path/"]
20+
`
21+
22+
tests := map[string]struct {
23+
description string
24+
host string
25+
path string
26+
}{
27+
"hub.docker.io": {
28+
description: "docker_io_mirror_without_path",
29+
host: "hub.docker.io",
30+
path: defaultPath,
31+
},
32+
"yourmirror.local:5000/proxy.docker.io": {
33+
description: "docker_io_mirror_with_path",
34+
host: "yourmirror.local:5000",
35+
path: path.Join(defaultPath, "proxy.docker.io"),
36+
},
37+
"yourmirror.local:5000/proxy.quay.io": {
38+
description: "docker_quay_mirror_with_path",
39+
host: "yourmirror.local:5000",
40+
path: path.Join(defaultPath, "proxy.quay.io"),
41+
},
42+
"https://url/": {
43+
description: "docker_fake_mirror_scheme_without_path",
44+
host: "url",
45+
path: defaultPath,
46+
},
47+
"https://url/path/": {
48+
description: "docker_fake_mirror_scheme_with_path",
49+
host: "url",
50+
path: path.Join(defaultPath, "path"),
51+
},
52+
}
53+
54+
cfg, err := config.Load(bytes.NewBuffer([]byte(testConfig)))
55+
require.NoError(t, err)
56+
57+
require.NotEqual(t, len(cfg.Registries), 0)
58+
for _, registry := range cfg.Registries {
59+
require.NotEqual(t, len(registry.Mirrors), 0)
60+
for _, m := range registry.Mirrors {
61+
test := tests[m]
62+
h := newMirrorRegistryHost(m)
63+
require.NotEqual(t, h, nil)
64+
require.Equal(t, h.Host, test.host)
65+
require.Equal(t, h.Path, test.path)
66+
}
67+
}
68+
}

util/resolver/utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package resolver
2+
3+
import (
4+
"fmt"
5+
"net/url"
6+
"strings"
7+
)
8+
9+
func extractMirrorHostAndPath(mirror string) (string, string) {
10+
var path string
11+
host := mirror
12+
13+
u, err := url.Parse(mirror)
14+
if err != nil || u.Host == "" {
15+
u, err = url.Parse(fmt.Sprintf("//%s", mirror))
16+
}
17+
if err != nil || u.Host == "" {
18+
return host, path
19+
}
20+
21+
return u.Host, strings.TrimRight(u.Path, "/")
22+
}

0 commit comments

Comments
 (0)