Skip to content

Commit

Permalink
Fix for K3s containerd (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
saurabh2253 authored Feb 2, 2022
1 parent b4c91d0 commit ac773a6
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 68 deletions.
115 changes: 60 additions & 55 deletions autodetect.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand Down
10 changes: 5 additions & 5 deletions cmd/vessel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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)
}
Expand Down
10 changes: 4 additions & 6 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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},
}
3 changes: 1 addition & 2 deletions containerd/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit ac773a6

Please sign in to comment.