summaryrefslogtreecommitdiff
path: root/pkg/domain/entities/container_ps.go
diff options
context:
space:
mode:
authorBrent Baude <bbaude@redhat.com>2020-04-14 09:02:28 -0500
committerBrent Baude <bbaude@redhat.com>2020-04-14 09:40:43 -0500
commit830f3a4462953b3698a21031db3964d0f0ae63b3 (patch)
tree26730d69cf6a96990b933bf3af2de3f2e891c4a4 /pkg/domain/entities/container_ps.go
parent004826653f2a8b064af8c1f055b1a402f4060d1c (diff)
downloadpodman-830f3a4462953b3698a21031db3964d0f0ae63b3.tar.gz
podman-830f3a4462953b3698a21031db3964d0f0ae63b3.tar.bz2
podman-830f3a4462953b3698a21031db3964d0f0ae63b3.zip
v2podman ps revert structure changes
reverting name changes to the listcontainer structure because it negatively impacted the direct consumption of the restful API. instead we now use a local structure in the CLI to modify the output as needed. Signed-off-by: Brent Baude <bbaude@redhat.com>
Diffstat (limited to 'pkg/domain/entities/container_ps.go')
-rw-r--r--pkg/domain/entities/container_ps.go171
1 files changed, 10 insertions, 161 deletions
diff --git a/pkg/domain/entities/container_ps.go b/pkg/domain/entities/container_ps.go
index f07b0364f..ceafecebc 100644
--- a/pkg/domain/entities/container_ps.go
+++ b/pkg/domain/entities/container_ps.go
@@ -1,23 +1,19 @@
package entities
import (
- "fmt"
"sort"
- "strconv"
"strings"
- "time"
"github.com/containers/libpod/cmd/podman/shared"
"github.com/containers/libpod/libpod"
"github.com/cri-o/ocicni/pkg/ocicni"
- "github.com/docker/go-units"
"github.com/pkg/errors"
)
// Listcontainer describes a container suitable for listing
type ListContainer struct {
// Container command
- Cmd []string
+ Command []string
// Container creation time
Created int64
// If container has exited/stopped
@@ -37,7 +33,7 @@ type ListContainer struct {
// User volume mounts
Mounts []string
// The names assigned to the container
- ContainerNames []string
+ Names []string
// Namespaces the container belongs to. Requires the
// namespace boolean to be true
Namespaces ListContainerNamespaces
@@ -50,69 +46,13 @@ type ListContainer struct {
// boolean to be set
PodName string
// Port mappings
- PortMappings []ocicni.PortMapping
+ Ports []ocicni.PortMapping
// Size of the container rootfs. Requires the size boolean to be true
- ContainerSize *shared.ContainerSize
+ Size *shared.ContainerSize
// Time when container started
StartedAt int64
// State of container
- ContainerState string
-}
-
-// State returns the container state in human duration
-func (l ListContainer) State() string {
- var state string
- switch l.ContainerState {
- case "running":
- t := units.HumanDuration(time.Since(time.Unix(l.StartedAt, 0)))
- state = "Up " + t + " ago"
- case "configured":
- state = "Created"
- case "exited", "stopped":
- t := units.HumanDuration(time.Since(time.Unix(l.ExitedAt, 0)))
- state = fmt.Sprintf("Exited (%d) %s ago", l.ExitCode, t)
- default:
- state = l.ContainerState
- }
- return state
-}
-
-// Command returns the container command in string format
-func (l ListContainer) Command() string {
- return strings.Join(l.Cmd, " ")
-}
-
-// Size returns the rootfs and virtual sizes in human duration in
-// and output form (string) suitable for ps
-func (l ListContainer) Size() string {
- virt := units.HumanSizeWithPrecision(float64(l.ContainerSize.RootFsSize), 3)
- s := units.HumanSizeWithPrecision(float64(l.ContainerSize.RwSize), 3)
- return fmt.Sprintf("%s (virtual %s)", s, virt)
-}
-
-// Names returns the container name in string format
-func (l ListContainer) Names() string {
- return l.ContainerNames[0]
-}
-
-// Ports converts from Portmappings to the string form
-// required by ps
-func (l ListContainer) Ports() string {
- if len(l.PortMappings) < 1 {
- return ""
- }
- return portsToString(l.PortMappings)
-}
-
-// CreatedAt returns the container creation time in string format. podman
-// and docker both return a timestamped value for createdat
-func (l ListContainer) CreatedAt() string {
- return time.Unix(l.Created, 0).String()
-}
-
-// CreateHuman allows us to output the created time in human readable format
-func (l ListContainer) CreatedHuman() string {
- return units.HumanDuration(time.Since(time.Unix(l.Created, 0))) + " ago"
+ State string
}
// ListContainer Namespaces contains the identifiers of the container's Linux namespaces
@@ -153,7 +93,7 @@ func (a SortListContainers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
type psSortedCommand struct{ SortListContainers }
func (a psSortedCommand) Less(i, j int) bool {
- return strings.Join(a.SortListContainers[i].Cmd, " ") < strings.Join(a.SortListContainers[j].Cmd, " ")
+ return strings.Join(a.SortListContainers[i].Command, " ") < strings.Join(a.SortListContainers[j].Command, " ")
}
type psSortedId struct{ SortListContainers }
@@ -171,7 +111,7 @@ func (a psSortedImage) Less(i, j int) bool {
type psSortedNames struct{ SortListContainers }
func (a psSortedNames) Less(i, j int) bool {
- return a.SortListContainers[i].ContainerNames[0] < a.SortListContainers[j].ContainerNames[0]
+ return a.SortListContainers[i].Names[0] < a.SortListContainers[j].Names[0]
}
type psSortedPod struct{ SortListContainers }
@@ -189,16 +129,16 @@ func (a psSortedRunningFor) Less(i, j int) bool {
type psSortedStatus struct{ SortListContainers }
func (a psSortedStatus) Less(i, j int) bool {
- return a.SortListContainers[i].ContainerState < a.SortListContainers[j].ContainerState
+ return a.SortListContainers[i].State < a.SortListContainers[j].State
}
type psSortedSize struct{ SortListContainers }
func (a psSortedSize) Less(i, j int) bool {
- if a.SortListContainers[i].ContainerSize == nil || a.SortListContainers[j].ContainerSize == nil {
+ if a.SortListContainers[i].Size == nil || a.SortListContainers[j].Size == nil {
return false
}
- return a.SortListContainers[i].ContainerSize.RootFsSize < a.SortListContainers[j].ContainerSize.RootFsSize
+ return a.SortListContainers[i].Size.RootFsSize < a.SortListContainers[j].Size.RootFsSize
}
type PsSortedCreateTime struct{ SortListContainers }
@@ -232,94 +172,3 @@ func SortPsOutput(sortBy string, psOutput SortListContainers) (SortListContainer
}
return psOutput, nil
}
-
-// portsToString converts the ports used to a string of the from "port1, port2"
-// and also groups a continuous list of ports into a readable format.
-func portsToString(ports []ocicni.PortMapping) string {
- type portGroup struct {
- first int32
- last int32
- }
- var portDisplay []string
- if len(ports) == 0 {
- return ""
- }
- //Sort the ports, so grouping continuous ports become easy.
- sort.Slice(ports, func(i, j int) bool {
- return comparePorts(ports[i], ports[j])
- })
-
- // portGroupMap is used for grouping continuous ports.
- portGroupMap := make(map[string]*portGroup)
- var groupKeyList []string
-
- for _, v := range ports {
-
- hostIP := v.HostIP
- if hostIP == "" {
- hostIP = "0.0.0.0"
- }
- // If hostPort and containerPort are not same, consider as individual port.
- if v.ContainerPort != v.HostPort {
- portDisplay = append(portDisplay, fmt.Sprintf("%s:%d->%d/%s", hostIP, v.HostPort, v.ContainerPort, v.Protocol))
- continue
- }
-
- portMapKey := fmt.Sprintf("%s/%s", hostIP, v.Protocol)
-
- portgroup, ok := portGroupMap[portMapKey]
- if !ok {
- portGroupMap[portMapKey] = &portGroup{first: v.ContainerPort, last: v.ContainerPort}
- // This list is required to traverse portGroupMap.
- groupKeyList = append(groupKeyList, portMapKey)
- continue
- }
-
- if portgroup.last == (v.ContainerPort - 1) {
- portgroup.last = v.ContainerPort
- continue
- }
- }
- // For each portMapKey, format group list and appned to output string.
- for _, portKey := range groupKeyList {
- group := portGroupMap[portKey]
- portDisplay = append(portDisplay, formatGroup(portKey, group.first, group.last))
- }
- return strings.Join(portDisplay, ", ")
-}
-
-func comparePorts(i, j ocicni.PortMapping) bool {
- if i.ContainerPort != j.ContainerPort {
- return i.ContainerPort < j.ContainerPort
- }
-
- if i.HostIP != j.HostIP {
- return i.HostIP < j.HostIP
- }
-
- if i.HostPort != j.HostPort {
- return i.HostPort < j.HostPort
- }
-
- return i.Protocol < j.Protocol
-}
-
-// formatGroup returns the group as <IP:startPort:lastPort->startPort:lastPort/Proto>
-// e.g 0.0.0.0:1000-1006->1000-1006/tcp.
-func formatGroup(key string, start, last int32) string {
- parts := strings.Split(key, "/")
- groupType := parts[0]
- var ip string
- if len(parts) > 1 {
- ip = parts[0]
- groupType = parts[1]
- }
- group := strconv.Itoa(int(start))
- if start != last {
- group = fmt.Sprintf("%s-%d", group, last)
- }
- if ip != "" {
- group = fmt.Sprintf("%s:%s->%s", ip, group, group)
- }
- return fmt.Sprintf("%s/%s", group, groupType)
-}