Merge pull request #12595 from Luap99/network-id

fix network id handling
This commit is contained in:
OpenShift Merge Robot
2021-12-14 20:56:55 +01:00
committed by GitHub
5 changed files with 26 additions and 47 deletions

View File

@ -13,7 +13,6 @@ import (
"github.com/containers/podman/v3/libpod/define" "github.com/containers/podman/v3/libpod/define"
"github.com/containers/podman/v3/libpod/network/types" "github.com/containers/podman/v3/libpod/network/types"
"github.com/containers/podman/v3/pkg/domain/entities" "github.com/containers/podman/v3/pkg/domain/entities"
"github.com/containers/podman/v3/pkg/network"
"github.com/containers/podman/v3/pkg/rootless" "github.com/containers/podman/v3/pkg/rootless"
systemdDefine "github.com/containers/podman/v3/pkg/systemd/define" systemdDefine "github.com/containers/podman/v3/pkg/systemd/define"
"github.com/containers/podman/v3/pkg/util" "github.com/containers/podman/v3/pkg/util"
@ -262,12 +261,11 @@ func getNetworks(cmd *cobra.Command, toComplete string, cType completeType) ([]s
} }
for _, n := range networks { for _, n := range networks {
id := network.GetNetworkID(n.Name)
// include ids in suggestions if cType == completeIDs or // include ids in suggestions if cType == completeIDs or
// more then 2 chars are typed and cType == completeDefault // more then 2 chars are typed and cType == completeDefault
if ((len(toComplete) > 1 && cType == completeDefault) || if ((len(toComplete) > 1 && cType == completeDefault) ||
cType == completeIDs) && strings.HasPrefix(id, toComplete) { cType == completeIDs) && strings.HasPrefix(n.ID, toComplete) {
suggestions = append(suggestions, id[0:12]) suggestions = append(suggestions, n.ID[0:12])
} }
// include name in suggestions // include name in suggestions
if cType != completeIDs && strings.HasPrefix(n.Name, toComplete) { if cType != completeIDs && strings.HasPrefix(n.Name, toComplete) {

View File

@ -8,7 +8,6 @@ import (
"github.com/containers/podman/v3/libpod" "github.com/containers/podman/v3/libpod"
"github.com/containers/podman/v3/libpod/define" "github.com/containers/podman/v3/libpod/define"
"github.com/containers/podman/v3/pkg/network"
"github.com/containers/podman/v3/pkg/util" "github.com/containers/podman/v3/pkg/util"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -210,6 +209,15 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
return false return false
}, nil }, nil
case "network": case "network":
var inputNetNames []string
for _, val := range filterValues {
net, err := r.Network().NetworkInspect(val)
if err != nil {
// ignore not found errors
break
}
inputNetNames = append(inputNetNames, net.Name)
}
return func(c *libpod.Container) bool { return func(c *libpod.Container) bool {
networkMode := c.NetworkMode() networkMode := c.NetworkMode()
// support docker like `--filter network=container:<IDorName>` // support docker like `--filter network=container:<IDorName>`
@ -247,14 +255,10 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
return false return false
} }
for _, net := range networks { for _, net := range networks {
netID := network.GetNetworkID(net) if util.StringInSlice(net, inputNetNames) {
for _, val := range filterValues {
// match by network name or id
if val == net || val == netID {
return true return true
} }
} }
}
return false return false
}, nil }, nil
case "restart-policy": case "restart-policy":

View File

@ -6,7 +6,6 @@ import (
"github.com/containers/podman/v3/libpod" "github.com/containers/podman/v3/libpod"
"github.com/containers/podman/v3/libpod/define" "github.com/containers/podman/v3/libpod/define"
"github.com/containers/podman/v3/pkg/network"
"github.com/containers/podman/v3/pkg/util" "github.com/containers/podman/v3/pkg/util"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -14,7 +13,7 @@ import (
// GeneratePodFilterFunc takes a filter and filtervalue (key, value) // GeneratePodFilterFunc takes a filter and filtervalue (key, value)
// and generates a libpod function that can be used to filter // and generates a libpod function that can be used to filter
// pods // pods
func GeneratePodFilterFunc(filter string, filterValues []string) ( func GeneratePodFilterFunc(filter string, filterValues []string, r *libpod.Runtime) (
func(pod *libpod.Pod) bool, error) { func(pod *libpod.Pod) bool, error) {
switch filter { switch filter {
case "ctr-ids": case "ctr-ids":
@ -128,6 +127,15 @@ func GeneratePodFilterFunc(filter string, filterValues []string) (
return false return false
}, nil }, nil
case "network": case "network":
var inputNetNames []string
for _, val := range filterValues {
net, err := r.Network().NetworkInspect(val)
if err != nil {
// ignore not found errors
break
}
inputNetNames = append(inputNetNames, net.Name)
}
return func(p *libpod.Pod) bool { return func(p *libpod.Pod) bool {
infra, err := p.InfraContainer() infra, err := p.InfraContainer()
// no infra, quick out // no infra, quick out
@ -140,14 +148,10 @@ func GeneratePodFilterFunc(filter string, filterValues []string) (
return false return false
} }
for _, net := range networks { for _, net := range networks {
netID := network.GetNetworkID(net) if util.StringInSlice(net, inputNetNames) {
for _, val := range filterValues {
// match by network name or id
if val == net || val == netID {
return true return true
} }
} }
}
return false return false
}, nil }, nil
} }

View File

@ -325,7 +325,7 @@ func (ic *ContainerEngine) PodPs(ctx context.Context, options entities.PodPSOpti
filters := make([]libpod.PodFilter, 0, len(options.Filters)) filters := make([]libpod.PodFilter, 0, len(options.Filters))
for k, v := range options.Filters { for k, v := range options.Filters {
f, err := dfilters.GeneratePodFilterFunc(k, v) f, err := dfilters.GeneratePodFilterFunc(k, v, ic.Libpod)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -1,27 +0,0 @@
package network
import (
"crypto/sha256"
"encoding/hex"
"strings"
"github.com/containernetworking/cni/libcni"
)
// GetCNIPlugins returns a list of plugins that a given network
// has in the form of a string
func GetCNIPlugins(list *libcni.NetworkConfigList) string {
plugins := make([]string, 0, len(list.Plugins))
for _, plug := range list.Plugins {
plugins = append(plugins, plug.Network.Type)
}
return strings.Join(plugins, ",")
}
// GetNetworkID return the network ID for a given name.
// It is just the sha256 hash but this should be good enough.
// The caller has to make sure it is only called with the network name.
func GetNetworkID(name string) string {
hash := sha256.Sum256([]byte(name))
return hex.EncodeToString(hash[:])
}