Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/install/installer/pkg/containerd/location.go
2501 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
/// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package containerd
6
7
import (
8
"errors"
9
"io/fs"
10
"os"
11
"path"
12
)
13
14
var (
15
ErrContainerDLocation = errors.New("cannot detect containerd location")
16
ErrContainerDSocketLocation = errors.New("cannot detect containerd socket location")
17
)
18
19
// This is the default location - this will be used for most installations
20
const (
21
ContainerdSocketLocationDefault ContainerdSocketLocation = "/run/containerd"
22
ContainerdLocationDefault ContainerdLocation = "/var/lib/containerd/io.containerd.runtime.v2.task/k8s.io"
23
)
24
25
type ContainerdSocketLocation string
26
type ContainerdLocation string
27
28
func (s ContainerdSocketLocation) String() string {
29
return string(s)
30
}
31
32
func (s ContainerdLocation) String() string {
33
return string(s)
34
}
35
36
var loc locations
37
38
type locations struct {
39
socket []ContainerdSocketLocation
40
containerd []ContainerdLocation
41
}
42
43
func (l *locations) AddContainerd(containerd ContainerdLocation) {
44
l.containerd = append(l.containerd, containerd)
45
}
46
47
func (l *locations) AddSocket(socket ContainerdSocketLocation) {
48
l.socket = append(l.socket, socket)
49
}
50
51
// GetContainerdList return the containerd locations with the default location appended
52
func (l *locations) GetContainerdList() []ContainerdLocation {
53
c := l.containerd
54
c = append(c, ContainerdLocationDefault)
55
return c
56
}
57
58
// GetSocketList return the socket locations with the default location appended
59
func (l *locations) GetSocketList() []ContainerdSocketLocation {
60
s := l.socket
61
s = append(s, ContainerdSocketLocationDefault)
62
return s
63
}
64
65
func detectContainerdLocation(mountPath string) (*ContainerdLocation, error) {
66
for _, location := range loc.GetContainerdList() {
67
// Ignore errors - the only error we're interested in is if cannot detect a location
68
fileInfo, err := os.Stat(path.Join(mountPath, location.String()))
69
if err != nil || !fileInfo.IsDir() {
70
// Not a directory - go to the next one
71
continue
72
}
73
74
// We have a winner
75
return &location, nil
76
}
77
78
return nil, ErrContainerDLocation
79
}
80
81
func detectContainerdSocketLocation(mountPath string) (*ContainerdSocketLocation, error) {
82
for _, location := range loc.GetSocketList() {
83
// Ignore errors - the only error we're interested in is if cannot detect a location
84
fileInfo, err := os.Stat(path.Join(mountPath, location.String()))
85
if err != nil || fileInfo.Mode().Type() != fs.ModeSocket {
86
// Not a socket - go to the next one
87
continue
88
}
89
90
// We have a winner
91
return &location, nil
92
}
93
94
return nil, ErrContainerDSocketLocation
95
}
96
97
func Detect(mountPath string) (*ContainerdLocation, *ContainerdSocketLocation, error) {
98
containerd, err := detectContainerdLocation(mountPath)
99
if err != nil {
100
return nil, nil, err
101
}
102
socket, err := detectContainerdSocketLocation(mountPath)
103
if err != nil {
104
return nil, nil, err
105
}
106
107
return containerd, socket, nil
108
}
109
110