Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/ws-daemon/pkg/container/config.go
2499 views
1
// Copyright (c) 2020 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 container
6
7
import (
8
"errors"
9
"io/fs"
10
"os"
11
"path/filepath"
12
"strings"
13
14
"golang.org/x/xerrors"
15
)
16
17
// NodeMountsLookupConfig confiugures the node mount/fs access
18
type NodeMountsLookupConfig struct {
19
// ProcLoc is the path to the node's /proc/mounts -
20
ProcLoc string `json:"proc"`
21
}
22
23
// Config configures the container runtime interface
24
type Config struct {
25
// Mounts configures the node mounts lookup
26
Mounts NodeMountsLookupConfig `json:"mounts"`
27
28
// Mapping mapps a path from the node to the container by stripping the key and prepending the value of this map.
29
// For example {"/var/lib/containerd": "/mnt/snapshots"} would translate /var/lib/containerd/io.containerd.snapshotter.v1.overlayfs/snapshots/ to /mnt/snapshots/io.containerd.snapshotter.v1.overlayfs/snapshots/
30
Mapping map[string]string `json:"nodeToContainerMapping"`
31
32
// Runtime marks the container runtime we ought to connect to.
33
// Depending on the value set here we expect the corresponding config struct to have a value.
34
Runtime RuntimeType `json:"runtime"`
35
36
// Containerd contains the containerd CRI config if runtime == RuntimeContainerd
37
Containerd *ContainerdConfig `json:"containerd,omitempty"`
38
39
RegistryFacadeHost string `json:"registryFacadeHost,omitempty"`
40
}
41
42
// RuntimeType lists the supported container runtimes
43
type RuntimeType string
44
45
const (
46
// RuntimeContainerd connects to containerd
47
RuntimeContainerd RuntimeType = "containerd"
48
)
49
50
// ContainerdConfig configures access to containerd
51
type ContainerdConfig struct {
52
// SocketPath is the path in the local file system pointing to the containerd socket.
53
// If this field is not set, full workspace backups are not available.
54
SocketPath string `json:"socket"`
55
}
56
57
// FromConfig produces a container runtime interface instance from the configuration
58
func FromConfig(cfg *Config) (rt Runtime, err error) {
59
if cfg == nil {
60
return
61
}
62
63
switch cfg.Runtime {
64
case RuntimeContainerd:
65
if cfg.Containerd == nil {
66
return nil, xerrors.Errorf("runtime is set to containerd, but not containerd config is provided")
67
}
68
return NewContainerd(cfg.Containerd, cfg.Mapping, cfg.RegistryFacadeHost)
69
default:
70
return nil, xerrors.Errorf("unknown runtime type: %s", cfg.Runtime)
71
}
72
}
73
74
// PathMapping maps a node path to a path in the container
75
type PathMapping map[string]string
76
77
// Translate maps a node-level (root mount namespace) path to a container-level path
78
func (mapping PathMapping) Translate(from string) (result string, err error) {
79
for np, cp := range mapping {
80
if !strings.HasPrefix(from, np) {
81
continue
82
}
83
pth := filepath.Join(cp, strings.TrimPrefix(from, np))
84
85
if _, err := os.Stat(pth); errors.Is(err, fs.ErrNotExist) {
86
return "", xerrors.Errorf("path does not exist in container at %s", pth)
87
} else if err != nil {
88
return "", err
89
}
90
91
return pth, nil
92
}
93
94
return "", xerrors.Errorf("mount entry %s has no appropriate mapping", from)
95
}
96
97