mirror of https://github.com/containers/podman.git
Merge pull request #6161 from kunalkushwaha/network-inspect
`--format` and `--filter` options for `network ls` and `network inspect` command
This commit is contained in:
commit
588df90397
|
@ -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"
|
||||
|
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
@ -1024,6 +1024,8 @@ _podman_network_inspect() {
|
|||
local boolean_options="
|
||||
--help
|
||||
-h
|
||||
--format
|
||||
-f
|
||||
"
|
||||
_complete_ "$options_with_args" "$boolean_options"
|
||||
|
||||
|
@ -1042,6 +1044,9 @@ _podman_network_ls() {
|
|||
-h
|
||||
--quiet
|
||||
-q
|
||||
--format
|
||||
-f
|
||||
-- filter
|
||||
"
|
||||
_complete_ "$options_with_args" "$boolean_options"
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
@ -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})
|
||||
|
|
Loading…
Reference in New Issue