Skip to content

Commit 0d3de59

Browse files
committed
integration: use credentials for test mirrors
Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 2a1cc96 commit 0d3de59

File tree

2 files changed

+43
-4
lines changed

2 files changed

+43
-4
lines changed

util/contentutil/refs.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,41 @@ import (
1616
"github.com/pkg/errors"
1717
)
1818

19-
func ProviderFromRef(ref string) (ocispecs.Descriptor, content.Provider, error) {
19+
type ResolveOpt struct {
20+
Credentials func(string) (string, string, error)
21+
}
22+
23+
type ResolveOptFunc func(*ResolveOpt)
24+
25+
func WithCredentials(c func(string) (string, string, error)) ResolveOptFunc {
26+
return func(o *ResolveOpt) {
27+
o.Credentials = func(host string) (string, string, error) {
28+
if host == "registry-1.docker.io" {
29+
host = "https://index.docker.io/v1/"
30+
}
31+
return c(host)
32+
}
33+
}
34+
}
35+
36+
func ProviderFromRef(ref string, opts ...ResolveOptFunc) (ocispecs.Descriptor, content.Provider, error) {
2037
headers := http.Header{}
2138
headers.Set("User-Agent", version.UserAgent())
22-
remote := docker.NewResolver(docker.ResolverOptions{
39+
40+
var ro ResolveOpt
41+
for _, f := range opts {
42+
f(&ro)
43+
}
44+
45+
dro := docker.ResolverOptions{
2346
Headers: headers,
24-
})
47+
}
48+
if ro.Credentials != nil {
49+
dro.Hosts = docker.ConfigureDefaultRegistries(
50+
docker.WithAuthorizer(docker.NewDockerAuthorizer(docker.WithAuthCreds(ro.Credentials))),
51+
)
52+
}
53+
remote := docker.NewResolver(dro)
2554

2655
name, desc, err := remote.Resolve(context.TODO(), ref)
2756
if err != nil {

util/testutil/integration/run.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
"github.com/containerd/containerd/v2/core/content"
2121
"github.com/containerd/containerd/v2/core/remotes/docker"
22+
"github.com/docker/cli/cli/config"
2223
"github.com/gofrs/flock"
2324
"github.com/moby/buildkit/util/appcontext"
2425
"github.com/moby/buildkit/util/contentutil"
@@ -257,7 +258,16 @@ func copyImagesLocal(t *testing.T, host string, images map[string]string) error
257258
defer closer()
258259
}
259260
} else {
260-
desc, provider, err = contentutil.ProviderFromRef(from)
261+
dockerConfig := config.LoadDefaultConfigFile(os.Stderr)
262+
263+
desc, provider, err = contentutil.ProviderFromRef(from, contentutil.WithCredentials(
264+
func(host string) (string, string, error) {
265+
ac, err := dockerConfig.GetAuthConfig(host)
266+
if err != nil {
267+
return "", "", err
268+
}
269+
return ac.Username, ac.Password, nil
270+
}))
261271
if err != nil {
262272
return err
263273
}

0 commit comments

Comments
 (0)