-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdocker.go
130 lines (109 loc) · 2.63 KB
/
docker.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package dogstatsd
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"strings"
"time"
)
type dockerClient struct {
host string
}
func (c *dockerClient) listContainers() (containers []dockerContainer, err error) {
err = c.get("/containers/json", &containers)
return
}
func (c *dockerClient) get(path string, ret interface{}) (err error) {
var req *http.Request
var res *http.Response
if len(c.host) == 0 {
return
}
dialContext := func(ctx context.Context, _, _ string) (net.Conn, error) {
network, address := dockerNetworkAddress(c.host)
return (&net.Dialer{Timeout: 4 * time.Second}).DialContext(ctx, network, address)
}
transport := &http.Transport{
DialContext: dialContext,
DisableKeepAlives: true,
DisableCompression: true,
ResponseHeaderTimeout: 5 * time.Second,
ExpectContinueTimeout: 5 * time.Second,
MaxResponseHeaderBytes: 1024 * 1024,
}
defer transport.CloseIdleConnections()
if req, err = http.NewRequest(http.MethodGet, "http://docker"+path, nil); err != nil {
return
}
if res, err = transport.RoundTrip(req); err != nil {
return
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
err = fmt.Errorf("%s: %s", req.URL, res.Status)
return
}
err = json.NewDecoder(res.Body).Decode(ret)
return
}
type dockerContainer struct {
Image dockerImage
NetworkSettings dockerNetworkSettings
}
type dockerNetworkSettings struct {
Networks map[string]dockerNetwork
}
type dockerNetwork struct {
IPAMConfig dockerIPAMConfig
IPAddress string
}
type dockerIPAMConfig struct {
IPv4Address string
IPv6Address string
}
type dockerImage string
func (image dockerImage) repo() string {
repo, _, _ := image.parts()
return repo
}
func (image dockerImage) name() string {
_, name, _ := image.parts()
return name
}
func (image dockerImage) version() string {
_, _, version := image.parts()
return version
}
func (image dockerImage) parts() (repo, name, version string) {
s := string(image)
i := strings.LastIndexByte(s, ':')
if i < 0 {
name = s
} else {
name, version = s[:i], s[i+1:]
}
j := strings.LastIndexByte(name, '/')
if j >= 0 {
repo, name = name[:j], name[j+1:]
}
return
}
func dockerNetworkAddress(host string) (network, address string) {
if len(host) != 0 {
if i := strings.Index(host, "://"); i >= 0 {
network, address = host[:i], host[i+3:]
} else if address = host; strings.HasPrefix(address, "/") {
network = "unix"
} else {
network = "tcp"
}
if network == "tcp" {
if _, port, _ := net.SplitHostPort(address); len(port) == 0 {
address = net.JoinHostPort(address, "2376") // default docker port
}
}
}
return
}