fix: custom timeout only for "server add"

This commit is contained in:
2024-06-25 16:13:57 +02:00
parent 7b7e1bfa97
commit 1fe601cd16
4 changed files with 43 additions and 15 deletions

View File

@ -2,6 +2,7 @@ package commandconn
import (
"context"
"fmt"
"net"
"net/url"
@ -14,14 +15,17 @@ import (
)
// GetConnectionHelper returns Docker-specific connection helper for the given URL.
// GetConnectionHelper returns nil without error when no helper is registered for the scheme.
//
// ssh://<host> URL requires Docker 18.09 or later on the remote host.
func GetConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
return getConnectionHelper(daemonURL)
func GetConnectionHelper(daemonURL string, timeout int) (*connhelper.ConnectionHelper, error) {
if timeout != 0 {
return getConnectionHelper(daemonURL, []string{fmt.Sprintf("-o ConnectTimeout=%v", timeout)})
}
return getConnectionHelper(daemonURL, []string{})
}
func getConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
// getConnectionHelper generates a connection helper from the underlying Docker
// libraries.
func getConnectionHelper(daemonURL string, sshFlags []string) (*connhelper.ConnectionHelper, error) {
url, err := url.Parse(daemonURL)
if err != nil {
return nil, err
@ -35,7 +39,7 @@ func getConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error)
return &connhelper.ConnectionHelper{
Dialer: func(ctx context.Context, network, addr string) (net.Conn, error) {
return New(ctx, "ssh", ctxConnDetails.Args("docker", "system", "dial-stdio")...)
return New(ctx, "ssh", append(sshFlags, ctxConnDetails.Args("docker", "system", "dial-stdio")...)...)
},
Host: "http://docker.example.com",
}, nil
@ -46,8 +50,8 @@ func getConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error)
}
// NewConnectionHelper creates new connection helper for a remote docker daemon.
func NewConnectionHelper(daemonURL string) (*connhelper.ConnectionHelper, error) {
helper, err := GetConnectionHelper(daemonURL)
func NewConnectionHelper(daemonURL string, timeout int) (*connhelper.ConnectionHelper, error) {
helper, err := GetConnectionHelper(daemonURL, timeout)
if err != nil {
return nil, err
}