Skip to content

Commit 4edfcdd

Browse files
authored
Merge pull request #1919 from Random-Liu/fix-containerd-wait
Fix containerd connection wait.
2 parents 31c5df3 + 021065e commit 4edfcdd

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

container/containerd/client.go

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ package containerd
1616

1717
import (
1818
"context"
19+
"fmt"
20+
"net"
1921
"sync"
2022
"time"
2123

@@ -49,24 +51,37 @@ type containerdClient interface {
4951
var once sync.Once
5052
var ctrdClient containerdClient = nil
5153

54+
const (
55+
address = "/run/containerd/containerd.sock"
56+
maxBackoffDelay = 3 * time.Second
57+
connectionTimeout = 2 * time.Second
58+
)
59+
5260
// Client creates a containerd client
5361
func Client() (containerdClient, error) {
5462
var retErr error
5563
once.Do(func() {
64+
tryConn, err := net.DialTimeout("unix", address, connectionTimeout)
65+
if err != nil {
66+
retErr = fmt.Errorf("containerd: cannot unix dial containerd api service: %v", err)
67+
return
68+
}
69+
tryConn.Close()
70+
5671
gopts := []grpc.DialOption{
5772
grpc.WithInsecure(),
5873
grpc.WithDialer(dialer.Dialer),
5974
grpc.WithBlock(),
60-
grpc.WithTimeout(2 * time.Second),
61-
grpc.WithBackoffMaxDelay(3 * time.Second),
75+
grpc.WithBackoffMaxDelay(maxBackoffDelay),
76+
grpc.WithTimeout(connectionTimeout),
6277
}
6378
unary, stream := newNSInterceptors(k8sNamespace)
6479
gopts = append(gopts,
6580
grpc.WithUnaryInterceptor(unary),
6681
grpc.WithStreamInterceptor(stream),
6782
)
6883

69-
conn, err := grpc.Dial(dialer.DialAddress("/var/run/containerd/containerd.sock"), gopts...)
84+
conn, err := grpc.Dial(dialer.DialAddress(address), gopts...)
7085
if err != nil {
7186
retErr = err
7287
return

0 commit comments

Comments
 (0)