浏览代码

Merge pull request #39940 from carlosedp/runtime-version

Change version parsing to support alternate runtimes
Akihiro Suda 5 年之前
父节点
当前提交
d2d8e96f51
共有 2 个文件被更改,包括 25 次插入10 次删除
  1. 10 8
      daemon/info_unix.go
  2. 15 2
      daemon/info_unix_test.go

+ 10 - 8
daemon/info_unix.go

@@ -35,7 +35,7 @@ func (daemon *Daemon) fillPlatformInfo(v *types.Info, sysInfo *sysinfo.SysInfo)
 
 
 	defaultRuntimeBinary := daemon.configStore.GetRuntime(v.DefaultRuntime).Path
 	defaultRuntimeBinary := daemon.configStore.GetRuntime(v.DefaultRuntime).Path
 	if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
 	if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
-		if _, commit, err := parseRuncVersion(string(rv)); err != nil {
+		if _, _, commit, err := parseRuntimeVersion(string(rv)); err != nil {
 			logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
 			logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
 			v.RuncCommit.ID = "N/A"
 			v.RuncCommit.ID = "N/A"
 		} else {
 		} else {
@@ -131,7 +131,7 @@ func (daemon *Daemon) fillPlatformVersion(v *types.Version) {
 	defaultRuntime := daemon.configStore.GetDefaultRuntimeName()
 	defaultRuntime := daemon.configStore.GetDefaultRuntimeName()
 	defaultRuntimeBinary := daemon.configStore.GetRuntime(defaultRuntime).Path
 	defaultRuntimeBinary := daemon.configStore.GetRuntime(defaultRuntime).Path
 	if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
 	if rv, err := exec.Command(defaultRuntimeBinary, "--version").Output(); err == nil {
-		if ver, commit, err := parseRuncVersion(string(rv)); err != nil {
+		if _, ver, commit, err := parseRuntimeVersion(string(rv)); err != nil {
 			logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
 			logrus.Warnf("failed to parse %s version: %v", defaultRuntimeBinary, err)
 		} else {
 		} else {
 			v.Components = append(v.Components, types.ComponentVersion{
 			v.Components = append(v.Components, types.ComponentVersion{
@@ -223,19 +223,21 @@ func parseInitVersion(v string) (version string, commit string, err error) {
 	return version, commit, err
 	return version, commit, err
 }
 }
 
 
-// parseRuncVersion parses the output of `runc --version` and extracts the
-// "version" and "git commit" from the output.
+// parseRuntimeVersion parses the output of `[runtime] --version` and extracts the
+// "name", "version" and "git commit" from the output.
 //
 //
 // Output example from `runc --version`:
 // Output example from `runc --version`:
 //
 //
 //   runc version 1.0.0-rc5+dev
 //   runc version 1.0.0-rc5+dev
 //   commit: 69663f0bd4b60df09991c08812a60108003fa340
 //   commit: 69663f0bd4b60df09991c08812a60108003fa340
 //   spec: 1.0.0
 //   spec: 1.0.0
-func parseRuncVersion(v string) (version string, commit string, err error) {
+func parseRuntimeVersion(v string) (runtime string, version string, commit string, err error) {
 	lines := strings.Split(strings.TrimSpace(v), "\n")
 	lines := strings.Split(strings.TrimSpace(v), "\n")
 	for _, line := range lines {
 	for _, line := range lines {
-		if strings.HasPrefix(line, "runc version") {
-			version = strings.TrimSpace(strings.TrimPrefix(line, "runc version"))
+		if strings.Contains(line, "version") {
+			s := strings.Split(line, "version")
+			runtime = strings.TrimSpace(s[0])
+			version = strings.TrimSpace(s[len(s)-1])
 			continue
 			continue
 		}
 		}
 		if strings.HasPrefix(line, "commit:") {
 		if strings.HasPrefix(line, "commit:") {
@@ -246,7 +248,7 @@ func parseRuncVersion(v string) (version string, commit string, err error) {
 	if version == "" && commit == "" {
 	if version == "" && commit == "" {
 		err = errors.Errorf("unknown output format: %s", v)
 		err = errors.Errorf("unknown output format: %s", v)
 	}
 	}
-	return version, commit, err
+	return runtime, version, commit, err
 }
 }
 
 
 func (daemon *Daemon) cgroupNamespacesEnabled(sysInfo *sysinfo.SysInfo) bool {
 func (daemon *Daemon) cgroupNamespacesEnabled(sysInfo *sysinfo.SysInfo) bool {

+ 15 - 2
daemon/info_unix_test.go

@@ -65,9 +65,10 @@ func TestParseInitVersion(t *testing.T) {
 	}
 	}
 }
 }
 
 
-func TestParseRuncVersion(t *testing.T) {
+func TestParseRuntimeVersion(t *testing.T) {
 	tests := []struct {
 	tests := []struct {
 		output  string
 		output  string
+		runtime string
 		version string
 		version string
 		commit  string
 		commit  string
 		invalid bool
 		invalid bool
@@ -78,6 +79,7 @@ runc version 1.0.0-rc5+dev
 commit: 69663f0bd4b60df09991c08812a60108003fa340
 commit: 69663f0bd4b60df09991c08812a60108003fa340
 spec: 1.0.0
 spec: 1.0.0
 `,
 `,
+			runtime: "runc",
 			version: "1.0.0-rc5+dev",
 			version: "1.0.0-rc5+dev",
 			commit:  "69663f0bd4b60df09991c08812a60108003fa340",
 			commit:  "69663f0bd4b60df09991c08812a60108003fa340",
 		},
 		},
@@ -86,6 +88,7 @@ spec: 1.0.0
 runc version 1.0.0-rc5+dev
 runc version 1.0.0-rc5+dev
 spec: 1.0.0
 spec: 1.0.0
 `,
 `,
+			runtime: "runc",
 			version: "1.0.0-rc5+dev",
 			version: "1.0.0-rc5+dev",
 		},
 		},
 		{
 		{
@@ -95,6 +98,15 @@ spec: 1.0.0
 `,
 `,
 			commit: "69663f0bd4b60df09991c08812a60108003fa340",
 			commit: "69663f0bd4b60df09991c08812a60108003fa340",
 		},
 		},
+		{
+			output: `
+crun version 0.7
+spec: 1.0.0
++SYSTEMD +SELINUX +CAP +SECCOMP +EBPF +YAJL
+`,
+			runtime: "crun",
+			version: "0.7",
+		},
 		{
 		{
 			output:  "",
 			output:  "",
 			invalid: true,
 			invalid: true,
@@ -106,12 +118,13 @@ spec: 1.0.0
 	}
 	}
 
 
 	for _, test := range tests {
 	for _, test := range tests {
-		version, commit, err := parseRuncVersion(test.output)
+		runtime, version, commit, err := parseRuntimeVersion(test.output)
 		if test.invalid {
 		if test.invalid {
 			assert.Check(t, is.ErrorContains(err, ""))
 			assert.Check(t, is.ErrorContains(err, ""))
 		} else {
 		} else {
 			assert.Check(t, err)
 			assert.Check(t, err)
 		}
 		}
+		assert.Equal(t, test.runtime, runtime)
 		assert.Equal(t, test.version, version)
 		assert.Equal(t, test.version, version)
 		assert.Equal(t, test.commit, commit)
 		assert.Equal(t, test.commit, commit)
 	}
 	}