Skip to content

Commit

Permalink
Add attempt to connect to socket before selecting it as runtime
Browse files Browse the repository at this point in the history
Signed-off-by: Evan Harris <[email protected]>
  • Loading branch information
eharris128 committed Oct 28, 2024
1 parent bcb4e34 commit 66f8d6d
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions pkg/crt/runtimes.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package crt

import (
"net"
"time"

log "github.com/sirupsen/logrus"

"os"
"strings"

Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}

0 comments on commit 66f8d6d

Please sign in to comment.