Skip to content

Commit

Permalink
Split libpod/network package
Browse files Browse the repository at this point in the history
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 containers#9710.

[NO TESTS NEEDED]

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Paul Holzinger committed Mar 15, 2021
1 parent fc02d16 commit 762148d
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 29 deletions.
2 changes: 1 addition & 1 deletion cmd/podman/networks/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/containers/podman/v3/cmd/podman/common"
"github.com/containers/podman/v3/cmd/podman/registry"
"github.com/containers/podman/v3/cmd/podman/validate"
"github.com/containers/podman/v3/libpod/network"
"github.com/containers/podman/v3/pkg/domain/entities"
"github.com/containers/podman/v3/pkg/network"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
Expand Down
13 changes: 2 additions & 11 deletions libpod/network/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand All @@ -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{})
Expand Down
7 changes: 4 additions & 3 deletions libpod/network/netconflist.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
}
Expand Down
9 changes: 0 additions & 9 deletions libpod/network/network.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package network

import (
"crypto/sha256"
"encoding/hex"
"encoding/json"
"net"
"os"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions libpod/runtime_ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions libpod/runtime_ctr_network.go
Original file line number Diff line number Diff line change
@@ -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)
}
12 changes: 12 additions & 0 deletions libpod/runtime_ctr_network_unsupported.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 2 additions & 1 deletion pkg/api/handlers/compat/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/containers/podman/v3/pkg/api/handlers/utils"
"github.com/containers/podman/v3/pkg/domain/entities"
"github.com/containers/podman/v3/pkg/domain/infra/abi"
networkid "github.com/containers/podman/v3/pkg/network"
"github.com/docker/docker/api/types"
dockerNetwork "github.com/docker/docker/api/types/network"
"github.com/gorilla/schema"
Expand Down Expand Up @@ -135,7 +136,7 @@ func getNetworkResourceByNameOrID(nameOrID string, runtime *libpod.Runtime, filt

report := types.NetworkResource{
Name: conf.Name,
ID: network.GetNetworkID(conf.Name),
ID: networkid.GetNetworkID(conf.Name),
Created: time.Unix(int64(stat.Ctim.Sec), int64(stat.Ctim.Nsec)), // nolint: unconvert
Scope: "local",
Driver: network.DefaultNetworkDriver,
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/filters/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/containers/podman/v3/libpod"
"github.com/containers/podman/v3/libpod/define"
"github.com/containers/podman/v3/libpod/network"
"github.com/containers/podman/v3/pkg/network"
"github.com/containers/podman/v3/pkg/timetype"
"github.com/containers/podman/v3/pkg/util"
"github.com/pkg/errors"
Expand Down
2 changes: 1 addition & 1 deletion pkg/domain/filters/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (

"github.com/containers/podman/v3/libpod"
"github.com/containers/podman/v3/libpod/define"
"github.com/containers/podman/v3/libpod/network"
"github.com/containers/podman/v3/pkg/network"
"github.com/containers/podman/v3/pkg/util"
"github.com/pkg/errors"
)
Expand Down
27 changes: 27 additions & 0 deletions pkg/network/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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[:])
}

0 comments on commit 762148d

Please sign in to comment.