diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2021-02-25 06:03:54 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-25 06:03:54 +0100 |
commit | 79e803203239d6f5840b18924e706325272e773d (patch) | |
tree | 74d3fbca2c05a1809a0ab14b426e734ff57dd043 | |
parent | 25d81955ec959d0cfa2958edba0935d1257dd787 (diff) | |
parent | ef549235eb63117d10537e6d54adf448e05b40ad (diff) | |
download | podman-79e803203239d6f5840b18924e706325272e773d.tar.gz podman-79e803203239d6f5840b18924e706325272e773d.tar.bz2 podman-79e803203239d6f5840b18924e706325272e773d.zip |
Merge pull request #8010 from ParkerVR/format-networks/list
networks/list.go updates for --format
-rw-r--r-- | cmd/podman/networks/list.go | 67 | ||||
-rw-r--r-- | cmd/podman/networks/network.go | 3 | ||||
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 4 | ||||
-rw-r--r-- | test/e2e/network_test.go | 7 | ||||
-rw-r--r-- | vendor/github.com/containers/common/pkg/config/containers.conf | 6 | ||||
-rw-r--r-- | vendor/github.com/containers/common/pkg/config/default.go | 2 | ||||
-rw-r--r-- | vendor/github.com/containers/common/pkg/report/template.go | 16 | ||||
-rw-r--r-- | vendor/github.com/containers/common/version/version.go | 2 | ||||
-rw-r--r-- | vendor/modules.txt | 2 |
10 files changed, 77 insertions, 34 deletions
diff --git a/cmd/podman/networks/list.go b/cmd/podman/networks/list.go index 6d8d35589..2181f850b 100644 --- a/cmd/podman/networks/list.go +++ b/cmd/podman/networks/list.go @@ -1,7 +1,6 @@ package network import ( - "encoding/json" "fmt" "os" "strings" @@ -11,7 +10,6 @@ import ( "github.com/containers/common/pkg/completion" "github.com/containers/common/pkg/report" "github.com/containers/podman/v3/cmd/podman/common" - "github.com/containers/podman/v3/cmd/podman/parse" "github.com/containers/podman/v3/cmd/podman/registry" "github.com/containers/podman/v3/cmd/podman/validate" "github.com/containers/podman/v3/libpod/network" @@ -78,13 +76,38 @@ func networkList(cmd *cobra.Command, args []string) error { } switch { - case report.IsJSON(networkListOptions.Format): - return jsonOut(responses) + // quiet means we only print the network names case networkListOptions.Quiet: - // quiet means we only print the network names - return quietOut(responses) + quietOut(responses) + + // JSON output formatting + case report.IsJSON(networkListOptions.Format): + err = jsonOut(responses) + + // table or other format output + default: + err = templateOut(responses, cmd) + } + + return err +} + +func quietOut(responses []*entities.NetworkListReport) { + for _, r := range responses { + fmt.Println(r.Name) + } +} + +func jsonOut(responses []*entities.NetworkListReport) error { + prettyJSON, err := json.MarshalIndent(responses, "", " ") + if err != nil { + return err } + fmt.Println(string(prettyJSON)) + return nil +} +func templateOut(responses []*entities.NetworkListReport, cmd *cobra.Command) error { nlprs := make([]ListPrintReports, 0, len(responses)) for _, r := range responses { nlprs = append(nlprs, ListPrintReports{r}) @@ -99,13 +122,16 @@ func networkList(cmd *cobra.Command, args []string) error { "Labels": "labels", "ID": "network id", }) - renderHeaders := true - row := "{{.Name}}\t{{.Version}}\t{{.Plugins}}\n" + + renderHeaders := report.HasTable(networkListOptions.Format) + var row, format string if cmd.Flags().Changed("format") { - renderHeaders = parse.HasTable(networkListOptions.Format) row = report.NormalizeFormat(networkListOptions.Format) + } else { // 'podman network ls' equivalent to 'podman network ls --format="table {{.ID}} {{.Name}} {{.Version}} {{.Plugins}}" ' + renderHeaders = true + row = "{{.ID}}\t{{.Name}}\t{{.Version}}\t{{.Plugins}}\n" } - format := parse.EnforceRange(row) + format = report.EnforceRange(row) tmpl, err := template.New("listNetworks").Parse(format) if err != nil { @@ -122,34 +148,22 @@ func networkList(cmd *cobra.Command, args []string) error { return tmpl.Execute(w, nlprs) } -func quietOut(responses []*entities.NetworkListReport) error { - for _, r := range responses { - fmt.Println(r.Name) - } - return nil -} - -func jsonOut(responses []*entities.NetworkListReport) error { - b, err := json.MarshalIndent(responses, "", " ") - if err != nil { - return err - } - fmt.Println(string(b)) - return nil -} - +// ListPrintReports returns the network list report type ListPrintReports struct { *entities.NetworkListReport } +// Version returns the CNI version func (n ListPrintReports) Version() string { return n.CNIVersion } +// Plugins returns the CNI Plugins func (n ListPrintReports) Plugins() string { return network.GetCNIPlugins(n.NetworkConfigList) } +// Labels returns any labels added to a Network func (n ListPrintReports) Labels() string { list := make([]string, 0, len(n.NetworkListReport.Labels)) for k, v := range n.NetworkListReport.Labels { @@ -158,6 +172,7 @@ func (n ListPrintReports) Labels() string { return strings.Join(list, ",") } +// ID returns the Podman Network ID func (n ListPrintReports) ID() string { length := 12 if noTrunc { diff --git a/cmd/podman/networks/network.go b/cmd/podman/networks/network.go index e729f35f3..4d6cd8abd 100644 --- a/cmd/podman/networks/network.go +++ b/cmd/podman/networks/network.go @@ -8,6 +8,9 @@ import ( ) var ( + // Pull in configured json library + json = registry.JSONLibrary() + // Command: podman _network_ networkCmd = &cobra.Command{ Use: "network", @@ -11,7 +11,7 @@ require ( github.com/containernetworking/cni v0.8.1 github.com/containernetworking/plugins v0.9.0 github.com/containers/buildah v1.19.6 - github.com/containers/common v0.34.3-0.20210208115708-8668c76dd577 + github.com/containers/common v0.35.0 github.com/containers/conmon v2.0.20+incompatible github.com/containers/image/v5 v5.10.2 github.com/containers/ocicrypt v1.1.0 @@ -99,8 +99,8 @@ github.com/containernetworking/plugins v0.9.0/go.mod h1:dbWv4dI0QrBGuVgj+TuVQ6wJ github.com/containers/buildah v1.19.6 h1:8mPysB7QzHxX9okR+Bwq/lsKAZA/FjDcqB+vebgwI1g= github.com/containers/buildah v1.19.6/go.mod h1:VnyHWgNmfR1d89/zJ/F4cbwOzaQS+6sBky46W7dCo3E= github.com/containers/common v0.33.4/go.mod h1:PhgL71XuC4jJ/1BIqeP7doke3aMFkCP90YBXwDeUr9g= -github.com/containers/common v0.34.3-0.20210208115708-8668c76dd577 h1:tUJcLouJ1bC3w9gdqgKqZBsj2uCuM8D8jSR592lxbhE= -github.com/containers/common v0.34.3-0.20210208115708-8668c76dd577/go.mod h1:mwZ9H8sK4+dtWxsnVLyWcjxK/gEQClrLsXsqLvbEKbI= +github.com/containers/common v0.35.0 h1:1OLZ2v+Tj/CN9BTQkKZ5VOriOiArJedinMMqfJRUI38= +github.com/containers/common v0.35.0/go.mod h1:gs1th7XFTOvVUl4LDPdQjOfOeNiVRDbQ7CNrZ0wS6F8= github.com/containers/conmon v2.0.20+incompatible h1:YbCVSFSCqFjjVwHTPINGdMX1F6JXHGTUje2ZYobNrkg= github.com/containers/conmon v2.0.20+incompatible/go.mod h1:hgwZ2mtuDrppv78a/cOBNiCm6O0UMWGx1mu7P00nu5I= github.com/containers/image/v5 v5.10.1 h1:tHhGQ8RCMxJfJLD/PEW1qrOKX8nndledW9qz6UiAxns= diff --git a/test/e2e/network_test.go b/test/e2e/network_test.go index a7eb6e629..53521cdc4 100644 --- a/test/e2e/network_test.go +++ b/test/e2e/network_test.go @@ -150,6 +150,13 @@ var _ = Describe("Podman network", func() { defer podmanTest.removeCNINetwork(net) Expect(session.ExitCode()).To(BeZero()) + // Tests Default Table Output + session = podmanTest.Podman([]string{"network", "ls", "--filter", "id=" + netID}) + session.WaitWithDefaultTimeout() + Expect(session.ExitCode()).To(BeZero()) + expectedTable := "NETWORK ID NAME VERSION PLUGINS" + Expect(session.OutputToString()).To(ContainSubstring(expectedTable)) + session = podmanTest.Podman([]string{"network", "ls", "--format", "{{.Name}} {{.ID}}", "--filter", "id=" + netID}) session.WaitWithDefaultTimeout() Expect(session.ExitCode()).To(BeZero()) diff --git a/vendor/github.com/containers/common/pkg/config/containers.conf b/vendor/github.com/containers/common/pkg/config/containers.conf index 18243f296..0114f2975 100644 --- a/vendor/github.com/containers/common/pkg/config/containers.conf +++ b/vendor/github.com/containers/common/pkg/config/containers.conf @@ -73,7 +73,6 @@ default_capabilities = [ "SYS_CHROOT" ] - # A list of sysctls to be set in containers by default, # specified as "name=value", # for example:"net.ipv4.ping_group_range = 0 0". @@ -241,6 +240,9 @@ default_sysctls = [ # # cni_plugin_dirs = ["/usr/libexec/cni"] +# The network name of the default CNI network to attach pods to. +# default_network = "podman" + # Path to the directory where CNI configuration files are located. # # network_config_dir = "/etc/cni/net.d/" @@ -324,7 +326,7 @@ default_sysctls = [ # associated with the pod. This container does nothing other then sleep, # reserving the pods resources for the lifetime of the pod. # -# infra_image = "k8s.gcr.io/pause:3.2" +# infra_image = "k8s.gcr.io/pause:3.4.1" # Specify the locking mechanism to use; valid values are "shm" and "file". # Change the default only if you are sure of what you are doing, in general diff --git a/vendor/github.com/containers/common/pkg/config/default.go b/vendor/github.com/containers/common/pkg/config/default.go index 918ce93e5..57f64c395 100644 --- a/vendor/github.com/containers/common/pkg/config/default.go +++ b/vendor/github.com/containers/common/pkg/config/default.go @@ -45,7 +45,7 @@ var ( // DefaultInitPath is the default path to the container-init binary DefaultInitPath = "/usr/libexec/podman/catatonit" // DefaultInfraImage to use for infra container - DefaultInfraImage = "k8s.gcr.io/pause:3.2" + DefaultInfraImage = "k8s.gcr.io/pause:3.4.1" // DefaultRootlessSHMLockPath is the default path for rootless SHM locks DefaultRootlessSHMLockPath = "/libpod_rootless_lock" // DefaultDetachKeys is the default keys sequence for detaching a diff --git a/vendor/github.com/containers/common/pkg/report/template.go b/vendor/github.com/containers/common/pkg/report/template.go index 559c1625b..f7b4506bb 100644 --- a/vendor/github.com/containers/common/pkg/report/template.go +++ b/vendor/github.com/containers/common/pkg/report/template.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "reflect" + "regexp" "strings" "text/template" @@ -155,3 +156,18 @@ func (t *Template) Funcs(funcMap FuncMap) *Template { func (t *Template) IsTable() bool { return t.isTable } + +var rangeRegex = regexp.MustCompile(`{{\s*range\s*\.\s*}}.*{{\s*end\s*}}`) + +// EnforceRange ensures that the format string contains a range +func EnforceRange(format string) string { + if !rangeRegex.MatchString(format) { + return "{{range .}}" + format + "{{end}}" + } + return format +} + +// HasTable returns whether the format is a table +func HasTable(format string) bool { + return strings.HasPrefix(format, "table ") +} diff --git a/vendor/github.com/containers/common/version/version.go b/vendor/github.com/containers/common/version/version.go index 8efc8b8a2..ff95a6522 100644 --- a/vendor/github.com/containers/common/version/version.go +++ b/vendor/github.com/containers/common/version/version.go @@ -1,4 +1,4 @@ package version // Version is the version of the build. -const Version = "0.34.3-dev" +const Version = "0.35.0" diff --git a/vendor/modules.txt b/vendor/modules.txt index c323f09fc..632ef3f87 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -89,7 +89,7 @@ github.com/containers/buildah/pkg/parse github.com/containers/buildah/pkg/rusage github.com/containers/buildah/pkg/supplemented github.com/containers/buildah/util -# github.com/containers/common v0.34.3-0.20210208115708-8668c76dd577 +# github.com/containers/common v0.35.0 github.com/containers/common/pkg/apparmor github.com/containers/common/pkg/apparmor/internal/supported github.com/containers/common/pkg/auth |