-
Notifications
You must be signed in to change notification settings - Fork 17
/
docker.go
171 lines (136 loc) · 4.26 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
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
package main
import (
"fmt"
"strings"
"time"
log "github.com/cihub/seelog"
"github.com/fsouza/go-dockerclient"
)
type dockerContainerInfo struct {
containerInfo
RefreshTime time.Time
}
type dockerContainerService struct {
containerIPMap map[string]dockerContainerInfo
docker *docker.Client
}
func newDockerContainerService(endpoint string) (*dockerContainerService, error) {
client, err := docker.NewClient(endpoint)
if err != nil {
return nil, err
}
return &dockerContainerService{
containerIPMap: make(map[string]dockerContainerInfo),
docker: client,
}, nil
}
func (d *dockerContainerService) TypeName() string {
return "docker"
}
func (d *dockerContainerService) ContainerForIP(containerIP string) (containerInfo, error) {
info, found := d.containerIPMap[containerIP]
now := time.Now()
if !found {
d.syncContainers(now)
info, found = d.containerIPMap[containerIP]
} else if now.After(info.RefreshTime) {
info, found = d.syncContainer(containerIP, info, now)
}
if !found {
return containerInfo{}, fmt.Errorf("No container found for IP %s", containerIP)
}
return info.containerInfo, nil
}
func (d *dockerContainerService) syncContainer(containerIP string, oldInfo dockerContainerInfo, now time.Time) (dockerContainerInfo, bool) {
log.Debug("Inspecting container: ", oldInfo.ID)
container, err := d.docker.InspectContainer(oldInfo.ID)
if err != nil || !container.State.Running {
if _, ok := err.(*docker.NoSuchContainer); ok {
log.Debug("Container not found, refreshing container info: ", oldInfo.ID)
} else {
log.Warn("Error inspecting container, refreshing container info: ", oldInfo.ID, ": ", err)
}
d.syncContainers(now)
info, found := d.containerIPMap[containerIP]
return info, found
}
oldInfo.RefreshTime = refreshTime(now)
d.containerIPMap[containerIP] = oldInfo
return oldInfo, true
}
func (d *dockerContainerService) syncContainers(now time.Time) {
log.Info("Synchronizing state with running docker containers")
apiContainers, err := d.docker.ListContainers(docker.ListContainersOptions{
All: false, // only running containers
Size: false, // do not need size information
Limit: 0, // all running containers
Since: "", // not applicable
Before: "", // not applicable
})
if err != nil {
log.Error("Error listing running containers: ", err)
return
}
refreshAt := refreshTime(now)
containerIPMap := make(map[string]dockerContainerInfo)
for _, apiContainer := range apiContainers {
container, err := d.docker.InspectContainer(apiContainer.ID)
if err != nil {
if _, ok := err.(*docker.NoSuchContainer); ok {
log.Debug("Container not found: ", apiContainer.ID)
} else {
log.Warn("Error inspecting container: ", apiContainer.ID, ": ", err)
}
continue
}
var containerIPs []string
if container.NetworkSettings.IPAddress != "" {
containerIPs = append(containerIPs, container.NetworkSettings.IPAddress)
}
for _, network := range container.NetworkSettings.Networks {
containerIPs = append(containerIPs, network.IPAddress)
}
if len(containerIPs) == 0 {
log.Error("No IP addresses discovered for container: ", apiContainer.ID)
continue
}
roleArn, iamPolicy, err := getRoleArnFromEnv(container.Config.Env)
if err != nil {
log.Error("Error getting role from container: ", apiContainer.ID, ": ", err)
continue
}
for _, ipAddress := range containerIPs {
log.Infof("Container: id=%s ip=%s image=%s role=%s", container.ID[:6], ipAddress, container.Config.Image, roleArn)
containerIPMap[ipAddress] = dockerContainerInfo{
containerInfo: containerInfo{
ID: container.ID,
Name: container.Name,
IamRole: roleArn,
IamPolicy: iamPolicy,
},
RefreshTime: refreshAt,
}
}
}
d.containerIPMap = containerIPMap
}
func refreshTime(now time.Time) time.Time {
return now.Add(1 * time.Second)
}
func getRoleArnFromEnv(env []string) (role roleArn, policy string, err error) {
for _, e := range env {
v := strings.SplitN(e, "=", 2)
if v[0] == "IAM_ROLE" && len(v) > 1 {
roleArn := strings.TrimSpace(v[1])
if len(roleArn) > 0 {
role, err = newRoleArn(roleArn)
if err != nil {
return
}
}
} else if v[0] == "IAM_POLICY" && len(v) > 1 {
policy = strings.TrimSpace(v[1])
}
}
return
}