Skip to content

Commit

Permalink
driver(docker): allow attaching additional headers 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 Sep 28, 2023
1 parent e6756d9 commit 110f029
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion driver/docker-container/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
return nil
}

func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
func (d *Driver) Client(ctx context.Context, copts ...driver.ClientOption) (*client.Client, error) {
_, conn, err := d.exec(ctx, []string{"buildctl", "dial-stdio"})
if err != nil {
return nil, err
Expand Down
9 changes: 7 additions & 2 deletions driver/docker/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@ func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
return nil
}

func (d *Driver) Client(ctx context.Context) (*client.Client, 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.Meta)
}), 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
14 changes: 13 additions & 1 deletion driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ type Driver interface {
Version(context.Context) (string, error)
Stop(ctx context.Context, force bool) error
Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error
Client(ctx context.Context) (*client.Client, error)
Client(ctx context.Context, opts ...ClientOption) (*client.Client, error)
Features(ctx context.Context) map[Feature]bool
IsMobyDriver() bool
Config() InitConfig
Expand Down Expand Up @@ -110,3 +110,15 @@ func historyAPISupported(ctx context.Context, c *client.Client) bool {
}
}
}

type ClientOption func(*ClientOptions)

type ClientOptions struct {
Meta map[string][]string
}

func WithMeta(meta map[string][]string) ClientOption {
return func(o *ClientOptions) {
o.Meta = meta
}
}
2 changes: 1 addition & 1 deletion driver/kubernetes/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
return nil
}

func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
func (d *Driver) Client(ctx context.Context, copts ...driver.ClientOption) (*client.Client, error) {
restClient := d.clientset.CoreV1().RESTClient()
restClientConfig, err := d.KubeClientConfig.ClientConfig()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions driver/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ type DriverHandle struct {
hostGatewayIPErr error
}

func (d *DriverHandle) Client(ctx context.Context) (*client.Client, error) {
func (d *DriverHandle) Client(ctx context.Context, copts ...ClientOption) (*client.Client, error) {
d.once.Do(func() {
d.client, d.err = d.Driver.Client(ctx)
d.client, d.err = d.Driver.Client(ctx, copts...)
})
return d.client, d.err
}
Expand All @@ -173,9 +173,9 @@ func (d *DriverHandle) Features(ctx context.Context) map[Feature]bool {
return d.features
}

func (d *DriverHandle) HistoryAPISupported(ctx context.Context) bool {
func (d *DriverHandle) HistoryAPISupported(ctx context.Context, copts ...ClientOption) bool {
d.historyAPISupportedOnce.Do(func() {
if c, err := d.Client(ctx); err == nil {
if c, err := d.Client(ctx, copts...); err == nil {
d.historyAPISupported = historyAPISupported(ctx, c)
}
})
Expand Down
2 changes: 1 addition & 1 deletion driver/remote/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
return nil
}

func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
func (d *Driver) Client(ctx context.Context, copts ...driver.ClientOption) (*client.Client, error) {
opts := []client.ClientOpt{}

exp, err := detect.Exporter()
Expand Down

0 comments on commit 110f029

Please sign in to comment.