summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKunal Kushwaha <kunal.kushwaha@gmail.com>2020-05-11 07:25:47 +0000
committerKunal Kushwaha <kunal.kushwaha@gmail.com>2020-05-20 02:53:47 +0000
commit1dde2730e85706177c9a0e9078c26560a280f074 (patch)
tree3ff395f30c83d1953de9654902c17df9747e9f3f
parentfa5b33e70b96ea8dfca3b46cab44a2e07028bf0c (diff)
downloadpodman-1dde2730e85706177c9a0e9078c26560a280f074.tar.gz
podman-1dde2730e85706177c9a0e9078c26560a280f074.tar.bz2
podman-1dde2730e85706177c9a0e9078c26560a280f074.zip
filter option added to network ls command.
filter option helps to filter output based on name or supported plugins by CNI networks. Signed-off-by: Kunal Kushwaha <kunal.kushwaha@gmail.com>
-rw-r--r--cmd/podman/networks/list.go13
-rw-r--r--docs/source/markdown/podman-network-ls.1.md18
-rw-r--r--pkg/domain/entities/network.go2
-rw-r--r--pkg/domain/infra/abi/network.go37
4 files changed, 66 insertions, 4 deletions
diff --git a/cmd/podman/networks/list.go b/cmd/podman/networks/list.go
index 1c0528e5c..24604c055 100644
--- a/cmd/podman/networks/list.go
+++ b/cmd/podman/networks/list.go
@@ -40,8 +40,9 @@ var (
func networkListFlags(flags *pflag.FlagSet) {
// TODO enable filters based on something
//flags.StringSliceVarP(&networklistCommand.Filter, "filter", "f", []string{}, "Pause all running containers")
- flags.StringVarP(&networkListOptions.Format, "format", "f", "", "Pretty-print containers to JSON or using a Go template")
+ flags.StringVarP(&networkListOptions.Format, "format", "f", "", "Pretty-print networks to JSON or using a Go template")
flags.BoolVarP(&networkListOptions.Quiet, "quiet", "q", false, "display only names")
+ flags.StringVarP(&networkListOptions.Filter, "filter", "", "", "Provide filter values (e.g. 'name=podman')")
}
func init() {
@@ -59,6 +60,14 @@ func networkList(cmd *cobra.Command, args []string) error {
nlprs []NetworkListPrintReports
)
+ // validate the filter pattern.
+ if len(networkListOptions.Filter) > 0 {
+ tokens := strings.Split(networkListOptions.Filter, "=")
+ if len(tokens) != 2 {
+ return fmt.Errorf("invalid filter syntax : %s", networkListOptions.Filter)
+ }
+ }
+
responses, err := registry.ContainerEngine().NetworkList(registry.Context(), networkListOptions)
if err != nil {
return err
@@ -69,7 +78,7 @@ func networkList(cmd *cobra.Command, args []string) error {
return quietOut(responses)
}
- if networkListOptions.Format == "json" {
+ if strings.ToLower(networkListOptions.Format) == "json" {
return jsonOut(responses)
}
diff --git a/docs/source/markdown/podman-network-ls.1.md b/docs/source/markdown/podman-network-ls.1.md
index 46e424593..7b20cf5e0 100644
--- a/docs/source/markdown/podman-network-ls.1.md
+++ b/docs/source/markdown/podman-network-ls.1.md
@@ -12,7 +12,15 @@ Displays a list of existing podman networks. This command is not available for r
## OPTIONS
**--quiet**, **-q**
-The `quiet` option will restrict the output to only the network names
+The `quiet` option will restrict the output to only the network names.
+
+**--format**, **-f**
+
+Pretty-print networks to JSON or using a Go template.
+
+**--filter**
+
+Provide filter values (e.g. 'name=podman').
## EXAMPLE
@@ -36,6 +44,14 @@ outside
podman9
```
+Display name of network which support bridge plugin
+```
+# podman network ls --filter plugin=portmap --format {{.Name}}
+podman
+podman2
+podman9
+```
+
## SEE ALSO
podman(1), podman-network(1), podman-network-inspect(1)
diff --git a/pkg/domain/entities/network.go b/pkg/domain/entities/network.go
index d001553e0..9beeeb042 100644
--- a/pkg/domain/entities/network.go
+++ b/pkg/domain/entities/network.go
@@ -10,6 +10,7 @@ import (
type NetworkListOptions struct {
Format string
Quiet bool
+ Filter string
}
// NetworkListReport describes the results from listing networks
@@ -19,6 +20,7 @@ type NetworkListReport struct {
// NetworkInspectOptions describes options for inspect networks
type NetworkInspectOptions struct {
+ Format string
}
// NetworkInspectReport describes the results from inspect networks
diff --git a/pkg/domain/infra/abi/network.go b/pkg/domain/infra/abi/network.go
index 5c39b5374..51805a36c 100644
--- a/pkg/domain/infra/abi/network.go
+++ b/pkg/domain/infra/abi/network.go
@@ -6,7 +6,9 @@ import (
"fmt"
"io/ioutil"
"path/filepath"
+ "strings"
+ "github.com/containernetworking/cni/libcni"
cniversion "github.com/containernetworking/cni/pkg/version"
"github.com/containers/libpod/libpod"
"github.com/containers/libpod/pkg/domain/entities"
@@ -39,8 +41,19 @@ func (ic *ContainerEngine) NetworkList(ctx context.Context, options entities.Net
return nil, err
}
+ var tokens []string
+ // tokenize the networkListOptions.Filter in key=value.
+ if len(options.Filter) > 0 {
+ tokens = strings.Split(options.Filter, "=")
+ if len(tokens) != 2 {
+ return nil, fmt.Errorf("invalid filter syntax : %s", options.Filter)
+ }
+ }
+
for _, n := range networks {
- reports = append(reports, &entities.NetworkListReport{NetworkConfigList: n})
+ if ifPassesFilterTest(n, tokens) {
+ reports = append(reports, &entities.NetworkListReport{NetworkConfigList: n})
+ }
}
return reports, nil
}
@@ -256,3 +269,25 @@ func createMacVLAN(r *libpod.Runtime, name string, options entities.NetworkCreat
err = ioutil.WriteFile(cniPathName, b, 0644)
return cniPathName, err
}
+
+func ifPassesFilterTest(netconf *libcni.NetworkConfigList, filter []string) bool {
+ result := false
+ if len(filter) == 0 {
+ // No filter, so pass
+ return true
+ }
+ switch strings.ToLower(filter[0]) {
+ case "name":
+ if filter[1] == netconf.Name {
+ result = true
+ }
+ case "plugin":
+ plugins := network.GetCNIPlugins(netconf)
+ if strings.Contains(plugins, filter[1]) {
+ result = true
+ }
+ default:
+ result = false
+ }
+ return result
+}