Path: blob/main/install/installer/pkg/containerd/location.go
2501 views
// Copyright (c) 2022 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 containerd56import (7"errors"8"io/fs"9"os"10"path"11)1213var (14ErrContainerDLocation = errors.New("cannot detect containerd location")15ErrContainerDSocketLocation = errors.New("cannot detect containerd socket location")16)1718// This is the default location - this will be used for most installations19const (20ContainerdSocketLocationDefault ContainerdSocketLocation = "/run/containerd"21ContainerdLocationDefault ContainerdLocation = "/var/lib/containerd/io.containerd.runtime.v2.task/k8s.io"22)2324type ContainerdSocketLocation string25type ContainerdLocation string2627func (s ContainerdSocketLocation) String() string {28return string(s)29}3031func (s ContainerdLocation) String() string {32return string(s)33}3435var loc locations3637type locations struct {38socket []ContainerdSocketLocation39containerd []ContainerdLocation40}4142func (l *locations) AddContainerd(containerd ContainerdLocation) {43l.containerd = append(l.containerd, containerd)44}4546func (l *locations) AddSocket(socket ContainerdSocketLocation) {47l.socket = append(l.socket, socket)48}4950// GetContainerdList return the containerd locations with the default location appended51func (l *locations) GetContainerdList() []ContainerdLocation {52c := l.containerd53c = append(c, ContainerdLocationDefault)54return c55}5657// GetSocketList return the socket locations with the default location appended58func (l *locations) GetSocketList() []ContainerdSocketLocation {59s := l.socket60s = append(s, ContainerdSocketLocationDefault)61return s62}6364func detectContainerdLocation(mountPath string) (*ContainerdLocation, error) {65for _, location := range loc.GetContainerdList() {66// Ignore errors - the only error we're interested in is if cannot detect a location67fileInfo, err := os.Stat(path.Join(mountPath, location.String()))68if err != nil || !fileInfo.IsDir() {69// Not a directory - go to the next one70continue71}7273// We have a winner74return &location, nil75}7677return nil, ErrContainerDLocation78}7980func detectContainerdSocketLocation(mountPath string) (*ContainerdSocketLocation, error) {81for _, location := range loc.GetSocketList() {82// Ignore errors - the only error we're interested in is if cannot detect a location83fileInfo, err := os.Stat(path.Join(mountPath, location.String()))84if err != nil || fileInfo.Mode().Type() != fs.ModeSocket {85// Not a socket - go to the next one86continue87}8889// We have a winner90return &location, nil91}9293return nil, ErrContainerDSocketLocation94}9596func Detect(mountPath string) (*ContainerdLocation, *ContainerdSocketLocation, error) {97containerd, err := detectContainerdLocation(mountPath)98if err != nil {99return nil, nil, err100}101socket, err := detectContainerdSocketLocation(mountPath)102if err != nil {103return nil, nil, err104}105106return containerd, socket, nil107}108109110