Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit d7a8323

Browse files
committed
feature: registry mirror support direct param.
Signed-off-by: zhouchencheng <[email protected]>
1 parent 266923d commit d7a8323

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

dfdaemon/config/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ var fs = afero.NewOsFs()
5252
// insecure: false
5353
// # optional certificates if the remote server uses self-signed certificates
5454
// certs: []
55+
// # whether to request the remote registry directly
56+
// direct: false
5557
//
5658
// proxies:
5759
// # proxy all http image layer download requests with dfget
@@ -174,6 +176,9 @@ type RegistryMirror struct {
174176

175177
// Whether to ignore certificates errors for the registry
176178
Insecure bool `yaml:"insecure" json:"insecure"`
179+
180+
// Request the remote registry directly.
181+
Direct bool `yaml:"direct" json:"direct"`
177182
}
178183

179184
// TLSConfig returns the tls.Config used to communicate with the mirror

dfdaemon/proxy/proxy.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ func (proxy *Proxy) mirrorRegistry(w http.ResponseWriter, r *http.Request) {
179179
t, err := transport.New(
180180
transport.WithDownloader(proxy.downloadFactory()),
181181
transport.WithTLS(proxy.registry.TLSConfig()),
182+
transport.WithCondition(proxy.shouldUseDfgetForMirror),
182183
)
183184
if err != nil {
184185
http.Error(w, fmt.Sprintf("failed to get transport: %v", err), http.StatusInternalServerError)
@@ -264,6 +265,12 @@ func (proxy *Proxy) shouldUseDfget(req *http.Request) bool {
264265
return false
265266
}
266267

268+
// shouldUseDfgetForMirror returns whether we should use dfget to proxy a request
269+
// when we use registry mirror.
270+
func (proxy *Proxy) shouldUseDfgetForMirror(req *http.Request) bool {
271+
return proxy.registry != nil && !proxy.registry.Direct && transport.NeedUseGetter(req)
272+
}
273+
267274
// tunnelHTTPS handles a CONNECT request and proxy an https request through an
268275
// http tunnel.
269276
func tunnelHTTPS(w http.ResponseWriter, r *http.Request) {

dfdaemon/proxy/proxy_test.go

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ type testItem struct {
3232
}
3333

3434
type testCase struct {
35-
Error error
36-
Rules []*config.Proxy
37-
Items []testItem
35+
Error error
36+
Rules []*config.Proxy
37+
RegistryMirror *config.RegistryMirror
38+
Items []testItem
3839
}
3940

4041
func newTestCase() *testCase {
@@ -52,6 +53,20 @@ func (tc *testCase) WithRule(regx string, direct bool, useHTTPS bool) *testCase
5253
return tc
5354
}
5455

56+
func (tc *testCase) WithRegistryMirror(url string, direct bool) *testCase {
57+
if tc.Error != nil {
58+
return tc
59+
}
60+
61+
var remote *config.URL
62+
remote, tc.Error = config.NewURL(url)
63+
tc.RegistryMirror = &config.RegistryMirror{
64+
Remote: remote,
65+
Direct: direct,
66+
}
67+
return tc
68+
}
69+
5570
func (tc *testCase) WithTest(url string, direct bool, useHTTPS bool) *testCase {
5671
tc.Items = append(tc.Items, testItem{url, direct, useHTTPS})
5772
return tc
@@ -82,6 +97,26 @@ func (tc *testCase) Test(t *testing.T) {
8297
}
8398
}
8499

100+
func (tc *testCase) TestMirror(t *testing.T) {
101+
a := assert.New(t)
102+
if !a.Nil(tc.Error) {
103+
return
104+
}
105+
tp, err := New(WithRegistryMirror(tc.RegistryMirror))
106+
if !a.Nil(err) {
107+
return
108+
}
109+
for _, item := range tc.Items {
110+
req, err := http.NewRequest("GET", item.URL, nil)
111+
if !a.Nil(err) {
112+
continue
113+
}
114+
if !a.Equal(tp.shouldUseDfgetForMirror(req), !item.Direct) {
115+
fmt.Println(item.URL)
116+
}
117+
}
118+
}
119+
85120
func TestMatch(t *testing.T) {
86121
newTestCase().
87122
WithRule("/blobs/sha256/", false, false).
@@ -101,4 +136,23 @@ func TestMatch(t *testing.T) {
101136
WithTest("http://h/a/d", false, true). // should match /a/d and use https
102137
WithTest("http://h/a/e", false, false). // should match /a, not /a/e
103138
Test(t)
139+
140+
newTestCase().
141+
WithTest("http://h/a", true, false).
142+
TestMirror(t)
143+
144+
newTestCase().
145+
WithRegistryMirror("http://index.docker.io", false).
146+
WithTest("http://h/a", true, false).
147+
TestMirror(t)
148+
149+
newTestCase().
150+
WithRegistryMirror("http://index.docker.io", false).
151+
WithTest("http://index.docker.io/v2/blobs/sha256/xxx", false, false).
152+
TestMirror(t)
153+
154+
newTestCase().
155+
WithRegistryMirror("http://index.docker.io", true).
156+
WithTest("http://index.docker.io/v2/blobs/sha256/xxx", true, false).
157+
TestMirror(t)
104158
}

dfdaemon/transport/transport.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func New(opts ...Option) (*DFRoundTripper, error) {
5252
rt := &DFRoundTripper{
5353
Round: defaultHTTPTransport(nil),
5454
Round2: http.NewFileTransport(http.Dir("/")),
55-
ShouldUseDfget: needUseGetter,
55+
ShouldUseDfget: NeedUseGetter,
5656
}
5757

5858
for _, opt := range opts {
@@ -162,6 +162,6 @@ func (roundTripper *DFRoundTripper) downloadByGetter(url string, header map[stri
162162

163163
// needUseGetter is the default value for ShouldUseDfget, which downloads all
164164
// images layers with dfget.
165-
func needUseGetter(req *http.Request) bool {
165+
func NeedUseGetter(req *http.Request) bool {
166166
return req.Method == http.MethodGet && layerReg.MatchString(req.URL.Path)
167167
}

0 commit comments

Comments
 (0)