Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attempt to connect to socket before selecting it as runtime #78

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is a good idea to check if the default/assumed runtime (when no explicit runtime param is specified) is available. A wishlist enhancement :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A future TODO

}

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
}
Loading