This repository was archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocker.go
More file actions
177 lines (146 loc) · 3.48 KB
/
docker.go
File metadata and controls
177 lines (146 loc) · 3.48 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package emissary
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"strings"
"time"
)
const defaultDockerHost string = "unix:///var/run/docker.sock"
// DockerClient is meant to be used in DockerResolver.
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 c.Host == "" {
c.Host = defaultDockerHost
}
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
}
func (c *DockerClient) getStatus(key string) (string, error) {
info := dockerInfo{}
err := c.get("/info", &info)
if err != nil {
return "", err
}
for _, s := range info.SystemStatus {
if len(s) != 2 {
continue
}
if s[0] == key {
return s[1], nil
}
}
return "", fmt.Errorf("%s status not found", key)
}
type dockerContainer struct {
Image dockerImage
NetworkSettings dockerNetworkSettings
Labels dockerLabels
Ports []dockerPort
}
type dockerLabels map[string]string
type dockerPort struct {
IP string
PrivatePort int
PublicPort int
Type string
}
type dockerNetworkSettings struct {
Networks map[string]dockerNetwork
}
type dockerNetwork struct {
IPAMConfig dockerIPAMConfig
IPAddress string
}
type dockerIPAMConfig struct {
IPv4Address string
IPv6Address string
}
type dockerInfo struct {
SystemStatus [][]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
}
func serviceName(c dockerContainer) string {
name, ok := c.Labels["com.amazonaws.ecs.container-name"]
if ok {
return name
}
return string(c.Image)
}