diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-01-25 23:23:39 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-01-25 23:23:39 -0800 |
commit | d07f611885311538895fe5b665dccf75d1448fc3 (patch) | |
tree | c308c5445220b675567d67e0e3c296d31b90528d /libpod/pod_status.go | |
parent | 689da532fa8f9c72d4fd7afe963d6eb44fe0e623 (diff) | |
parent | 5da70b04dd95263a536cc148288d2e20cd9dea30 (diff) | |
download | podman-d07f611885311538895fe5b665dccf75d1448fc3.tar.gz podman-d07f611885311538895fe5b665dccf75d1448fc3.tar.bz2 podman-d07f611885311538895fe5b665dccf75d1448fc3.zip |
Merge pull request #4965 from baude/reviewcorrections3
APIv2 review corrections #3
Diffstat (limited to 'libpod/pod_status.go')
-rw-r--r-- | libpod/pod_status.go | 59 |
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 + } +} |