From f88d7012333fea526fe09133761023c790187598 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Thu, 31 Mar 2016 14:23:54 +1100 Subject: [PATCH] containerd: use docker/listeners to create the GRPC socket This allows for the use of socket activation with the flag `-l fd://` and similar. The semantics are identical to Docker, but it's important to note that the semantics of the --listen flag have changed to require a proto://addr formatted string. Signed-off-by: Aleksa Sarai --- containerd/main.go | 26 ++++++++++++++++++-------- containerd/main_linux.go | 3 +-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/containerd/main.go b/containerd/main.go index becc181..e39e1cb 100644 --- a/containerd/main.go +++ b/containerd/main.go @@ -2,9 +2,9 @@ package main import ( "fmt" - "net" "os" "os/signal" + "strings" "sync" "syscall" "time" @@ -18,6 +18,7 @@ import ( "github.com/docker/containerd/api/grpc/types" "github.com/docker/containerd/osutils" "github.com/docker/containerd/supervisor" + "github.com/docker/docker/pkg/listeners" ) const ( @@ -43,7 +44,7 @@ var daemonFlags = []cli.Flag{ cli.StringFlag{ Name: "listen,l", Value: defaultGRPCEndpoint, - Usage: "Address on which GRPC API will listen", + Usage: "proto://address on which the GRPC API will listen", }, cli.StringFlag{ Name: "runtime,r", @@ -127,7 +128,13 @@ func daemon(context *cli.Context) error { if err := sv.Start(); err != nil { return err } - server, err := startServer(context.String("listen"), sv) + // Split the listen string of the form proto://addr + listenSpec := context.String("listen") + listenParts := strings.SplitN(listenSpec, "://", 2) + if len(listenParts) != 2 { + return fmt.Errorf("bad listen address format %s, expected proto://address", listenSpec) + } + server, err := startServer(listenParts[0], listenParts[1], sv) if err != nil { return err } @@ -146,14 +153,17 @@ func daemon(context *cli.Context) error { return nil } -func startServer(address string, sv *supervisor.Supervisor) (*grpc.Server, error) { - if err := os.RemoveAll(address); err != nil { - return nil, err - } - l, err := net.Listen(defaultListenType, address) +func startServer(protocol, address string, sv *supervisor.Supervisor) (*grpc.Server, error) { + // TODO: We should use TLS. + // TODO: Add an option for the SocketGroup. + sockets, err := listeners.Init(protocol, address, "", nil) if err != nil { return nil, err } + if len(sockets) != 1 { + return nil, fmt.Errorf("incorrect number of listeners") + } + l := sockets[0] s := grpc.NewServer() types.RegisterAPIServer(s, server.NewServer(sv)) go func() { diff --git a/containerd/main_linux.go b/containerd/main_linux.go index 8ee74a2..02922c2 100644 --- a/containerd/main_linux.go +++ b/containerd/main_linux.go @@ -20,8 +20,7 @@ import ( const ( defaultStateDir = "/run/containerd" - defaultListenType = "unix" - defaultGRPCEndpoint = "/run/containerd/containerd.sock" + defaultGRPCEndpoint = "unix:///run/containerd/containerd.sock" ) func appendPlatformFlags() {