Skip to content

Commit

Permalink
Merge pull request #4748 from NevilleC/nc-podname
Browse files Browse the repository at this point in the history
[Issue #4703] Add the pod name when we use `podman ps -p`
  • Loading branch information
openshift-merge-robot authored Dec 29, 2019
2 parents fa551fd + 8bc394c commit 9e03aa1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 11 deletions.
5 changes: 3 additions & 2 deletions cmd/podman/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
hsize = "SIZE"
hinfra = "IS INFRA" //nolint
hpod = "POD"
hpodname = "POD NAME"
nspid = "PID"
nscgroup = "CGROUPNS"
nsipc = "IPC"
Expand Down Expand Up @@ -355,7 +356,7 @@ func psDisplay(c *cliconfig.PsValues, runtime *adapter.LocalRuntime) error {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t%s\t%s", hid, himage, hcommand, hcreated, hstatus, hports, hnames)
// User wants pod info
if opts.Pod {
fmt.Fprintf(w, "\t%s", hpod)
fmt.Fprintf(w, "\t%s\t%s", hpod, hpodname)
}
//User wants size info
if opts.Size {
Expand All @@ -374,7 +375,7 @@ func psDisplay(c *cliconfig.PsValues, runtime *adapter.LocalRuntime) error {
fmt.Fprintf(w, "\n%s\t%s\t%s\t%s\t%s\t%s\t%s", container.ID, container.Image, container.Command, container.Created, container.Status, container.Ports, container.Names)
// User wants pod info
if opts.Pod {
fmt.Fprintf(w, "\t%s", container.Pod)
fmt.Fprintf(w, "\t%s\t%s", container.Pod, container.PodName)
}
//User wants size info
if opts.Size {
Expand Down
33 changes: 25 additions & 8 deletions cmd/podman/shared/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type PsContainerOutput struct {
Pid int
Size *ContainerSize
Pod string
PodName string
CreatedAt time.Time
ExitedAt time.Time
StartedAt time.Time
Expand Down Expand Up @@ -112,7 +113,7 @@ type ContainerSize struct {

// NewBatchContainer runs a batch process under one lock to get container information and only
// be called in PBatch.
func NewBatchContainer(ctr *libpod.Container, opts PsOptions) (PsContainerOutput, error) {
func NewBatchContainer(r *libpod.Runtime, ctr *libpod.Container, opts PsOptions) (PsContainerOutput, error) {
var (
conState define.ContainerStatus
command string
Expand Down Expand Up @@ -204,11 +205,11 @@ func NewBatchContainer(ctr *libpod.Container, opts PsOptions) (PsContainerOutput

_, imageName := ctr.Image()
cid := ctr.ID()
pod := ctr.PodID()
podID := ctr.PodID()
if !opts.NoTrunc {
cid = cid[0:cidTruncLength]
if len(pod) > podTruncLength {
pod = pod[0:podTruncLength]
if len(podID) > podTruncLength {
podID = podID[0:podTruncLength]
}
if len(command) > cmdTruncLength {
command = command[0:cmdTruncLength] + "..."
Expand All @@ -231,13 +232,29 @@ func NewBatchContainer(ctr *libpod.Container, opts PsOptions) (PsContainerOutput
pso.State = conState
pso.Pid = pid
pso.Size = size
pso.Pod = pod
pso.ExitedAt = exitedAt
pso.CreatedAt = ctr.CreatedTime()
pso.StartedAt = startedAt
pso.Labels = ctr.Labels()
pso.Mounts = strings.Join(ctr.UserVolumes(), " ")

// Add pod name and pod ID if requested by user.
// No need to look up the pod if its ID is empty.
if opts.Pod && len(podID) > 0 {
// The pod name is not in the container definition
// so we need to retrieve it using the pod ID.
var podName string
pod, err := r.LookupPod(podID)
if err != nil {
logrus.Errorf("unable to lookup pod for container %s", ctr.ID())
} else {
podName = pod.Name()
}

pso.Pod = podID
pso.PodName = podName
}

if opts.Namespace {
pso.Cgroup = ns.Cgroup
pso.IPC = ns.IPC
Expand Down Expand Up @@ -462,13 +479,13 @@ func GetPsContainerOutput(r *libpod.Runtime, opts PsOptions, filters []string, m
outputContainers = []*libpod.Container{latestCtr}
}

pss := PBatch(outputContainers, maxWorkers, opts)
pss := PBatch(r, outputContainers, maxWorkers, opts)
return pss, nil
}

// PBatch performs batch operations on a container in parallel. It spawns the
// number of workers relative to the number of parallel operations desired.
func PBatch(containers []*libpod.Container, workers int, opts PsOptions) []PsContainerOutput {
func PBatch(r *libpod.Runtime, containers []*libpod.Container, workers int, opts PsOptions) []PsContainerOutput {
var wg sync.WaitGroup
psResults := []PsContainerOutput{}

Expand All @@ -492,7 +509,7 @@ func PBatch(containers []*libpod.Container, workers int, opts PsOptions) []PsCon
j := j
wg.Add(1)
f := func() (PsContainerOutput, error) {
return NewBatchContainer(j, opts)
return NewBatchContainer(r, j, opts)
}
jobs <- workerInput{
parallelFunc: f,
Expand Down
2 changes: 1 addition & 1 deletion libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ func (r *Runtime) GetContainers(filters ...ContainerFilter) ([]*Container, error
return nil, define.ErrRuntimeStopped
}

ctrs, err := r.state.AllContainers()
ctrs, err := r.GetAllContainers()
if err != nil {
return nil, err
}
Expand Down
22 changes: 22 additions & 0 deletions test/e2e/ps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,29 @@ var _ = Describe("Podman ps", func() {
Expect(session.ExitCode()).To(Equal(0))

Expect(session.OutputToString()).To(ContainSubstring(podid))
})

It("podman --pod with a non-empty pod name", func() {
SkipIfRemote()

podName := "testPodName"
_, ec, podid := podmanTest.CreatePod(podName)
Expect(ec).To(Equal(0))

session := podmanTest.RunTopContainerInPod("", podName)
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))

// "--no-trunc" must be given. If not it will trunc the pod ID
// in the output and you will have to trunc it in the test too.
session = podmanTest.Podman([]string{"ps", "--pod", "--no-trunc"})

session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))

output := session.OutputToString()
Expect(output).To(ContainSubstring(podid))
Expect(output).To(ContainSubstring(podName))
})

It("podman ps test with port range", func() {
Expand Down

0 comments on commit 9e03aa1

Please sign in to comment.