summaryrefslogtreecommitdiff
path: root/libpod/pod_status.go
blob: 3a44c44571fd64023a65091581f23209d845f058 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package libpod

import "github.com/containers/libpod/libpod/define"

// GetPodStatus determines the status of the pod based on the
// statuses of the containers in the pod.
// Returns a string representation of the pod status
func (p *Pod) GetPodStatus() (string, error) {
	ctrStatuses, err := p.Status()
	if err != nil {
		return define.PodStateErrored, err
	}
	return CreatePodStatusResults(ctrStatuses)
}

func CreatePodStatusResults(ctrStatuses map[string]define.ContainerStatus) (string, error) {
	ctrNum := len(ctrStatuses)
	if ctrNum == 0 {
		return define.PodStateCreated, nil
	}
	statuses := map[string]int{
		define.PodStateStopped: 0,
		define.PodStateRunning: 0,
		define.PodStatePaused:  0,
		define.PodStateCreated: 0,
		define.PodStateErrored: 0,
	}
	for _, ctrStatus := range ctrStatuses {
		switch ctrStatus {
		case define.ContainerStateExited:
			fallthrough
		case define.ContainerStateStopped:
			statuses[define.PodStateStopped]++
		case define.ContainerStateRunning:
			statuses[define.PodStateRunning]++
		case define.ContainerStatePaused:
			statuses[define.PodStatePaused]++
		case define.ContainerStateCreated, define.ContainerStateConfigured:
			statuses[define.PodStateCreated]++
		default:
			statuses[define.PodStateErrored]++
		}
	}

	switch {
	case statuses[define.PodStateRunning] > 0:
		return define.PodStateRunning, nil
	case statuses[define.PodStatePaused] == ctrNum:
		return define.PodStatePaused, nil
	case statuses[define.PodStateStopped] == ctrNum:
		return define.PodStateExited, nil
	case statuses[define.PodStateStopped] > 0:
		return define.PodStateStopped, nil
	case statuses[define.PodStateErrored] > 0:
		return define.PodStateErrored, nil
	default:
		return define.PodStateCreated, nil
	}
}