forked from rancher/agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
launch_unix.go
executable file
·90 lines (73 loc) · 1.92 KB
/
launch_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// +build linux freebsd solaris openbsd darwin
package docker
import (
"net/http"
"os"
"path/filepath"
"sync"
"time"
dclient "github.com/docker/docker/client"
"github.com/docker/go-connections/tlsconfig"
"github.com/pkg/errors"
"github.com/rancher/agent/utilities/constants"
)
var (
versionClients = map[string]*dclient.Client{}
timeoutClients = map[time.Duration]*dclient.Client{}
clientLock = sync.Mutex{}
)
const DefaultVersion = "1.22"
func launchDefaultClient(version string) (*dclient.Client, error) {
clientLock.Lock()
defer clientLock.Unlock()
if c, ok := versionClients[version]; ok {
return c, nil
}
cli, err := dclient.NewEnvClient()
if err != nil {
return nil, errors.Wrap(err, constants.LaunchDefaultClientError)
}
cli.UpdateClientVersion(version)
versionClients[version] = cli
return cli, nil
}
func NewEnvClientWithTimeout(timeout time.Duration) (*dclient.Client, error) {
clientLock.Lock()
defer clientLock.Unlock()
if c, ok := timeoutClients[timeout]; ok {
return c, nil
}
var client *http.Client
if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); dockerCertPath != "" {
options := tlsconfig.Options{
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
InsecureSkipVerify: os.Getenv("DOCKER_TLS_VERIFY") == "",
}
tlsc, err := tlsconfig.Client(options)
if err != nil {
return nil, err
}
client = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsc,
},
Timeout: timeout,
}
}
host := os.Getenv("DOCKER_HOST")
if host == "" {
host = dclient.DefaultDockerHost
}
version := os.Getenv("DOCKER_API_VERSION")
if version == "" {
version = dclient.DefaultVersion
}
c, err := dclient.NewClient(host, version, client, nil)
if err != nil {
return nil, err
}
timeoutClients[timeout] = c
return c, nil
}