driver: initialize tracer delegate in driver handle instead of individual plugins

This refactors the driver handle to initialize the tracer delegate
inside of the driver handle instead of the individual plugins.

This provides more uniformity to how the tracer delegate is created by
allowing the driver handle to pass additional client options to the
drivers when they create the client. It also avoids creating the tracer
delegate client multiple times because the driver handle will only
initialize the client once. This prevents some drivers, like the remote
driver, from accidentally registering multiple clients as tracer
delegates.

Signed-off-by: Jonathan A. Sternberg <jonathan.sternberg@docker.com>
This commit is contained in:
Jonathan A. Sternberg
2024-03-27 15:13:43 -05:00
parent 8abef59087
commit 5c89004bb6
8 changed files with 48 additions and 65 deletions

View File

@ -30,7 +30,6 @@ import (
"github.com/docker/docker/pkg/idtools"
"github.com/docker/docker/pkg/stdcopy"
"github.com/moby/buildkit/client"
"github.com/moby/buildkit/util/tracing/detect"
"github.com/pkg/errors"
)
@ -396,28 +395,21 @@ func (d *Driver) Dial(ctx context.Context) (net.Conn, error) {
return conn, nil
}
func (d *Driver) Client(ctx context.Context) (*client.Client, error) {
func (d *Driver) Client(ctx context.Context, opts ...client.ClientOpt) (*client.Client, error) {
conn, err := d.Dial(ctx)
if err != nil {
return nil, err
}
exp, _, err := detect.Exporter()
if err != nil {
return nil, err
}
var opts []client.ClientOpt
var counter int64
opts = append(opts, client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
if atomic.AddInt64(&counter, 1) > 1 {
return nil, net.ErrClosed
}
return conn, nil
}))
if td, ok := exp.(client.TracerDelegate); ok {
opts = append(opts, client.WithTracerDelegate(td))
}
opts = append([]client.ClientOpt{
client.WithContextDialer(func(context.Context, string) (net.Conn, error) {
if atomic.AddInt64(&counter, 1) > 1 {
return nil, net.ErrClosed
}
return conn, nil
}),
}, opts...)
return client.New(ctx, "", opts...)
}