-
Notifications
You must be signed in to change notification settings - Fork 69
/
driver.py
309 lines (259 loc) · 10.6 KB
/
driver.py
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
304
305
306
307
308
309
#
# Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
#
import uuid
from neutron.common import exceptions as n_exc
from neutron.openstack.common import log as logging
try:
from neutron.services.loadbalancer.drivers import abstract_driver
except ImportError:
from neutron_lbaas.services.loadbalancer.drivers import abstract_driver
from vnc_api.vnc_api import ServiceInstance, ServiceInstanceType
from vnc_api.vnc_api import ServiceScaleOutType, ServiceInstanceInterfaceType
from vnc_api.vnc_api import NoIdError, RefsExistError
import utils
LOG = logging.getLogger(__name__)
LOADBALANCER_SERVICE_TEMPLATE = [
'default-domain',
'haproxy-loadbalancer-template'
]
class OpencontrailLoadbalancerDriver(
abstract_driver.LoadBalancerAbstractDriver):
def __init__(self, plugin):
self.plugin = plugin
self._api = plugin.get_api_client()
self._lb_template = None
def _get_template(self):
if self._lb_template is not None:
return
try:
tmpl = self._api.service_template_read(
fq_name=LOADBALANCER_SERVICE_TEMPLATE)
except NoIdError:
msg = ('Loadbalancer service-template not found when '
'attempting to create pool')
raise n_exc.BadRequest(resource='pool', msg=msg)
self._lb_template = tmpl
def _get_virtual_ip_interface(self, vip):
vmi_list = vip.get_virtual_machine_interface_refs()
if vmi_list is None:
return None
try:
vmi = self._api.virtual_machine_interface_read(
id=vmi_list[0]['uuid'])
except NoIdError as ex:
LOG.error(ex)
return None
return vmi
def _get_interface_address(self, vmi):
ip_refs = vmi.get_instance_ip_back_refs()
if ip_refs is None:
return None
try:
iip = self._api.instance_ip_read(id=ip_refs[0]['uuid'])
except NoIdError as ex:
LOG.error(ex)
return None
return iip.get_instance_ip_address()
def _calculate_instance_properties(self, pool, vip):
""" ServiceInstance settings
- right network: public side, determined by the vip
- left network: backend, determined by the pool subnet
"""
props = ServiceInstanceType()
if_list = []
vmi = self._get_virtual_ip_interface(vip)
if not vmi:
return None
vnet_refs = vmi.get_virtual_network_refs()
if vnet_refs is None:
return None
right_virtual_network = ':'.join(vnet_refs[0]['to'])
right_ip_address = self._get_interface_address(vmi)
if right_ip_address is None:
return None
right_if = ServiceInstanceInterfaceType(
virtual_network=right_virtual_network,
ip_address=right_ip_address)
if_list.append(right_if)
pool_attrs = pool.get_loadbalancer_pool_properties()
backnet_id = utils.get_subnet_network_id(
self._api, pool_attrs.subnet_id)
if backnet_id != vnet_refs[0]['uuid']:
try:
vnet = self._api.virtual_network_read(id=backnet_id)
except NoIdError as ex:
LOG.error(ex)
return None
left_virtual_network = ':'.join(vnet.get_fq_name())
left_if = ServiceInstanceInterfaceType(
virtual_network=left_virtual_network)
if_list.append(left_if)
# set interfaces and ha
props.set_interface_list(if_list)
props.set_ha_mode('active-standby')
scale_out = ServiceScaleOutType(max_instances=2, auto_scale=False)
props.set_scale_out(scale_out)
return props
def _service_instance_update_props(self, si_obj, nprops):
fields = [
'right_virtual_network',
'right_ip_address',
'left_virtual_network'
]
current = si_obj.get_service_instance_properties()
update = False
for field in fields:
if getattr(current, field) != getattr(nprops, field):
update = True
break
si_obj.set_service_instance_properties(nprops)
return update
def _update_loadbalancer_instance(self, pool_id, vip_id):
""" Update the loadbalancer service instance.
Prerequisites:
pool and vip must be known.
"""
try:
pool = self._api.loadbalancer_pool_read(id=pool_id)
except NoIdError:
msg = ('Unable to retrieve pool %s' % pool_id)
raise n_exc.BadRequest(resource='pool', msg=msg)
try:
vip = self._api.virtual_ip_read(id=vip_id)
except NoIdError:
msg = ('Unable to retrieve virtual-ip %s' % vip_id)
raise n_exc.BadRequest(resource='vip', msg=msg)
fq_name = pool.get_fq_name()[:-1]
fq_name.append(pool_id)
props = self._calculate_instance_properties(pool, vip)
if props is None:
try:
self._api.service_instance_delete(fq_name=fq_name)
except RefsExistError as ex:
LOG.error(ex)
return
self._get_template()
try:
si_obj = self._api.service_instance_read(fq_name=fq_name)
update = self._service_instance_update_props(si_obj, props)
if update:
self._api.service_instance_update(si_obj)
except NoIdError:
proj_obj = self._api.project_read(fq_name=fq_name[:-1])
si_obj = ServiceInstance(name=fq_name[-1], parent_obj=proj_obj,
service_instance_properties=props)
si_obj.set_service_template(self._lb_template)
self._api.service_instance_create(si_obj)
si_refs = pool.get_service_instance_refs()
if si_refs is None or si_refs[0]['uuid'] != si_obj.uuid:
pool.set_service_instance(si_obj)
self._api.loadbalancer_pool_update(pool)
def _clear_loadbalancer_instance(self, tenant_id, pool_id):
try:
project = self._api.project_read(
id=str(uuid.UUID(tenant_id)))
except NoIdError as ex:
LOG.error(ex)
return
fq_name = list(project.get_fq_name())
fq_name.append(pool_id)
try:
si_obj = self._api.service_instance_read(fq_name=fq_name)
except NoIdError as ex:
LOG.error(ex)
return
pool_back_refs = si_obj.get_loadbalancer_pool_back_refs()
for pool_back_ref in pool_back_refs or []:
pool_obj = self._api.loadbalancer_pool_read(
id=pool_back_ref['uuid'])
pool_obj.del_service_instance(si_obj)
self._api.loadbalancer_pool_update(pool_obj)
try:
self._api.service_instance_delete(fq_name=fq_name)
except RefsExistError as ex:
LOG.error(ex)
def create_vip(self, context, vip):
"""A real driver would invoke a call to his backend
and set the Vip status to ACTIVE/ERROR according
to the backend call result
self.plugin.update_status(context, Vip, vip["id"],
constants.ACTIVE)
"""
self._get_template()
if vip['pool_id']:
self._update_loadbalancer_instance(vip['pool_id'], vip['id'])
def update_vip(self, context, old_vip, vip):
"""Driver may call the code below in order to update the status.
self.plugin.update_status(context, Vip, id, constants.ACTIVE)
"""
if old_vip['pool_id'] != vip['pool_id']:
self._clear_loadbalancer_instance(
old_vip['tenant_id'], old_vip['pool_id'])
if vip['pool_id']:
self._update_loadbalancer_instance(vip['pool_id'], vip['id'])
def delete_vip(self, context, vip):
"""A real driver would invoke a call to his backend
and try to delete the Vip.
if the deletion was successful, delete the record from the database.
if the deletion has failed, set the Vip status to ERROR.
"""
if vip['pool_id']:
self._clear_loadbalancer_instance(vip['tenant_id'], vip['pool_id'])
def create_pool(self, context, pool):
"""Driver may call the code below in order to update the status.
self.plugin.update_status(context, Pool, pool["id"],
constants.ACTIVE)
"""
self._get_template()
if pool.get('vip_id'):
self._update_loadbalancer_instance(pool['id'], pool['vip_id'])
def update_pool(self, context, old_pool, pool):
"""Driver may call the code below in order to update the status.
self.plugin.update_status(context,
Pool,
pool["id"], constants.ACTIVE)
"""
if pool['vip_id']:
self._update_loadbalancer_instance(pool['id'], pool['vip_id'])
def delete_pool(self, context, pool):
"""Driver can call the code below in order to delete the pool.
self.plugin._delete_db_pool(context, pool["id"])
or set the status to ERROR if deletion failed
"""
pass
def stats(self, context, pool_id):
pass
def create_member(self, context, member):
"""Driver may call the code below in order to update the status.
self.plugin.update_status(context, Member, member["id"],
constants.ACTIVE)
"""
pass
def update_member(self, context, old_member, member):
"""Driver may call the code below in order to update the status.
self.plugin.update_status(context, Member,
member["id"], constants.ACTIVE)
"""
pass
def delete_member(self, context, member):
pass
def update_pool_health_monitor(self, context,
old_health_monitor,
health_monitor,
pool_id):
pass
def create_pool_health_monitor(self, context,
health_monitor,
pool_id):
"""Driver may call the code below in order to update the status.
self.plugin.update_pool_health_monitor(context,
health_monitor["id"],
pool_id,
constants.ACTIVE)
"""
pass
def delete_pool_health_monitor(self, context, health_monitor, pool_id):
pass
def update_health_monitor(self, context, id, health_monitor):
pass