@@ -17,85 +17,57 @@ var (
1717 // (10.x.x.x/24) which do not overlap with the networks in `PredefinedBroadNetworks`
1818 PredefinedGranularNetworks []* net.IPNet
1919 initNetworksOnce sync.Once
20+
21+ defaultBroadNetwork = []* NetworkToSplit {{"172.17.0.0/16" , 16 }, {"172.18.0.0/16" , 16 }, {"172.19.0.0/16" , 16 },
22+ {"172.20.0.0/14" , 16 }, {"172.24.0.0/14" , 16 }, {"172.28.0.0/14" , 16 },
23+ {"192.168.0.0/16" , 20 }}
24+ defaultGranularNetwork = []* NetworkToSplit {{"10.0.0.0/8" , 24 }}
2025)
2126
22- // PredefinedPools represent a set of address pools with prefix length Size.
23- // Each pool in the set is derived from the Base pool. Base is to be passed
24- // in CIDR format.Currently we support only local scope networks
27+ // NetworkToSplit represent a network that has to be split in chunks with mask length Size.
28+ // Each subnet in the set is derived from the Base pool. Base is to be passed
29+ // in CIDR format.
2530// Example: a Base "10.10.0.0/16 with Size 24 will define the set of 256
2631// 10.10.[0-255].0/24 address pools
27- type PredefinedPools struct {
32+ type NetworkToSplit struct {
2833 Base string `json:"base"`
2934 Size int `json:"size"`
3035}
3136
32- // InitNetworks initializes the local predefined address pools
33- // with the default values.
34- func InitNetworks (defaultAddressPool []* PredefinedPools ) {
37+ // InitNetworks initializes the broad network pool and the granular network pool
38+ func InitNetworks (defaultAddressPool []* NetworkToSplit ) {
3539 initNetworksOnce .Do (func () {
36- isPredefinedPool := false
37- PredefinedGranularNetworks = initGranularPredefinedNetworks ( )
40+ // error ingnored should never fail
41+ PredefinedGranularNetworks , _ = splitNetworks ( defaultGranularNetwork )
3842 if defaultAddressPool == nil {
39- defaultAddressPool = []* PredefinedPools {{"172.17.0.0/12" , 16 }, {"192.168.0.0/16" , 20 }}
40- isPredefinedPool = true
43+ defaultAddressPool = defaultBroadNetwork
4144 }
42- if err := InitAddressPools (defaultAddressPool ); err != nil {
45+ var err error
46+ if PredefinedBroadNetworks , err = splitNetworks (defaultAddressPool ); err != nil {
4347 logrus .WithError (err ).Error ("InitAddressPools failed to initialize the default address pool" )
4448 }
45- if isPredefinedPool {
46- PredefinedBroadNetworks = append (PredefinedBroadNetworks [:0 ], PredefinedBroadNetworks [1 :]... )
47- }
4849 })
4950}
50- func initBroadPredefinedNetworks () []* net.IPNet {
51- pl := make ([]* net.IPNet , 0 , 31 )
52- mask := []byte {255 , 255 , 0 , 0 }
53- for i := 17 ; i < 32 ; i ++ {
54- pl = append (pl , & net.IPNet {IP : []byte {172 , byte (i ), 0 , 0 }, Mask : mask })
55- }
56- mask20 := []byte {255 , 255 , 240 , 0 }
57- for i := 0 ; i < 16 ; i ++ {
58- pl = append (pl , & net.IPNet {IP : []byte {192 , 168 , byte (i << 4 ), 0 }, Mask : mask20 })
59- }
60- return pl
61- }
6251
63- func initGranularPredefinedNetworks () []* net.IPNet {
64- pl := make ([]* net.IPNet , 0 , 256 * 256 )
65- mask := []byte {255 , 255 , 255 , 0 }
66- for i := 0 ; i < 256 ; i ++ {
67- for j := 0 ; j < 256 ; j ++ {
68- pl = append (pl , & net.IPNet {IP : []byte {10 , byte (i ), byte (j ), 0 }, Mask : mask })
69- }
70- }
71- return pl
72- }
73-
74- // InitAddressPools allows to initialize the local scope predefined
75- // address pools to the desired values. It fails is invalid input is passed
76- // or if the predefined pools were already initialized.
77- func InitAddressPools (list []* PredefinedPools ) error {
52+ // splitNetworks takes a slice of networks, split them accordingly and returns them
53+ func splitNetworks (list []* NetworkToSplit ) ([]* net.IPNet , error ) {
7854 localPools := make ([]* net.IPNet , 0 , len (list ))
7955
8056 for _ , p := range list {
81- if p == nil {
82- continue
83- }
8457 _ , b , err := net .ParseCIDR (p .Base )
8558 if err != nil {
86- return fmt .Errorf ("invalid base pool %q: %v" , p .Base , err )
59+ return nil , fmt .Errorf ("invalid base pool %q: %v" , p .Base , err )
8760 }
8861 ones , _ := b .Mask .Size ()
8962 if p .Size <= 0 || p .Size < ones {
90- return fmt .Errorf ("invalid pools size: %d" , p .Size )
63+ return nil , fmt .Errorf ("invalid pools size: %d" , p .Size )
9164 }
92- localPools = append (localPools , initPools (p .Size , b )... )
65+ localPools = append (localPools , splitNetwork (p .Size , b )... )
9366 }
94- PredefinedBroadNetworks = localPools
95- return nil
67+ return localPools , nil
9668}
9769
98- func initPools (size int , base * net.IPNet ) []* net.IPNet {
70+ func splitNetwork (size int , base * net.IPNet ) []* net.IPNet {
9971 one , bits := base .Mask .Size ()
10072 mask := net .CIDRMask (size , bits )
10173 n := 1 << uint (size - one )
0 commit comments