aboutsummaryrefslogtreecommitdiff
path: root/pkg/domain
diff options
context:
space:
mode:
authorPaul Holzinger <pholzing@redhat.com>2021-08-16 16:11:26 +0200
committerPaul Holzinger <pholzing@redhat.com>2021-09-15 20:00:20 +0200
commit85e8fbf7f33717ef6a0d6cf9e2143b52c874c2de (patch)
tree82b0c29102d2779c18ea8a6f10df5dc1139e3817 /pkg/domain
parent218f132fdf4939d9e0374ef860d534f19e71df54 (diff)
downloadpodman-85e8fbf7f33717ef6a0d6cf9e2143b52c874c2de.tar.gz
podman-85e8fbf7f33717ef6a0d6cf9e2143b52c874c2de.tar.bz2
podman-85e8fbf7f33717ef6a0d6cf9e2143b52c874c2de.zip
Wire network interface into libpod
Make use of the new network interface in libpod. This commit contains several breaking changes: - podman network create only outputs the new network name and not file path. - podman network ls shows the network driver instead of the cni version and plugins. - podman network inspect outputs the new network struct and not the cni conflist. - The bindings and libpod api endpoints have been changed to use the new network structure. The container network status is stored in a new field in the state. The status should be received with the new `c.getNetworkStatus`. This will migrate the old status to the new format. Therefore old containers should contine to work correctly in all cases even when network connect/ disconnect is used. New features: - podman network reload keeps the ip and mac for more than one network. - podman container restore keeps the ip and mac for more than one network. - The network create compat endpoint can now use more than one ipam config. The man pages and the swagger doc are updated to reflect the latest changes. Signed-off-by: Paul Holzinger <pholzing@redhat.com>
Diffstat (limited to 'pkg/domain')
-rw-r--r--pkg/domain/entities/engine_container.go7
-rw-r--r--pkg/domain/entities/network.go14
-rw-r--r--pkg/domain/infra/abi/network.go122
-rw-r--r--pkg/domain/infra/tunnel/network.go23
4 files changed, 68 insertions, 98 deletions
diff --git a/pkg/domain/entities/engine_container.go b/pkg/domain/entities/engine_container.go
index 3da31d8a0..b916d6fc6 100644
--- a/pkg/domain/entities/engine_container.go
+++ b/pkg/domain/entities/engine_container.go
@@ -6,6 +6,7 @@ import (
"github.com/containers/common/pkg/config"
"github.com/containers/podman/v3/libpod/define"
+ "github.com/containers/podman/v3/libpod/network/types"
"github.com/containers/podman/v3/pkg/domain/entities/reports"
"github.com/containers/podman/v3/pkg/specgen"
)
@@ -58,11 +59,11 @@ type ContainerEngine interface {
HealthCheckRun(ctx context.Context, nameOrID string, options HealthCheckOptions) (*define.HealthCheckResults, error)
Info(ctx context.Context) (*define.Info, error)
NetworkConnect(ctx context.Context, networkname string, options NetworkConnectOptions) error
- NetworkCreate(ctx context.Context, name string, options NetworkCreateOptions) (*NetworkCreateReport, error)
+ NetworkCreate(ctx context.Context, network types.Network) (*NetworkCreateReport, error)
NetworkDisconnect(ctx context.Context, networkname string, options NetworkDisconnectOptions) error
NetworkExists(ctx context.Context, networkname string) (*BoolReport, error)
- NetworkInspect(ctx context.Context, namesOrIds []string, options InspectOptions) ([]NetworkInspectReport, []error, error)
- NetworkList(ctx context.Context, options NetworkListOptions) ([]*NetworkListReport, error)
+ NetworkInspect(ctx context.Context, namesOrIds []string, options InspectOptions) ([]types.Network, []error, error)
+ NetworkList(ctx context.Context, options NetworkListOptions) ([]types.Network, error)
NetworkPrune(ctx context.Context, options NetworkPruneOptions) ([]*NetworkPruneReport, error)
NetworkReload(ctx context.Context, names []string, options NetworkReloadOptions) ([]*NetworkReloadReport, error)
NetworkRm(ctx context.Context, namesOrIds []string, options NetworkRmOptions) ([]*NetworkRmReport, error)
diff --git a/pkg/domain/entities/network.go b/pkg/domain/entities/network.go
index a89501664..b61297d41 100644
--- a/pkg/domain/entities/network.go
+++ b/pkg/domain/entities/network.go
@@ -2,8 +2,6 @@ package entities
import (
"net"
-
- "github.com/containernetworking/cni/libcni"
)
// NetworkListOptions describes options for listing networks in cli
@@ -13,15 +11,6 @@ type NetworkListOptions struct {
Filters map[string][]string
}
-// NetworkListReport describes the results from listing networks
-type NetworkListReport struct {
- *libcni.NetworkConfigList
- Labels map[string]string
-}
-
-// NetworkInspectReport describes the results from inspect networks
-type NetworkInspectReport map[string]interface{}
-
// NetworkReloadOptions describes options for reloading container network
// configuration.
type NetworkReloadOptions struct {
@@ -48,7 +37,6 @@ type NetworkRmReport struct {
}
// NetworkCreateOptions describes options to create a network
-// swagger:model NetworkCreateOptions
type NetworkCreateOptions struct {
DisableDNS bool
Driver string
@@ -65,7 +53,7 @@ type NetworkCreateOptions struct {
// NetworkCreateReport describes a created network for the cli
type NetworkCreateReport struct {
- Filename string
+ Name string
}
// NetworkDisconnectOptions describes options for disconnecting
diff --git a/pkg/domain/infra/abi/network.go b/pkg/domain/infra/abi/network.go
index 7900caaa6..45d2c6925 100644
--- a/pkg/domain/infra/abi/network.go
+++ b/pkg/domain/infra/abi/network.go
@@ -4,60 +4,38 @@ import (
"context"
"github.com/containers/podman/v3/libpod/define"
- "github.com/containers/podman/v3/libpod/network"
+ "github.com/containers/podman/v3/libpod/network/types"
+ netutil "github.com/containers/podman/v3/libpod/network/util"
"github.com/containers/podman/v3/pkg/domain/entities"
"github.com/containers/podman/v3/pkg/util"
"github.com/pkg/errors"
)
-func (ic *ContainerEngine) NetworkList(ctx context.Context, options entities.NetworkListOptions) ([]*entities.NetworkListReport, error) {
- reports := make([]*entities.NetworkListReport, 0)
-
- config, err := ic.Libpod.GetConfig()
- if err != nil {
- return nil, err
- }
-
- networks, err := network.LoadCNIConfsFromDir(network.GetCNIConfDir(config))
+func (ic *ContainerEngine) NetworkList(ctx context.Context, options entities.NetworkListOptions) ([]types.Network, error) {
+ filters, err := netutil.GenerateNetworkFilters(options.Filters)
if err != nil {
return nil, err
}
-
- for _, n := range networks {
- ok, err := network.IfPassesFilter(n, options.Filters)
- if err != nil {
- return nil, err
- }
- if ok {
- reports = append(reports, &entities.NetworkListReport{
- NetworkConfigList: n,
- Labels: network.GetNetworkLabels(n),
- })
- }
- }
- return reports, nil
+ nets, err := ic.Libpod.Network().NetworkList(filters...)
+ return nets, err
}
-func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []string, options entities.InspectOptions) ([]entities.NetworkInspectReport, []error, error) {
- config, err := ic.Libpod.GetConfig()
- if err != nil {
- return nil, nil, err
- }
+func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []string, options entities.InspectOptions) ([]types.Network, []error, error) {
var errs []error
- rawCNINetworks := make([]entities.NetworkInspectReport, 0, len(namesOrIds))
+ networks := make([]types.Network, 0, len(namesOrIds))
for _, name := range namesOrIds {
- rawList, err := network.InspectNetwork(config, name)
+ net, err := ic.Libpod.Network().NetworkInspect(name)
if err != nil {
if errors.Cause(err) == define.ErrNoSuchNetwork {
- errs = append(errs, errors.Errorf("no such network %s", name))
+ errs = append(errs, errors.Wrapf(err, "network %s", name))
continue
} else {
return nil, nil, errors.Wrapf(err, "error inspecting network %s", name)
}
}
- rawCNINetworks = append(rawCNINetworks, rawList)
+ networks = append(networks, net)
}
- return rawCNINetworks, errs, nil
+ return networks, errs, nil
}
func (ic *ContainerEngine) NetworkReload(ctx context.Context, names []string, options entities.NetworkReloadOptions) ([]*entities.NetworkReloadReport, error) {
@@ -83,12 +61,7 @@ func (ic *ContainerEngine) NetworkReload(ctx context.Context, names []string, op
}
func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, options entities.NetworkRmOptions) ([]*entities.NetworkRmReport, error) {
- reports := []*entities.NetworkRmReport{}
-
- config, err := ic.Libpod.GetConfig()
- if err != nil {
- return nil, err
- }
+ reports := make([]*entities.NetworkRmReport, 0, len(namesOrIds))
for _, name := range namesOrIds {
report := entities.NetworkRmReport{Name: name}
@@ -126,7 +99,7 @@ func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, o
}
}
}
- if err := network.RemoveNetwork(config, name); err != nil {
+ if err := ic.Libpod.Network().NetworkRemove(name); err != nil {
report.Err = err
}
reports = append(reports, &report)
@@ -134,12 +107,12 @@ func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, o
return reports, nil
}
-func (ic *ContainerEngine) NetworkCreate(ctx context.Context, name string, options entities.NetworkCreateOptions) (*entities.NetworkCreateReport, error) {
- runtimeConfig, err := ic.Libpod.GetConfig()
+func (ic *ContainerEngine) NetworkCreate(ctx context.Context, network types.Network) (*entities.NetworkCreateReport, error) {
+ network, err := ic.Libpod.Network().NetworkCreate(network)
if err != nil {
return nil, err
}
- return network.Create(name, options, runtimeConfig)
+ return &entities.NetworkCreateReport{Name: network.Name}, nil
}
// NetworkDisconnect removes a container from a given network
@@ -153,12 +126,12 @@ func (ic *ContainerEngine) NetworkConnect(ctx context.Context, networkname strin
// NetworkExists checks if the given network exists
func (ic *ContainerEngine) NetworkExists(ctx context.Context, networkname string) (*entities.BoolReport, error) {
- config, err := ic.Libpod.GetConfig()
- if err != nil {
- return nil, err
- }
- exists, err := network.Exists(config, networkname)
- if err != nil {
+ _, err := ic.Libpod.Network().NetworkInspect(networkname)
+ exists := true
+ // if err is ErrNoSuchNetwork do not return it
+ if errors.Is(err, define.ErrNoSuchNetwork) {
+ exists = false
+ } else if err != nil {
return nil, err
}
return &entities.BoolReport{
@@ -168,19 +141,10 @@ func (ic *ContainerEngine) NetworkExists(ctx context.Context, networkname string
// Network prune removes unused cni networks
func (ic *ContainerEngine) NetworkPrune(ctx context.Context, options entities.NetworkPruneOptions) ([]*entities.NetworkPruneReport, error) {
- runtimeConfig, err := ic.Libpod.GetConfig()
- if err != nil {
- return nil, err
- }
cons, err := ic.Libpod.GetAllContainers()
if err != nil {
return nil, err
}
- networks, err := network.LoadCNIConfsFromDir(network.GetCNIConfDir(runtimeConfig))
- if err != nil {
- return nil, err
- }
-
// Gather up all the non-default networks that the
// containers want
networksToKeep := make(map[string]bool)
@@ -193,20 +157,34 @@ func (ic *ContainerEngine) NetworkPrune(ctx context.Context, options entities.Ne
networksToKeep[n] = true
}
}
- if len(options.Filters) != 0 {
- for _, n := range networks {
- // This network will be kept anyway
- if _, found := networksToKeep[n.Name]; found {
- continue
- }
- ok, err := network.IfPassesPruneFilter(runtimeConfig, n, options.Filters)
- if err != nil {
- return nil, err
- }
- if !ok {
- networksToKeep[n.Name] = true
+ // ignore the default network, this one cannot be deleted
+ networksToKeep[ic.Libpod.GetDefaultNetworkName()] = true
+
+ // get all filters
+ filters, err := netutil.GenerateNetworkPruneFilters(options.Filters)
+ if err != nil {
+ return nil, err
+ }
+ danglingFilterFunc := func(net types.Network) bool {
+ for network := range networksToKeep {
+ if network == net.Name {
+ return false
}
}
+ return true
+ }
+ filters = append(filters, danglingFilterFunc)
+ nets, err := ic.Libpod.Network().NetworkList(filters...)
+ if err != nil {
+ return nil, err
+ }
+
+ pruneReport := make([]*entities.NetworkPruneReport, 0, len(nets))
+ for _, net := range nets {
+ pruneReport = append(pruneReport, &entities.NetworkPruneReport{
+ Name: net.Name,
+ Error: ic.Libpod.Network().NetworkRemove(net.Name),
+ })
}
- return network.PruneNetworks(runtimeConfig, networksToKeep)
+ return pruneReport, nil
}
diff --git a/pkg/domain/infra/tunnel/network.go b/pkg/domain/infra/tunnel/network.go
index 7e59e44c2..711c2e00c 100644
--- a/pkg/domain/infra/tunnel/network.go
+++ b/pkg/domain/infra/tunnel/network.go
@@ -3,20 +3,22 @@ package tunnel
import (
"context"
+ "github.com/containers/podman/v3/libpod/define"
+ "github.com/containers/podman/v3/libpod/network/types"
"github.com/containers/podman/v3/pkg/bindings/network"
"github.com/containers/podman/v3/pkg/domain/entities"
"github.com/containers/podman/v3/pkg/errorhandling"
"github.com/pkg/errors"
)
-func (ic *ContainerEngine) NetworkList(ctx context.Context, opts entities.NetworkListOptions) ([]*entities.NetworkListReport, error) {
+func (ic *ContainerEngine) NetworkList(ctx context.Context, opts entities.NetworkListOptions) ([]types.Network, error) {
options := new(network.ListOptions).WithFilters(opts.Filters)
return network.List(ic.ClientCtx, options)
}
-func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []string, opts entities.InspectOptions) ([]entities.NetworkInspectReport, []error, error) {
+func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []string, opts entities.InspectOptions) ([]types.Network, []error, error) {
var (
- reports = make([]entities.NetworkInspectReport, 0, len(namesOrIds))
+ reports = make([]types.Network, 0, len(namesOrIds))
errs = []error{}
)
options := new(network.InspectOptions)
@@ -28,12 +30,12 @@ func (ic *ContainerEngine) NetworkInspect(ctx context.Context, namesOrIds []stri
return nil, nil, err
}
if errModel.ResponseCode == 404 {
- errs = append(errs, errors.Errorf("no such network %q", name))
+ errs = append(errs, errors.Wrapf(define.ErrNoSuchNetwork, "network %s", name))
continue
}
return nil, nil, err
}
- reports = append(reports, report...)
+ reports = append(reports, report)
}
return reports, errs, nil
}
@@ -60,11 +62,12 @@ func (ic *ContainerEngine) NetworkRm(ctx context.Context, namesOrIds []string, o
return reports, nil
}
-func (ic *ContainerEngine) NetworkCreate(ctx context.Context, name string, opts entities.NetworkCreateOptions) (*entities.NetworkCreateReport, error) {
- options := new(network.CreateOptions).WithName(name).WithDisableDNS(opts.DisableDNS).WithDriver(opts.Driver).WithGateway(opts.Gateway)
- options.WithInternal(opts.Internal).WithIPRange(opts.Range).WithIPv6(opts.IPv6).WithLabels(opts.Labels).WithIPv6(opts.IPv6)
- options.WithMacVLAN(opts.MacVLAN).WithOptions(opts.Options).WithSubnet(opts.Subnet)
- return network.Create(ic.ClientCtx, options)
+func (ic *ContainerEngine) NetworkCreate(ctx context.Context, net types.Network) (*entities.NetworkCreateReport, error) {
+ net, err := network.Create(ic.ClientCtx, &net)
+ if err != nil {
+ return nil, err
+ }
+ return &entities.NetworkCreateReport{Name: net.Name}, nil
}
// NetworkDisconnect removes a container from a given network