99 "context"
1010 "fmt"
1111 "io/ioutil"
12+ "math/rand"
1213 "net"
1314 "os"
1415 "path"
@@ -23,6 +24,8 @@ import (
2324 "github.com/containerd/containerd"
2425 "github.com/containerd/containerd/defaults"
2526 "github.com/containerd/containerd/pkg/dialer"
27+ "github.com/containerd/containerd/remotes/docker"
28+ "github.com/docker/distribution/reference"
2629 "github.com/docker/docker/api/types"
2730 containertypes "github.com/docker/docker/api/types/container"
2831 "github.com/docker/docker/api/types/swarm"
@@ -36,6 +39,8 @@ import (
3639 "github.com/docker/docker/daemon/logger"
3740 "github.com/docker/docker/daemon/network"
3841 "github.com/docker/docker/errdefs"
42+ "github.com/moby/buildkit/util/resolver"
43+ "github.com/moby/buildkit/util/tracing"
3944 "github.com/sirupsen/logrus"
4045 // register graph drivers
4146 _ "github.com/docker/docker/daemon/graphdriver/register"
@@ -141,6 +146,57 @@ func (daemon *Daemon) Features() *map[string]bool {
141146 return & daemon .configStore .Features
142147}
143148
149+ // NewResolveOptionsFunc returns a call back function to resolve "registry-mirrors" and
150+ // "insecure-registries" for buildkit
151+ func (daemon * Daemon ) NewResolveOptionsFunc () resolver.ResolveOptionsFunc {
152+ return func (ref string ) docker.ResolverOptions {
153+ var (
154+ registryKey = "docker.io"
155+ mirrors = make ([]string , len (daemon .configStore .Mirrors ))
156+ m = map [string ]resolver.RegistryConf {}
157+ )
158+ // must trim "https://" or "http://" prefix
159+ for i , v := range daemon .configStore .Mirrors {
160+ v = strings .TrimPrefix (v , "https://" )
161+ v = strings .TrimPrefix (v , "http://" )
162+ mirrors [i ] = v
163+ }
164+ // set "registry-mirrors"
165+ m [registryKey ] = resolver.RegistryConf {Mirrors : mirrors }
166+ // set "insecure-registries"
167+ for _ , v := range daemon .configStore .InsecureRegistries {
168+ v = strings .TrimPrefix (v , "http://" )
169+ m [v ] = resolver.RegistryConf {
170+ PlainHTTP : true ,
171+ }
172+ }
173+ def := docker.ResolverOptions {
174+ Client : tracing .DefaultClient ,
175+ }
176+
177+ parsed , err := reference .ParseNormalizedNamed (ref )
178+ if err != nil {
179+ return def
180+ }
181+ host := reference .Domain (parsed )
182+
183+ c , ok := m [host ]
184+ if ! ok {
185+ return def
186+ }
187+
188+ if len (c .Mirrors ) > 0 {
189+ def .Host = func (string ) (string , error ) {
190+ return c .Mirrors [rand .Intn (len (c .Mirrors ))], nil
191+ }
192+ }
193+
194+ def .PlainHTTP = c .PlainHTTP
195+
196+ return def
197+ }
198+ }
199+
144200func (daemon * Daemon ) restore () error {
145201 containers := make (map [string ]* container.Container )
146202
0 commit comments