diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2020-05-20 23:32:52 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-20 23:32:52 +0200 |
commit | 588df9039700e53eb3d8222c15494a5dd727e4f5 (patch) | |
tree | 782863dbf1231dea2fe6410ad1e5ec3cda92049b /pkg/domain/infra/abi | |
parent | e8e5a5f96e0b5d915baeeb0915cc0ca788b70e64 (diff) | |
parent | 087fdda199aaef9f1f0ae1d111e60d22a40ed106 (diff) | |
download | podman-588df9039700e53eb3d8222c15494a5dd727e4f5.tar.gz podman-588df9039700e53eb3d8222c15494a5dd727e4f5.tar.bz2 podman-588df9039700e53eb3d8222c15494a5dd727e4f5.zip |
Merge pull request #6161 from kunalkushwaha/network-inspect
`--format` and `--filter` options for `network ls` and `network inspect` command
Diffstat (limited to 'pkg/domain/infra/abi')
-rw-r--r-- | pkg/domain/infra/abi/network.go | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/pkg/domain/infra/abi/network.go b/pkg/domain/infra/abi/network.go index dfde3a939..8e3515824 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" @@ -28,8 +30,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 } @@ -255,3 +268,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 +} |