From 082fa3635cf0d5ce3a726383689189e520aa6a47 Mon Sep 17 00:00:00 2001 From: CrazyMax Date: Thu, 28 Sep 2023 15:36:35 +0200 Subject: [PATCH] driver(docker): allow attaching additional headers to the client Signed-off-by: CrazyMax --- builder/builder.go | 8 ++++++++ builder/node.go | 2 +- driver/docker/driver.go | 2 +- driver/manager.go | 7 ++++--- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/builder/builder.go b/builder/builder.go index 5a877ae86c2..1e0a2656972 100644 --- a/builder/builder.go +++ b/builder/builder.go @@ -32,6 +32,7 @@ type builderOpts struct { name string txn *store.Txn contextPathHash string + meta map[string][]string validate bool } @@ -59,6 +60,13 @@ func WithContextPathHash(contextPathHash string) Option { } } +// WithMeta sets additional headers for the client. +func WithMeta(meta map[string][]string) Option { + return func(b *Builder) { + b.opts.meta = meta + } +} + // WithSkippedValidation skips builder context validation. func WithSkippedValidation() Option { return func(b *Builder) { diff --git a/builder/node.go b/builder/node.go index a8056240437..90fd9e500ce 100644 --- a/builder/node.go +++ b/builder/node.go @@ -109,7 +109,7 @@ func (b *Builder) LoadNodes(ctx context.Context, withData bool) (_ []Node, err e } } - 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.meta, b.opts.contextPathHash) if err != nil { node.Err = err return nil diff --git a/driver/docker/driver.go b/driver/docker/driver.go index e31175c33b8..5175220c303 100644 --- a/driver/docker/driver.go +++ b/driver/docker/driver.go @@ -54,7 +54,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.Meta) }), client.WithSessionDialer(func(ctx context.Context, proto string, meta map[string][]string) (net.Conn, error) { return d.DockerAPI.DialHijack(ctx, "/session", proto, meta) }), diff --git a/driver/manager.go b/driver/manager.go index b4ec318f396..bf397a34083 100644 --- a/driver/manager.go +++ b/driver/manager.go @@ -58,8 +58,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 + Meta map[string][]string // Meta is used for passing additional headers to the client (only docker one atm) + ContextPathHash string // ContextPathHash can be used for determining pods in the driver instance } var drivers map[string]Factory @@ -104,7 +104,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, meta map[string][]string, contextPathHash string) (*DriverHandle, error) { ic := InitConfig{ EndpointAddr: endpointAddr, DockerAPI: api, @@ -116,6 +116,7 @@ func GetDriver(ctx context.Context, name string, f Factory, endpointAddr string, Platforms: platforms, ContextPathHash: contextPathHash, Files: files, + Meta: meta, } if f == nil { var err error