From 1dde2730e85706177c9a0e9078c26560a280f074 Mon Sep 17 00:00:00 2001 From: Kunal Kushwaha Date: Mon, 11 May 2020 07:25:47 +0000 Subject: 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 --- pkg/domain/infra/abi/network.go | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'pkg/domain/infra') 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 +} -- cgit v1.2.3-54-g00ecf