From 66f8d6d15b015d59b08a869e031f6ca17ea1fe40 Mon Sep 17 00:00:00 2001 From: Evan Harris Date: Mon, 28 Oct 2024 14:37:15 -0400 Subject: [PATCH] Add attempt to connect to socket before selecting it as runtime Signed-off-by: Evan Harris --- pkg/crt/runtimes.go | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/pkg/crt/runtimes.go b/pkg/crt/runtimes.go index 8c74021d..caa056cd 100644 --- a/pkg/crt/runtimes.go +++ b/pkg/crt/runtimes.go @@ -1,6 +1,11 @@ package crt import ( + "net" + "time" + + log "github.com/sirupsen/logrus" + "os" "strings" @@ -81,7 +86,9 @@ func AvailableRuntimes() []string { if strings.HasPrefix(info.Socket, "/") { if HasSocket(info.Socket) { - usable[info.Name] = struct{}{} + if CanConnect(info.Socket) { + usable[info.Name] = struct{}{} + } } } else { //adding remote paths (for podman and others; without checking, for now) @@ -106,11 +113,12 @@ func AvailableRuntimes() []string { func AutoSelectRuntime() string { available := AvailableRuntimes() + log.Debugf("Available runtimes: %v", available) if len(available) > 0 { return available[0] } - return DockerRuntime + return DockerRuntime // Question -> This runtime may not necessarily be available? } func HasSocket(name string) bool { @@ -121,3 +129,18 @@ func HasSocket(name string) bool { return false } + +func CanConnect(socket string) bool { + timeout := 5 * time.Second + conn, err := net.DialTimeout("unix", socket, timeout) + + if err != nil { + // If there are permission issues, this line will be tripped + // when trying to connect to the socket. + log.Debugf("Error connecting to socket: %s: %v", socket, err) + return false + } + defer conn.Close() + + return true +}