From fec2b144feaaa18998ec2ed34c9bc843c4c29abd Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Fri, 3 Nov 2017 01:21:18 +0100 Subject: [PATCH] Use containerd API to get version The `docker info` code was shelling out to obtain the version of containerd (using the `--version` flag). Parsing the output of this version string is error-prone, and not needed, as the containerd API can return the version. This patch adds a `Version()` method to the containerd Client interface, and uses this to get the containerd version. Signed-off-by: Sebastiaan van Stijn --- daemon/info_unix.go | 17 ++++------------- libcontainerd/client_daemon.go | 4 ++++ libcontainerd/client_local_windows.go | 5 +++++ libcontainerd/types.go | 2 ++ 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/daemon/info_unix.go b/daemon/info_unix.go index fd2bbb45c3..9433434bcb 100644 --- a/daemon/info_unix.go +++ b/daemon/info_unix.go @@ -3,6 +3,7 @@ package daemon import ( + "context" "os/exec" "strings" @@ -48,20 +49,10 @@ func (daemon *Daemon) FillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo) } v.ContainerdCommit.Expected = dockerversion.ContainerdCommitID - if rv, err := exec.Command("docker-containerd", "--version").Output(); err == nil { - parts := strings.Split(strings.TrimSpace(string(rv)), " ") - if len(parts) == 3 { - v.ContainerdCommit.ID = parts[2] - } - switch { - case v.ContainerdCommit.ID == "": - logrus.Warnf("failed to retrieve docker-containerd version: unknown format", string(rv)) - v.ContainerdCommit.ID = "N/A" - case strings.HasSuffix(v.ContainerdCommit.ID, "-g"+v.ContainerdCommit.ID[len(v.ContainerdCommit.ID)-7:]): - v.ContainerdCommit.ID = v.ContainerdCommit.Expected - } + if rv, err := daemon.containerd.Version(context.Background()); err == nil { + v.ContainerdCommit.ID = rv.Revision } else { - logrus.Warnf("failed to retrieve docker-containerd version: %v", err) + logrus.Warnf("failed to retrieve containerd version: %v", err) v.ContainerdCommit.ID = "N/A" } diff --git a/libcontainerd/client_daemon.go b/libcontainerd/client_daemon.go index e6514374ce..b0cdcfcfd9 100644 --- a/libcontainerd/client_daemon.go +++ b/libcontainerd/client_daemon.go @@ -60,6 +60,10 @@ type client struct { containers map[string]*container } +func (c *client) Version(ctx context.Context) (containerd.Version, error) { + return c.remote.Version(ctx) +} + func (c *client) Restore(ctx context.Context, id string, attachStdio StdioCallback) (alive bool, pid int, err error) { c.Lock() defer c.Unlock() diff --git a/libcontainerd/client_local_windows.go b/libcontainerd/client_local_windows.go index c33e346a7a..8ce9dfedab 100644 --- a/libcontainerd/client_local_windows.go +++ b/libcontainerd/client_local_windows.go @@ -17,6 +17,7 @@ import ( "github.com/Microsoft/hcsshim" opengcs "github.com/Microsoft/opengcs/client" + "github.com/containerd/containerd" "github.com/docker/docker/pkg/sysinfo" "github.com/docker/docker/pkg/system" specs "github.com/opencontainers/runtime-spec/specs-go" @@ -70,6 +71,10 @@ const ( // of docker. const defaultOwner = "docker" +func (c *client) Version(ctx context.Context) (containerd.Version, error) { + return containerd.Version{}, errors.New("not implemented on Windows") +} + // Create is the entrypoint to create a container from a spec. // Table below shows the fields required for HCS JSON calling parameters, // where if not populated, is omitted. diff --git a/libcontainerd/types.go b/libcontainerd/types.go index 9e05c16bf8..9eede43a49 100644 --- a/libcontainerd/types.go +++ b/libcontainerd/types.go @@ -82,6 +82,8 @@ type Backend interface { // Client provides access to containerd features. type Client interface { + Version(ctx context.Context) (containerd.Version, error) + Restore(ctx context.Context, containerID string, attachStdio StdioCallback) (alive bool, pid int, err error) Create(ctx context.Context, containerID string, spec *specs.Spec, runtimeOptions interface{}) error