Path: blob/main/components/ws-daemon/pkg/container/config.go
2499 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package container56import (7"errors"8"io/fs"9"os"10"path/filepath"11"strings"1213"golang.org/x/xerrors"14)1516// NodeMountsLookupConfig confiugures the node mount/fs access17type NodeMountsLookupConfig struct {18// ProcLoc is the path to the node's /proc/mounts -19ProcLoc string `json:"proc"`20}2122// Config configures the container runtime interface23type Config struct {24// Mounts configures the node mounts lookup25Mounts NodeMountsLookupConfig `json:"mounts"`2627// Mapping mapps a path from the node to the container by stripping the key and prepending the value of this map.28// 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/29Mapping map[string]string `json:"nodeToContainerMapping"`3031// Runtime marks the container runtime we ought to connect to.32// Depending on the value set here we expect the corresponding config struct to have a value.33Runtime RuntimeType `json:"runtime"`3435// Containerd contains the containerd CRI config if runtime == RuntimeContainerd36Containerd *ContainerdConfig `json:"containerd,omitempty"`3738RegistryFacadeHost string `json:"registryFacadeHost,omitempty"`39}4041// RuntimeType lists the supported container runtimes42type RuntimeType string4344const (45// RuntimeContainerd connects to containerd46RuntimeContainerd RuntimeType = "containerd"47)4849// ContainerdConfig configures access to containerd50type ContainerdConfig struct {51// SocketPath is the path in the local file system pointing to the containerd socket.52// If this field is not set, full workspace backups are not available.53SocketPath string `json:"socket"`54}5556// FromConfig produces a container runtime interface instance from the configuration57func FromConfig(cfg *Config) (rt Runtime, err error) {58if cfg == nil {59return60}6162switch cfg.Runtime {63case RuntimeContainerd:64if cfg.Containerd == nil {65return nil, xerrors.Errorf("runtime is set to containerd, but not containerd config is provided")66}67return NewContainerd(cfg.Containerd, cfg.Mapping, cfg.RegistryFacadeHost)68default:69return nil, xerrors.Errorf("unknown runtime type: %s", cfg.Runtime)70}71}7273// PathMapping maps a node path to a path in the container74type PathMapping map[string]string7576// Translate maps a node-level (root mount namespace) path to a container-level path77func (mapping PathMapping) Translate(from string) (result string, err error) {78for np, cp := range mapping {79if !strings.HasPrefix(from, np) {80continue81}82pth := filepath.Join(cp, strings.TrimPrefix(from, np))8384if _, err := os.Stat(pth); errors.Is(err, fs.ErrNotExist) {85return "", xerrors.Errorf("path does not exist in container at %s", pth)86} else if err != nil {87return "", err88}8990return pth, nil91}9293return "", xerrors.Errorf("mount entry %s has no appropriate mapping", from)94}959697