Skip to content

Commit

Permalink
Merge pull request #6161 from kunalkushwaha/network-inspect
Browse files Browse the repository at this point in the history
`--format` and `--filter` options for `network ls` and `network inspect` command
  • Loading branch information
openshift-merge-robot authored May 20, 2020
2 parents e8e5a5f + 087fdda commit 588df90
Show file tree
Hide file tree
Showing 8 changed files with 151 additions and 5 deletions.
28 changes: 27 additions & 1 deletion cmd/podman/networks/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ package network
import (
"encoding/json"
"fmt"
"html/template"
"io"
"os"
"strings"

"github.com/containers/libpod/cmd/podman/registry"
"github.com/containers/libpod/pkg/domain/entities"
Expand All @@ -24,12 +28,18 @@ var (
}
)

var (
networkInspectOptions entities.NetworkInspectOptions
)

func init() {
registry.Commands = append(registry.Commands, registry.CliCommand{
Mode: []entities.EngineMode{entities.ABIMode, entities.TunnelMode},
Command: networkinspectCommand,
Parent: networkCmd,
})
flags := networkinspectCommand.Flags()
flags.StringVarP(&networkInspectOptions.Format, "format", "f", "", "Pretty-print network to JSON or using a Go template")
}

func networkInspect(cmd *cobra.Command, args []string) error {
Expand All @@ -41,6 +51,22 @@ func networkInspect(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
fmt.Println(string(b))
if strings.ToLower(networkInspectOptions.Format) == "json" || networkInspectOptions.Format == "" {
fmt.Println(string(b))
} else {
var w io.Writer = os.Stdout
//There can be more than 1 in the inspect output.
format := "{{range . }}" + networkInspectOptions.Format + "{{end}}"
tmpl, err := template.New("inspectNetworks").Parse(format)
if err != nil {
return err
}
if err := tmpl.Execute(w, responses); err != nil {
return err
}
if flusher, ok := w.(interface{ Flush() error }); ok {
return flusher.Flush()
}
}
return nil
}
13 changes: 11 additions & 2 deletions cmd/podman/networks/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand All @@ -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)
}

Expand Down
5 changes: 5 additions & 0 deletions completions/bash/podman
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,8 @@ _podman_network_inspect() {
local boolean_options="
--help
-h
--format
-f
"
_complete_ "$options_with_args" "$boolean_options"

Expand All @@ -1042,6 +1044,9 @@ _podman_network_ls() {
-h
--quiet
-q
--format
-f
-- filter
"
_complete_ "$options_with_args" "$boolean_options"

Expand Down
14 changes: 14 additions & 0 deletions docs/source/markdown/podman-network-inspect.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@ podman\-network\-inspect - Displays the raw CNI network configuration for one or
## DESCRIPTION
Display the raw (JSON format) network configuration. This command is not available for rootless users.

## OPTIONS
**--quiet**, **-q**

The `quiet` option will restrict the output to only the network names.

**--format**, **-f**

Pretty-print networks to JSON or using a Go template.

## EXAMPLE

Inspect the default podman network
Expand Down Expand Up @@ -43,6 +52,11 @@ Inspect the default podman network
]
```

```
# podman network inspect podman --format '{{(index .plugins 0).ipam.ranges}}'
[[map[gateway:10.88.0.1 subnet:10.88.0.0/16]]]
```

## SEE ALSO
podman(1), podman-network(1), podman-network-ls(1)

Expand Down
18 changes: 17 additions & 1 deletion docs/source/markdown/podman-network-ls.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down
2 changes: 2 additions & 0 deletions pkg/domain/entities/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type NetworkListOptions struct {
Format string
Quiet bool
Filter string
}

// NetworkListReport describes the results from listing networks
Expand All @@ -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
Expand Down
37 changes: 36 additions & 1 deletion pkg/domain/infra/abi/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
39 changes: 39 additions & 0 deletions test/e2e/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,32 @@ var _ = Describe("Podman network", func() {
Expect(session.LineInOutputContains("podman-integrationtest")).To(BeTrue())
})

It("podman network list --filter success", func() {
// Setup, use uuid to prevent conflict with other tests
uuid := stringid.GenerateNonCryptoID()
secondPath := filepath.Join(cniPath, fmt.Sprintf("%s.conflist", uuid))
writeConf([]byte(secondConf), secondPath)
defer removeConf(secondPath)

session := podmanTest.Podman([]string{"network", "ls", "--filter", "plugin=bridge"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(session.LineInOutputContains("podman-integrationtest")).To(BeTrue())
})

It("podman network list --filter failure", func() {
// Setup, use uuid to prevent conflict with other tests
uuid := stringid.GenerateNonCryptoID()
secondPath := filepath.Join(cniPath, fmt.Sprintf("%s.conflist", uuid))
writeConf([]byte(secondConf), secondPath)
defer removeConf(secondPath)

session := podmanTest.Podman([]string{"network", "ls", "--filter", "plugin=test"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(session.LineInOutputContains("podman-integrationtest")).To(BeFalse())
})

It("podman network rm no args", func() {
session := podmanTest.Podman([]string{"network", "rm"})
session.WaitWithDefaultTimeout()
Expand Down Expand Up @@ -152,6 +178,19 @@ var _ = Describe("Podman network", func() {
Expect(session.IsJSONOutputValid()).To(BeTrue())
})

It("podman network inspect", func() {
// Setup, use uuid to prevent conflict with other tests
uuid := stringid.GenerateNonCryptoID()
secondPath := filepath.Join(cniPath, fmt.Sprintf("%s.conflist", uuid))
writeConf([]byte(secondConf), secondPath)
defer removeConf(secondPath)

session := podmanTest.Podman([]string{"network", "inspect", "podman-integrationtest", "--format", "{{.cniVersion}}"})
session.WaitWithDefaultTimeout()
Expect(session.ExitCode()).To(Equal(0))
Expect(session.LineInOutputContains("0.3.0")).To(BeTrue())
})

It("podman inspect container single CNI network", func() {
netName := "testNetSingleCNI"
network := podmanTest.Podman([]string{"network", "create", "--subnet", "10.50.50.0/24", netName})
Expand Down

0 comments on commit 588df90

Please sign in to comment.