pkg: listeners: clean up to act like a library

Now that listeners is no longer an internal of the client, make it less
Docker-specific (despite there still being some open questions as how to
deal with some of the warnings that listeners has to emit). We should
move as much of the Docker-specific stuff (especially the port
allocation) to docker/ where it belongs (or maybe pass a check function).

Signed-off-by: Aleksa Sarai <asarai@suse.de>
This commit is contained in:
Aleksa Sarai 2016-04-02 00:51:29 +11:00
parent d61bb3048b
commit 501a898e0e
3 changed files with 12 additions and 8 deletions

View file

@ -10,6 +10,8 @@ import (
func initTCPSocket(addr string, tlsConfig *tls.Config) (l net.Listener, err error) { func initTCPSocket(addr string, tlsConfig *tls.Config) (l net.Listener, err error) {
if tlsConfig == nil || tlsConfig.ClientAuth != tls.RequireAndVerifyClientCert { if tlsConfig == nil || tlsConfig.ClientAuth != tls.RequireAndVerifyClientCert {
// TODO: Move this outside pkg/listeners since it's Docker-specific.
// ... and slightly scary.
logrus.Warn("/!\\ DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\") logrus.Warn("/!\\ DON'T BIND ON ANY IP ADDRESS WITHOUT setting -tlsverify IF YOU DON'T KNOW WHAT YOU'RE DOING /!\\")
} }
if l, err = sockets.NewTCPSocket(addr, tlsConfig); err != nil { if l, err = sockets.NewTCPSocket(addr, tlsConfig); err != nil {

View file

@ -35,7 +35,7 @@ func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) (ls []net.List
} }
ls = append(ls, l) ls = append(ls, l)
default: default:
return nil, fmt.Errorf("Invalid protocol format: %q", proto) return nil, fmt.Errorf("invalid protocol format: %q", proto)
} }
return return
@ -59,7 +59,7 @@ func listenFD(addr string, tlsConfig *tls.Config) ([]net.Listener, error) {
} }
if len(listeners) == 0 { if len(listeners) == 0 {
return nil, fmt.Errorf("No sockets found. Make sure the docker daemon was started by systemd.") return nil, fmt.Errorf("no sockets found via socket activation: make sure the service was started by systemd")
} }
// default to all fds just like unix:// and tcp:// // default to all fds just like unix:// and tcp://
@ -69,21 +69,22 @@ func listenFD(addr string, tlsConfig *tls.Config) ([]net.Listener, error) {
fdNum, err := strconv.Atoi(addr) fdNum, err := strconv.Atoi(addr)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse systemd address, should be number: %v", err) return nil, fmt.Errorf("failed to parse systemd fd address: should be a number: %v", addr)
} }
fdOffset := fdNum - 3 fdOffset := fdNum - 3
if len(listeners) < int(fdOffset)+1 { if len(listeners) < int(fdOffset)+1 {
return nil, fmt.Errorf("Too few socket activated files passed in") return nil, fmt.Errorf("too few socket activated files passed in by systemd")
} }
if listeners[fdOffset] == nil { if listeners[fdOffset] == nil {
return nil, fmt.Errorf("failed to listen on systemd activated file at fd %d", fdOffset+3) return nil, fmt.Errorf("failed to listen on systemd activated file: fd %d", fdOffset+3)
} }
for i, ls := range listeners { for i, ls := range listeners {
if i == fdOffset || ls == nil { if i == fdOffset || ls == nil {
continue continue
} }
if err := ls.Close(); err != nil { if err := ls.Close(); err != nil {
logrus.Errorf("Failed to close systemd activated file at fd %d: %v", fdOffset+3, err) // TODO: We shouldn't log inside a library. Remove this or error out.
logrus.Errorf("failed to close systemd activated file: fd %d: %v", fdOffset+3, err)
} }
} }
return []net.Listener{listeners[fdOffset]}, nil return []net.Listener{listeners[fdOffset]}, nil
@ -91,6 +92,8 @@ func listenFD(addr string, tlsConfig *tls.Config) ([]net.Listener, error) {
// allocateDaemonPort ensures that there are no containers // allocateDaemonPort ensures that there are no containers
// that try to use any port allocated for the docker server. // that try to use any port allocated for the docker server.
// TODO: Move this outside pkg/listeners since it's Docker-specific, and requires
// libnetwork which increases the dependency tree quite drastically.
func allocateDaemonPort(addr string) error { func allocateDaemonPort(addr string) error {
host, port, err := net.SplitHostPort(addr) host, port, err := net.SplitHostPort(addr)
if err != nil { if err != nil {

View file

@ -2,7 +2,6 @@ package listeners
import ( import (
"crypto/tls" "crypto/tls"
"errors"
"fmt" "fmt"
"net" "net"
"strings" "strings"
@ -45,7 +44,7 @@ func Init(proto, addr, socketGroup string, tlsConfig *tls.Config) (ls []net.List
ls = append(ls, l) ls = append(ls, l)
default: default:
return nil, errors.New("Invalid protocol format. Windows only supports tcp and npipe.") return nil, fmt.Errorf("invalid protocol format: windows only supports tcp and npipe")
} }
return return