-
Notifications
You must be signed in to change notification settings - Fork 13
/
infoblox-ipam.go
303 lines (262 loc) · 10 KB
/
infoblox-ipam.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Copyright 2016 Infoblox Inc.
// All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package ibcni
import (
"errors"
"fmt"
"log"
"net"
"strings"
ibclient "github.com/infobloxopen/infoblox-go-client"
)
type Container struct {
NetworkContainer string // CIDR of Network Container
NetworkView string // Network view
ContainerObj *ibclient.NetworkContainer
exhausted bool
}
type IBInfobloxDriver interface {
RequestNetworkView(netviewName string) (string, error)
RequestAddress(netviewName string, cidr string, ipAddr string, macAddr string, name string, vmID string) (string, error)
GetAddress(netviewName string, cidr string, ipAddr string, macAddr string) (*ibclient.FixedAddress, error)
UpdateAddress(fixedAddrRef string, macAddr string, name string, vmID string) (*ibclient.FixedAddress, error)
ReleaseAddress(netviewName string, ipAddr string, macAddr string) (ref string, err error)
RequestNetwork(netconf NetConfig, netviewName string) (network string, err error)
CreateGateway(cidr string, gw net.IP, netviewName string) (string, error)
}
type InfobloxDriver struct {
objMgr ibclient.IBObjectManager
Containers []Container
DefaultNetworkView string
DefaultPrefixLen uint
}
func (ibDrv *InfobloxDriver) RequestNetworkView(netviewName string) (string, error) {
var netview *ibclient.NetworkView
if netviewName == "" {
netviewName = ibDrv.DefaultNetworkView
}
netview, _ = ibDrv.objMgr.GetNetworkView(netviewName)
if netview == nil {
netview, _ = ibDrv.objMgr.CreateNetworkView(netviewName)
}
log.Printf("RequestNetworkView: netview result is '%s'", *netview)
return netview.Name, nil
}
func (ibDrv *InfobloxDriver) GetAddress(netviewName string, cidr string, ipAddr string, macAddr string) (*ibclient.FixedAddress, error) {
if netviewName == "" {
netviewName = ibDrv.DefaultNetworkView
}
fixedAddr, err := ibDrv.objMgr.GetFixedAddress(netviewName, cidr, ipAddr, macAddr)
return fixedAddr, err
}
func (ibDrv *InfobloxDriver) RequestAddress(netviewName string, cidr string, ipAddr string, macAddr string, name string, vmID string) (string, error) {
var fixedAddr *ibclient.FixedAddress
if netviewName == "" {
netviewName = ibDrv.DefaultNetworkView
}
if len(macAddr) == 0 {
log.Println("RequestAddressRequest contains empty MAC Address. '00:00:00:00:00:00' will be used.")
} else {
fixedAddr, _ = ibDrv.objMgr.GetFixedAddress(netviewName, cidr, ipAddr, macAddr)
}
if fixedAddr == nil {
fixedAddr, _ = ibDrv.objMgr.AllocateIP(netviewName, cidr, ipAddr, macAddr, name, vmID)
}
log.Printf("RequestAddress: fixedAddr result is '%s'", *fixedAddr)
return fmt.Sprintf("%s", fixedAddr.IPAddress), nil
}
func (ibDrv *InfobloxDriver) UpdateAddress(fixedAddrRef string, macAddr string, name string, vmID string) (*ibclient.FixedAddress, error) {
fixedAddr, err := ibDrv.objMgr.UpdateFixedAddress(fixedAddrRef, macAddr, name, vmID)
if err != nil {
log.Printf("UpdateAddress failed with error '%s'", err)
}
return fixedAddr, err
}
func (ibDrv *InfobloxDriver) ReleaseAddress(netviewName string, ipAddr string, macAddr string) (ref string, err error) {
if netviewName == "" {
netviewName = ibDrv.DefaultNetworkView
}
ref, err = ibDrv.objMgr.ReleaseIP(netviewName, "", ipAddr, macAddr)
if ref == "" {
log.Printf("ReleaseAddress: ***** IP Cannot be deleted '%s', '%s', '%s'! *******", netviewName, ipAddr, macAddr)
}
return
}
func (ibDrv *InfobloxDriver) createNetworkContainer(netview string, pool string) (*ibclient.NetworkContainer, error) {
container, err := ibDrv.objMgr.GetNetworkContainer(netview, pool)
if container == nil {
container, err = ibDrv.objMgr.CreateNetworkContainer(netview, pool)
}
return container, err
}
func (ibDrv *InfobloxDriver) nextAvailableContainer() *Container {
for i := range ibDrv.Containers {
if !ibDrv.Containers[i].exhausted {
return &ibDrv.Containers[i]
}
}
return nil
}
func (ibDrv *InfobloxDriver) resetContainers() {
for i := range ibDrv.Containers {
ibDrv.Containers[i].exhausted = false
ibDrv.Containers[i].ContainerObj = nil
ibDrv.Containers[i].NetworkView = ""
}
}
func (ibDrv *InfobloxDriver) allocateNetworkHelper(netview string, prefixLen uint, name string) (network *ibclient.Network, err error) {
log.Printf("allocateNetworkHelper: netview='%s', prefixLen='%d', name='%s'", netview, prefixLen, name)
container := ibDrv.nextAvailableContainer()
for container != nil {
log.Printf("Allocating network from Container:'%s'", container.NetworkContainer)
if container.ContainerObj == nil || container.NetworkView != netview {
var err error
container.ContainerObj, err = ibDrv.createNetworkContainer(netview, container.NetworkContainer)
container.NetworkView = netview
if err != nil || container.ContainerObj == nil {
return nil, err
}
}
network, err = ibDrv.objMgr.AllocateNetwork(netview, container.NetworkContainer, prefixLen, name)
if network != nil {
break
}
container.exhausted = true
container = ibDrv.nextAvailableContainer()
}
return network, nil
}
func (ibDrv *InfobloxDriver) allocateNetwork(prefixLen uint, name string, netviewName string) (network *ibclient.Network, err error) {
log.Printf("allocateNetwork: prefixLen='%d', name='%s'", prefixLen, name)
if prefixLen == 0 {
prefixLen = ibDrv.DefaultPrefixLen
}
network, err = ibDrv.allocateNetworkHelper(netviewName, prefixLen, name)
if network == nil {
ibDrv.resetContainers()
network, err = ibDrv.allocateNetworkHelper(netviewName, prefixLen, name)
}
if network == nil {
err = errors.New("Cannot allocate network in Address Space")
}
return
}
func (ibDrv *InfobloxDriver) requestSpecificNetwork(netview string, subnet string, name string) (*ibclient.Network, error) {
network, err := ibDrv.objMgr.GetNetwork(netview, subnet, nil)
if err != nil {
return nil, err
}
if network != nil {
if n, ok := network.Ea["Network Name"]; !ok || n != name {
log.Printf("requestSpecificNetwork: network is already used '%s'", *network)
return nil, nil
}
} else {
networkByName, err := ibDrv.objMgr.GetNetwork(netview, "", ibclient.EA{"Network Name": name})
if err != nil {
return nil, err
}
if networkByName != nil {
if networkByName.Cidr != subnet {
log.Printf("requestSpecificNetwork: network name has different Cidr '%s'", networkByName.Cidr)
return nil, nil
}
}
}
if network == nil {
network, err = ibDrv.objMgr.CreateNetwork(netview, subnet, name)
log.Printf("requestSpecificNetwork: CreateNetwork returns '%s', err='%s'", *network, err)
}
return network, err
}
func (ibDrv *InfobloxDriver) RequestNetwork(netconf NetConfig, netviewName string) (network string, err error) {
var ibNetwork *ibclient.Network
// netviewName := netconf.IPAM.NetworkView
cidr := net.IPNet{}
log.Printf("RequestNetwork: IPAM.Subnet='%s'", netconf.IPAM.Subnet)
if netconf.IPAM.Subnet.IP != nil {
cidr = net.IPNet{IP: netconf.IPAM.Subnet.IP, Mask: netconf.IPAM.Subnet.Mask}
} else {
_, ipmask, _ := net.ParseCIDR(ibDrv.Containers[0].NetworkContainer)
cidr = net.IPNet{IP: ipmask.IP, Mask: ipmask.Mask}
// There is no support for network container in this release so commenting out.
/* networkByName, err := ibDrv.objMgr.GetNetwork(netviewName, "", ibclient.EA{"Network Name": netconf.Name})
if err != nil {
return "", err
}
if networkByName != nil {
log.Printf("RequestNetwork: GetNetwork by name returns '%s'", *networkByName)
ibNetwork = networkByName
} else {
prefixLen := ibDrv.DefaultPrefixLen
if netconf.IPAM.PrefixLength != 0 {
prefixLen = netconf.IPAM.PrefixLength
}
ibNetwork, err = ibDrv.allocateNetwork(prefixLen, netconf.Name, netviewName)
} */
}
ibNetwork, err = ibDrv.requestSpecificNetwork(netviewName, cidr.String(), netconf.Name)
log.Printf("RequestNetwork: result='%s'", ibNetwork)
if ibNetwork != nil {
network = ibNetwork.Cidr
}
return network, err
}
func (ibDrv *InfobloxDriver) CreateGateway(cidr string, gw net.IP, netviewName string) (string, error) {
gw = gw.To4() //making sure it is only 4 bytes
//check for the format of gateway is in 0.0.0.x given by customer
//it happens when no subnet given in the conf file
if gw[0] == 0 {
subnetIp, subnet, _ := net.ParseCIDR(cidr)
subnetIp = subnetIp.To4()
for index := 0; index <= 3; index++ {
if gw[index] == 0 {
gw[index] = subnetIp[index]
}
}
if subnet.Contains(gw) == false {
return "", fmt.Errorf("gateway given is invalid, should lie on subnet:'%s'", subnet)
}
}
gateway := gw.String()
//checking for gw ip already created ,if not creating
gatewayIp, err := ibDrv.objMgr.GetFixedAddress(netviewName, cidr, gateway, "")
if err == nil && gatewayIp != nil {
log.Println("The Gateway already created")
} else if gatewayIp == nil {
gatewayIp, err = ibDrv.objMgr.AllocateIP(netviewName, cidr, gateway, "", "", "")
if err != nil {
log.Printf("Gateway creation failed with error:'%s'", err)
}
}
return fmt.Sprintf("%s", gatewayIp), nil
}
// This method should be removed because there is no support for list of subnet hereafter.
func makeContainers(containerList string) []Container {
var containers []Container
parts := strings.Split(containerList, ",")
for _, p := range parts {
containers = append(containers, Container{p, "", nil, false})
}
return containers
}
func NewInfobloxDriver(objMgr ibclient.IBObjectManager, networkView string, networkContainer string, prefixLength uint) *InfobloxDriver {
return &InfobloxDriver{
objMgr: objMgr,
DefaultNetworkView: networkView,
DefaultPrefixLen: prefixLength,
Containers: makeContainers(networkContainer),
}
}