From 762148deb6be6925d17bd12f219f7385e1402439 Mon Sep 17 00:00:00 2001 From: Paul Holzinger Date: Mon, 15 Mar 2021 10:45:24 +0100 Subject: Split libpod/network package The `libpod/network` package should only be used on the backend and not the client. The client used this package only for two functions so move them into a new `pkg/network` package. This is needed so we can put linux only code into `libpod/network`, see #9710. [NO TESTS NEEDED] Signed-off-by: Paul Holzinger --- libpod/network/files.go | 13 ++----------- libpod/network/netconflist.go | 7 ++++--- libpod/network/network.go | 9 --------- libpod/runtime_ctr.go | 3 +-- libpod/runtime_ctr_network.go | 12 ++++++++++++ libpod/runtime_ctr_network_unsupported.go | 12 ++++++++++++ 6 files changed, 31 insertions(+), 25 deletions(-) create mode 100644 libpod/runtime_ctr_network.go create mode 100644 libpod/runtime_ctr_network_unsupported.go (limited to 'libpod') diff --git a/libpod/network/files.go b/libpod/network/files.go index fe483e25c..d876113f9 100644 --- a/libpod/network/files.go +++ b/libpod/network/files.go @@ -11,6 +11,7 @@ import ( "github.com/containernetworking/plugins/plugins/ipam/host-local/backend/allocator" "github.com/containers/common/pkg/config" "github.com/containers/podman/v3/libpod/define" + "github.com/containers/podman/v3/pkg/network" "github.com/pkg/errors" "github.com/sirupsen/logrus" ) @@ -67,7 +68,7 @@ func GetCNIConfigPathByNameOrID(config *config.Config, name string) (string, err if conf.Name == name { return confFile, nil } - if strings.HasPrefix(GetNetworkID(conf.Name), name) { + if strings.HasPrefix(network.GetNetworkID(conf.Name), name) { idMatch++ file = confFile } @@ -92,16 +93,6 @@ func ReadRawCNIConfByNameOrID(config *config.Config, name string) ([]byte, error return b, err } -// 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, ",") -} - // GetNetworkLabels returns a list of labels as a string func GetNetworkLabels(list *libcni.NetworkConfigList) NcLabels { cniJSON := make(map[string]interface{}) diff --git a/libpod/network/netconflist.go b/libpod/network/netconflist.go index 1a1583587..a45a4109a 100644 --- a/libpod/network/netconflist.go +++ b/libpod/network/netconflist.go @@ -7,6 +7,7 @@ import ( "strings" "github.com/containernetworking/cni/libcni" + "github.com/containers/podman/v3/pkg/network" "github.com/containers/podman/v3/pkg/util" "github.com/pkg/errors" ) @@ -211,7 +212,7 @@ func IfPassesFilter(netconf *libcni.NetworkConfigList, filters map[string][]stri case "plugin": // match one plugin - plugins := GetCNIPlugins(netconf) + plugins := network.GetCNIPlugins(netconf) for _, val := range filterValues { if strings.Contains(plugins, val) { result = true @@ -243,7 +244,7 @@ func IfPassesFilter(netconf *libcni.NetworkConfigList, filters map[string][]stri case "driver": // matches only for the DefaultNetworkDriver for _, filterValue := range filterValues { - plugins := GetCNIPlugins(netconf) + plugins := network.GetCNIPlugins(netconf) if filterValue == DefaultNetworkDriver && strings.Contains(plugins, DefaultNetworkDriver) { result = true @@ -253,7 +254,7 @@ func IfPassesFilter(netconf *libcni.NetworkConfigList, filters map[string][]stri case "id": // matches part of one id for _, filterValue := range filterValues { - if strings.Contains(GetNetworkID(netconf.Name), filterValue) { + if strings.Contains(network.GetNetworkID(netconf.Name), filterValue) { result = true break } diff --git a/libpod/network/network.go b/libpod/network/network.go index f19a764ef..ed4e6388a 100644 --- a/libpod/network/network.go +++ b/libpod/network/network.go @@ -1,8 +1,6 @@ package network import ( - "crypto/sha256" - "encoding/hex" "encoding/json" "net" "os" @@ -245,13 +243,6 @@ func Exists(config *config.Config, name string) (bool, error) { return true, nil } -// GetNetworkID return the network ID for a given name. -// It is just the sha256 hash but this should be good enough. -func GetNetworkID(name string) string { - hash := sha256.Sum256([]byte(name)) - return hex.EncodeToString(hash[:]) -} - // PruneNetworks removes networks that are not being used and that is not the default // network. To keep proper fencing for imports, you must provide the used networks // to this function as a map. the key is meaningful in the map, the book is a no-op diff --git a/libpod/runtime_ctr.go b/libpod/runtime_ctr.go index 19690d79b..057313c82 100644 --- a/libpod/runtime_ctr.go +++ b/libpod/runtime_ctr.go @@ -12,7 +12,6 @@ import ( "github.com/containers/common/pkg/config" "github.com/containers/podman/v3/libpod/define" "github.com/containers/podman/v3/libpod/events" - "github.com/containers/podman/v3/libpod/network" "github.com/containers/podman/v3/libpod/shutdown" "github.com/containers/podman/v3/pkg/cgroups" "github.com/containers/podman/v3/pkg/domain/entities/reports" @@ -204,7 +203,7 @@ func (r *Runtime) setupContainer(ctx context.Context, ctr *Container) (_ *Contai if len(ctr.config.Networks) > 0 { netNames := make([]string, 0, len(ctr.config.Networks)) for _, nameOrID := range ctr.config.Networks { - netName, err := network.NormalizeName(r.config, nameOrID) + netName, err := normalizeNetworkName(r.config, nameOrID) if err != nil { return nil, err } diff --git a/libpod/runtime_ctr_network.go b/libpod/runtime_ctr_network.go new file mode 100644 index 000000000..51ed982e2 --- /dev/null +++ b/libpod/runtime_ctr_network.go @@ -0,0 +1,12 @@ +// +build linux + +package libpod + +import ( + "github.com/containers/common/pkg/config" + "github.com/containers/podman/v3/libpod/network" +) + +func normalizeNetworkName(config *config.Config, nameOrID string) (string, error) { + return network.NormalizeName(config, nameOrID) +} diff --git a/libpod/runtime_ctr_network_unsupported.go b/libpod/runtime_ctr_network_unsupported.go new file mode 100644 index 000000000..fb7e802ac --- /dev/null +++ b/libpod/runtime_ctr_network_unsupported.go @@ -0,0 +1,12 @@ +// +build !linux + +package libpod + +import ( + "github.com/containers/common/pkg/config" + "github.com/containers/podman/v3/libpod/define" +) + +func normalizeNetworkName(config *config.Config, nameOrID string) (string, error) { + return "", define.ErrNotImplemented +} -- cgit v1.2.3-54-g00ecf