-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
91 lines (75 loc) · 1.96 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package containerd
import (
"context"
"net"
"os"
"strings"
"github.com/MR5356/aurora/pkg/util/sshutil"
"github.com/containerd/containerd"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
const (
localSocketFmt = "./tmp/%s-containerd.sock"
remoteSocketCmd = "find /run -name containerd.sock"
)
var remoteSocket = ""
func init() {
os.MkdirAll("./tmp", os.ModePerm)
}
type Client struct {
client *containerd.Client
socket string
tunnel net.Conn
grocConn *grpc.ClientConn
}
func NewClientWithSSH(sshInfo *sshutil.HostInfo) (*Client, error) {
c := &Client{}
sshClient, err := sshutil.NewSSHClient(*sshInfo)
if err != nil {
logrus.Debugf("new ssh client error: %+v", err)
return nil, err
}
session, err := sshClient.GetSession()
if err != nil {
logrus.Debugf("get ssh session error: %+v", err)
return nil, err
}
// get remote containerd.sock path
if output, err := session.Output(remoteSocketCmd); err != nil {
return nil, err
} else {
remoteSocket = strings.Trim(string(output), "\n")
}
logrus.Debugf("remote socket: %s", remoteSocket)
// connect to containerd via ssh
// FIXME: BUG: ssh: rejected: administratively prohibited (open failed)
tunnel, err := sshClient.GetClient().Dial("unix", remoteSocket)
if err != nil {
logrus.Errorf("Failed to connect to containerd: %v", err)
return nil, err
}
c.tunnel = tunnel
conn, err := grpc.Dial("", grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
return tunnel, nil
}), grpc.WithInsecure())
if err != nil {
logrus.Debugf("create grpc client error: %+v", err)
return nil, err
}
c.grocConn = conn
// create containerd client
containerdClient, err := containerd.NewWithConn(conn)
if err != nil {
logrus.Errorf("Failed to create containerd client: %v", err)
return nil, err
}
c.client = containerdClient
return c, nil
}
func (c *Client) Close() {
_ = c.client.Close()
_ = c.tunnel.Close()
_ = c.grocConn.Close()
_ = os.Remove(c.socket)
}