From 021065e3b9403116fc0a193bccc19725de8ed281 Mon Sep 17 00:00:00 2001 From: Lantao Liu Date: Thu, 29 Mar 2018 23:52:32 +0000 Subject: [PATCH] Fix containerd connection wait. Signed-off-by: Lantao Liu --- container/containerd/client.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/container/containerd/client.go b/container/containerd/client.go index 365f54a7..d73648c1 100644 --- a/container/containerd/client.go +++ b/container/containerd/client.go @@ -16,6 +16,8 @@ package containerd import ( "context" + "fmt" + "net" "sync" "time" @@ -49,16 +51,29 @@ type containerdClient interface { var once sync.Once var ctrdClient containerdClient = nil +const ( + address = "/run/containerd/containerd.sock" + maxBackoffDelay = 3 * time.Second + connectionTimeout = 2 * time.Second +) + // Client creates a containerd client func Client() (containerdClient, error) { var retErr error once.Do(func() { + tryConn, err := net.DialTimeout("unix", address, connectionTimeout) + if err != nil { + retErr = fmt.Errorf("containerd: cannot unix dial containerd api service: %v", err) + return + } + tryConn.Close() + gopts := []grpc.DialOption{ grpc.WithInsecure(), grpc.WithDialer(dialer.Dialer), grpc.WithBlock(), - grpc.WithTimeout(2 * time.Second), - grpc.WithBackoffMaxDelay(3 * time.Second), + grpc.WithBackoffMaxDelay(maxBackoffDelay), + grpc.WithTimeout(connectionTimeout), } unary, stream := newNSInterceptors(k8sNamespace) gopts = append(gopts, @@ -66,7 +81,7 @@ func Client() (containerdClient, error) { grpc.WithStreamInterceptor(stream), ) - conn, err := grpc.Dial(dialer.DialAddress("/var/run/containerd/containerd.sock"), gopts...) + conn, err := grpc.Dial(dialer.DialAddress(address), gopts...) if err != nil { retErr = err return