All checks were successful
continuous-integration/drone/push Build is passing
This is an attempt to set sensible timeouts on abra connections. This might not be the last word on this but it seems that SSH connections now bail out correctly and other kinds of commands don't explode (e.g. logs). Closes coop-cloud/organising#222. Closes coop-cloud/organising#218.
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
// Package client provides Docker client initiatialisation functions.
|
|
package client
|
|
|
|
import (
|
|
"net/http"
|
|
"os"
|
|
"time"
|
|
|
|
commandconnPkg "coopcloud.tech/abra/pkg/upstream/commandconn"
|
|
"github.com/docker/docker/client"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// New initiates a new Docker client.
|
|
func New(contextName string) (*client.Client, error) {
|
|
var clientOpts []client.Opt
|
|
|
|
if contextName != "default" {
|
|
context, err := GetContext(contextName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
ctxEndpoint, err := GetContextEndpoint(context)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
helper := commandconnPkg.NewConnectionHelper(ctxEndpoint)
|
|
httpClient := &http.Client{
|
|
// No tls, no proxy
|
|
Transport: &http.Transport{
|
|
DialContext: helper.Dialer,
|
|
IdleConnTimeout: 30 * time.Second,
|
|
},
|
|
}
|
|
|
|
clientOpts = append(clientOpts,
|
|
client.WithHTTPClient(httpClient),
|
|
client.WithHost(helper.Host),
|
|
client.WithDialContext(helper.Dialer),
|
|
)
|
|
}
|
|
|
|
version := os.Getenv("DOCKER_API_VERSION")
|
|
if version != "" {
|
|
clientOpts = append(clientOpts, client.WithVersion(version))
|
|
} else {
|
|
clientOpts = append(clientOpts, client.WithAPIVersionNegotiation())
|
|
}
|
|
|
|
cl, err := client.NewClientWithOpts(clientOpts...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
logrus.Debugf("created client for '%s'", contextName)
|
|
|
|
return cl, nil
|
|
}
|