Skip to content

Commit

Permalink
more graceful implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonSc committed May 14, 2024
1 parent f242ab1 commit aa9afed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 18 deletions.
45 changes: 27 additions & 18 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"net"
"net/url"
"os"
Expand All @@ -14,6 +13,13 @@ import (
contentapi "github.com/containerd/containerd/api/services/content/v1"
"github.com/containerd/containerd/defaults"
grpc_retry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
controlapi "github.com/moby/buildkit/api/services/control"
"github.com/moby/buildkit/client/connhelper"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/grpchijack"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/grpcerrors"
"github.com/moby/buildkit/util/tracing/otlptracegrpc"
"github.com/pkg/errors"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
Expand All @@ -24,14 +30,6 @@ import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"

controlapi "github.com/moby/buildkit/api/services/control"
"github.com/moby/buildkit/client/connhelper"
"github.com/moby/buildkit/session"
"github.com/moby/buildkit/session/grpchijack"
"github.com/moby/buildkit/util/appdefaults"
"github.com/moby/buildkit/util/grpcerrors"
"github.com/moby/buildkit/util/tracing/otlptracegrpc"
)

type Client struct {
Expand All @@ -54,6 +52,7 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
grpc.WithDefaultCallOptions(grpc_retry.WithBackoff(grpc_retry.BackoffExponentialWithJitter(10*time.Millisecond, 0.1))), //earthly
}
needDialer := true
useDefaultDialer := false // earthly-specific

var unary []grpc.UnaryClientInterceptor
var stream []grpc.StreamClientInterceptor
Expand Down Expand Up @@ -96,6 +95,11 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
headersKV = h.kv
}

// earthly-specific
if _, ok := o.(*withDefaultGRPCDialer); ok {
useDefaultDialer = true
}

if opt, ok := o.(*withGRPCDialOption); ok {
customDialOptions = append(customDialOptions, opt.opt)
}
Expand Down Expand Up @@ -123,13 +127,12 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
stream = append(stream, otelgrpc.StreamClientInterceptor(otelgrpc.WithTracerProvider(tracerProvider), otelgrpc.WithPropagators(propagators)))
}

if needDialer {
fmt.Println("need dialer")
//dialFn, err := resolveDialer(address)
//if err != nil {
// return nil, err
//}
//gopts = append(gopts, grpc.WithContextDialer(dialFn))
if needDialer && !useDefaultDialer {
dialFn, err := resolveDialer(address)
if err != nil {
return nil, err
}
gopts = append(gopts, grpc.WithContextDialer(dialFn))
}
if address == "" {
address = appdefaults.Address
Expand Down Expand Up @@ -167,8 +170,14 @@ func New(ctx context.Context, address string, opts ...ClientOpt) (*Client, error
gopts = append(gopts, grpc.WithChainStreamInterceptor(stream...))
gopts = append(gopts, customDialOptions...)

address = strings.Trim(address, "tcp://")
fmt.Println("address: " + address)
// earthly-specific
if useDefaultDialer {
split := strings.Split(address, "://")
if len(split) > 0 {
address = split[1]
}
}

conn, err := grpc.DialContext(ctx, address, gopts...)
if err != nil {
return nil, errors.Wrapf(err, "failed to dial %q . make sure buildkitd is running", address)
Expand Down
11 changes: 11 additions & 0 deletions client/client_earthly.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,14 @@ func headersStreamInterceptor(kv ...string) grpc.StreamClientInterceptor {
return streamer(ctx, desc, cc, method, opts...)
}
}

// WithDefaultGRPCDialer triggers the internal gRPC dialer to be used instead of the buildkit default.
// This can be important when buildkit server is behind an HTTP connect proxy,
// since the default dialer in gRPC already knows how to use those.
func WithDefaultGRPCDialer() ClientOpt {
return &withDefaultGRPCDialer{}
}

type withDefaultGRPCDialer struct{}

func (*withDefaultGRPCDialer) isClientOpt() {}

0 comments on commit aa9afed

Please sign in to comment.