Skip to content
This repository was archived by the owner on Feb 8, 2021. It is now read-only.

Commit 440b4aa

Browse files
committed
[refactor] get exit code for container
Signed-off-by: Wang Xu <gnawux@gmail.com>
1 parent 471b835 commit 440b4aa

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

daemon/exec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func (daemon *Daemon) ExitCode(containerId, execId string) (int, error) {
1818

1919
glog.V(1).Infof("Get Exec Code for container %s", containerId)
2020

21-
code, err := p.GetExecExitCode(id, execId)
21+
code, err := p.GetExitCode(id, execId)
2222
return int(code), err
2323
}
2424

daemon/pod/container.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ func (c *Container) StatusString() string {
338338
return strings.Join([]string{s.ContainerID, s.ContainerName, s.PodID, s.Status}, ":")
339339
}
340340

341+
func (c *Container) GetExitCode() (uint8, error) {
342+
c.status.RLock()
343+
code := uint8(c.status.ExitCode)
344+
if c.status.Killed {
345+
code = uint8(137)
346+
}
347+
c.status.RUnlock()
348+
return code, nil
349+
}
350+
341351
func (c *Container) HasTty() bool {
342352
return c.spec.Tty
343353
}

daemon/pod/pod.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,18 @@ func (p *XPod) SandboxStatusString() string {
215215
return ""
216216
}
217217

218+
func (p *XPod) GetExitCode(cid, execId string) (uint8, error) {
219+
if execId != "" {
220+
return p.GetExecExitCode(cid, execId)
221+
}
222+
if c, ok := p.containers[cid]; ok {
223+
return c.GetExitCode()
224+
}
225+
err := fmt.Errorf("cannot find container %s", cid)
226+
p.Log(ERROR, "failed to get exit code: %v", err)
227+
return 255, err
228+
}
229+
218230
func (p *XPod) ContainerBriefStatus(cid string) *apitypes.ContainerListResult {
219231
if c, ok := p.containers[cid]; ok {
220232
return c.BriefStatus()

0 commit comments

Comments
 (0)