Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

podman ps/pod ps add network filter and .Networks format placeholder #8920

Merged
merged 3 commits into from
Jan 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions cmd/podman/common/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,9 +982,10 @@ func AutocompletePsFilters(cmd *cobra.Command, args []string, toComplete string)
return []string{define.HealthCheckHealthy,
define.HealthCheckUnhealthy}, cobra.ShellCompDirectiveNoFileComp
},
"label=": nil,
"exited=": nil,
"until=": nil,
"network=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s) },
"label=": nil,
"exited=": nil,
"until=": nil,
}
return completeKeyValues(toComplete, kv)
}
Expand All @@ -1004,7 +1005,8 @@ func AutocompletePodPsFilters(cmd *cobra.Command, args []string, toComplete stri
"ctr-status=": func(_ string) ([]string, cobra.ShellCompDirective) {
return containerStatuses, cobra.ShellCompDirectiveNoFileComp
},
"label=": nil,
"network=": func(s string) ([]string, cobra.ShellCompDirective) { return getNetworks(cmd, s) },
"label=": nil,
}
return completeKeyValues(toComplete, kv)
}
Expand Down
5 changes: 5 additions & 0 deletions cmd/podman/containers/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,11 @@ func (l psReporter) Names() string {
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
// required by ps
func (l psReporter) Ports() string {
Expand Down
5 changes: 5 additions & 0 deletions cmd/podman/pods/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ func (l ListPodReporter) Labels() map[string]string {
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
// the number of containers belonging to the pod
func (l ListPodReporter) NumberOfContainers() int {
Expand Down
3 changes: 3 additions & 0 deletions docs/source/markdown/podman-pod-ps.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ Valid placeholders for the Go template are listed below:
| .Cgroup | Cgroup path of pod |
| .Created | Creation time of pod |
| .InfraID | Pod infra container ID |
| .Networks | Show all networks connected to the infra container |

#### **--sort**

Sort by created, ID, name, status, or number of containers
Expand All @@ -93,6 +95,7 @@ Valid filters are listed below:
| name | [Name] Pod's name (accepts regex) |
| label | [Key] or [Key=Value] Label assigned to a container |
| status | Pod's status: `stopped`, `running`, `paused`, `exited`, `dead`, `created`, `degraded` |
| network | [Network] name or full ID of network |
| ctr-names | Container name within the pod (accepts regex) |
| ctr-ids | Container ID within the pod (accepts regex) |
| ctr-status | Container status within the pod |
Expand Down
2 changes: 2 additions & 0 deletions docs/source/markdown/podman-ps.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Valid filters are listed below:
| volume | [VolumeName] or [MountpointDestination] Volume mounted in container |
| health | [Status] healthy or unhealthy |
| pod | [Pod] name or full or partial ID of pod |
| network | [Network] name or full ID of network |


#### **--format**=*format*
Expand All @@ -79,6 +80,7 @@ Valid placeholders for the Go template are listed below:
| .Ports | Exposed ports |
| .Size | Size of container |
| .Names | Name of container |
| .Networks | Show all networks connected to the container |
| .Labels | All the labels assigned to the container |
| .Mounts | Volumes mounted in the container |

Expand Down
7 changes: 6 additions & 1 deletion pkg/api/handlers/libpod/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func PodCreate(w http.ResponseWriter, r *http.Request) {
}

func Pods(w http.ResponseWriter, r *http.Request) {
runtime := r.Context().Value("runtime").(*libpod.Runtime)
decoder := r.Context().Value("decoder").(*schema.Decoder)
query := struct {
Filters map[string][]string `schema:"filters"`
Expand All @@ -55,7 +56,11 @@ func Pods(w http.ResponseWriter, r *http.Request) {
return
}

pods, err := utils.GetPods(w, r)
containerEngine := abi.ContainerEngine{Libpod: runtime}
podPSOptions := entities.PodPSOptions{
Filters: query.Filters,
}
pods, err := containerEngine.PodPs(r.Context(), podPSOptions)
if err != nil {
utils.Error(w, "Something went wrong", http.StatusInternalServerError, err)
return
Expand Down
87 changes: 0 additions & 87 deletions pkg/api/handlers/utils/pods.go

This file was deleted.

2 changes: 2 additions & 0 deletions pkg/domain/entities/container_ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type ListContainer struct {
// Namespaces the container belongs to. Requires the
// namespace boolean to be true
Namespaces ListContainerNamespaces
// The network names assigned to the container
Networks []string
// The process id of the container
Pid int
// If the container is part of Pod, the Pod ID. Requires the pod
Expand Down
6 changes: 4 additions & 2 deletions pkg/domain/entities/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type ListPodsReport struct {
InfraId string //nolint
Name string
Namespace string
Status string
Labels map[string]string
// Network names connected to infra container
Networks []string
Status string
Labels map[string]string
}

type ListPodContainer struct {
Expand Down
19 changes: 19 additions & 0 deletions pkg/domain/filters/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/containers/podman/v2/libpod"
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/libpod/network"
"github.com/containers/podman/v2/pkg/timetype"
"github.com/containers/podman/v2/pkg/util"
"github.com/pkg/errors"
Expand Down Expand Up @@ -233,6 +234,24 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
}
return false
}, nil
case "network":
return func(c *libpod.Container) bool {
networks, _, err := c.Networks()
// if err or no networks, quick out
if err != nil || len(networks) == 0 {
return false
}
for _, net := range networks {
netID := network.GetNetworkID(net)
for _, val := range filterValues {
// match by network name or id
if val == net || val == netID {
return true
}
}
}
return false
}, nil
}
return nil, errors.Errorf("%s is an invalid filter", filter)
}
24 changes: 24 additions & 0 deletions pkg/domain/filters/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/containers/podman/v2/libpod"
"github.com/containers/podman/v2/libpod/define"
"github.com/containers/podman/v2/libpod/network"
"github.com/containers/podman/v2/pkg/util"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -134,6 +135,29 @@ func GeneratePodFilterFunc(filter string, filterValues []string) (
}
return true
}, nil
case "network":
return func(p *libpod.Pod) bool {
infra, err := p.InfraContainer()
// no infra, quick out
if err != nil {
return false
}
networks, _, err := infra.Networks()
// if err or no networks, quick out
if err != nil || len(networks) == 0 {
return false
}
for _, net := range networks {
netID := network.GetNetworkID(net)
for _, val := range filterValues {
// match by network name or id
if val == net || val == netID {
return true
}
}
}
return false
}, nil
}
return nil, errors.Errorf("%s is an invalid filter", filter)
}
12 changes: 12 additions & 0 deletions pkg/domain/infra/abi/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,17 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
if err != nil {
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{
Cgroup: p.CgroupParent(),
Containers: lpcs,
Expand All @@ -341,6 +352,7 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
InfraId: infraID,
Name: p.Name(),
Namespace: p.Namespace(),
Networks: networks,
Status: status,
Labels: p.Labels(),
})
Expand Down
6 changes: 6 additions & 0 deletions pkg/ps/ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,11 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
return entities.ListContainer{}, err
}

networks, _, err := ctr.Networks()
if err != nil {
return entities.ListContainer{}, err
}

ps := entities.ListContainer{
AutoRemove: ctr.AutoRemove(),
Command: conConfig.Command,
Expand All @@ -192,6 +197,7 @@ func ListContainerBatch(rt *libpod.Runtime, ctr *libpod.Container, opts entities
Labels: conConfig.Labels,
Mounts: ctr.UserVolumes(),
Names: []string{conConfig.Name},
Networks: networks,
Pid: pid,
Pod: conConfig.Pod,
Ports: portMappings,
Expand Down
Loading