Skip to content

Commit

Permalink
Merge pull request moby#49158 from akerouanton/netdrivers-store-param
Browse files Browse the repository at this point in the history
libnet: pass store as an arg to netdrivers
  • Loading branch information
thaJeztah authored Dec 20, 2024
2 parents ad69293 + e5bf6d8 commit b5d5fef
Show file tree
Hide file tree
Showing 20 changed files with 135 additions and 134 deletions.
20 changes: 20 additions & 0 deletions internal/testutils/storeutils/store.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package storeutils

import (
"testing"

"github.com/docker/docker/libnetwork/datastore"
"gotest.tools/v3/assert"
)

// NewTempStore creates a new temporary libnetwork store for testing purposes.
// The store is created in a temporary directory that is cleaned up when the
// test finishes.
func NewTempStore(t *testing.T) *datastore.Store {
t.Helper()

ds, err := datastore.New(t.TempDir(), "libnetwork")
assert.NilError(t, err)

return ds
}
6 changes: 2 additions & 4 deletions libnetwork/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func New(cfgOptions ...config.Option) (*Controller, error) {
return nil, err
}

if err := registerNetworkDrivers(&c.drvRegistry, c.makeDriverConfig); err != nil {
if err := registerNetworkDrivers(&c.drvRegistry, c.store, c.makeDriverConfig); err != nil {
return nil, err
}

Expand Down Expand Up @@ -333,9 +333,7 @@ func (c *Controller) makeDriverConfig(ntype string) map[string]interface{} {
return nil
}

cfg := map[string]interface{}{
netlabel.LocalKVClient: c.store,
}
cfg := map[string]interface{}{}
for _, label := range c.cfg.Labels {
key, val, _ := strings.Cut(label, "=")
if !strings.HasPrefix(key, netlabel.DriverPrefix+"."+ntype) {
Expand Down
9 changes: 5 additions & 4 deletions libnetwork/drivers/bridge/bridge_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,16 @@ const (
)

// New constructs a new bridge driver
func newDriver() *driver {
func newDriver(store *datastore.Store) *driver {
return &driver{
store: store,
networks: map[string]*bridgeNetwork{},
}
}

// Register registers a new instance of bridge driver.
func Register(r driverapi.Registerer, config map[string]interface{}) error {
d := newDriver()
func Register(r driverapi.Registerer, store *datastore.Store, config map[string]interface{}) error {
d := newDriver(store)
if err := d.configure(config); err != nil {
return err
}
Expand Down Expand Up @@ -570,7 +571,7 @@ func (d *driver) configure(option map[string]interface{}) error {
d.config = config
d.Unlock()

return d.initStore(option)
return d.initStore()
}

func setupHashNetIpset(name string, family uint8) error {
Expand Down
23 changes: 12 additions & 11 deletions libnetwork/drivers/bridge/bridge_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/docker/docker/internal/nlwrap"
"github.com/docker/docker/internal/testutils/netnsutils"
"github.com/docker/docker/internal/testutils/storeutils"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/internal/netiputil"
"github.com/docker/docker/libnetwork/ipamapi"
Expand Down Expand Up @@ -243,7 +244,7 @@ func getIPv6Data(t *testing.T) []driverapi.IPAMData {

func TestCreateFullOptions(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

config := &configuration{
EnableIPForwarding: true,
Expand Down Expand Up @@ -298,7 +299,7 @@ func TestCreateFullOptions(t *testing.T) {

func TestCreateNoConfig(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

netconfig := &networkConfiguration{BridgeName: DefaultBridgeName, EnableIPv4: true}
genericOption := make(map[string]interface{})
Expand All @@ -311,7 +312,7 @@ func TestCreateNoConfig(t *testing.T) {

func TestCreateFullOptionsLabels(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

config := &configuration{
EnableIPForwarding: true,
Expand Down Expand Up @@ -422,7 +423,7 @@ func TestCreateFullOptionsLabels(t *testing.T) {
func TestCreate(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()

d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

if err := d.configure(nil); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
Expand All @@ -448,7 +449,7 @@ func TestCreate(t *testing.T) {
func TestCreateFail(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()

d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

if err := d.configure(nil); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
Expand All @@ -466,7 +467,7 @@ func TestCreateFail(t *testing.T) {
func TestCreateMultipleNetworks(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()

d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

config := &configuration{
EnableIPTables: true,
Expand Down Expand Up @@ -669,7 +670,7 @@ func TestQueryEndpointInfoHairpin(t *testing.T) {

func testQueryEndpointInfo(t *testing.T, ulPxyEnabled bool) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))
portallocator.Get().ReleaseAll()

var proxyBinary string
Expand Down Expand Up @@ -782,7 +783,7 @@ func getPortMapping() []types.PortBinding {
func TestLinkContainers(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()

d := newDriver()
d := newDriver(storeutils.NewTempStore(t))
iptable := iptables.GetIptable(iptables.IPv4)

config := &configuration{
Expand Down Expand Up @@ -1091,7 +1092,7 @@ func TestValidateFixedCIDRV6(t *testing.T) {
func TestSetDefaultGw(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()

d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

if err := d.configure(nil); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
Expand Down Expand Up @@ -1199,7 +1200,7 @@ func TestCleanupIptableRules(t *testing.T) {

func TestCreateWithExistingBridge(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

if err := d.configure(nil); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
Expand Down Expand Up @@ -1271,7 +1272,7 @@ func TestCreateParallel(t *testing.T) {
c := netnsutils.SetupTestOSContextEx(t)
defer c.Cleanup(t)

d := newDriver()
d := newDriver(storeutils.NewTempStore(t))
portallocator.Get().ReleaseAll()

if err := d.configure(nil); err != nil {
Expand Down
25 changes: 8 additions & 17 deletions libnetwork/drivers/bridge/bridge_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/containerd/log"
"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/netlabel"
"github.com/docker/docker/libnetwork/types"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
Expand All @@ -25,23 +24,15 @@ const (
bridgeEndpointPrefix = "bridge-endpoint"
)

func (d *driver) initStore(option map[string]interface{}) error {
if data, ok := option[netlabel.LocalKVClient]; ok {
var ok bool
d.store, ok = data.(*datastore.Store)
if !ok {
return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
}

err := d.populateNetworks()
if err != nil {
return err
}
func (d *driver) initStore() error {
err := d.populateNetworks()
if err != nil {
return err
}

err = d.populateEndpoints()
if err != nil {
return err
}
err = d.populateEndpoints()
if err != nil {
return err
}

return nil
Expand Down
9 changes: 5 additions & 4 deletions libnetwork/drivers/bridge/network_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@ import (

"github.com/docker/docker/internal/nlwrap"
"github.com/docker/docker/internal/testutils/netnsutils"
"github.com/docker/docker/internal/testutils/storeutils"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/netlabel"
)

func TestLinkCreate(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

if err := d.configure(nil); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
Expand Down Expand Up @@ -108,7 +109,7 @@ func TestLinkCreate(t *testing.T) {

func TestLinkCreateTwo(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

if err := d.configure(nil); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
Expand Down Expand Up @@ -147,7 +148,7 @@ func TestLinkCreateTwo(t *testing.T) {

func TestLinkCreateNoEnableIPv6(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

if err := d.configure(nil); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
Expand Down Expand Up @@ -183,7 +184,7 @@ func TestLinkCreateNoEnableIPv6(t *testing.T) {

func TestLinkDelete(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

if err := d.configure(nil); err != nil {
t.Fatalf("Failed to setup driver config: %v", err)
Expand Down
7 changes: 4 additions & 3 deletions libnetwork/drivers/bridge/port_mapping_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/containerd/log"
"github.com/docker/docker/internal/testutils/netnsutils"
"github.com/docker/docker/internal/testutils/storeutils"
"github.com/docker/docker/libnetwork/iptables"
"github.com/docker/docker/libnetwork/netlabel"
"github.com/docker/docker/libnetwork/ns"
Expand All @@ -27,7 +28,7 @@ import (

func TestPortMappingConfig(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

config := &configuration{
EnableIPTables: true,
Expand Down Expand Up @@ -111,7 +112,7 @@ func TestPortMappingV6Config(t *testing.T) {
t.Fatalf("Could not bring loopback iface up: %v", err)
}

d := newDriver()
d := newDriver(storeutils.NewTempStore(t))

config := &configuration{
EnableIPTables: true,
Expand Down Expand Up @@ -828,7 +829,7 @@ func TestAddPortMappings(t *testing.T) {
GwModeIPv4: tc.gwMode4,
GwModeIPv6: tc.gwMode6,
},
driver: newDriver(),
driver: newDriver(storeutils.NewTempStore(t)),
}
genericOption := map[string]interface{}{
netlabel.GenericData: &configuration{
Expand Down
5 changes: 3 additions & 2 deletions libnetwork/drivers/bridge/setup_ip_tables_linux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/docker/docker/internal/nlwrap"
"github.com/docker/docker/internal/testutils/netnsutils"
"github.com/docker/docker/internal/testutils/storeutils"
"github.com/docker/docker/libnetwork/driverapi"
"github.com/docker/docker/libnetwork/iptables"
"github.com/docker/docker/libnetwork/netlabel"
Expand Down Expand Up @@ -191,7 +192,7 @@ func assertBridgeConfig(config *networkConfiguration, br *bridgeInterface, d *dr
// Regression test for https://github.com/moby/moby/issues/46445
func TestSetupIP6TablesWithHostIPv4(t *testing.T) {
defer netnsutils.SetupTestOSContext(t)()
d := newDriver()
d := newDriver(storeutils.NewTempStore(t))
dc := &configuration{
EnableIPTables: true,
EnableIP6Tables: true,
Expand Down Expand Up @@ -364,7 +365,7 @@ func TestOutgoingNATRules(t *testing.T) {
EnableIP6Tables: tc.enableIP6Tables,
}
r := &testRegisterer{t: t}
if err := Register(r, map[string]interface{}{netlabel.GenericData: dc}); err != nil {
if err := Register(r, storeutils.NewTempStore(t), map[string]interface{}{netlabel.GenericData: dc}); err != nil {
t.Fatal(err)
}
if r.d == nil {
Expand Down
5 changes: 3 additions & 2 deletions libnetwork/drivers/ipvlan/ipvlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,12 @@ type network struct {
}

// Register initializes and registers the libnetwork ipvlan driver.
func Register(r driverapi.Registerer, config map[string]interface{}) error {
func Register(r driverapi.Registerer, store *datastore.Store, config map[string]interface{}) error {
d := &driver{
store: store,
networks: networkTable{},
}
if err := d.initStore(config); err != nil {
if err := d.initStore(); err != nil {
return err
}
return r.RegisterDriver(NetworkType, d, driverapi.Capability{
Expand Down
25 changes: 8 additions & 17 deletions libnetwork/drivers/ipvlan/ipvlan_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/containerd/log"
"github.com/docker/docker/libnetwork/datastore"
"github.com/docker/docker/libnetwork/netlabel"
"github.com/docker/docker/libnetwork/types"
)

Expand Down Expand Up @@ -41,22 +40,14 @@ type ipSubnet struct {
}

// initStore drivers are responsible for caching their own persistent state
func (d *driver) initStore(option map[string]interface{}) error {
if data, ok := option[netlabel.LocalKVClient]; ok {
var ok bool
d.store, ok = data.(*datastore.Store)
if !ok {
return types.InternalErrorf("incorrect data in datastore configuration: %v", data)
}

err := d.populateNetworks()
if err != nil {
return err
}
err = d.populateEndpoints()
if err != nil {
return err
}
func (d *driver) initStore() error {
err := d.populateNetworks()
if err != nil {
return err
}
err = d.populateEndpoints()
if err != nil {
return err
}

return nil
Expand Down
Loading

0 comments on commit b5d5fef

Please sign in to comment.