Skip to content

Commit

Permalink
Merge pull request #1072 from cpuguy83/reduce_init_cost
Browse files Browse the repository at this point in the history
Remove pre-defined networks from package init
  • Loading branch information
mavenugo committed Apr 4, 2016
2 parents f7e3338 + fc4ef5f commit 36acee5
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 4 deletions.
4 changes: 4 additions & 0 deletions drivers/bridge/bridge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ import (
"github.com/docker/libnetwork/types"
)

func init() {
ipamutils.InitNetworks()
}

func getIPv4Data(t *testing.T) []driverapi.IPAMData {
ipd := driverapi.IPAMData{AddressSpace: "full"}
nw, _, err := ipamutils.ElectInterfaceAddresses("")
Expand Down
1 change: 1 addition & 0 deletions ipam/allocator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func randomLocalStore() (datastore.DataStore, error) {
}

func getAllocator() (*Allocator, error) {
ipamutils.InitNetworks()
ds, err := randomLocalStore()
if err != nil {
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions ipams/builtin/builtin_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/docker/libnetwork/datastore"
"github.com/docker/libnetwork/ipam"
"github.com/docker/libnetwork/ipamapi"
"github.com/docker/libnetwork/ipamutils"
)

// Init registers the built-in ipam service with libnetwork
Expand All @@ -28,6 +29,9 @@ func Init(ic ipamapi.Callback, l, g interface{}) error {
return fmt.Errorf("incorrect global datastore passed to built-in ipam init")
}
}

ipamutils.InitNetworks()

a, err := ipam.NewAllocator(localDs, globalDs)
if err != nil {
return err
Expand Down
16 changes: 12 additions & 4 deletions ipamutils/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Package ipamutils provides utililty functions for ipam management
package ipamutils

import "net"
import (
"net"
"sync"
)

var (
// PredefinedBroadNetworks contains a list of 31 IPv4 private networks with host size 16 and 12
Expand All @@ -10,11 +13,16 @@ var (
// PredefinedGranularNetworks contains a list of 64K IPv4 private networks with host size 8
// (10.x.x.x/24) which do not overlap with the networks in `PredefinedBroadNetworks`
PredefinedGranularNetworks []*net.IPNet

initNetworksOnce sync.Once
)

func init() {
PredefinedBroadNetworks = initBroadPredefinedNetworks()
PredefinedGranularNetworks = initGranularPredefinedNetworks()
// InitNetworks initializes the pre-defined networks used by the built-in IP allocator
func InitNetworks() {
initNetworksOnce.Do(func() {
PredefinedBroadNetworks = initBroadPredefinedNetworks()
PredefinedGranularNetworks = initGranularPredefinedNetworks()
})
}

func initBroadPredefinedNetworks() []*net.IPNet {
Expand Down
2 changes: 2 additions & 0 deletions ipamutils/utils_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func ElectInterfaceAddresses(name string) (*net.IPNet, []*net.IPNet, error) {
err error
)

InitNetworks()

defer osl.InitOSContext()()

link, _ := netlink.LinkByName(name)
Expand Down
4 changes: 4 additions & 0 deletions ipamutils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"github.com/vishvananda/netlink"
)

func init() {
InitNetworks()
}

func TestGranularPredefined(t *testing.T) {
for _, nw := range PredefinedGranularNetworks {
if ones, bits := nw.Mask.Size(); bits != 32 || ones != 24 {
Expand Down

0 comments on commit 36acee5

Please sign in to comment.