@@ -16,6 +16,8 @@ package containerd
16
16
17
17
import (
18
18
"context"
19
+ "fmt"
20
+ "net"
19
21
"sync"
20
22
"time"
21
23
@@ -49,24 +51,37 @@ type containerdClient interface {
49
51
var once sync.Once
50
52
var ctrdClient containerdClient = nil
51
53
54
+ const (
55
+ address = "/run/containerd/containerd.sock"
56
+ maxBackoffDelay = 3 * time .Second
57
+ connectionTimeout = 2 * time .Second
58
+ )
59
+
52
60
// Client creates a containerd client
53
61
func Client () (containerdClient , error ) {
54
62
var retErr error
55
63
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
+
56
71
gopts := []grpc.DialOption {
57
72
grpc .WithInsecure (),
58
73
grpc .WithDialer (dialer .Dialer ),
59
74
grpc .WithBlock (),
60
- grpc .WithTimeout ( 2 * time . Second ),
61
- grpc .WithBackoffMaxDelay ( 3 * time . Second ),
75
+ grpc .WithBackoffMaxDelay ( maxBackoffDelay ),
76
+ grpc .WithTimeout ( connectionTimeout ),
62
77
}
63
78
unary , stream := newNSInterceptors (k8sNamespace )
64
79
gopts = append (gopts ,
65
80
grpc .WithUnaryInterceptor (unary ),
66
81
grpc .WithStreamInterceptor (stream ),
67
82
)
68
83
69
- conn , err := grpc .Dial (dialer .DialAddress ("/var/run/containerd/containerd.sock" ), gopts ... )
84
+ conn , err := grpc .Dial (dialer .DialAddress (address ), gopts ... )
70
85
if err != nil {
71
86
retErr = err
72
87
return
0 commit comments