Skip to content

Commit ad0e42f

Browse files
committed
infoutil: make runc version output consistent with Docker
Eliminate extra fields from `ComponentVersion.Details` Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent e016c55 commit ad0e42f

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

pkg/infoutil/infoutil.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,30 +208,40 @@ func runcVersion() dockercompat.ComponentVersion {
208208
logrus.Warnf("unable to determine runc version: %s", err.Error())
209209
return dockercompat.ComponentVersion{Name: "runc"}
210210
}
211+
v, err := parseRuncVersion(stdout)
212+
if err != nil {
213+
logrus.Warn(err)
214+
return dockercompat.ComponentVersion{Name: "runc"}
215+
}
216+
return *v
217+
}
211218

212-
var versionList = strings.Split(strings.TrimSpace(string(stdout)), "\n")
219+
func parseRuncVersion(runcVersionStdout []byte) (*dockercompat.ComponentVersion, error) {
220+
var versionList = strings.Split(strings.TrimSpace(string(runcVersionStdout)), "\n")
213221
firstLine := strings.Fields(versionList[0])
214-
if len(firstLine) != 3 {
215-
logrus.Errorf("unable to determine runc version, got: %s", firstLine)
216-
return dockercompat.ComponentVersion{Name: "runc"}
222+
if len(firstLine) != 3 || firstLine[0] != "runc" {
223+
return nil, fmt.Errorf("unable to determine runc version, got: %s", string(runcVersionStdout))
217224
}
218225
version := firstLine[2]
219226

220227
details := map[string]string{}
221228
for _, detailsLine := range versionList[1:] {
222-
detail := strings.Split(detailsLine, ": ")
229+
detail := strings.SplitN(detailsLine, ":", 2)
223230
if len(detail) != 2 {
224231
logrus.Warnf("unable to determine one of runc details, got: %s, %d", detail, len(detail))
225232
continue
226233
}
227-
details[detail[0]] = detail[1]
234+
switch strings.TrimSpace(detail[0]) {
235+
case "commit":
236+
details["GitCommit"] = strings.TrimSpace(detail[1])
237+
}
228238
}
229239

230-
return dockercompat.ComponentVersion{
240+
return &dockercompat.ComponentVersion{
231241
Name: "runc",
232242
Version: version,
233243
Details: details,
234-
}
244+
}, nil
235245
}
236246

237247
//BlockIOWeight return whether Block IO weight is supported or not

pkg/infoutil/infoutil_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,29 @@ func TestParseBuildctlVersion(t *testing.T) {
5656
}
5757
}
5858
}
59+
60+
func TestParseRuncVersion(t *testing.T) {
61+
testCases := map[string]*dockercompat.ComponentVersion{
62+
`runc version 1.1.2
63+
commit: v1.1.2-0-ga916309f
64+
spec: 1.0.2-dev
65+
go: go1.18.3
66+
libseccomp: 2.5.1`: {
67+
Name: "runc",
68+
Version: "1.1.2",
69+
Details: map[string]string{
70+
"GitCommit": "v1.1.2-0-ga916309f",
71+
},
72+
},
73+
}
74+
75+
for s, expected := range testCases {
76+
got, err := parseRuncVersion([]byte(s))
77+
if expected != nil {
78+
assert.NilError(t, err)
79+
assert.DeepEqual(t, expected, got)
80+
} else {
81+
assert.Assert(t, err != nil)
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)