소스 검색

Merge pull request #42510 from thaJeztah/proxy_cleanup

libnetwork/portmapper: some minor cleaning up
Akihiro Suda 3 년 전
부모
커밋
f95be5e2f3
4개의 변경된 파일62개의 추가작업 그리고 61개의 파일을 삭제
  1. 2 1
      libnetwork/portmapper/mapper.go
  2. 0 0
      libnetwork/portmapper/mock_proxy_test.go
  3. 0 60
      libnetwork/portmapper/proxy.go
  4. 60 0
      libnetwork/portmapper/proxy_linux.go

+ 2 - 1
libnetwork/portmapper/mapper.go

@@ -17,6 +17,7 @@ type mapping struct {
 	container     net.Addr
 	container     net.Addr
 }
 }
 
 
+// newProxy is used to mock out the proxy server in tests
 var newProxy = newProxyCommand
 var newProxy = newProxyCommand
 
 
 var (
 var (
@@ -214,7 +215,7 @@ func (pm *PortMapper) Unmap(host net.Addr) error {
 	return ErrUnknownBackendAddressType
 	return ErrUnknownBackendAddressType
 }
 }
 
 
-//ReMapAll will re-apply all port mappings
+// ReMapAll re-applies all port mappings
 func (pm *PortMapper) ReMapAll() {
 func (pm *PortMapper) ReMapAll() {
 	pm.lock.Lock()
 	pm.lock.Lock()
 	defer pm.lock.Unlock()
 	defer pm.lock.Unlock()

+ 0 - 0
libnetwork/portmapper/mock_proxy.go → libnetwork/portmapper/mock_proxy_test.go


+ 0 - 60
libnetwork/portmapper/proxy.go

@@ -3,17 +3,11 @@ package portmapper
 import (
 import (
 	"fmt"
 	"fmt"
 	"io"
 	"io"
-	"io/ioutil"
 	"net"
 	"net"
-	"os"
-	"os/exec"
-	"time"
 
 
 	"github.com/ishidawataru/sctp"
 	"github.com/ishidawataru/sctp"
 )
 )
 
 
-var userlandProxyCommandName = "docker-proxy"
-
 type userlandProxy interface {
 type userlandProxy interface {
 	Start() error
 	Start() error
 	Stop() error
 	Stop() error
@@ -29,60 +23,6 @@ const (
 	ipv6 ipVersion = "6"
 	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
 // dummyProxy just listen on some port, it is needed to prevent accidental
 // port allocations on bound port, because without userland proxy we using
 // port allocations on bound port, because without userland proxy we using
 // iptables rules and not net.Listen
 // iptables rules and not net.Listen

+ 60 - 0
libnetwork/portmapper/proxy_linux.go

@@ -1,12 +1,18 @@
 package portmapper
 package portmapper
 
 
 import (
 import (
+	"fmt"
+	"io/ioutil"
 	"net"
 	"net"
+	"os"
 	"os/exec"
 	"os/exec"
 	"strconv"
 	"strconv"
 	"syscall"
 	"syscall"
+	"time"
 )
 )
 
 
+const userlandProxyCommandName = "docker-proxy"
+
 func newProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int, proxyPath string) (userlandProxy, error) {
 func newProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.IP, containerPort int, proxyPath string) (userlandProxy, error) {
 	path := proxyPath
 	path := proxyPath
 	if proxyPath == "" {
 	if proxyPath == "" {
@@ -36,3 +42,57 @@ func newProxyCommand(proto string, hostIP net.IP, hostPort int, containerIP net.
 		},
 		},
 	}, nil
 	}, 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
+}