Add NewWithGRPCOpts to specify grpc dial opts

Signed-off-by: Michael Crosby <crosbymichael@gmail.com>
This commit is contained in:
Michael Crosby 2017-06-26 10:18:01 -07:00
parent e2b5ef5f3d
commit 3508e135e6

View File

@ -46,7 +46,8 @@ func init() {
} }
type clientOpts struct { type clientOpts struct {
defaultns string defaultns string
dialOptions []grpc.DialOption
} }
type ClientOpt func(c *clientOpts) error type ClientOpt func(c *clientOpts) error
@ -58,6 +59,14 @@ func WithDefaultNamespace(ns string) ClientOpt {
} }
} }
// WithDialOpts allows grpc.DialOptions to be set on the connection
func WithDialOpts(opts []grpc.DialOption) ClientOpt {
return func(c *clientOpts) error {
c.dialOptions = opts
return nil
}
}
// New returns a new containerd client that is connected to the containerd // New returns a new containerd client that is connected to the containerd
// instance provided by address // instance provided by address
func New(address string, opts ...ClientOpt) (*Client, error) { func New(address string, opts ...ClientOpt) (*Client, error) {
@ -67,13 +76,15 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
return nil, err return nil, err
} }
} }
gopts := []grpc.DialOption{ gopts := []grpc.DialOption{
grpc.WithBlock(), grpc.WithBlock(),
grpc.WithInsecure(), grpc.WithInsecure(),
grpc.WithTimeout(100 * time.Second), grpc.WithTimeout(100 * time.Second),
grpc.WithDialer(dialer),
grpc.FailOnNonTempDialError(true), grpc.FailOnNonTempDialError(true),
grpc.WithDialer(dialer),
}
if len(copts.dialOptions) > 0 {
gopts = copts.dialOptions
} }
if copts.defaultns != "" { if copts.defaultns != "" {
unary, stream := newNSInterceptors(copts.defaultns) unary, stream := newNSInterceptors(copts.defaultns)
@ -82,11 +93,16 @@ func New(address string, opts ...ClientOpt) (*Client, error) {
grpc.WithStreamInterceptor(stream), grpc.WithStreamInterceptor(stream),
) )
} }
conn, err := grpc.Dial(dialAddress(address), gopts...) conn, err := grpc.Dial(dialAddress(address), gopts...)
if err != nil { if err != nil {
return nil, errors.Wrapf(err, "failed to dial %q", address) return nil, errors.Wrapf(err, "failed to dial %q", address)
} }
return NewWithConn(conn, opts...)
}
// NewWithConn returns a new containerd client that is connected to the containerd
// instance provided by the connection
func NewWithConn(conn *grpc.ClientConn, opts ...ClientOpt) (*Client, error) {
return &Client{ return &Client{
conn: conn, conn: conn,
runtime: fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS), runtime: fmt.Sprintf("%s.%s", plugin.RuntimePlugin, runtime.GOOS),