4
4
"fmt"
5
5
"net"
6
6
"sync"
7
+
8
+ utilruntime "k8s.io/apimachinery/pkg/util/runtime"
7
9
)
8
10
9
11
var ErrSubnetAllocatorFull = fmt .Errorf ("No subnets available." )
@@ -23,14 +25,14 @@ type SubnetAllocator struct {
23
25
func NewSubnetAllocator (network string , hostBits uint32 , inUse []string ) (* SubnetAllocator , error ) {
24
26
_ , netIP , err := net .ParseCIDR (network )
25
27
if err != nil {
26
- return nil , fmt .Errorf ("Failed to parse network address: %q" , network )
28
+ return nil , fmt .Errorf ("failed to parse network address: %q" , network )
27
29
}
28
30
29
31
netMaskSize , _ := netIP .Mask .Size ()
30
32
if hostBits == 0 {
31
- return nil , fmt .Errorf ("Host capacity cannot be zero." )
33
+ return nil , fmt .Errorf ("host capacity cannot be zero." )
32
34
} else if hostBits > (32 - uint32 (netMaskSize )) {
33
- return nil , fmt .Errorf ("Subnet capacity cannot be larger than number of networks available." )
35
+ return nil , fmt .Errorf ("subnet capacity cannot be larger than number of networks available." )
34
36
}
35
37
subnetBits := 32 - uint32 (netMaskSize ) - hostBits
36
38
@@ -61,29 +63,41 @@ func NewSubnetAllocator(network string, hostBits uint32, inUse []string) (*Subne
61
63
rightMask = 0
62
64
}
63
65
64
- amap := make (map [string ]bool )
65
- for _ , netStr := range inUse {
66
- _ , nIp , err := net .ParseCIDR (netStr )
67
- if err != nil {
68
- fmt .Println ("Failed to parse network address: " , netStr )
69
- continue
70
- }
71
- if ! netIP .Contains (nIp .IP ) {
72
- fmt .Println ("Provided subnet doesn't belong to network: " , nIp )
73
- continue
74
- }
75
- amap [nIp .String ()] = true
76
- }
77
- return & SubnetAllocator {
66
+ sa := & SubnetAllocator {
78
67
network : netIP ,
79
68
hostBits : hostBits ,
80
69
leftShift : leftShift ,
81
70
leftMask : leftMask ,
82
71
rightShift : rightShift ,
83
72
rightMask : rightMask ,
84
73
next : 0 ,
85
- allocMap : amap ,
86
- }, nil
74
+ allocMap : make (map [string ]bool ),
75
+ }
76
+ for _ , netStr := range inUse {
77
+ _ , ipNet , err := net .ParseCIDR (netStr )
78
+ if err != nil {
79
+ utilruntime .HandleError (fmt .Errorf ("failed to parse network address: %s" , netStr ))
80
+ continue
81
+ }
82
+ if err = sa .AllocateNetwork (ipNet ); err != nil {
83
+ utilruntime .HandleError (err )
84
+ continue
85
+ }
86
+ }
87
+ return sa , nil
88
+ }
89
+
90
+ func (sna * SubnetAllocator ) AllocateNetwork (ipNet * net.IPNet ) error {
91
+ sna .mutex .Lock ()
92
+ defer sna .mutex .Unlock ()
93
+
94
+ if ! sna .network .Contains (ipNet .IP ) {
95
+ return fmt .Errorf ("provided subnet doesn't belong to network: %v" , ipNet )
96
+ }
97
+ if ! sna .allocMap [ipNet .String ()] {
98
+ sna .allocMap [ipNet .String ()] = true
99
+ }
100
+ return nil
87
101
}
88
102
89
103
func (sna * SubnetAllocator ) GetNetwork () (* net.IPNet , error ) {
@@ -120,16 +134,16 @@ func (sna *SubnetAllocator) GetNetwork() (*net.IPNet, error) {
120
134
func (sna * SubnetAllocator ) ReleaseNetwork (ipnet * net.IPNet ) error {
121
135
sna .mutex .Lock ()
122
136
defer sna .mutex .Unlock ()
137
+
123
138
if ! sna .network .Contains (ipnet .IP ) {
124
- return fmt .Errorf ("Provided subnet %v doesn't belong to the network %v." , ipnet , sna .network )
139
+ return fmt .Errorf ("provided subnet %v doesn't belong to the network %v." , ipnet , sna .network )
125
140
}
126
141
127
142
ipnetStr := ipnet .String ()
128
143
if ! sna .allocMap [ipnetStr ] {
129
- return fmt .Errorf ("Provided subnet %v is already available." , ipnet )
144
+ return fmt .Errorf ("provided subnet %v is already available." , ipnet )
145
+ } else {
146
+ sna .allocMap [ipnetStr ] = false
130
147
}
131
-
132
- sna .allocMap [ipnetStr ] = false
133
-
134
148
return nil
135
149
}
0 commit comments