1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/libcontainerd/remote/client_io_windows.go

162 lines
3.7 KiB
Go
Raw Normal View History

Windows: Experimental: Allow containerd for runtime Signed-off-by: John Howard <jhoward@microsoft.com> This is the first step in refactoring moby (dockerd) to use containerd on Windows. Similar to the current model in Linux, this adds the option to enable it for runtime. It does not switch the graphdriver to containerd snapshotters. - Refactors libcontainerd to a series of subpackages so that either a "local" containerd (1) or a "remote" (2) containerd can be loaded as opposed to conditional compile as "local" for Windows and "remote" for Linux. - Updates libcontainerd such that Windows has an option to allow the use of a "remote" containerd. Here, it communicates over a named pipe using GRPC. This is currently guarded behind the experimental flag, an environment variable, and the providing of a pipename to connect to containerd. - Infrastructure pieces such as under pkg/system to have helper functions for determining whether containerd is being used. (1) "local" containerd is what the daemon on Windows has used since inception. It's not really containerd at all - it's simply local invocation of HCS APIs directly in-process from the daemon through the Microsoft/hcsshim library. (2) "remote" containerd is what docker on Linux uses for it's runtime. It means that there is a separate containerd service running, and docker communicates over GRPC to it. To try this out, you will need to start with something like the following: Window 1: containerd --log-level debug Window 2: $env:DOCKER_WINDOWS_CONTAINERD=1 dockerd --experimental -D --containerd \\.\pipe\containerd-containerd You will need the following binary from github.com/containerd/containerd in your path: - containerd.exe You will need the following binaries from github.com/Microsoft/hcsshim in your path: - runhcs.exe - containerd-shim-runhcs-v1.exe For LCOW, it will require and initrd.img and kernel in `C:\Program Files\Linux Containers`. This is no different to the current requirements. However, you may need updated binaries, particularly initrd.img built from Microsoft/opengcs as (at the time of writing), Linuxkit binaries are somewhat out of date. Note that containerd and hcsshim for HCS v2 APIs do not yet support all the required functionality needed for docker. This will come in time - this is a baby (although large) step to migrating Docker on Windows to containerd. Note that the HCS v2 APIs are only called on RS5+ builds. RS1..RS4 will still use HCS v1 APIs as the v2 APIs were not fully developed enough on these builds to be usable. This abstraction is done in HCSShim. (Referring specifically to runtime) Note the LCOW graphdriver still uses HCS v1 APIs regardless. Note also that this does not migrate docker to use containerd snapshotters rather than graphdrivers. This needs to be done in conjunction with Linux also doing the same switch.
2019-01-08 17:30:52 -05:00
package remote // import "github.com/docker/docker/libcontainerd/remote"
import (
"io"
"net"
"sync"
winio "github.com/Microsoft/go-winio"
"github.com/containerd/containerd/cio"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
// "golang.org/x/net/context"
)
type delayedConnection struct {
l net.Listener
con net.Conn
wg sync.WaitGroup
once sync.Once
}
func (dc *delayedConnection) Write(p []byte) (int, error) {
dc.wg.Wait()
if dc.con != nil {
return dc.con.Write(p)
}
return 0, errors.New("use of closed network connection")
}
func (dc *delayedConnection) Read(p []byte) (int, error) {
dc.wg.Wait()
if dc.con != nil {
return dc.con.Read(p)
}
return 0, errors.New("use of closed network connection")
}
func (dc *delayedConnection) unblockConnectionWaiters() {
defer dc.once.Do(func() {
dc.wg.Done()
})
}
func (dc *delayedConnection) Close() error {
dc.l.Close()
if dc.con != nil {
return dc.con.Close()
}
dc.unblockConnectionWaiters()
return nil
}
type stdioPipes struct {
stdin io.WriteCloser
stdout io.ReadCloser
stderr io.ReadCloser
}
// newStdioPipes creates actual fifos for stdio.
func (c *client) newStdioPipes(fifos *cio.FIFOSet) (_ *stdioPipes, err error) {
p := &stdioPipes{}
if fifos.Stdin != "" {
c.logger.WithFields(logrus.Fields{"stdin": fifos.Stdin}).Debug("listen")
l, err := winio.ListenPipe(fifos.Stdin, nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to create stdin pipe %s", fifos.Stdin)
}
dc := &delayedConnection{
l: l,
}
dc.wg.Add(1)
defer func() {
if err != nil {
dc.Close()
}
}()
p.stdin = dc
go func() {
c.logger.WithFields(logrus.Fields{"stdin": fifos.Stdin}).Debug("accept")
conn, err := l.Accept()
if err != nil {
dc.Close()
if err != winio.ErrPipeListenerClosed {
c.logger.WithError(err).Errorf("failed to accept stdin connection on %s", fifos.Stdin)
}
return
}
c.logger.WithFields(logrus.Fields{"stdin": fifos.Stdin}).Debug("connected")
dc.con = conn
dc.unblockConnectionWaiters()
}()
}
if fifos.Stdout != "" {
c.logger.WithFields(logrus.Fields{"stdout": fifos.Stdout}).Debug("listen")
l, err := winio.ListenPipe(fifos.Stdout, nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to create stdout pipe %s", fifos.Stdout)
}
dc := &delayedConnection{
l: l,
}
dc.wg.Add(1)
defer func() {
if err != nil {
dc.Close()
}
}()
p.stdout = dc
go func() {
c.logger.WithFields(logrus.Fields{"stdout": fifos.Stdout}).Debug("accept")
conn, err := l.Accept()
if err != nil {
dc.Close()
if err != winio.ErrPipeListenerClosed {
c.logger.WithError(err).Errorf("failed to accept stdout connection on %s", fifos.Stdout)
}
return
}
c.logger.WithFields(logrus.Fields{"stdout": fifos.Stdout}).Debug("connected")
dc.con = conn
dc.unblockConnectionWaiters()
}()
}
if fifos.Stderr != "" {
c.logger.WithFields(logrus.Fields{"stderr": fifos.Stderr}).Debug("listen")
l, err := winio.ListenPipe(fifos.Stderr, nil)
if err != nil {
return nil, errors.Wrapf(err, "failed to create stderr pipe %s", fifos.Stderr)
}
dc := &delayedConnection{
l: l,
}
dc.wg.Add(1)
defer func() {
if err != nil {
dc.Close()
}
}()
p.stderr = dc
go func() {
c.logger.WithFields(logrus.Fields{"stderr": fifos.Stderr}).Debug("accept")
conn, err := l.Accept()
if err != nil {
dc.Close()
if err != winio.ErrPipeListenerClosed {
c.logger.WithError(err).Errorf("failed to accept stderr connection on %s", fifos.Stderr)
}
return
}
c.logger.WithFields(logrus.Fields{"stderr": fifos.Stderr}).Debug("connected")
dc.con = conn
dc.unblockConnectionWaiters()
}()
}
return p, nil
}