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 12, 2023
1 parent 6e259a2 commit 14ae6f8
Show file tree
Hide file tree
Showing 13 changed files with 55 additions and 30 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
17 changes: 12 additions & 5 deletions builder/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type LoadNodesOption func(*loadNodesOptions)
type loadNodesOptions struct {
data bool
cachedClient bool
dialMeta map[string][]string
}

func WithData() LoadNodesOption {
Expand All @@ -64,6 +65,12 @@ func WithCachedClient(enabled bool) 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 @@ -84,7 +91,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 @@ -152,7 +159,7 @@ func (b *Builder) LoadNodes(ctx context.Context, opts ...LoadNodesOption) (_ []N
node.ImageOpt = imageopt

if lno.data {
if err := node.loadData(ctx, lno.cachedClient); err != nil {
if err := node.loadData(ctx, lno); err != nil {
node.Err = err
}
}
Expand Down Expand Up @@ -203,7 +210,7 @@ func (b *Builder) LoadNodes(ctx context.Context, opts ...LoadNodesOption) (_ []N
return b.nodes, nil
}

func (n *Node) loadData(ctx context.Context, cachedClient bool) error {
func (n *Node) loadData(ctx context.Context, lno loadNodesOptions) error {
if n.Driver == nil {
return nil
}
Expand All @@ -213,11 +220,11 @@ func (n *Node) loadData(ctx context.Context, cachedClient bool) error {
}
n.DriverInfo = info
if n.DriverInfo.Status == driver.Running {
c, err := n.Driver.Client(ctx, driver.WithCachedClient(cachedClient))
c, err := n.Driver.Client(ctx, driver.WithCachedClient(lno.cachedClient), driver.WithDialMeta(lno.dialMeta))
if err != nil {
return err
}
if !cachedClient {
if !lno.cachedClient {
defer c.Close()
}

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
4 changes: 2 additions & 2 deletions driver/docker-container/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ func (d *Driver) Factory() driver.Factory {
return d.factory
}

func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
func (d *Driver) Features(ctx context.Context, copts ...driver.ClientOption) map[driver.Feature]bool {
return map[driver.Feature]bool{
driver.OCIExporter: true,
driver.DockerExporter: true,
Expand All @@ -399,7 +399,7 @@ func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
}
}

func (d *Driver) HostGatewayIP(ctx context.Context) (net.IP, error) {
func (d *Driver) HostGatewayIP(ctx context.Context, copts ...driver.ClientOption) (net.IP, error) {
return nil, errors.New("host-gateway is not supported by the docker-container driver")
}

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, copts ...driver.ClientOption) int {
if api == nil {
return priorityUnsupported
}
Expand Down
15 changes: 10 additions & 5 deletions driver/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
}

func (d *Driver) Client(ctx context.Context, copts ...driver.ClientOption) (*client.Client, error) {
co := driver.ClientOptions{}
for _, opt := range copts {
opt(&co)
}

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", co.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 All @@ -79,10 +84,10 @@ type features struct {
list map[driver.Feature]bool
}

func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
func (d *Driver) Features(ctx context.Context, copts ...driver.ClientOption) map[driver.Feature]bool {
d.features.once.Do(func() {
var useContainerdSnapshotter bool
if c, err := d.Client(ctx); err == nil {
if c, err := d.Client(ctx, copts...); err == nil {
defer c.Close()
workers, _ := c.ListWorkers(ctx)
for _, w := range workers {
Expand All @@ -107,9 +112,9 @@ type hostGateway struct {
err error
}

func (d *Driver) HostGatewayIP(ctx context.Context) (net.IP, error) {
func (d *Driver) HostGatewayIP(ctx context.Context, copts ...driver.ClientOption) (net.IP, error) {
d.hostGateway.once.Do(func() {
c, err := d.Client(ctx)
c, err := d.Client(ctx, copts...)
if err != nil {
d.hostGateway.err = err
return
Expand Down
9 changes: 7 additions & 2 deletions driver/docker/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@ 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, copts ...driver.ClientOption) int {
if api == nil {
return priorityUnsupported
}

c, err := api.DialHijack(ctx, "/grpc", "h2c", nil)
co := driver.ClientOptions{}
for _, opt := range copts {
opt(&co)
}

c, err := api.DialHijack(ctx, "/grpc", "h2c", co.DialMeta)
if err != nil {
return priorityUnsupported
}
Expand Down
4 changes: 2 additions & 2 deletions driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ type Driver interface {
Stop(ctx context.Context, force bool) error
Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error
Client(ctx context.Context, copts ...ClientOption) (*client.Client, error)
Features(ctx context.Context) map[Feature]bool
HostGatewayIP(ctx context.Context) (net.IP, error)
Features(ctx context.Context, copts ...ClientOption) map[Feature]bool
HostGatewayIP(ctx context.Context, copts ...ClientOption) (net.IP, error)
IsMobyDriver() bool
Config() InitConfig
}
Expand Down
4 changes: 2 additions & 2 deletions driver/kubernetes/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (d *Driver) Factory() driver.Factory {
return d.factory
}

func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
func (d *Driver) Features(ctx context.Context, copts ...driver.ClientOption) map[driver.Feature]bool {
return map[driver.Feature]bool{
driver.OCIExporter: true,
driver.DockerExporter: d.DockerAPI != nil,
Expand All @@ -237,6 +237,6 @@ func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
}
}

func (d *Driver) HostGatewayIP(ctx context.Context) (net.IP, error) {
func (d *Driver) HostGatewayIP(ctx context.Context, copts ...driver.ClientOption) (net.IP, error) {
return nil, errors.New("host-gateway is not supported by the kubernetes driver")
}
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, copts ...driver.ClientOption) int {
if api == nil {
return priorityUnsupported
}
Expand Down
16 changes: 12 additions & 4 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, copts ...ClientOption) int
New(ctx context.Context, cfg InitConfig) (Driver, error)
AllowsInstances() bool
}
Expand Down Expand Up @@ -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, WithDialMeta(dialMeta))})
}
sort.Slice(dd, func(i, j int) bool {
return dd[i].priority < dd[j].priority
Expand Down Expand Up @@ -118,7 +118,7 @@ func GetDriver(ctx context.Context, name string, f Factory, endpointAddr string,
}
if f == nil {
var err error
f, err = GetDefaultFactory(ctx, endpointAddr, api, false)
f, err = GetDefaultFactory(ctx, endpointAddr, api, false, nil)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -171,10 +171,18 @@ type ClientOption func(*ClientOptions)

type ClientOptions struct {
cachedClient bool

DialMeta map[string][]string
}

func WithCachedClient(enabled bool) ClientOption {
return func(o *ClientOptions) {
o.cachedClient = enabled
}
}

func WithDialMeta(dialMeta map[string][]string) ClientOption {
return func(o *ClientOptions) {
o.DialMeta = dialMeta
}
}
4 changes: 2 additions & 2 deletions driver/remote/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func (d *Driver) Client(ctx context.Context, copts ...driver.ClientOption) (*cli
return client.New(ctx, d.InitConfig.EndpointAddr, opts...)
}

func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
func (d *Driver) Features(ctx context.Context, copts ...driver.ClientOption) map[driver.Feature]bool {
return map[driver.Feature]bool{
driver.OCIExporter: true,
driver.DockerExporter: true,
Expand All @@ -93,7 +93,7 @@ func (d *Driver) Features(ctx context.Context) map[driver.Feature]bool {
}
}

func (d *Driver) HostGatewayIP(ctx context.Context) (net.IP, error) {
func (d *Driver) HostGatewayIP(ctx context.Context, copts ...driver.ClientOption) (net.IP, error) {
return nil, errors.New("host-gateway is not supported by the remote driver")
}

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, copts ...driver.ClientOption) int {
if util.IsValidEndpoint(endpoint) != nil {
return priorityUnsupported
}
Expand Down

0 comments on commit 14ae6f8

Please sign in to comment.