Browse code

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 <github@gone.nl>

Sebastiaan van Stijn authored on 2017/11/03 09:21:18
Showing 4 changed files
... ...
@@ -3,6 +3,7 @@
3 3
 package daemon
4 4
 
5 5
 import (
6
+	"context"
6 7
 	"os/exec"
7 8
 	"strings"
8 9
 
... ...
@@ -48,20 +49,10 @@ func (daemon *Daemon) FillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo)
48 48
 	}
49 49
 
50 50
 	v.ContainerdCommit.Expected = dockerversion.ContainerdCommitID
51
-	if rv, err := exec.Command("docker-containerd", "--version").Output(); err == nil {
52
-		parts := strings.Split(strings.TrimSpace(string(rv)), " ")
53
-		if len(parts) == 3 {
54
-			v.ContainerdCommit.ID = parts[2]
55
-		}
56
-		switch {
57
-		case v.ContainerdCommit.ID == "":
58
-			logrus.Warnf("failed to retrieve docker-containerd version: unknown format", string(rv))
59
-			v.ContainerdCommit.ID = "N/A"
60
-		case strings.HasSuffix(v.ContainerdCommit.ID, "-g"+v.ContainerdCommit.ID[len(v.ContainerdCommit.ID)-7:]):
61
-			v.ContainerdCommit.ID = v.ContainerdCommit.Expected
62
-		}
51
+	if rv, err := daemon.containerd.Version(context.Background()); err == nil {
52
+		v.ContainerdCommit.ID = rv.Revision
63 53
 	} else {
64
-		logrus.Warnf("failed to retrieve docker-containerd version: %v", err)
54
+		logrus.Warnf("failed to retrieve containerd version: %v", err)
65 55
 		v.ContainerdCommit.ID = "N/A"
66 56
 	}
67 57
 
... ...
@@ -60,6 +60,10 @@ type client struct {
60 60
 	containers map[string]*container
61 61
 }
62 62
 
63
+func (c *client) Version(ctx context.Context) (containerd.Version, error) {
64
+	return c.remote.Version(ctx)
65
+}
66
+
63 67
 func (c *client) Restore(ctx context.Context, id string, attachStdio StdioCallback) (alive bool, pid int, err error) {
64 68
 	c.Lock()
65 69
 	defer c.Unlock()
... ...
@@ -17,6 +17,7 @@ import (
17 17
 
18 18
 	"github.com/Microsoft/hcsshim"
19 19
 	opengcs "github.com/Microsoft/opengcs/client"
20
+	"github.com/containerd/containerd"
20 21
 	"github.com/docker/docker/pkg/sysinfo"
21 22
 	"github.com/docker/docker/pkg/system"
22 23
 	specs "github.com/opencontainers/runtime-spec/specs-go"
... ...
@@ -70,6 +71,10 @@ const (
70 70
 // of docker.
71 71
 const defaultOwner = "docker"
72 72
 
73
+func (c *client) Version(ctx context.Context) (containerd.Version, error) {
74
+	return containerd.Version{}, errors.New("not implemented on Windows")
75
+}
76
+
73 77
 // Create is the entrypoint to create a container from a spec.
74 78
 // Table below shows the fields required for HCS JSON calling parameters,
75 79
 // where if not populated, is omitted.
... ...
@@ -82,6 +82,8 @@ type Backend interface {
82 82
 
83 83
 // Client provides access to containerd features.
84 84
 type Client interface {
85
+	Version(ctx context.Context) (containerd.Version, error)
86
+
85 87
 	Restore(ctx context.Context, containerID string, attachStdio StdioCallback) (alive bool, pid int, err error)
86 88
 
87 89
 	Create(ctx context.Context, containerID string, spec *specs.Spec, runtimeOptions interface{}) error