6
6
"net"
7
7
"sync"
8
8
9
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9
10
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
10
11
)
11
12
@@ -23,7 +24,7 @@ type SubnetAllocator struct {
23
24
mutex sync.Mutex
24
25
}
25
26
26
- func NewSubnetAllocator (network string , hostBits uint32 , inUse [] string ) (* SubnetAllocator , error ) {
27
+ func newSubnetAllocator (network string , hostBits uint32 ) (* SubnetAllocator , error ) {
27
28
_ , netIP , err := net .ParseCIDR (network )
28
29
if err != nil {
29
30
return nil , fmt .Errorf ("failed to parse network address: %q" , network )
@@ -64,7 +65,7 @@ func NewSubnetAllocator(network string, hostBits uint32, inUse []string) (*Subne
64
65
rightMask = 0
65
66
}
66
67
67
- sa := & SubnetAllocator {
68
+ return & SubnetAllocator {
68
69
network : netIP ,
69
70
hostBits : hostBits ,
70
71
leftShift : leftShift ,
@@ -73,22 +74,10 @@ func NewSubnetAllocator(network string, hostBits uint32, inUse []string) (*Subne
73
74
rightMask : rightMask ,
74
75
next : 0 ,
75
76
allocMap : make (map [string ]bool ),
76
- }
77
- for _ , netStr := range inUse {
78
- _ , ipNet , err := net .ParseCIDR (netStr )
79
- if err != nil {
80
- utilruntime .HandleError (fmt .Errorf ("failed to parse network address: %s" , netStr ))
81
- continue
82
- }
83
- if err = sa .AllocateNetwork (ipNet ); err != nil {
84
- utilruntime .HandleError (err )
85
- continue
86
- }
87
- }
88
- return sa , nil
77
+ }, nil
89
78
}
90
79
91
- func (sna * SubnetAllocator ) AllocateNetwork (ipNet * net.IPNet ) error {
80
+ func (sna * SubnetAllocator ) markAllocatedNetwork (ipNet * net.IPNet ) error {
92
81
sna .mutex .Lock ()
93
82
defer sna .mutex .Unlock ()
94
83
@@ -101,7 +90,7 @@ func (sna *SubnetAllocator) AllocateNetwork(ipNet *net.IPNet) error {
101
90
return nil
102
91
}
103
92
104
- func (sna * SubnetAllocator ) GetNetwork () (* net.IPNet , error ) {
93
+ func (sna * SubnetAllocator ) allocateNetwork () (* net.IPNet , error ) {
105
94
var (
106
95
numSubnets uint32
107
96
numSubnetBits uint32
@@ -132,7 +121,7 @@ func (sna *SubnetAllocator) GetNetwork() (*net.IPNet, error) {
132
121
return nil , ErrSubnetAllocatorFull
133
122
}
134
123
135
- func (sna * SubnetAllocator ) ReleaseNetwork (ipnet * net.IPNet ) error {
124
+ func (sna * SubnetAllocator ) releaseNetwork (ipnet * net.IPNet ) error {
136
125
sna .mutex .Lock ()
137
126
defer sna .mutex .Unlock ()
138
127
@@ -158,3 +147,88 @@ func Uint32ToIP(u uint32) net.IP {
158
147
binary .BigEndian .PutUint32 (ip , u )
159
148
return net .IPv4 (ip [0 ], ip [1 ], ip [2 ], ip [3 ])
160
149
}
150
+
151
+ //--------------------- Master methods ----------------------
152
+
153
+ func (master * OsdnMaster ) initSubnetAllocators () error {
154
+ for _ , cn := range master .networkInfo .ClusterNetworks {
155
+ sa , err := newSubnetAllocator (cn .ClusterCIDR .String (), cn .HostSubnetLength )
156
+ if err != nil {
157
+ return err
158
+ }
159
+ master .subnetAllocatorList = append (master .subnetAllocatorList , sa )
160
+ master .subnetAllocatorMap [cn ] = sa
161
+ }
162
+
163
+ // Populate subnet allocator
164
+ subnets , err := master .networkClient .Network ().HostSubnets ().List (metav1.ListOptions {})
165
+ if err != nil {
166
+ return err
167
+ }
168
+ for _ , sn := range subnets .Items {
169
+ if err := master .markAllocatedNetwork (sn .Subnet ); err != nil {
170
+ utilruntime .HandleError (err )
171
+ }
172
+ }
173
+
174
+ return nil
175
+ }
176
+
177
+ func (master * OsdnMaster ) markAllocatedNetwork (subnet string ) error {
178
+ sa , ipnet , err := master .getSubnetAllocator (subnet )
179
+ if err != nil {
180
+ return err
181
+ }
182
+ if err = sa .markAllocatedNetwork (ipnet ); err != nil {
183
+ return err
184
+ }
185
+ return nil
186
+ }
187
+
188
+ func (master * OsdnMaster ) allocateNetwork (nodeName string ) (string , error ) {
189
+ var sn * net.IPNet
190
+ var err error
191
+
192
+ for _ , possibleSubnet := range master .subnetAllocatorList {
193
+ sn , err = possibleSubnet .allocateNetwork ()
194
+ if err == ErrSubnetAllocatorFull {
195
+ // Current subnet exhausted, check the next one
196
+ continue
197
+ } else if err != nil {
198
+ utilruntime .HandleError (fmt .Errorf ("Error allocating network from subnet: %v" , possibleSubnet ))
199
+ continue
200
+ } else {
201
+ return sn .String (), nil
202
+ }
203
+ }
204
+ return "" , fmt .Errorf ("error allocating network for node %s: %v" , nodeName , err )
205
+ }
206
+
207
+ func (master * OsdnMaster ) releaseNetwork (subnet string ) error {
208
+ sa , ipnet , err := master .getSubnetAllocator (subnet )
209
+ if err != nil {
210
+ return err
211
+ }
212
+ if err = sa .releaseNetwork (ipnet ); err != nil {
213
+ return err
214
+ }
215
+ return nil
216
+ }
217
+
218
+ func (master * OsdnMaster ) getSubnetAllocator (subnet string ) (* SubnetAllocator , * net.IPNet , error ) {
219
+ _ , ipnet , err := net .ParseCIDR (subnet )
220
+ if err != nil {
221
+ return nil , nil , fmt .Errorf ("error parsing subnet %q: %v" , subnet , err )
222
+ }
223
+
224
+ for _ , cn := range master .networkInfo .ClusterNetworks {
225
+ if cn .ClusterCIDR .Contains (ipnet .IP ) {
226
+ sa , ok := master .subnetAllocatorMap [cn ]
227
+ if ! ok || sa == nil {
228
+ return nil , nil , fmt .Errorf ("subnet allocator not found for cluster network: %v" , cn )
229
+ }
230
+ return sa , ipnet , nil
231
+ }
232
+ }
233
+ return nil , nil , fmt .Errorf ("subnet %q not found in the cluster networks: %v" , subnet , master .networkInfo .ClusterNetworks )
234
+ }
0 commit comments