-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathautodetect.go
323 lines (286 loc) · 9.05 KB
/
autodetect.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package vessel
import (
"context"
"fmt"
"net"
"net/url"
"os/exec"
"strings"
"sync"
"github.com/containerd/containerd"
"github.com/containerd/containerd/namespaces"
selfContainerd "github.com/deepfence/vessel/containerd"
"github.com/deepfence/vessel/crio"
"github.com/deepfence/vessel/docker"
selfPodman "github.com/deepfence/vessel/podman"
"github.com/deepfence/vessel/utils"
containerTypes "github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)
func init() {
customFormatter := new(logrus.TextFormatter)
customFormatter.TimestampFormat = "2006-01-02 15:04:05"
logrus.SetFormatter(customFormatter)
customFormatter.FullTimestamp = true
}
// GetAddressAndDialer returns the address parsed from the given endpoint and a context dialer.
func GetAddressAndDialer(endpoint string) (string, func(ctx context.Context, addr string) (net.Conn, error), error) {
protocol, addr, err := parseEndpointWithFallbackProtocol(endpoint, utils.UnixProtocol)
if err != nil {
return "", nil, err
}
if protocol != utils.UnixProtocol {
return "", nil, fmt.Errorf("only support unix socket endpoint")
}
return addr, dial, nil
}
func dial(ctx context.Context, addr string) (net.Conn, error) {
return (&net.Dialer{}).DialContext(ctx, utils.UnixProtocol, addr)
}
func parseEndpointWithFallbackProtocol(endpoint string, fallbackProtocol string) (protocol string, addr string, err error) {
if protocol, addr, err = parseEndpoint(endpoint); err != nil && protocol == "" {
fallbackEndpoint := fallbackProtocol + "://" + endpoint
protocol, addr, err = parseEndpoint(fallbackEndpoint)
if err == nil {
logrus.Warningf("Using %q as endpoint is deprecated, please consider using full url format %q.", endpoint, fallbackEndpoint)
}
}
return
}
func parseEndpoint(endpoint string) (string, string, error) {
u, err := url.Parse(endpoint)
if err != nil {
return "", "", err
}
switch u.Scheme {
case "tcp":
return "tcp", u.Host, nil
case "unix":
return "unix", u.Path, nil
case "":
return "", "", fmt.Errorf("using %q as endpoint is deprecated, please consider using full url format", endpoint)
default:
return u.Scheme, "", fmt.Errorf("protocol %q not supported", u.Scheme)
}
}
func checkDockerRuntime(endPoint string) (bool, error) {
addr, _, err := GetAddressAndDialer(endPoint)
if err != nil {
return false, err
}
_, err = net.DialTimeout(utils.UnixProtocol, addr, utils.Timeout)
if err != nil {
return false, errors.New("could not connect to endpoint '" + endPoint + "'")
}
running, err := isDockerRunning(endPoint)
if err != nil {
return false, err
}
if !running {
logrus.Debugf("no running containers found with endpoint %s", endPoint)
return false, nil
}
return true, nil
}
func checkPodmanRuntime(endPoint string) (bool, error) {
running, err := isPodmanRunning(endPoint)
if err != nil {
return false, err
}
if !running {
logrus.Debugf("no running containers found with endpoint %s", endPoint)
return false, nil
}
return true, nil
}
func checkContainerdRuntime(endPoint string) (bool, error) {
addr, dialer, err := GetAddressAndDialer(endPoint)
if err != nil {
return false, err
}
ctx, cancel := context.WithTimeout(context.Background(), utils.Timeout)
defer cancel()
_, err = grpc.DialContext(ctx, addr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock(), grpc.WithContextDialer(dialer))
if err != nil {
return false, errors.New("could not connect to endpoint '" + endPoint + "'")
}
running, err := isContainerdRunning(endPoint)
if err != nil {
return false, err
}
if !running {
logrus.Debugf("no running containers found with endpoint %s", endPoint)
return false, nil
}
return true, nil
}
func checkCrioRuntime(endPoint string) (bool, error) {
addr, _, err := GetAddressAndDialer(endPoint)
if err != nil {
return false, err
}
_, err = net.DialTimeout(utils.UnixProtocol, addr, utils.Timeout)
if err != nil {
return false, errors.New("could not connect to endpoint '" + endPoint + "'")
}
running, err := isCRIORunning(endPoint)
if err != nil {
return false, err
}
if !running {
logrus.Debugf("no running containers found with endpoint %s", endPoint)
return false, nil
}
return true, nil
}
type containerRuntime struct {
Runtime string
Endpoint string
Connected bool
}
// getContainerRuntime returns the underlying container runtime, and it's socket path
func getContainerRuntime() (string, string, error) {
var wg sync.WaitGroup
var detectedRuntimes []containerRuntime
var connectedRuntimes []containerRuntime
detectedRuntimeChannel := make(chan containerRuntime, 1)
for runtime, endPoints := range utils.SupportedRuntimes {
for _, endPoint := range endPoints {
wg.Add(1)
go func(runtime, endPoint string) {
logrus.Debugf("trying to connect to endpoint '%s' with timeout '%s'", endPoint, utils.Timeout)
var connected bool
var err error
switch runtime {
case utils.DOCKER:
connected, err = checkDockerRuntime(endPoint)
case utils.CONTAINERD:
connected, err = checkContainerdRuntime(endPoint)
case utils.CRIO:
connected, err = checkCrioRuntime(endPoint)
case utils.PODMAN:
connected, err = checkPodmanRuntime(endPoint)
default:
err = fmt.Errorf("unknown container runtime %s", runtime)
}
if err != nil {
logrus.Debugf(err.Error())
wg.Done()
return
}
detectedRuntimeChannel <- containerRuntime{Runtime: runtime, Endpoint: endPoint, Connected: connected}
if connected {
logrus.Infof("connected successfully to endpoint: %s", endPoint)
}
}(runtime, endPoint)
}
}
go func() {
for detectedRuntime := range detectedRuntimeChannel {
detectedRuntimes = append(detectedRuntimes, detectedRuntime)
if detectedRuntime.Connected {
connectedRuntimes = append(connectedRuntimes, detectedRuntime)
}
wg.Done()
}
}()
wg.Wait()
if len(connectedRuntimes) == 0 {
if len(detectedRuntimes) == 0 {
return "", "", nil
} else {
logrus.Infof("No running runtimes, selecting first detected runtime")
return detectedRuntimes[0].Runtime, detectedRuntimes[0].Endpoint, nil
}
}
return connectedRuntimes[0].Runtime, connectedRuntimes[0].Endpoint, nil
}
// AutoDetectRuntime auto detects the underlying container runtime like docker, containerd
func AutoDetectRuntime() (string, string, error) {
runtime, endpoint, err := getContainerRuntime()
if err != nil {
return "", "", err
}
if runtime == "" {
return "", "", errors.New("could not detect container runtime")
}
logrus.Infof("container runtime detected: %s\n", runtime)
return runtime, endpoint, nil
}
func isDockerRunning(host string) (bool, error) {
dockerCli, err := client.NewClientWithOpts(client.WithAPIVersionNegotiation(), client.WithHost(host), client.WithTimeout(utils.Timeout))
if err != nil {
return false, errors.Wrapf(err, " :error creating docker client")
}
defer dockerCli.Close()
containers, err := dockerCli.ContainerList(context.Background(),
containerTypes.ListOptions{
All: true, Size: false,
})
if err != nil {
return false, errors.Wrapf(err, " :error creating docker client")
}
return len(containers) > 0, nil
}
func isPodmanRunning(host string) (bool, error) {
op, err := utils.RunCommand(exec.Command("podman", "--remote", "--url", host, "ps"), "podman ps:")
if err != nil {
logrus.Warn(err.Error())
return false, err
}
return len(strings.Split(strings.TrimSpace(op.String()), "\n")) > 1, nil
}
func isContainerdRunning(host string) (bool, error) {
clientd, err := containerd.New(strings.Replace(host, "unix://", "", 1))
if err != nil {
return false, errors.Wrapf(err, " :error creating containerd client")
}
defer clientd.Close()
namespace_store := clientd.NamespaceService()
list, err := namespace_store.List(context.Background())
if err != nil {
return false, errors.Wrapf(err, " :error creating containerd client")
}
for _, l := range list {
namespace := namespaces.WithNamespace(context.Background(), l)
containers, err := clientd.Containers(namespace)
if err != nil {
return false, errors.Wrapf(err, " :error creating containerd client")
}
if len(containers) > 0 {
return true, nil
}
}
return false, nil
}
func isCRIORunning(host string) (bool, error) {
cmd := exec.Command("crictl", "--runtime-endpoint", host, "ps", "-q")
logrus.Debugf("command: %s", cmd.String())
output, err := cmd.Output()
if err != nil {
logrus.Errorf("%s, error: %s", output, err)
return false, err
}
return true, nil
}
// NewRuntime Auto detect and returns the runtime available for the current system
func NewRuntime() (Runtime, error) {
runtime, endpoint, err := AutoDetectRuntime()
if err != nil {
return nil, err
}
if runtime == utils.DOCKER {
return docker.New(endpoint), nil
} else if runtime == utils.CONTAINERD {
return selfContainerd.New(endpoint), nil
} else if runtime == utils.CRIO {
return crio.New(endpoint), nil
} else if runtime == utils.PODMAN {
return selfPodman.New(endpoint), nil
}
return nil, errors.New("Unknown runtime")
}