mirror of https://github.com/containers/podman.git
Add Networks format placeholder to podman ps and pod ps
`podman ps --format {{.Networks}}` will show all connected networks for this container. For `pod ps` it will show the infra container networks. Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
This commit is contained in:
parent
1242e7b7a6
commit
38baf3d5e2
|
@ -392,6 +392,11 @@ func (l psReporter) Names() string {
|
||||||
return l.ListContainer.Names[0]
|
return l.ListContainer.Names[0]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Networks returns the container network names in string format
|
||||||
|
func (l psReporter) Networks() string {
|
||||||
|
return strings.Join(l.ListContainer.Networks, ",")
|
||||||
|
}
|
||||||
|
|
||||||
// Ports converts from Portmappings to the string form
|
// Ports converts from Portmappings to the string form
|
||||||
// required by ps
|
// required by ps
|
||||||
func (l psReporter) Ports() string {
|
func (l psReporter) Ports() string {
|
||||||
|
|
|
@ -191,6 +191,11 @@ func (l ListPodReporter) Labels() map[string]string {
|
||||||
return l.ListPodsReport.Labels
|
return l.ListPodsReport.Labels
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Networks returns the infra container network names in string format
|
||||||
|
func (l ListPodReporter) Networks() string {
|
||||||
|
return strings.Join(l.ListPodsReport.Networks, ",")
|
||||||
|
}
|
||||||
|
|
||||||
// NumberOfContainers returns an int representation for
|
// NumberOfContainers returns an int representation for
|
||||||
// the number of containers belonging to the pod
|
// the number of containers belonging to the pod
|
||||||
func (l ListPodReporter) NumberOfContainers() int {
|
func (l ListPodReporter) NumberOfContainers() int {
|
||||||
|
|
|
@ -72,6 +72,8 @@ Valid placeholders for the Go template are listed below:
|
||||||
| .Cgroup | Cgroup path of pod |
|
| .Cgroup | Cgroup path of pod |
|
||||||
| .Created | Creation time of pod |
|
| .Created | Creation time of pod |
|
||||||
| .InfraID | Pod infra container ID |
|
| .InfraID | Pod infra container ID |
|
||||||
|
| .Networks | Show all networks connected to the infra container |
|
||||||
|
|
||||||
#### **--sort**
|
#### **--sort**
|
||||||
|
|
||||||
Sort by created, ID, name, status, or number of containers
|
Sort by created, ID, name, status, or number of containers
|
||||||
|
|
|
@ -80,6 +80,7 @@ Valid placeholders for the Go template are listed below:
|
||||||
| .Ports | Exposed ports |
|
| .Ports | Exposed ports |
|
||||||
| .Size | Size of container |
|
| .Size | Size of container |
|
||||||
| .Names | Name of container |
|
| .Names | Name of container |
|
||||||
|
| .Networks | Show all networks connected to the container |
|
||||||
| .Labels | All the labels assigned to the container |
|
| .Labels | All the labels assigned to the container |
|
||||||
| .Mounts | Volumes mounted in the container |
|
| .Mounts | Volumes mounted in the container |
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,8 @@ type ListContainer struct {
|
||||||
// Namespaces the container belongs to. Requires the
|
// Namespaces the container belongs to. Requires the
|
||||||
// namespace boolean to be true
|
// namespace boolean to be true
|
||||||
Namespaces ListContainerNamespaces
|
Namespaces ListContainerNamespaces
|
||||||
|
// The network names assigned to the container
|
||||||
|
Networks []string
|
||||||
// The process id of the container
|
// The process id of the container
|
||||||
Pid int
|
Pid int
|
||||||
// If the container is part of Pod, the Pod ID. Requires the pod
|
// If the container is part of Pod, the Pod ID. Requires the pod
|
||||||
|
|
|
@ -28,8 +28,10 @@ type ListPodsReport struct {
|
||||||
InfraId string //nolint
|
InfraId string //nolint
|
||||||
Name string
|
Name string
|
||||||
Namespace string
|
Namespace string
|
||||||
Status string
|
// Network names connected to infra container
|
||||||
Labels map[string]string
|
Networks []string
|
||||||
|
Status string
|
||||||
|
Labels map[string]string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ListPodContainer struct {
|
type ListPodContainer struct {
|
||||||
|
|
|
@ -333,6 +333,17 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
networks := []string{}
|
||||||
|
if len(infraID) > 0 {
|
||||||
|
infra, err := p.InfraContainer()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
networks, _, err = infra.Networks()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
reports = append(reports, &entities.ListPodsReport{
|
reports = append(reports, &entities.ListPodsReport{
|
||||||
Cgroup: p.CgroupParent(),
|
Cgroup: p.CgroupParent(),
|
||||||
Containers: lpcs,
|
Containers: lpcs,
|
||||||
|
@ -341,6 +352,7 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
|
||||||
InfraId: infraID,
|
InfraId: infraID,
|
||||||
Name: p.Name(),
|
Name: p.Name(),
|
||||||
Namespace: p.Namespace(),
|
Namespace: p.Namespace(),
|
||||||
|
Networks: networks,
|
||||||
Status: status,
|
Status: status,
|
||||||
Labels: p.Labels(),
|
Labels: p.Labels(),
|
||||||
})
|
})
|
||||||
|
|
|
@ -178,6 +178,11 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
|
||||||
return entities.ListContainer{}, err
|
return entities.ListContainer{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
networks, _, err := ctr.Networks()
|
||||||
|
if err != nil {
|
||||||
|
return entities.ListContainer{}, err
|
||||||
|
}
|
||||||
|
|
||||||
ps := entities.ListContainer{
|
ps := entities.ListContainer{
|
||||||
AutoRemove: ctr.AutoRemove(),
|
AutoRemove: ctr.AutoRemove(),
|
||||||
Command: conConfig.Command,
|
Command: conConfig.Command,
|
||||||
|
@ -192,6 +197,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
|
||||||
Labels: conConfig.Labels,
|
Labels: conConfig.Labels,
|
||||||
Mounts: ctr.UserVolumes(),
|
Mounts: ctr.UserVolumes(),
|
||||||
Names: []string{conConfig.Name},
|
Names: []string{conConfig.Name},
|
||||||
|
Networks: networks,
|
||||||
Pid: pid,
|
Pid: pid,
|
||||||
Pod: conConfig.Pod,
|
Pod: conConfig.Pod,
|
||||||
Ports: portMappings,
|
Ports: portMappings,
|
||||||
|
|
|
@ -305,6 +305,45 @@ var _ = Describe("Podman ps", func() {
|
||||||
Expect(session.OutputToString()).To(Not(ContainSubstring(podWithoutNet)))
|
Expect(session.OutputToString()).To(Not(ContainSubstring(podWithoutNet)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman pod ps --format networks", func() {
|
||||||
|
session := podmanTest.Podman([]string{"pod", "create"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"pod", "ps", "--format", "{{ .Networks }}"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
if isRootless() {
|
||||||
|
// rootless container don't have a network by default
|
||||||
|
Expect(session.OutputToString()).To(Equal(""))
|
||||||
|
} else {
|
||||||
|
// default network name is podman
|
||||||
|
Expect(session.OutputToString()).To(Equal("podman"))
|
||||||
|
}
|
||||||
|
|
||||||
|
net1 := stringid.GenerateNonCryptoID()
|
||||||
|
session = podmanTest.Podman([]string{"network", "create", net1})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
defer podmanTest.removeCNINetwork(net1)
|
||||||
|
net2 := stringid.GenerateNonCryptoID()
|
||||||
|
session = podmanTest.Podman([]string{"network", "create", net2})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
defer podmanTest.removeCNINetwork(net2)
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"pod", "create", "--network", net1 + "," + net2})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
pid := session.OutputToString()
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"pod", "ps", "--format", "{{ .Networks }}", "--filter", "id=" + pid})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
// the output is not deterministic so check both possible orders
|
||||||
|
Expect(session.OutputToString()).To(Or(Equal(net1+","+net2), Equal(net2+","+net1)))
|
||||||
|
})
|
||||||
|
|
||||||
It("pod no infra should ps", func() {
|
It("pod no infra should ps", func() {
|
||||||
session := podmanTest.Podman([]string{"pod", "create", "--infra=false"})
|
session := podmanTest.Podman([]string{"pod", "create", "--infra=false"})
|
||||||
session.WaitWithDefaultTimeout()
|
session.WaitWithDefaultTimeout()
|
||||||
|
|
|
@ -749,4 +749,43 @@ var _ = Describe("Podman ps", func() {
|
||||||
Expect(session.OutputToString()).To(Not(ContainSubstring(ctrWithoutNet)))
|
Expect(session.OutputToString()).To(Not(ContainSubstring(ctrWithoutNet)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
It("podman ps --format networks", func() {
|
||||||
|
session := podmanTest.Podman([]string{"create", ALPINE})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"ps", "--all", "--format", "{{ .Networks }}"})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
if isRootless() {
|
||||||
|
// rootless container don't have a network by default
|
||||||
|
Expect(session.OutputToString()).To(Equal(""))
|
||||||
|
} else {
|
||||||
|
// default network name is podman
|
||||||
|
Expect(session.OutputToString()).To(Equal("podman"))
|
||||||
|
}
|
||||||
|
|
||||||
|
net1 := stringid.GenerateNonCryptoID()
|
||||||
|
session = podmanTest.Podman([]string{"network", "create", net1})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
defer podmanTest.removeCNINetwork(net1)
|
||||||
|
net2 := stringid.GenerateNonCryptoID()
|
||||||
|
session = podmanTest.Podman([]string{"network", "create", net2})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
defer podmanTest.removeCNINetwork(net2)
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"create", "--network", net1 + "," + net2, ALPINE})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
cid := session.OutputToString()
|
||||||
|
|
||||||
|
session = podmanTest.Podman([]string{"ps", "--all", "--format", "{{ .Networks }}", "--filter", "id=" + cid})
|
||||||
|
session.WaitWithDefaultTimeout()
|
||||||
|
Expect(session.ExitCode()).To(BeZero())
|
||||||
|
// the output is not deterministic so check both possible orders
|
||||||
|
Expect(session.OutputToString()).To(Or(Equal(net1+","+net2), Equal(net2+","+net1)))
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue