From ac773a6aa4cfa4bdc398c7d2424d1765516945c1 Mon Sep 17 00:00:00 2001 From: Saurabh Kumar Date: Wed, 2 Feb 2022 23:26:06 +0530 Subject: [PATCH] Fix for K3s containerd (#14) --- autodetect.go | 115 ++++++++++++++++++++------------------- cmd/vessel/main.go | 10 ++-- constants/constants.go | 10 ++-- containerd/containerd.go | 3 +- 4 files changed, 70 insertions(+), 68 deletions(-) diff --git a/autodetect.go b/autodetect.go index ea9c293..90b574c 100644 --- a/autodetect.go +++ b/autodetect.go @@ -76,80 +76,85 @@ func parseEndpoint(endpoint string) (string, string, error) { } // getContainerRuntime returns the underlying container runtime and it's socket path -func getContainerRuntime(endPoints map[string]string) (string, error) { - if endPoints == nil || len(endPoints) == 0 { - return "", fmt.Errorf("endpoint is not set") +func getContainerRuntime(endPointsMap map[string][]string) (string, string, error) { + if endPointsMap == nil || len(endPointsMap) == 0 { + return "", "", fmt.Errorf("endpoint is not set") } var detectedRuntime string + var detectedEndPoint string detectedRuntimes := []string{} - for runtime, endPoint := range endPoints { - logrus.Infof("trying to connect to endpoint '%s' with timeout '%s'", endPoint, constants.Timeout) - addr, dialer, err := GetAddressAndDialer(endPoint) - if err != nil { - logrus.Warn(err) - continue - } - - if runtime == constants.DOCKER { - _, err = net.DialTimeout(constants.UnixProtocol, addr, constants.Timeout) - if err != nil { - errMsg := errors.Wrapf(err, "could not connect to endpoint '%s'", endPoint) - logrus.Warn(errMsg) - continue - } - running, err := isDockerRunning(endPoint) - if err != nil { - logrus.Warn(err) - continue - } - detectedRuntimes = append(detectedRuntimes, runtime) - if !running { - logrus.Warn(errors.New(fmt.Sprintf("no running containers found with endpoint %s", endPoint))) - continue - } - logrus.Infof("connected successfully using endpoint: %s", endPoint) - detectedRuntime = runtime - break - } else { - _, err = grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(constants.Timeout), grpc.WithContextDialer(dialer)) - if err != nil { - errMsg := errors.Wrapf(err, "could not connect to endpoint '%s'", endPoint) - logrus.Warn(errMsg) - continue - } - running, err := isContainerdRunning(endPoint) + for runtime, endPoints := range endPointsMap { + for _, endPoint := range endPoints { + logrus.Infof("trying to connect to endpoint '%s' with timeout '%s'", endPoint, constants.Timeout) + addr, dialer, err := GetAddressAndDialer(endPoint) if err != nil { logrus.Warn(err) continue } - detectedRuntimes = append(detectedRuntimes, runtime) - if !running { - logrus.Warn(errors.New(fmt.Sprintf("no running containers found with endpoint %s", endPoint))) - continue + + if runtime == constants.DOCKER { + _, err = net.DialTimeout(constants.UnixProtocol, addr, constants.Timeout) + if err != nil { + errMsg := errors.Wrapf(err, "could not connect to endpoint '%s'", endPoint) + logrus.Warn(errMsg) + continue + } + running, err := isDockerRunning(endPoint) + if err != nil { + logrus.Warn(err) + continue + } + detectedRuntimes = append(detectedRuntimes, runtime) + if !running { + logrus.Warn(errors.New(fmt.Sprintf("no running containers found with endpoint %s", endPoint))) + continue + } + logrus.Infof("connected successfully using endpoint: %s", endPoint) + detectedRuntime = runtime + detectedEndPoint = endPoint + break + } else { + _, err = grpc.Dial(addr, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(constants.Timeout), grpc.WithContextDialer(dialer)) + if err != nil { + errMsg := errors.Wrapf(err, "could not connect to endpoint '%s'", endPoint) + logrus.Warn(errMsg) + continue + } + running, err := isContainerdRunning(endPoint) + if err != nil { + logrus.Warn(err) + continue + } + detectedRuntimes = append(detectedRuntimes, runtime) + if !running { + logrus.Warn(errors.New(fmt.Sprintf("no running containers found with endpoint %s", endPoint))) + continue + } + logrus.Infof("connected successfully using endpoint: %s", endPoint) + detectedRuntime = runtime + detectedEndPoint = endPoint + break } - logrus.Infof("connected successfully using endpoint: %s", endPoint) - detectedRuntime = runtime - break } } if detectedRuntime == "" && len(detectedRuntimes) > 0 { logrus.Warn("No running runtimes, selecting first available found") detectedRuntime = detectedRuntimes[0] } - return detectedRuntime, nil + return detectedRuntime, detectedEndPoint, nil } // AutoDetectRuntime auto detects the underlying container runtime like docker, containerd -func AutoDetectRuntime() (string, error) { - runtime, err := getContainerRuntime(constants.SupportedRuntimes) +func AutoDetectRuntime() (string, string, error) { + runtime, endpoint, err := getContainerRuntime(constants.SupportedRuntimes) if err != nil { - return "", err + return "", "", err } if runtime == "" { - return "", errors.New("could not detect container runtime") + return "", "", errors.New("could not detect container runtime") } logrus.Infof("container runtime detected: %s\n", runtime) - return runtime, nil + return runtime, endpoint, nil } func isDockerRunning(host string) (bool, error) { @@ -198,15 +203,15 @@ func isContainerdRunning(host string) (bool, error) { // Auto detect and returns the runtime available for the current system func NewRuntime() (Runtime, error) { - runtime, err := AutoDetectRuntime() + runtime, endpoint, err := AutoDetectRuntime() if err != nil { return nil, err } if runtime == constants.DOCKER { return docker.New(), nil - } else if runtime == constants.CONTAINERD || runtime == constants.K3S_CONTAINERD { - return self_containerd.New(), nil + } else if runtime == constants.CONTAINERD { + return self_containerd.New(endpoint), nil } return nil, errors.New("Unknown runtime") diff --git a/cmd/vessel/main.go b/cmd/vessel/main.go index aad121e..15617a1 100644 --- a/cmd/vessel/main.go +++ b/cmd/vessel/main.go @@ -5,23 +5,23 @@ import ( "os" "github.com/deepfence/vessel" - "github.com/deepfence/vessel/constants" "github.com/joho/godotenv" "github.com/sirupsen/logrus" ) var activeRuntime string -var containerRuntime string +var activeEndpoint string func init() { var err error // Auto-detect underlying container runtime - containerRuntime, err = vessel.AutoDetectRuntime() + containerRuntime, endpoint, err := vessel.AutoDetectRuntime() if err != nil { logrus.Error(err) os.Exit(1) } activeRuntime = containerRuntime + activeEndpoint = endpoint // create .env os.Create(".env") } @@ -42,8 +42,8 @@ func setDotEnvVariable(envars map[string]string) error { func main() { if activeRuntime != "" { envVars := map[string]string{ - "CONTAINER_RUNTIME": containerRuntime, - "CRI_ENDPOINT": constants.SupportedRuntimes[containerRuntime], + "CONTAINER_RUNTIME": activeRuntime, + "CRI_ENDPOINT": activeEndpoint, } setDotEnvVariable(envVars) } diff --git a/constants/constants.go b/constants/constants.go index 0aec88a..e94fd3e 100644 --- a/constants/constants.go +++ b/constants/constants.go @@ -7,7 +7,6 @@ const ( Timeout = 10 * time.Second CONTAINERD_K8S_NS = "k8s.io" CONTAINERD = "containerd" - K3S_CONTAINERD = "k3s_containerd" DOCKER = "docker" CONTAINERD_SOCKET_ADDRESS = "/run/containerd/containerd.sock" K3S_CONTAINERD_SOCKET_ADDRESS = "/run/k3s/containerd/containerd.sock" @@ -17,8 +16,7 @@ const ( K3S_CONTAINERD_SOCKET_IRI = "unix://" + K3S_CONTAINERD_SOCKET_ADDRESS ) -var SupportedRuntimes = map[string]string{ - DOCKER: DOCKER_SOCKET_IRI, - CONTAINERD: CONTAINERD_SOCKET_IRI, - K3S_CONTAINERD: K3S_CONTAINERD_SOCKET_IRI, -} +var SupportedRuntimes = map[string][]string{ + DOCKER: {DOCKER_SOCKET_IRI}, + CONTAINERD: {CONTAINERD_SOCKET_IRI,K3S_CONTAINERD_SOCKET_IRI}, +} \ No newline at end of file diff --git a/containerd/containerd.go b/containerd/containerd.go index 5056296..7c5743d 100644 --- a/containerd/containerd.go +++ b/containerd/containerd.go @@ -22,8 +22,7 @@ import ( ) // New instantiates a new Containerd runtime object -func New() *Containerd { - host := "unix:///run/containerd/containerd.sock" +func New(host string) *Containerd { return &Containerd{ socketPath: host, namespaces: getNamespaces(host),