driver(docker): opt to set additional dial meta to the client

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2023-10-17 00:45:41 +02:00
parent fd251d2a7b
commit 511e41386f
No known key found for this signature in database
GPG Key ID: 3248E46B6BB8C7F7
9 changed files with 27 additions and 19 deletions

View File

@ -208,7 +208,7 @@ type driverFactory struct {
} }
// Factory returns the driver factory. // Factory returns the driver factory.
func (b *Builder) Factory(ctx context.Context) (_ driver.Factory, err error) { func (b *Builder) Factory(ctx context.Context, dialMeta map[string][]string) (_ driver.Factory, err error) {
b.driverFactory.once.Do(func() { b.driverFactory.once.Do(func() {
if b.Driver != "" { if b.Driver != "" {
b.driverFactory.Factory, err = driver.GetFactory(b.Driver, true) b.driverFactory.Factory, err = driver.GetFactory(b.Driver, true)
@ -231,7 +231,7 @@ func (b *Builder) Factory(ctx context.Context) (_ driver.Factory, err error) {
if _, err = dockerapi.Ping(ctx); err != nil { if _, err = dockerapi.Ping(ctx); err != nil {
return return
} }
b.driverFactory.Factory, err = driver.GetDefaultFactory(ctx, ep, dockerapi, false) b.driverFactory.Factory, err = driver.GetDefaultFactory(ctx, ep, dockerapi, false, dialMeta)
if err != nil { if err != nil {
return return
} }

View File

@ -45,7 +45,8 @@ func (b *Builder) Nodes() []Node {
type LoadNodesOption func(*loadNodesOptions) type LoadNodesOption func(*loadNodesOptions)
type loadNodesOptions struct { type loadNodesOptions struct {
data bool data bool
dialMeta map[string][]string
} }
func WithData() LoadNodesOption { func WithData() LoadNodesOption {
@ -54,6 +55,12 @@ func WithData() LoadNodesOption {
} }
} }
func WithDialMeta(dialMeta map[string][]string) LoadNodesOption {
return func(o *loadNodesOptions) {
o.dialMeta = dialMeta
}
}
// LoadNodes loads and returns nodes for this builder. // LoadNodes loads and returns nodes for this builder.
// TODO: this should be a method on a Node object and lazy load data for each driver. // TODO: this should be a method on a Node object and lazy load data for each driver.
func (b *Builder) LoadNodes(ctx context.Context, opts ...LoadNodesOption) (_ []Node, err error) { func (b *Builder) LoadNodes(ctx context.Context, opts ...LoadNodesOption) (_ []Node, err error) {
@ -73,7 +80,7 @@ func (b *Builder) LoadNodes(ctx context.Context, opts ...LoadNodesOption) (_ []N
} }
}() }()
factory, err := b.Factory(ctx) factory, err := b.Factory(ctx, lno.dialMeta)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -132,7 +139,7 @@ func (b *Builder) LoadNodes(ctx context.Context, opts ...LoadNodesOption) (_ []N
} }
} }
d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, factory, n.Endpoint, dockerapi, imageopt.Auth, kcc, n.Flags, n.Files, n.DriverOpts, n.Platforms, b.opts.contextPathHash) d, err := driver.GetDriver(ctx, "buildx_buildkit_"+n.Name, factory, n.Endpoint, dockerapi, imageopt.Auth, kcc, n.Flags, n.Files, n.DriverOpts, n.Platforms, b.opts.contextPathHash, lno.dialMeta)
if err != nil { if err != nil {
node.Err = err node.Err = err
return nil return nil

View File

@ -123,7 +123,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
if len(args) > 0 { if len(args) > 0 {
arg = args[0] arg = args[0]
} }
f, err := driver.GetDefaultFactory(ctx, arg, dockerCli.Client(), true) f, err := driver.GetDefaultFactory(ctx, arg, dockerCli.Client(), true, nil)
if err != nil { if err != nil {
return err return err
} }

View File

@ -28,7 +28,7 @@ func (*factory) Usage() string {
return "docker-container" return "docker-container"
} }
func (*factory) Priority(ctx context.Context, endpoint string, api dockerclient.APIClient) int { func (*factory) Priority(ctx context.Context, endpoint string, api dockerclient.APIClient, dialMeta map[string][]string) int {
if api == nil { if api == nil {
return priorityUnsupported return priorityUnsupported
} }

View File

@ -58,7 +58,7 @@ func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
func (d *Driver) Client(ctx context.Context) (*client.Client, error) { func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
opts := []client.ClientOpt{ opts := []client.ClientOpt{
client.WithContextDialer(func(context.Context, string) (net.Conn, error) { client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return d.DockerAPI.DialHijack(ctx, "/grpc", "h2c", nil) return d.DockerAPI.DialHijack(ctx, "/grpc", "h2c", d.DialMeta)
}), client.WithSessionDialer(func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) { }), client.WithSessionDialer(func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) {
return d.DockerAPI.DialHijack(ctx, "/session", proto, meta) return d.DockerAPI.DialHijack(ctx, "/session", proto, meta)
}), }),

View File

@ -26,12 +26,12 @@ func (*factory) Usage() string {
return "docker" return "docker"
} }
func (*factory) Priority(ctx context.Context, endpoint string, api dockerclient.APIClient) int { func (*factory) Priority(ctx context.Context, endpoint string, api dockerclient.APIClient, dialMeta map[string][]string) int {
if api == nil { if api == nil {
return priorityUnsupported return priorityUnsupported
} }
c, err := api.DialHijack(ctx, "/grpc", "h2c", nil) c, err := api.DialHijack(ctx, "/grpc", "h2c", dialMeta)
if err != nil { if err != nil {
return priorityUnsupported return priorityUnsupported
} }

View File

@ -34,7 +34,7 @@ func (*factory) Usage() string {
return DriverName return DriverName
} }
func (*factory) Priority(ctx context.Context, endpoint string, api dockerclient.APIClient) int { func (*factory) Priority(ctx context.Context, endpoint string, api dockerclient.APIClient, dialMeta map[string][]string) int {
if api == nil { if api == nil {
return priorityUnsupported return priorityUnsupported
} }

View File

@ -17,7 +17,7 @@ import (
type Factory interface { type Factory interface {
Name() string Name() string
Usage() string Usage() string
Priority(ctx context.Context, endpoint string, api dockerclient.APIClient) int Priority(ctx context.Context, endpoint string, api dockerclient.APIClient, dialMeta map[string][]string) int
New(ctx context.Context, cfg InitConfig) (Driver, error) New(ctx context.Context, cfg InitConfig) (Driver, error)
AllowsInstances() bool AllowsInstances() bool
} }
@ -57,8 +57,8 @@ type InitConfig struct {
DriverOpts map[string]string DriverOpts map[string]string
Auth Auth Auth Auth
Platforms []specs.Platform Platforms []specs.Platform
// ContextPathHash can be used for determining pods in the driver instance ContextPathHash string // can be used for determining pods in the driver instance
ContextPathHash string DialMeta map[string][]string
} }
var drivers map[string]Factory var drivers map[string]Factory
@ -70,7 +70,7 @@ func Register(f Factory) {
drivers[f.Name()] = f drivers[f.Name()] = f
} }
func GetDefaultFactory(ctx context.Context, ep string, c dockerclient.APIClient, instanceRequired bool) (Factory, error) { func GetDefaultFactory(ctx context.Context, ep string, c dockerclient.APIClient, instanceRequired bool, dialMeta map[string][]string) (Factory, error) {
if len(drivers) == 0 { if len(drivers) == 0 {
return nil, errors.Errorf("no drivers available") return nil, errors.Errorf("no drivers available")
} }
@ -83,7 +83,7 @@ func GetDefaultFactory(ctx context.Context, ep string, c dockerclient.APIClient,
if instanceRequired && !f.AllowsInstances() { if instanceRequired && !f.AllowsInstances() {
continue continue
} }
dd = append(dd, p{f: f, priority: f.Priority(ctx, ep, c)}) dd = append(dd, p{f: f, priority: f.Priority(ctx, ep, c, dialMeta)})
} }
sort.Slice(dd, func(i, j int) bool { sort.Slice(dd, func(i, j int) bool {
return dd[i].priority < dd[j].priority return dd[i].priority < dd[j].priority
@ -103,7 +103,7 @@ func GetFactory(name string, instanceRequired bool) (Factory, error) {
return nil, errors.Errorf("failed to find driver %q", name) return nil, errors.Errorf("failed to find driver %q", name)
} }
func GetDriver(ctx context.Context, name string, f Factory, endpointAddr string, api dockerclient.APIClient, auth Auth, kcc KubeClientConfig, flags []string, files map[string][]byte, do map[string]string, platforms []specs.Platform, contextPathHash string) (*DriverHandle, error) { func GetDriver(ctx context.Context, name string, f Factory, endpointAddr string, api dockerclient.APIClient, auth Auth, kcc KubeClientConfig, flags []string, files map[string][]byte, do map[string]string, platforms []specs.Platform, contextPathHash string, dialMeta map[string][]string) (*DriverHandle, error) {
ic := InitConfig{ ic := InitConfig{
EndpointAddr: endpointAddr, EndpointAddr: endpointAddr,
DockerAPI: api, DockerAPI: api,
@ -114,11 +114,12 @@ func GetDriver(ctx context.Context, name string, f Factory, endpointAddr string,
Auth: auth, Auth: auth,
Platforms: platforms, Platforms: platforms,
ContextPathHash: contextPathHash, ContextPathHash: contextPathHash,
DialMeta: dialMeta,
Files: files, Files: files,
} }
if f == nil { if f == nil {
var err error var err error
f, err = GetDefaultFactory(ctx, endpointAddr, api, false) f, err = GetDefaultFactory(ctx, endpointAddr, api, false, dialMeta)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -35,7 +35,7 @@ func (*factory) Usage() string {
return "remote" return "remote"
} }
func (*factory) Priority(ctx context.Context, endpoint string, api dockerclient.APIClient) int { func (*factory) Priority(ctx context.Context, endpoint string, api dockerclient.APIClient, dialMeta map[string][]string) int {
if util.IsValidEndpoint(endpoint) != nil { if util.IsValidEndpoint(endpoint) != nil {
return priorityUnsupported return priorityUnsupported
} }