forked from robbertkl/docker-ipv6nat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.go
290 lines (241 loc) · 6.3 KB
/
state.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package dockeripv6nat
import (
"log"
"net"
"strconv"
"github.com/fsouza/go-dockerclient"
)
// State keeps track of the current Docker containers and networks to apply relative updates to the manager
type State struct {
manager *Manager
networks map[string]*managedNetwork
containers map[string]*managedContainer
}
// fc00::/7, Unique Local IPv6 Unicast Addresses, see RFC 4193
var ulaCIDR = net.IPNet{
IP: net.ParseIP("fc00::"),
Mask: net.CIDRMask(7, 128),
}
// NewState constructs a new state
func NewState(debug bool) (*State, error) {
manager, err := NewManager(debug)
if err != nil {
return nil, err
}
return &State{
manager: manager,
networks: make(map[string]*managedNetwork),
containers: make(map[string]*managedContainer),
}, nil
}
// Cleanup resets the state
func (s *State) Cleanup() error {
s.RemoveMissingContainers([]string{})
s.RemoveMissingNetworks([]string{})
if err := s.manager.Cleanup(); err != nil {
return err
}
return nil
}
// RemoveMissingNetworks removes any of the given networks, if they don't exist
func (s *State) RemoveMissingNetworks(networkIDs []string) error {
for id := range s.networks {
if !contains(networkIDs, id) {
if err := s.UpdateNetwork(id, nil); err != nil {
return err
}
}
}
return nil
}
// RemoveMissingContainers removes any of the given containers if they don't exist
func (s *State) RemoveMissingContainers(containerIDs []string) error {
for id := range s.containers {
if !contains(containerIDs, id) {
if err := s.UpdateContainer(id, nil); err != nil {
return err
}
}
}
return nil
}
// UpdateNetwork applies a network, which can add, remove or update it
func (s *State) UpdateNetwork(id string, network *docker.Network) error {
oldNetwork := s.networks[id]
newNetwork := s.parseNetwork(network)
if oldNetwork != nil || newNetwork != nil {
if err := s.manager.ReplaceNetwork(oldNetwork, newNetwork); err != nil {
return err
}
}
if newNetwork == nil {
delete(s.networks, id)
} else {
s.networks[id] = newNetwork
}
return nil
}
// UpdateContainer applies a container, which can add, remove or update it
func (s *State) UpdateContainer(id string, container *docker.Container) error {
oldContainer := s.containers[id]
newContainer := s.parseContainer(container)
if oldContainer != nil || newContainer != nil {
if err := s.manager.ReplaceContainer(oldContainer, newContainer); err != nil {
return err
}
}
if newContainer == nil {
delete(s.containers, id)
} else {
s.containers[id] = newContainer
}
return nil
}
func (s *State) parseNetwork(network *docker.Network) *managedNetwork {
if network == nil {
return nil
}
// Don't check network.EnableIPv6, since this will be false before Docker 1.11.0, even if we have IPv6 subnets.
if network.Driver != "bridge" {
return nil
}
n := managedNetwork{
id: network.ID,
bridge: "br-" + network.ID[:12],
icc: true,
masquerade: true,
internal: network.Internal,
binding: net.ParseIP("::"),
}
for _, config := range network.IPAM.Config {
_, subnet, err := net.ParseCIDR(config.Subnet)
if err != nil {
continue
}
if ulaCIDR.Contains(subnet.IP) {
n.subnet = *subnet
break
}
}
if n.subnet.IP == nil {
return nil
}
for key, value := range network.Options {
switch key {
case "com.docker.network.bridge.name":
n.bridge = value
case "com.docker.network.bridge.enable_icc":
b, err := strconv.ParseBool(value)
if err != nil {
log.Printf("invalid value for com.docker.network.bridge.enable_icc (network %s)", network.ID)
break
}
n.icc = b
case "com.docker.network.bridge.enable_ip_masquerade":
b, err := strconv.ParseBool(value)
if err != nil {
log.Printf("invalid value for com.docker.network.bridge.enable_ip_masquerade (network %s)", network.ID)
break
}
n.masquerade = b
case "com.docker.network.bridge.host_binding_ipv6":
ip := net.ParseIP(value)
if ip == nil || ip.To4() != nil {
log.Printf("invalid value for com.docker.network.bridge.host_binding_ipv6 (network %s)", network.ID)
break
}
n.binding = ip
}
}
return &n
}
func (s *State) findFirstKnownNetwork(networks map[string]docker.ContainerNetwork) (*managedNetwork, net.IP) {
for _, network := range networks {
ip := net.ParseIP(network.GlobalIPv6Address)
if !ulaCIDR.Contains(ip) {
continue
}
n, found := s.networks[network.NetworkID]
if !found || n.internal {
continue
}
return n, ip
}
return nil, nil
}
func (s *State) getKnownNetworks() []*managedNetwork {
networks := make([]*managedNetwork, len(s.networks))
index := 0
for _, network := range s.networks {
networks[index] = network
index++
}
return networks
}
func (s *State) parseContainer(container *docker.Container) *managedContainer {
if container == nil {
return nil
}
network, containerAddress := s.findFirstKnownNetwork(container.NetworkSettings.Networks)
if network == nil {
return nil
}
if network.internal {
return nil
}
ports := make([]managedPort, 0)
for port, bindings := range container.HostConfig.PortBindings {
proto := port.Proto()
containerPort, err := parsePort(port.Port())
if err != nil {
log.Printf("invalid port %s for container %s", port.Port(), container.ID)
continue
}
for _, binding := range bindings {
hostAddress := network.binding
if binding.HostIP != "" && binding.HostIP != "0.0.0.0" {
ip := net.ParseIP(binding.HostIP)
if ip == nil || ip.To4() != nil {
// Skip bindings to IPv4.
continue
}
hostAddress = ip
}
hostPort, err := parsePort(binding.HostPort)
if err != nil {
log.Printf("invalid port %s for container %s", binding.HostPort, container.ID)
continue
}
ports = append(ports, managedPort{
port: containerPort,
proto: proto,
hostAddress: hostAddress,
hostPort: hostPort,
})
}
}
if len(ports) == 0 {
return nil
}
return &managedContainer{
id: container.ID,
address: containerAddress,
bridge: network.bridge,
ports: ports,
}
}
func parsePort(rawPort string) (uint16, error) {
port, err := strconv.ParseUint(rawPort, 10, 16)
if err != nil {
return 0, err
}
return uint16(port), nil
}
func contains(haystack []string, needle string) bool {
for _, item := range haystack {
if item == needle {
return true
}
}
return false
}