Skip to content

Commit

Permalink
driver(docker): opt to set additional dial meta to the client
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Oct 16, 2023
1 parent fd251d2 commit 511e413
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 19 deletions.
4 changes: 2 additions & 2 deletions builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ type driverFactory struct {
}

// 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() {
if b.Driver != "" {
b.driverFactory.Factory, err = driver.GetFactory(b.Driver, true)
Expand All @@ -231,7 +231,7 @@ func (b *Builder) Factory(ctx context.Context) (_ driver.Factory, err error) {
if _, err = dockerapi.Ping(ctx); err != nil {
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 {
return
}
Expand Down
13 changes: 10 additions & 3 deletions builder/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ func (b *Builder) Nodes() []Node {
type LoadNodesOption func(*loadNodesOptions)

type loadNodesOptions struct {
data bool
data bool
dialMeta map[string][]string
}

func WithData() LoadNodesOption {
Expand All @@ -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.
// 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) {
Expand All @@ -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 {
return nil, err
}
Expand Down Expand Up @@ -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 {
node.Err = err
return nil
Expand Down
2 changes: 1 addition & 1 deletion commands/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func runCreate(dockerCli command.Cli, in createOptions, args []string) error {
if len(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 {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion driver/docker-container/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (*factory) Usage() string {
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 {
return priorityUnsupported
}
Expand Down
2 changes: 1 addition & 1 deletion driver/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
opts := []client.ClientOpt{
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) {
return d.DockerAPI.DialHijack(ctx, "/session", proto, meta)
}),
Expand Down
4 changes: 2 additions & 2 deletions driver/docker/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func (*factory) Usage() string {
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 {
return priorityUnsupported
}

c, err := api.DialHijack(ctx, "/grpc", "h2c", nil)
c, err := api.DialHijack(ctx, "/grpc", "h2c", dialMeta)
if err != nil {
return priorityUnsupported
}
Expand Down
2 changes: 1 addition & 1 deletion driver/kubernetes/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (*factory) Usage() string {
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 {
return priorityUnsupported
}
Expand Down
15 changes: 8 additions & 7 deletions driver/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type Factory interface {
Name() 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)
AllowsInstances() bool
}
Expand Down Expand Up @@ -57,8 +57,8 @@ type InitConfig struct {
DriverOpts map[string]string
Auth Auth
Platforms []specs.Platform
// ContextPathHash can be used for determining pods in the driver instance
ContextPathHash string
ContextPathHash string // can be used for determining pods in the driver instance
DialMeta map[string][]string
}

var drivers map[string]Factory
Expand All @@ -70,7 +70,7 @@ func Register(f Factory) {
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 {
return nil, errors.Errorf("no drivers available")
}
Expand All @@ -83,7 +83,7 @@ func GetDefaultFactory(ctx context.Context, ep string, c dockerclient.APIClient,
if instanceRequired && !f.AllowsInstances() {
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 {
return dd[i].priority < dd[j].priority
Expand All @@ -103,7 +103,7 @@ func GetFactory(name string, instanceRequired bool) (Factory, error) {
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{
EndpointAddr: endpointAddr,
DockerAPI: api,
Expand All @@ -114,11 +114,12 @@ func GetDriver(ctx context.Context, name string, f Factory, endpointAddr string,
Auth: auth,
Platforms: platforms,
ContextPathHash: contextPathHash,
DialMeta: dialMeta,
Files: files,
}
if f == nil {
var err error
f, err = GetDefaultFactory(ctx, endpointAddr, api, false)
f, err = GetDefaultFactory(ctx, endpointAddr, api, false, dialMeta)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion driver/remote/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (*factory) Usage() string {
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 {
return priorityUnsupported
}
Expand Down

0 comments on commit 511e413

Please sign in to comment.