forked from moby/moby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
77 lines (65 loc) · 2 KB
/
link.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
//go:build linux
package bridge
import (
"context"
"fmt"
"net"
"github.com/containerd/log"
"github.com/docker/docker/libnetwork/iptables"
"github.com/docker/docker/libnetwork/types"
)
type link struct {
parentIP net.IP
childIP net.IP
ports []types.TransportPort
bridge string
}
func (l *link) String() string {
return fmt.Sprintf("%s <-> %s [%v] on %s", l.parentIP, l.childIP, l.ports, l.bridge)
}
func newLink(parentIP, childIP net.IP, ports []types.TransportPort, bridge string) (*link, error) {
if parentIP == nil {
return nil, fmt.Errorf("cannot link to a container with an empty parent IP address")
}
if childIP == nil {
return nil, fmt.Errorf("cannot link to a container with an empty child IP address")
}
return &link{
childIP: childIP,
parentIP: parentIP,
ports: ports,
bridge: bridge,
}, nil
}
func (l *link) Enable() error {
linkFunction := func() error {
return linkContainers(iptables.Append, l.parentIP, l.childIP, l.ports, l.bridge, false)
}
if err := linkFunction(); err != nil {
return err
}
iptables.OnReloaded(func() { _ = linkFunction() })
return nil
}
func (l *link) Disable() {
if err := linkContainers(iptables.Delete, l.parentIP, l.childIP, l.ports, l.bridge, true); err != nil {
// @TODO: Return error once we have the iptables package return typed errors.
log.G(context.TODO()).WithError(err).Errorf("Error removing IPTables rules for link: %s", l.String())
}
}
func linkContainers(action iptables.Action, parentIP, childIP net.IP, ports []types.TransportPort, bridge string, ignoreErrors bool) error {
if parentIP == nil {
return fmt.Errorf("cannot link to a container with an empty parent IP address")
}
if childIP == nil {
return fmt.Errorf("cannot link to a container with an empty child IP address")
}
chain := iptables.ChainInfo{Name: DockerChain}
for _, port := range ports {
err := chain.Link(action, parentIP, childIP, int(port.Port), port.Proto.String(), bridge)
if !ignoreErrors && err != nil {
return err
}
}
return nil
}