Browse code

Merge pull request #39940 from carlosedp/runtime-version

Change version parsing to support alternate runtimes

Akihiro Suda authored on 2019/11/26 09:55:53
Showing 2 changed files
... ...
@@ -35,7 +35,7 @@ func (daemon *Daemon) fillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo)
35 35
 
36 36
 	defaultRuntimeBinary := daemon.configStore.GetRuntime(v.DefaultRuntime).Path
37 37
 	if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
38
-		if _, commit, err := parseRuncVersion(string(rv)); err != nil {
38
+		if _, _, commit, err := parseRuntimeVersion(string(rv)); err != nil {
39 39
 			logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
40 40
 			v.RuncCommit.ID = "N/A"
41 41
 		} else {
... ...
@@ -131,7 +131,7 @@ func (daemon *Daemon) fillPlatformVersion(v *types.Version) {
131 131
 	defaultRuntime := daemon.configStore.GetDefaultRuntimeName()
132 132
 	defaultRuntimeBinary := daemon.configStore.GetRuntime(defaultRuntime).Path
133 133
 	if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
134
-		if ver, commit, err := parseRuncVersion(string(rv)); err != nil {
134
+		if _, ver, commit, err := parseRuntimeVersion(string(rv)); err != nil {
135 135
 			logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
136 136
 		} else {
137 137
 			v.Components = append(v.Components, types.ComponentVersion{
... ...
@@ -223,19 +223,21 @@ func parseInitVersion(v string) (version string, commit string, err error) {
223 223
 	return version, commit, err
224 224
 }
225 225
 
226
-// parseRuncVersion parses the output of `runc --version` and extracts the
227
-// "version" and "git commit" from the output.
226
+// parseRuntimeVersion parses the output of `[runtime] --version` and extracts the
227
+// "name", "version" and "git commit" from the output.
228 228
 //
229 229
 // Output example from `runc --version`:
230 230
 //
231 231
 //   runc version 1.0.0-rc5+dev
232 232
 //   commit: 69663f0bd4b60df09991c08812a60108003fa340
233 233
 //   spec: 1.0.0
234
-func parseRuncVersion(v string) (version string, commit string, err error) {
234
+func parseRuntimeVersion(v string) (runtime string, version string, commit string, err error) {
235 235
 	lines := strings.Split(strings.TrimSpace(v), "\n")
236 236
 	for _, line := range lines {
237
-		if strings.HasPrefix(line, "runc version") {
238
-			version = strings.TrimSpace(strings.TrimPrefix(line, "runc version"))
237
+		if strings.Contains(line, "version") {
238
+			s := strings.Split(line, "version")
239
+			runtime = strings.TrimSpace(s[0])
240
+			version = strings.TrimSpace(s[len(s)-1])
239 241
 			continue
240 242
 		}
241 243
 		if strings.HasPrefix(line, "commit:") {
... ...
@@ -246,7 +248,7 @@ func parseRuncVersion(v string) (version string, commit string, err error) {
246 246
 	if version == "" && commit == "" {
247 247
 		err = errors.Errorf("unknown output format: %s", v)
248 248
 	}
249
-	return version, commit, err
249
+	return runtime, version, commit, err
250 250
 }
251 251
 
252 252
 func (daemon *Daemon) cgroupNamespacesEnabled(sysInfo *sysinfo.SysInfo) bool {
... ...
@@ -65,9 +65,10 @@ func TestParseInitVersion(t *testing.T) {
65 65
 	}
66 66
 }
67 67
 
68
-func TestParseRuncVersion(t *testing.T) {
68
+func TestParseRuntimeVersion(t *testing.T) {
69 69
 	tests := []struct {
70 70
 		output  string
71
+		runtime string
71 72
 		version string
72 73
 		commit  string
73 74
 		invalid bool
... ...
@@ -78,6 +79,7 @@ runc version 1.0.0-rc5+dev
78 78
 commit: 69663f0bd4b60df09991c08812a60108003fa340
79 79
 spec: 1.0.0
80 80
 `,
81
+			runtime: "runc",
81 82
 			version: "1.0.0-rc5+dev",
82 83
 			commit:  "69663f0bd4b60df09991c08812a60108003fa340",
83 84
 		},
... ...
@@ -86,6 +88,7 @@ spec: 1.0.0
86 86
 runc version 1.0.0-rc5+dev
87 87
 spec: 1.0.0
88 88
 `,
89
+			runtime: "runc",
89 90
 			version: "1.0.0-rc5+dev",
90 91
 		},
91 92
 		{
... ...
@@ -96,6 +99,15 @@ spec: 1.0.0
96 96
 			commit: "69663f0bd4b60df09991c08812a60108003fa340",
97 97
 		},
98 98
 		{
99
+			output: `
100
+crun version 0.7
101
+spec: 1.0.0
102
++SYSTEMD +SELINUX +CAP +SECCOMP +EBPF +YAJL
103
+`,
104
+			runtime: "crun",
105
+			version: "0.7",
106
+		},
107
+		{
99 108
 			output:  "",
100 109
 			invalid: true,
101 110
 		},
... ...
@@ -106,12 +118,13 @@ spec: 1.0.0
106 106
 	}
107 107
 
108 108
 	for _, test := range tests {
109
-		version, commit, err := parseRuncVersion(test.output)
109
+		runtime, version, commit, err := parseRuntimeVersion(test.output)
110 110
 		if test.invalid {
111 111
 			assert.Check(t, is.ErrorContains(err, ""))
112 112
 		} else {
113 113
 			assert.Check(t, err)
114 114
 		}
115
+		assert.Equal(t, test.runtime, runtime)
115 116
 		assert.Equal(t, test.version, version)
116 117
 		assert.Equal(t, test.commit, commit)
117 118
 	}