Skip to content

Commit 90f15c4

Browse files
authored
Fix image build failed for GAR (#20596)
1 parent cdd79f3 commit 90f15c4

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

components/image-builder-bob/pkg/proxy/proxy.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func rewriteDockerAPIURL(u *url.URL, fromRepo, toRepo, host, tag string) {
6060
from = "/v2/" + strings.Trim(fromRepo, "/") + "/"
6161
to = "/v2/" + strings.Trim(toRepo, "/") + "/"
6262
)
63-
u.Path = to + strings.TrimPrefix(u.Path, from)
63+
u.Path = to + strings.TrimPrefix(strings.TrimPrefix(u.Path, from), "/")
6464

6565
// we reset the escaped encoding hint, because EscapedPath will produce a valid encoding.
6666
u.RawPath = ""
@@ -95,7 +95,7 @@ func rewriteNonDockerAPIURL(u *url.URL, fromPrefix, toPrefix, host string) {
9595
if toPrefix == "" {
9696
to = "/"
9797
}
98-
u.Path = to + strings.TrimPrefix(u.Path, from)
98+
u.Path = to + strings.TrimPrefix(strings.TrimPrefix(u.Path, from), "/")
9999

100100
// we reset the escaped encoding hint, because EscapedPath will produce a valid encoding.
101101
u.RawPath = ""

components/image-builder-bob/pkg/proxy/proxy_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,22 @@ func TestRewriteNonDockerAPIURL(t *testing.T) {
6969
Path: "/to/some/random/path",
7070
},
7171
},
72+
{
73+
Name: "fromPrefix and toPrefix are not empty and origin url is not start with fromPrefix",
74+
in: input{
75+
fromPrefix: "from",
76+
toPrefix: "to",
77+
host: "localhost.com",
78+
u: url.URL{
79+
Host: "example.com",
80+
Path: "/other-string/some/random/path",
81+
},
82+
},
83+
u: url.URL{
84+
Host: "localhost.com",
85+
Path: "/to/other-string/some/random/path",
86+
},
87+
},
7288
{
7389
Name: "fromPrefix and toPrefix are empty",
7490
in: input{
@@ -134,6 +150,23 @@ func TestRewriteDockerAPIURL(t *testing.T) {
134150
Path: "/v2/base/some/random/path",
135151
},
136152
},
153+
{
154+
Name: "remote to localhost without repo",
155+
in: input{
156+
fromRepo: "base-images",
157+
toRepo: "base",
158+
host: "localhost.com",
159+
tag: "",
160+
u: url.URL{
161+
Host: "prince.azurecr.io",
162+
Path: "/v2/other-string/some/random/path",
163+
},
164+
},
165+
u: url.URL{
166+
Host: "localhost.com",
167+
Path: "/v2/base/v2/other-string/some/random/path",
168+
},
169+
},
137170
{
138171
Name: "localhost to remote",
139172
in: input{

0 commit comments

Comments
 (0)