Skip to content

Commit 46a2715

Browse files
authored
Add attempt to connect to socket before selecting it as runtime (#78)
Signed-off-by: Evan Harris <echarris@smcm.edu>
1 parent 42a97f1 commit 46a2715

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

pkg/crt/runtimes.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
package crt
22

33
import (
4+
"net"
5+
"time"
6+
7+
log "github.com/sirupsen/logrus"
8+
49
"os"
510
"strings"
611

@@ -81,7 +86,9 @@ func AvailableRuntimes() []string {
8186

8287
if strings.HasPrefix(info.Socket, "/") {
8388
if HasSocket(info.Socket) {
84-
usable[info.Name] = struct{}{}
89+
if CanConnect(info.Socket) {
90+
usable[info.Name] = struct{}{}
91+
}
8592
}
8693
} else {
8794
//adding remote paths (for podman and others; without checking, for now)
@@ -106,11 +113,12 @@ func AvailableRuntimes() []string {
106113

107114
func AutoSelectRuntime() string {
108115
available := AvailableRuntimes()
116+
log.Debugf("Available runtimes: %v", available)
109117
if len(available) > 0 {
110118
return available[0]
111119
}
112120

113-
return DockerRuntime
121+
return DockerRuntime // Question -> This runtime may not necessarily be available?
114122
}
115123

116124
func HasSocket(name string) bool {
@@ -121,3 +129,18 @@ func HasSocket(name string) bool {
121129

122130
return false
123131
}
132+
133+
func CanConnect(socket string) bool {
134+
timeout := 5 * time.Second
135+
conn, err := net.DialTimeout("unix", socket, timeout)
136+
137+
if err != nil {
138+
// If there are permission issues, this line will be tripped
139+
// when trying to connect to the socket.
140+
log.Debugf("Error connecting to socket: %s: %v", socket, err)
141+
return false
142+
}
143+
defer conn.Close()
144+
145+
return true
146+
}

0 commit comments

Comments
 (0)