Skip to content

Commit

Permalink
bridge,portmapper: custom docker-proxy path
Browse files Browse the repository at this point in the history
Signed-off-by: Antonio Murdaca <runcom@redhat.com>
  • Loading branch information
runcom committed Sep 25, 2016
1 parent bf3d9cc commit d43e87e
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
3 changes: 2 additions & 1 deletion drivers/bridge/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type configuration struct {
EnableIPForwarding bool
EnableIPTables bool
EnableUserlandProxy bool
UserlandProxyPath string
}

// networkConfiguration for network specific configuration
Expand Down Expand Up @@ -638,7 +639,7 @@ func (d *driver) createNetwork(config *networkConfiguration) error {
id: config.ID,
endpoints: make(map[string]*bridgeEndpoint),
config: config,
portMapper: portmapper.New(),
portMapper: portmapper.New(d.config.UserlandProxyPath),
driver: d,
}

Expand Down
2 changes: 1 addition & 1 deletion drivers/bridge/setup_ip_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func assertChainConfig(d *driver, t *testing.T) {

// Assert function which pushes chains based on bridge config parameters.
func assertBridgeConfig(config *networkConfiguration, br *bridgeInterface, d *driver, t *testing.T) {
nw := bridgeNetwork{portMapper: portmapper.New(),
nw := bridgeNetwork{portMapper: portmapper.New(""),
config: config}
nw.driver = d

Expand Down
13 changes: 8 additions & 5 deletions portmapper/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,22 @@ type PortMapper struct {
currentMappings map[string]*mapping
lock sync.Mutex

proxyPath string

Allocator *portallocator.PortAllocator
}

// New returns a new instance of PortMapper
func New() *PortMapper {
return NewWithPortAllocator(portallocator.Get())
func New(proxyPath string) *PortMapper {
return NewWithPortAllocator(portallocator.Get(), proxyPath)
}

// NewWithPortAllocator returns a new instance of PortMapper which will use the specified PortAllocator
func NewWithPortAllocator(allocator *portallocator.PortAllocator) *PortMapper {
func NewWithPortAllocator(allocator *portallocator.PortAllocator, proxyPath string) *PortMapper {
return &PortMapper{
currentMappings: make(map[string]*mapping),
Allocator: allocator,
proxyPath: proxyPath,
}
}

Expand Down Expand Up @@ -90,7 +93,7 @@ func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart,
}

if useProxy {
m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, container.(*net.TCPAddr).IP, container.(*net.TCPAddr).Port)
m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, container.(*net.TCPAddr).IP, container.(*net.TCPAddr).Port, pm.proxyPath)
if err != nil {
return nil, err
}
Expand All @@ -110,7 +113,7 @@ func (pm *PortMapper) MapRange(container net.Addr, hostIP net.IP, hostPortStart,
}

if useProxy {
m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, container.(*net.UDPAddr).IP, container.(*net.UDPAddr).Port)
m.userlandProxy, err = newProxy(proto, hostIP, allocatedHostPort, container.(*net.UDPAddr).IP, container.(*net.UDPAddr).Port, pm.proxyPath)
if err != nil {
return nil, err
}
Expand Down
12 changes: 6 additions & 6 deletions portmapper/mapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func init() {
}

func TestSetIptablesChain(t *testing.T) {
pm := New()
pm := New("")

c := &iptables.ChainInfo{
Name: "TEST",
Expand All @@ -32,7 +32,7 @@ func TestSetIptablesChain(t *testing.T) {
}

func TestMapTCPPorts(t *testing.T) {
pm := New()
pm := New("")
dstIP1 := net.ParseIP("192.168.0.1")
dstIP2 := net.ParseIP("192.168.0.2")
dstAddr1 := &net.TCPAddr{IP: dstIP1, Port: 80}
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestGetUDPIPAndPort(t *testing.T) {
}

func TestMapUDPPorts(t *testing.T) {
pm := New()
pm := New("")
dstIP1 := net.ParseIP("192.168.0.1")
dstIP2 := net.ParseIP("192.168.0.2")
dstAddr1 := &net.UDPAddr{IP: dstIP1, Port: 80}
Expand Down Expand Up @@ -157,7 +157,7 @@ func TestMapUDPPorts(t *testing.T) {
}

func TestMapAllPortsSingleInterface(t *testing.T) {
pm := New()
pm := New("")
dstIP1 := net.ParseIP("0.0.0.0")
srcAddr1 := &net.TCPAddr{Port: 1080, IP: net.ParseIP("172.16.0.1")}

Expand Down Expand Up @@ -196,7 +196,7 @@ func TestMapAllPortsSingleInterface(t *testing.T) {
}

func TestMapTCPDummyListen(t *testing.T) {
pm := New()
pm := New("")
dstIP := net.ParseIP("0.0.0.0")
dstAddr := &net.TCPAddr{IP: dstIP, Port: 80}

Expand Down Expand Up @@ -233,7 +233,7 @@ func TestMapTCPDummyListen(t *testing.T) {
}

func TestMapUDPDummyListen(t *testing.T) {
pm := New()
pm := New("")
dstIP := net.ParseIP("0.0.0.0")
dstAddr := &net.UDPAddr{IP: dstIP, Port: 80}

Expand Down
2 changes: 1 addition & 1 deletion portmapper/mock_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package portmapper

import "net"

func newMockProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int) (userlandProxy, error) {
func newMockProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int, userlandProxyPath string) (userlandProxy, error) {
return &mockProxyCommand{}, nil
}

Expand Down
17 changes: 10 additions & 7 deletions portmapper/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ type proxyCommand struct {
cmd *exec.Cmd
}

func newProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int) (userlandProxy, error) {
cmd, err := exec.LookPath(userlandProxyCommandName)

if err != nil {
return nil, err
func newProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int, proxyPath string) (userlandProxy, error) {
path := proxyPath
if proxyPath == "" {
cmd, err := exec.LookPath(userlandProxyCommandName)
if err != nil {
return nil, err
}
path = cmd
}

args := []string{
cmd,
path,
"-proto", proto,
"-host-ip", hostIP.String(),
"-host-port", strconv.Itoa(hostPort),
Expand All @@ -43,7 +46,7 @@ func newProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.

return &proxyCommand{
cmd: &exec.Cmd{
Path: cmd,
Path: path,
Args: args,
SysProcAttr: &syscall.SysProcAttr{
Pdeathsig: syscall.SIGTERM, // send a sigterm to the proxy if the daemon process dies
Expand Down

0 comments on commit d43e87e

Please sign in to comment.