Skip to content

Commit

Permalink
libnetwork/{netavark,cni}: accept containers.conf
Browse files Browse the repository at this point in the history
Just pass down the full containers.conf as this is needed by
rootlessnetns code, also remove the now duplicated fields and read the
options directly from the config struct.

Signed-off-by: Paul Holzinger <[email protected]>
  • Loading branch information
Luap99 committed Dec 5, 2023
1 parent 0373a1c commit 175c8c6
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 65 deletions.
10 changes: 8 additions & 2 deletions libnetwork/cni/cni_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (
"path/filepath"
"testing"

"github.com/containers/common/internal/attributedstring"
"github.com/containers/common/libnetwork/cni"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/config"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
Expand All @@ -28,8 +30,12 @@ func TestCni(t *testing.T) {

func getNetworkInterface(cniConfDir string) (types.ContainerNetwork, error) {
return cni.NewCNINetworkInterface(&cni.InitConfig{
CNIConfigDir: cniConfDir,
CNIPluginDirs: cniPluginDirs,
CNIConfigDir: cniConfDir,
Config: &config.Config{
Network: config.NetworkConfig{
CNIPluginDirs: attributedstring.NewSlice(cniPluginDirs),
},
},
})
}

Expand Down
25 changes: 9 additions & 16 deletions libnetwork/cni/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,14 @@ type network struct {
type InitConfig struct {
// CNIConfigDir is directory where the cni config files are stored.
CNIConfigDir string
// CNIPluginDirs is a list of directories where cni should look for the plugins.
CNIPluginDirs []string
// RunDir is a directory where temporary files can be stored.
RunDir string

// DefaultNetwork is the name for the default network.
DefaultNetwork string
// DefaultSubnet is the default subnet for the default network.
DefaultSubnet string

// DefaultsubnetPools contains the subnets which must be used to allocate a free subnet by network create
DefaultsubnetPools []config.SubnetPool

// IsMachine describes whenever podman runs in a podman machine environment.
IsMachine bool

// Config containers.conf options
Config *config.Config
}

// NewCNINetworkInterface creates the ContainerNetwork interface for the CNI backend.
Expand All @@ -100,12 +93,12 @@ func NewCNINetworkInterface(conf *InitConfig) (types.ContainerNetwork, error) {
return nil, err
}

defaultNetworkName := conf.DefaultNetwork
defaultNetworkName := conf.Config.Network.DefaultNetwork
if defaultNetworkName == "" {
defaultNetworkName = types.DefaultNetworkName
}

defaultSubnet := conf.DefaultSubnet
defaultSubnet := conf.Config.Network.DefaultSubnet
if defaultSubnet == "" {
defaultSubnet = types.DefaultSubnet
}
Expand All @@ -114,23 +107,23 @@ func NewCNINetworkInterface(conf *InitConfig) (types.ContainerNetwork, error) {
return nil, fmt.Errorf("failed to parse default subnet: %w", err)
}

defaultSubnetPools := conf.DefaultsubnetPools
defaultSubnetPools := conf.Config.Network.DefaultSubnetPools
if defaultSubnetPools == nil {
defaultSubnetPools = config.DefaultSubnetPools
}

var netns *rootlessnetns.Netns
if unshare.IsRootless() {
netns, err = rootlessnetns.New(conf.RunDir, rootlessnetns.CNI, nil)
netns, err = rootlessnetns.New(conf.RunDir, rootlessnetns.CNI, conf.Config)
if err != nil {
return nil, err
}
}

cni := libcni.NewCNIConfig(conf.CNIPluginDirs, &cniExec{})
cni := libcni.NewCNIConfig(conf.Config.Network.CNIPluginDirs.Values, &cniExec{})
n := &cniNetwork{
cniConfigDir: conf.CNIConfigDir,
cniPluginDirs: conf.CNIPluginDirs,
cniPluginDirs: conf.Config.Network.CNIPluginDirs.Get(),
cniConf: cni,
defaultNetwork: defaultNetworkName,
defaultSubnet: defaultNet,
Expand Down
4 changes: 0 additions & 4 deletions libnetwork/internal/rootlessnetns/netns_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ func New(dir string, backend NetworkBackend, conf *config.Config) (*Netns, error
if err := os.MkdirAll(netnsDir, 0o700); err != nil {
return nil, wrapError("", err)
}
conf, err := config.Default()
if err != nil {
return nil, err
}
return &Netns{
dir: netnsDir,
backend: backend,
Expand Down
4 changes: 4 additions & 0 deletions libnetwork/netavark/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/containers/common/libnetwork/netavark"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/libnetwork/util"
"github.com/containers/common/pkg/config"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
gomegaTypes "github.com/onsi/gomega/types"
Expand Down Expand Up @@ -684,6 +685,7 @@ var _ = Describe("Config", func() {

It("update NetworkDNSServers AddDNSServers", func() {
libpodNet, err := netavark.NewNetworkInterface(&netavark.InitConfig{
Config: &config.Config{},
NetworkConfigDir: networkConfDir,
NetworkRunDir: networkConfDir,
NetavarkBinary: "true",
Expand All @@ -710,6 +712,7 @@ var _ = Describe("Config", func() {

It("update NetworkDNSServers RemoveDNSServers", func() {
libpodNet, err := netavark.NewNetworkInterface(&netavark.InitConfig{
Config: &config.Config{},
NetworkConfigDir: networkConfDir,
NetworkRunDir: networkConfDir,
NetavarkBinary: "true",
Expand All @@ -736,6 +739,7 @@ var _ = Describe("Config", func() {

It("update NetworkDNSServers Add and Remove DNSServers", func() {
libpodNet, err := netavark.NewNetworkInterface(&netavark.InitConfig{
Config: &config.Config{},
NetworkConfigDir: networkConfDir,
NetworkRunDir: networkConfDir,
NetavarkBinary: "true",
Expand Down
2 changes: 2 additions & 0 deletions libnetwork/netavark/ipam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"

"github.com/containers/common/libnetwork/types"
"github.com/containers/common/pkg/config"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/sirupsen/logrus"
Expand All @@ -34,6 +35,7 @@ var _ = Describe("IPAM", func() {

JustBeforeEach(func() {
libpodNet, err := NewNetworkInterface(&InitConfig{
Config: &config.Config{},
NetworkConfigDir: networkConfDir,
NetworkRunDir: networkConfDir,
})
Expand Down
9 changes: 8 additions & 1 deletion libnetwork/netavark/netavark_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"reflect"
"testing"

"github.com/containers/common/internal/attributedstring"
"github.com/containers/common/libnetwork/netavark"
"github.com/containers/common/libnetwork/types"
"github.com/containers/common/libnetwork/util"
"github.com/containers/common/pkg/config"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
gomegaTypes "github.com/onsi/gomega/types"
Expand All @@ -34,6 +36,7 @@ func init() {

func getNetworkInterface(confDir string) (types.ContainerNetwork, error) {
return netavark.NewNetworkInterface(&netavark.InitConfig{
Config: &config.Config{},
NetworkConfigDir: confDir,
NetavarkBinary: netavarkBinary,
NetworkRunDir: confDir,
Expand All @@ -45,7 +48,11 @@ func getNetworkInterfaceWithPlugins(confDir string, pluginDirs []string) (types.
NetworkConfigDir: confDir,
NetavarkBinary: netavarkBinary,
NetworkRunDir: confDir,
PluginDirs: pluginDirs,
Config: &config.Config{
Network: config.NetworkConfig{
NetavarkPluginDirs: attributedstring.NewSlice(pluginDirs),
},
},
})
}

Expand Down
34 changes: 10 additions & 24 deletions libnetwork/netavark/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,12 @@ type InitConfig struct {
// NetworkRunDir is where temporary files are stored, i.e.the ipam db, aardvark config
NetworkRunDir string

// FirewallDriver sets the firewall driver to use
FirewallDriver string

// DefaultNetwork is the name for the default network.
DefaultNetwork string
// DefaultSubnet is the default subnet for the default network.
DefaultSubnet string

// DefaultsubnetPools contains the subnets which must be used to allocate a free subnet by network create
DefaultsubnetPools []config.SubnetPool

// DNSBindPort is set the port to pass to netavark for aardvark
DNSBindPort uint16

// PluginDirs list of directories were netavark plugins are located
PluginDirs []string

// Syslog describes whenever the netavark debug output should be log to the syslog as well.
// This will use logrus to do so, make sure logrus is set up to log to the syslog.
Syslog bool

// Config containers.conf options
Config *config.Config
}

// NewNetworkInterface creates the ContainerNetwork interface for the netavark backend.
Expand All @@ -122,12 +108,12 @@ func NewNetworkInterface(conf *InitConfig) (types.ContainerNetwork, error) {
return nil, err
}

defaultNetworkName := conf.DefaultNetwork
defaultNetworkName := conf.Config.Network.DefaultNetwork
if defaultNetworkName == "" {
defaultNetworkName = types.DefaultNetworkName
}

defaultSubnet := conf.DefaultSubnet
defaultSubnet := conf.Config.Network.DefaultSubnet
if defaultSubnet == "" {
defaultSubnet = types.DefaultSubnet
}
Expand All @@ -144,14 +130,14 @@ func NewNetworkInterface(conf *InitConfig) (types.ContainerNetwork, error) {
return nil, err
}

defaultSubnetPools := conf.DefaultsubnetPools
defaultSubnetPools := conf.Config.Network.DefaultSubnetPools
if defaultSubnetPools == nil {
defaultSubnetPools = config.DefaultSubnetPools
}

var netns *rootlessnetns.Netns
if unshare.IsRootless() {
netns, err = rootlessnetns.New(conf.NetworkRunDir, rootlessnetns.Netavark, nil)
netns, err = rootlessnetns.New(conf.NetworkRunDir, rootlessnetns.Netavark, conf.Config)
if err != nil {
return nil, err
}
Expand All @@ -164,12 +150,12 @@ func NewNetworkInterface(conf *InitConfig) (types.ContainerNetwork, error) {
aardvarkBinary: conf.AardvarkBinary,
networkRootless: unshare.IsRootless(),
ipamDBPath: filepath.Join(conf.NetworkRunDir, "ipam.db"),
firewallDriver: conf.FirewallDriver,
firewallDriver: conf.Config.Network.FirewallDriver,
defaultNetwork: defaultNetworkName,
defaultSubnet: defaultNet,
defaultsubnetPools: defaultSubnetPools,
dnsBindPort: conf.DNSBindPort,
pluginDirs: conf.PluginDirs,
dnsBindPort: conf.Config.Network.DNSBindPort,
pluginDirs: conf.Config.Network.NetavarkPluginDirs.Get(),
lock: lock,
syslog: conf.Syslog,
rootlessNetns: netns,
Expand Down
28 changes: 10 additions & 18 deletions libnetwork/network/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,12 @@ func NetworkBackend(store storage.Store, conf *config.Config, syslog bool) (type
}

netInt, err := netavark.NewNetworkInterface(&netavark.InitConfig{
NetworkConfigDir: confDir,
NetworkRunDir: runDir,
NetavarkBinary: netavarkBin,
AardvarkBinary: aardvarkBin,
PluginDirs: conf.Network.NetavarkPluginDirs.Get(),
FirewallDriver: conf.Network.FirewallDriver,
DefaultNetwork: conf.Network.DefaultNetwork,
DefaultSubnet: conf.Network.DefaultSubnet,
DefaultsubnetPools: conf.Network.DefaultSubnetPools,
DNSBindPort: conf.Network.DNSBindPort,
Syslog: syslog,
Config: conf,
NetworkConfigDir: confDir,
NetworkRunDir: runDir,
NetavarkBinary: netavarkBin,
AardvarkBinary: aardvarkBin,
Syslog: syslog,
})
return types.Netavark, netInt, err
case types.CNI:
Expand Down Expand Up @@ -181,13 +176,10 @@ func getCniInterface(conf *config.Config) (types.ContainerNetwork, error) {
}
}
return cni.NewCNINetworkInterface(&cni.InitConfig{
CNIConfigDir: confDir,
CNIPluginDirs: conf.Network.CNIPluginDirs.Get(),
RunDir: conf.Engine.TmpDir,
DefaultNetwork: conf.Network.DefaultNetwork,
DefaultSubnet: conf.Network.DefaultSubnet,
DefaultsubnetPools: conf.Network.DefaultSubnetPools,
IsMachine: machine.IsGvProxyBased(),
Config: conf,
CNIConfigDir: confDir,
RunDir: conf.Engine.TmpDir,
IsMachine: machine.IsGvProxyBased(),
})
}

Expand Down

0 comments on commit 175c8c6

Please sign in to comment.