From 4231dbca23e900201f2c40ede4ebd1267d7a8a39 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 23 Sep 2020 17:33:03 +0200 Subject: [PATCH 1/4] portmapper: don't compile linux-only code on Windows Signed-off-by: Sebastiaan van Stijn --- libnetwork/portmapper/proxy.go | 60 ---------------------------- libnetwork/portmapper/proxy_linux.go | 60 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 60 deletions(-) diff --git a/libnetwork/portmapper/proxy.go b/libnetwork/portmapper/proxy.go index f945851d54..9ab70ffa0e 100644 --- a/libnetwork/portmapper/proxy.go +++ b/libnetwork/portmapper/proxy.go @@ -3,17 +3,11 @@ package portmapper import ( "fmt" "io" - "io/ioutil" "net" - "os" - "os/exec" - "time" "github.com/ishidawataru/sctp" ) -var userlandProxyCommandName = "docker-proxy" - type userlandProxy interface { Start() error Stop() error @@ -29,60 +23,6 @@ const ( ipv6 ipVersion = "6" ) -// proxyCommand wraps an exec.Cmd to run the userland TCP and UDP -// proxies as separate processes. -type proxyCommand struct { - cmd *exec.Cmd -} - -func (p *proxyCommand) Start() error { - r, w, err := os.Pipe() - if err != nil { - return fmt.Errorf("proxy unable to open os.Pipe %s", err) - } - defer r.Close() - p.cmd.ExtraFiles = []*os.File{w} - if err := p.cmd.Start(); err != nil { - return err - } - w.Close() - - errchan := make(chan error, 1) - go func() { - buf := make([]byte, 2) - r.Read(buf) - - if string(buf) != "0\n" { - errStr, err := ioutil.ReadAll(r) - if err != nil { - errchan <- fmt.Errorf("Error reading exit status from userland proxy: %v", err) - return - } - - errchan <- fmt.Errorf("Error starting userland proxy: %s", errStr) - return - } - errchan <- nil - }() - - select { - case err := <-errchan: - return err - case <-time.After(16 * time.Second): - return fmt.Errorf("Timed out proxy starting the userland proxy") - } -} - -func (p *proxyCommand) Stop() error { - if p.cmd.Process != nil { - if err := p.cmd.Process.Signal(os.Interrupt); err != nil { - return err - } - return p.cmd.Wait() - } - return nil -} - // dummyProxy just listen on some port, it is needed to prevent accidental // port allocations on bound port, because without userland proxy we using // iptables rules and not net.Listen diff --git a/libnetwork/portmapper/proxy_linux.go b/libnetwork/portmapper/proxy_linux.go index 947cd0ba4b..ac32a30c5e 100644 --- a/libnetwork/portmapper/proxy_linux.go +++ b/libnetwork/portmapper/proxy_linux.go @@ -1,12 +1,18 @@ package portmapper import ( + "fmt" + "io/ioutil" "net" + "os" "os/exec" "strconv" "syscall" + "time" ) +var userlandProxyCommandName = "docker-proxy" + func newProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int, proxyPath string) (userlandProxy, error) { path := proxyPath if proxyPath == "" { @@ -36,3 +42,57 @@ func newProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net. }, }, nil } + +// proxyCommand wraps an exec.Cmd to run the userland TCP and UDP +// proxies as separate processes. +type proxyCommand struct { + cmd *exec.Cmd +} + +func (p *proxyCommand) Start() error { + r, w, err := os.Pipe() + if err != nil { + return fmt.Errorf("proxy unable to open os.Pipe %s", err) + } + defer r.Close() + p.cmd.ExtraFiles = []*os.File{w} + if err := p.cmd.Start(); err != nil { + return err + } + w.Close() + + errchan := make(chan error, 1) + go func() { + buf := make([]byte, 2) + r.Read(buf) + + if string(buf) != "0\n" { + errStr, err := ioutil.ReadAll(r) + if err != nil { + errchan <- fmt.Errorf("Error reading exit status from userland proxy: %v", err) + return + } + + errchan <- fmt.Errorf("Error starting userland proxy: %s", errStr) + return + } + errchan <- nil + }() + + select { + case err := <-errchan: + return err + case <-time.After(16 * time.Second): + return fmt.Errorf("Timed out proxy starting the userland proxy") + } +} + +func (p *proxyCommand) Stop() error { + if p.cmd.Process != nil { + if err := p.cmd.Process.Signal(os.Interrupt); err != nil { + return err + } + return p.cmd.Wait() + } + return nil +} From f6be7f2945dbdf2aa4be3e0f3d76e8b520adf697 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 23 Sep 2020 17:34:55 +0200 Subject: [PATCH 2/4] portmapper: minor linting fix, and comment purpose of newProxy variable Signed-off-by: Sebastiaan van Stijn --- libnetwork/portmapper/mapper.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/libnetwork/portmapper/mapper.go b/libnetwork/portmapper/mapper.go index c03fd4f614..3315158c97 100644 --- a/libnetwork/portmapper/mapper.go +++ b/libnetwork/portmapper/mapper.go @@ -17,6 +17,7 @@ type mapping struct { container net.Addr } +// newProxy is used to mock out the proxy server in tests var newProxy = newProxyCommand var ( @@ -214,7 +215,7 @@ func (pm *PortMapper) Unmap(host net.Addr) error { return ErrUnknownBackendAddressType } -//ReMapAll will re-apply all port mappings +// ReMapAll re-applies all port mappings func (pm *PortMapper) ReMapAll() { pm.lock.Lock() defer pm.lock.Unlock() From ac8c80d6f1d8547c6fd79daeeb8239b85378be45 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 23 Sep 2020 17:36:32 +0200 Subject: [PATCH 3/4] portmapper: change userlandProxyCommandName to a const it's not overridden anywhere, so may as well be a const Signed-off-by: Sebastiaan van Stijn --- libnetwork/portmapper/proxy_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libnetwork/portmapper/proxy_linux.go b/libnetwork/portmapper/proxy_linux.go index ac32a30c5e..d09d334036 100644 --- a/libnetwork/portmapper/proxy_linux.go +++ b/libnetwork/portmapper/proxy_linux.go @@ -11,7 +11,7 @@ import ( "time" ) -var userlandProxyCommandName = "docker-proxy" +const userlandProxyCommandName = "docker-proxy" func newProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int, proxyPath string) (userlandProxy, error) { path := proxyPath From c21eaf9a0766942ae625e5852313bfb569dd8e31 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Wed, 23 Sep 2020 17:38:35 +0200 Subject: [PATCH 4/4] portmapper: move mockProxyCommand to a _test file No need to vendor this file in other projects, and it's only used during tests. Signed-off-by: Sebastiaan van Stijn --- libnetwork/portmapper/{mock_proxy.go => mock_proxy_test.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename libnetwork/portmapper/{mock_proxy.go => mock_proxy_test.go} (100%) diff --git a/libnetwork/portmapper/mock_proxy.go b/libnetwork/portmapper/mock_proxy_test.go similarity index 100% rename from libnetwork/portmapper/mock_proxy.go rename to libnetwork/portmapper/mock_proxy_test.go