driver: allow attaching additional metadata to the client

Signed-off-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax
2023-08-23 09:41:26 +02:00
parent 29a496cdab
commit 905bee450a
6 changed files with 74 additions and 9 deletions

View File

@@ -9,6 +9,7 @@ import (
"github.com/moby/buildkit/client"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
"google.golang.org/grpc/metadata"
)
type Driver struct {
@@ -63,8 +64,13 @@ func (d *Driver) Rm(ctx context.Context, force, rmVolume, rmDaemon bool) error {
return nil
}
func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
opts := []client.ClientOpt{}
func (d *Driver) Client(ctx context.Context, copts ...driver.ClientOption) (*client.Client, error) {
co := driver.ClientOptions{}
for _, opt := range copts {
opt(&co)
}
var opts []client.ClientOpt
backoffConfig := backoff.DefaultConfig
backoffConfig.MaxDelay = 1 * time.Second
@@ -79,6 +85,14 @@ func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
}...)
}
if len(co.Meta) > 0 {
opts = append(opts, client.WithGRPCDialOption(grpc.WithChainUnaryInterceptor(func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
// merge the existing context with new metadata.
ctx = metadata.NewOutgoingContext(ctx, co.Meta)
return invoker(ctx, method, req, reply, cc, opts...)
})))
}
return client.New(ctx, d.InitConfig.EndpointAddr, opts...)
}