Add network filter for podman ps and pod ps

Allow to filter on the network name or full id.
For pod ps it will filter on the infra container networks.

Signed-off-by: Paul Holzinger <paul.holzinger@web.de>
This commit is contained in:
Paul Holzinger
2021-01-09 15:54:07 +01:00
parent 49db79e735
commit 1242e7b7a6
7 changed files with 101 additions and 4 deletions

View File

@ -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)
}
@ -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)
}

View File

@ -93,6 +93,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 |

View File

@ -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*

View File

@ -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"
@ -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)
}

View File

@ -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"
)
@ -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)
}

View File

@ -6,6 +6,7 @@ import (
"sort"
. "github.com/containers/podman/v2/test/utils"
"github.com/containers/storage/pkg/stringid"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
@ -280,6 +281,30 @@ var _ = Describe("Podman ps", func() {
Expect(session.OutputToString()).To(Not(ContainSubstring(podid3)))
})
It("podman pod ps filter network", func() {
net := stringid.GenerateNonCryptoID()
session := podmanTest.Podman([]string{"network", "create", net})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(net)
session = podmanTest.Podman([]string{"pod", "create", "--network", net})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
podWithNet := session.OutputToString()
session = podmanTest.Podman([]string{"pod", "create"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
podWithoutNet := session.OutputToString()
session = podmanTest.Podman([]string{"pod", "ps", "--no-trunc", "--filter", "network=" + net})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
Expect(session.OutputToString()).To(ContainSubstring(podWithNet))
Expect(session.OutputToString()).To(Not(ContainSubstring(podWithoutNet)))
})
It("pod no infra should ps", func() {
session := podmanTest.Podman([]string{"pod", "create", "--infra=false"})
session.WaitWithDefaultTimeout()

View File

@ -8,6 +8,7 @@ import (
"strings"
. "github.com/containers/podman/v2/test/utils"
"github.com/containers/storage/pkg/stringid"
"github.com/docker/go-units"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
@ -724,4 +725,28 @@ var _ = Describe("Podman ps", func() {
})
It("podman ps filter network", func() {
net := stringid.GenerateNonCryptoID()
session := podmanTest.Podman([]string{"network", "create", net})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
defer podmanTest.removeCNINetwork(net)
session = podmanTest.Podman([]string{"create", "--network", net, ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
ctrWithNet := session.OutputToString()
session = podmanTest.Podman([]string{"create", ALPINE})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
ctrWithoutNet := session.OutputToString()
session = podmanTest.Podman([]string{"ps", "--all", "--no-trunc", "--filter", "network=" + net})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(BeZero())
Expect(session.OutputToString()).To(ContainSubstring(ctrWithNet))
Expect(session.OutputToString()).To(Not(ContainSubstring(ctrWithoutNet)))
})
})