summaryrefslogtreecommitdiff
path: root/libpod/pod_status.go
diff options
context:
space:
mode:
authorBrent Baude <bbaude@redhat.com>2020-01-24 08:59:20 -0600
committerBrent Baude <bbaude@redhat.com>2020-01-25 15:09:09 -0600
commit5da70b04dd95263a536cc148288d2e20cd9dea30 (patch)
tree3d28c000a7941d9f8ea4aef906d29a44c2e80484 /libpod/pod_status.go
parent81e59a742b46d41848c8c213e155fbc9ecc4e5f8 (diff)
downloadpodman-5da70b04dd95263a536cc148288d2e20cd9dea30.tar.gz
podman-5da70b04dd95263a536cc148288d2e20cd9dea30.tar.bz2
podman-5da70b04dd95263a536cc148288d2e20cd9dea30.zip
APIv2 review corrections #3
The third pass of corrections for the APIv2. Signed-off-by: Brent Baude <bbaude@redhat.com>
Diffstat (limited to 'libpod/pod_status.go')
-rw-r--r--libpod/pod_status.go59
1 files changed, 59 insertions, 0 deletions
diff --git a/libpod/pod_status.go b/libpod/pod_status.go
new file mode 100644
index 000000000..3a44c4457
--- /dev/null
+++ b/libpod/pod_status.go
@@ -0,0 +1,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
+ }
+}