summaryrefslogtreecommitdiff
path: root/pkg/domain
diff options
context:
space:
mode:
authorBrent Baude <bbaude@redhat.com>2020-04-04 13:59:55 -0500
committerBrent Baude <bbaude@redhat.com>2020-04-06 12:12:59 -0500
commitb22254dc8bfdea5153761e93298182286f874bac (patch)
tree16ff841496724a6b600824d06900a075d429e7dc /pkg/domain
parente318b09b6800ddb013ddf3b9a2fb99ebc55bd920 (diff)
downloadpodman-b22254dc8bfdea5153761e93298182286f874bac.tar.gz
podman-b22254dc8bfdea5153761e93298182286f874bac.tar.bz2
podman-b22254dc8bfdea5153761e93298182286f874bac.zip
podmanv2 ps
add the ability to list containers Signed-off-by: Brent Baude <bbaude@redhat.com>
Diffstat (limited to 'pkg/domain')
-rw-r--r--pkg/domain/entities/container_ps.go174
-rw-r--r--pkg/domain/entities/containers.go17
-rw-r--r--pkg/domain/entities/engine_container.go1
-rw-r--r--pkg/domain/infra/abi/containers.go5
-rw-r--r--pkg/domain/infra/abi/pods.go5
-rw-r--r--pkg/domain/infra/tunnel/containers.go9
-rw-r--r--pkg/domain/infra/tunnel/helpers.go5
7 files changed, 208 insertions, 8 deletions
diff --git a/pkg/domain/entities/container_ps.go b/pkg/domain/entities/container_ps.go
new file mode 100644
index 000000000..ceafecebc
--- /dev/null
+++ b/pkg/domain/entities/container_ps.go
@@ -0,0 +1,174 @@
+package entities
+
+import (
+ "sort"
+ "strings"
+
+ "github.com/containers/libpod/cmd/podman/shared"
+ "github.com/containers/libpod/libpod"
+ "github.com/cri-o/ocicni/pkg/ocicni"
+ "github.com/pkg/errors"
+)
+
+// Listcontainer describes a container suitable for listing
+type ListContainer struct {
+ // Container command
+ Command []string
+ // Container creation time
+ Created int64
+ // If container has exited/stopped
+ Exited bool
+ // Time container exited
+ ExitedAt int64
+ // If container has exited, the return code from the command
+ ExitCode int32
+ // The unique identifier for the container
+ ID string `json:"Id"`
+ // Container image
+ Image string
+ // If this container is a Pod infra container
+ IsInfra bool
+ // Labels for container
+ Labels map[string]string
+ // User volume mounts
+ Mounts []string
+ // The names assigned to the container
+ Names []string
+ // Namespaces the container belongs to. Requires the
+ // namespace boolean to be true
+ Namespaces ListContainerNamespaces
+ // The process id of the container
+ Pid int
+ // If the container is part of Pod, the Pod ID. Requires the pod
+ // boolean to be set
+ Pod string
+ // If the container is part of Pod, the Pod name. Requires the pod
+ // boolean to be set
+ PodName string
+ // Port mappings
+ Ports []ocicni.PortMapping
+ // Size of the container rootfs. Requires the size boolean to be true
+ Size *shared.ContainerSize
+ // Time when container started
+ StartedAt int64
+ // State of container
+ State string
+}
+
+// ListContainer Namespaces contains the identifiers of the container's Linux namespaces
+type ListContainerNamespaces struct {
+ // Mount namespace
+ MNT string `json:"Mnt,omitempty"`
+ // Cgroup namespace
+ Cgroup string `json:"Cgroup,omitempty"`
+ // IPC namespace
+ IPC string `json:"Ipc,omitempty"`
+ // Network namespace
+ NET string `json:"Net,omitempty"`
+ // PID namespace
+ PIDNS string `json:"Pidns,omitempty"`
+ // UTS namespace
+ UTS string `json:"Uts,omitempty"`
+ // User namespace
+ User string `json:"User,omitempty"`
+}
+
+// SortContainers helps us set-up ability to sort by createTime
+type SortContainers []*libpod.Container
+
+func (a SortContainers) Len() int { return len(a) }
+func (a SortContainers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
+
+type SortCreateTime struct{ SortContainers }
+
+func (a SortCreateTime) Less(i, j int) bool {
+ return a.SortContainers[i].CreatedTime().Before(a.SortContainers[j].CreatedTime())
+}
+
+type SortListContainers []ListContainer
+
+func (a SortListContainers) Len() int { return len(a) }
+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].Command, " ") < strings.Join(a.SortListContainers[j].Command, " ")
+}
+
+type psSortedId struct{ SortListContainers }
+
+func (a psSortedId) Less(i, j int) bool {
+ return a.SortListContainers[i].ID < a.SortListContainers[j].ID
+}
+
+type psSortedImage struct{ SortListContainers }
+
+func (a psSortedImage) Less(i, j int) bool {
+ return a.SortListContainers[i].Image < a.SortListContainers[j].Image
+}
+
+type psSortedNames struct{ SortListContainers }
+
+func (a psSortedNames) Less(i, j int) bool {
+ return a.SortListContainers[i].Names[0] < a.SortListContainers[j].Names[0]
+}
+
+type psSortedPod struct{ SortListContainers }
+
+func (a psSortedPod) Less(i, j int) bool {
+ return a.SortListContainers[i].Pod < a.SortListContainers[j].Pod
+}
+
+type psSortedRunningFor struct{ SortListContainers }
+
+func (a psSortedRunningFor) Less(i, j int) bool {
+ return a.SortListContainers[i].StartedAt < a.SortListContainers[j].StartedAt
+}
+
+type psSortedStatus struct{ SortListContainers }
+
+func (a psSortedStatus) Less(i, j int) bool {
+ 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].Size == nil || a.SortListContainers[j].Size == nil {
+ return false
+ }
+ return a.SortListContainers[i].Size.RootFsSize < a.SortListContainers[j].Size.RootFsSize
+}
+
+type PsSortedCreateTime struct{ SortListContainers }
+
+func (a PsSortedCreateTime) Less(i, j int) bool {
+ return a.SortListContainers[i].Created < a.SortListContainers[j].Created
+}
+
+func SortPsOutput(sortBy string, psOutput SortListContainers) (SortListContainers, error) {
+ switch sortBy {
+ case "id":
+ sort.Sort(psSortedId{psOutput})
+ case "image":
+ sort.Sort(psSortedImage{psOutput})
+ case "command":
+ sort.Sort(psSortedCommand{psOutput})
+ case "runningfor":
+ sort.Sort(psSortedRunningFor{psOutput})
+ case "status":
+ sort.Sort(psSortedStatus{psOutput})
+ case "size":
+ sort.Sort(psSortedSize{psOutput})
+ case "names":
+ sort.Sort(psSortedNames{psOutput})
+ case "created":
+ sort.Sort(PsSortedCreateTime{psOutput})
+ case "pod":
+ sort.Sort(psSortedPod{psOutput})
+ default:
+ return nil, errors.Errorf("invalid option for --sort, options are: command, created, id, image, names, runningfor, size, or status")
+ }
+ return psOutput, nil
+}
diff --git a/pkg/domain/entities/containers.go b/pkg/domain/entities/containers.go
index 6b4bb9ba2..4f94e009f 100644
--- a/pkg/domain/entities/containers.go
+++ b/pkg/domain/entities/containers.go
@@ -207,3 +207,20 @@ type ContainerStartReport struct {
Err error
ExitCode int
}
+
+// ContainerListOptions describes the CLI options
+// for listing containers
+type ContainerListOptions struct {
+ All bool
+ Filters map[string][]string
+ Format string
+ Last int
+ Latest bool
+ Namespace bool
+ Pod bool
+ Quiet bool
+ Size bool
+ Sort string
+ Sync bool
+ Watch uint
+}
diff --git a/pkg/domain/entities/engine_container.go b/pkg/domain/entities/engine_container.go
index 264780771..36c20c38d 100644
--- a/pkg/domain/entities/engine_container.go
+++ b/pkg/domain/entities/engine_container.go
@@ -19,6 +19,7 @@ type ContainerEngine interface {
ContainerExport(ctx context.Context, nameOrId string, options ContainerExportOptions) error
ContainerKill(ctx context.Context, namesOrIds []string, options KillOptions) ([]*KillReport, error)
ContainerPause(ctx context.Context, namesOrIds []string, options PauseUnPauseOptions) ([]*PauseUnpauseReport, error)
+ ContainerList(ctx context.Context, options ContainerListOptions) ([]ListContainer, error)
ContainerRestart(ctx context.Context, namesOrIds []string, options RestartOptions) ([]*RestartReport, error)
ContainerRm(ctx context.Context, namesOrIds []string, options RmOptions) ([]*RmReport, error)
ContainerStart(ctx context.Context, namesOrIds []string, options ContainerStartOptions) ([]*ContainerStartReport, error)
diff --git a/pkg/domain/infra/abi/containers.go b/pkg/domain/infra/abi/containers.go
index 929c3f335..54f91a189 100644
--- a/pkg/domain/infra/abi/containers.go
+++ b/pkg/domain/infra/abi/containers.go
@@ -17,6 +17,7 @@ import (
"github.com/containers/libpod/pkg/checkpoint"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/domain/infra/abi/terminal"
+ "github.com/containers/libpod/pkg/ps"
"github.com/containers/libpod/pkg/signal"
"github.com/containers/libpod/pkg/specgen"
"github.com/containers/libpod/pkg/specgen/generate"
@@ -617,3 +618,7 @@ func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []stri
}
return reports, nil
}
+
+func (ic *ContainerEngine) ContainerList(ctx context.Context, options entities.ContainerListOptions) ([]entities.ListContainer, error) {
+ return ps.GetContainerLists(ic.Libpod, options)
+}
diff --git a/pkg/domain/infra/abi/pods.go b/pkg/domain/infra/abi/pods.go
index 073cd8d5c..c3e5d59bc 100644
--- a/pkg/domain/infra/abi/pods.go
+++ b/pkg/domain/infra/abi/pods.go
@@ -5,9 +5,10 @@ package abi
import (
"context"
+ lpfilters "github.com/containers/libpod/libpod/filters"
+
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/libpod/define"
- "github.com/containers/libpod/libpod/podfilters"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/signal"
"github.com/containers/libpod/pkg/specgen"
@@ -281,7 +282,7 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
)
for k, v := range options.Filters {
for _, filter := range v {
- f, err := podfilters.GeneratePodFilterFunc(k, filter)
+ f, err := lpfilters.GeneratePodFilterFunc(k, filter)
if err != nil {
return nil, err
}
diff --git a/pkg/domain/infra/tunnel/containers.go b/pkg/domain/infra/tunnel/containers.go
index 4101068ba..b6807d5b6 100644
--- a/pkg/domain/infra/tunnel/containers.go
+++ b/pkg/domain/infra/tunnel/containers.go
@@ -7,7 +7,6 @@ import (
"github.com/containers/image/v5/docker/reference"
"github.com/containers/libpod/libpod/define"
- "github.com/containers/libpod/pkg/api/handlers/libpod"
"github.com/containers/libpod/pkg/bindings/containers"
"github.com/containers/libpod/pkg/domain/entities"
"github.com/containers/libpod/pkg/specgen"
@@ -233,7 +232,7 @@ func (ic *ContainerEngine) ContainerCheckpoint(ctx context.Context, namesOrIds [
var (
reports []*entities.CheckpointReport
err error
- ctrs []libpod.ListContainer
+ ctrs []entities.ListContainer
)
if options.All {
@@ -268,7 +267,7 @@ func (ic *ContainerEngine) ContainerRestore(ctx context.Context, namesOrIds []st
var (
reports []*entities.RestoreReport
err error
- ctrs []libpod.ListContainer
+ ctrs []entities.ListContainer
)
if options.All {
allCtrs, err := getContainersByContext(ic.ClientCxt, true, []string{})
@@ -317,3 +316,7 @@ func (ic *ContainerEngine) ContainerExec(ctx context.Context, nameOrId string, o
func (ic *ContainerEngine) ContainerStart(ctx context.Context, namesOrIds []string, options entities.ContainerStartOptions) ([]*entities.ContainerStartReport, error) {
return nil, errors.New("not implemented")
}
+
+func (ic *ContainerEngine) ContainerList(ctx context.Context, options entities.ContainerListOptions) ([]entities.ListContainer, error) {
+ return containers.List(ic.ClientCxt, options.Filters, &options.All, &options.Last, &options.Pod, &options.Size, &options.Sync)
+}
diff --git a/pkg/domain/infra/tunnel/helpers.go b/pkg/domain/infra/tunnel/helpers.go
index f9183c955..682d60d6a 100644
--- a/pkg/domain/infra/tunnel/helpers.go
+++ b/pkg/domain/infra/tunnel/helpers.go
@@ -5,7 +5,6 @@ import (
"strings"
"github.com/containers/libpod/libpod/define"
- "github.com/containers/libpod/pkg/api/handlers/libpod"
"github.com/containers/libpod/pkg/bindings"
"github.com/containers/libpod/pkg/bindings/containers"
"github.com/containers/libpod/pkg/bindings/pods"
@@ -14,9 +13,9 @@ import (
"github.com/pkg/errors"
)
-func getContainersByContext(contextWithConnection context.Context, all bool, namesOrIds []string) ([]libpod.ListContainer, error) {
+func getContainersByContext(contextWithConnection context.Context, all bool, namesOrIds []string) ([]entities.ListContainer, error) {
var (
- cons []libpod.ListContainer
+ cons []entities.ListContainer
)
if all && len(namesOrIds) > 0 {
return nil, errors.New("cannot lookup containers and all")