NSX|P Check lb service on listener create/update

Also fallback to the old behavior of service id the same as
the lb id.

Change-Id: I7ab34b32fd59f8d12693b2826f4019843360fe54
This commit is contained in:
asarfaty 2020-11-05 12:35:32 +02:00
parent b7a2187a10
commit 042a3e1620
3 changed files with 40 additions and 9 deletions

View File

@ -23,6 +23,7 @@ from vmware_nsx.common import locking
from vmware_nsx.services.lbaas import lb_const
from vmware_nsx.services.lbaas.nsx_p.implementation import lb_const as p_const
from vmware_nsx.services.lbaas.nsx_v3.implementation import lb_utils
from vmware_nsxlib.v3 import exceptions as nsxlib_exc
from vmware_nsxlib.v3 import load_balancer as nsxlib_lb
from vmware_nsxlib.v3.policy import constants as p_constants
from vmware_nsxlib.v3.policy import utils as p_utils
@ -256,7 +257,7 @@ def setup_session_persistence(nsxpolicy, pool, pool_tags, switch_type,
'listener_id': listener['id'],
'pool_id': pool['id']})
if switch_type:
# There is aso a persistence profile to remove!
# There is also a persistence profile to remove!
return (pp_id, functools.partial(delete_persistence_profile,
nsxpolicy, profile_path))
@ -276,7 +277,7 @@ def get_router_nsx_lb_service(nsxpolicy, router_id):
return non_delete_services[0]
def get_lb_nsx_lb_service(nsxpolicy, lb_id):
def get_lb_nsx_lb_service(nsxpolicy, lb_id, try_old=False):
tags_to_search = [{'scope': SERVICE_LB_TAG_SCOPE, 'tag': lb_id}]
lb_services = nsxpolicy.search_by_tags(
tags_to_search,
@ -287,6 +288,17 @@ def get_lb_nsx_lb_service(nsxpolicy, lb_id):
if non_delete_services:
return non_delete_services[0]
if try_old:
# old Lb service might just have the LB id.
try:
lb_service = nsxpolicy.load_balancer.lb_service.get(lb_id)
except nsxlib_exc.ResourceNotFound:
LOG.error("Did not find LB %s service by tags or ID", lb_id)
else:
LOG.warning("Found LB service by Lb ID %s. Tags are not updated "
"properly", lb_id)
return lb_service
def get_service_lb_name(lb, router_id=None):
if router_id:

View File

@ -80,7 +80,7 @@ class EdgeListenerManagerFromDict(base_mgr.NsxpLoadbalancerBaseManager):
}
def _get_virtual_server_kwargs(self, context, listener, vs_name, tags,
certificate=None):
lb_service_id, certificate=None):
# If loadbalancer vip_port already has floating ip, use floating
# IP as the virtual server VIP address. Else, use the loadbalancer
# vip_address directly on virtual server.
@ -91,14 +91,12 @@ class EdgeListenerManagerFromDict(base_mgr.NsxpLoadbalancerBaseManager):
lb_vip_address = floating_ips[0]['floating_ip_address']
else:
lb_vip_address = listener['loadbalancer']['vip_address']
lb_service = lb_utils.get_lb_nsx_lb_service(
self.core_plugin.nsxpolicy, listener['loadbalancer_id'])
kwargs = {'virtual_server_id': listener['id'],
'ip_address': lb_vip_address,
'ports': [listener['protocol_port']],
'application_profile_id': listener['id'],
'lb_service_id': lb_service['id'],
'lb_service_id': lb_service_id,
'description': listener.get('description')}
if vs_name:
kwargs['name'] = vs_name
@ -173,12 +171,23 @@ class EdgeListenerManagerFromDict(base_mgr.NsxpLoadbalancerBaseManager):
listener['id'])
tags = self._get_listener_tags(context, listener)
self._validate_default_pool(listener, completor)
lb_service = lb_utils.get_lb_nsx_lb_service(
self.core_plugin.nsxpolicy, listener['loadbalancer_id'],
try_old=True)
if not lb_service:
completor(success=False)
msg = (_('Cannot find loadbalancer %(lb_id)s service') %
{'lb_id': listener['loadbalancer_id']})
raise n_exc.BadRequest(resource='lbaas-listener', msg=msg)
try:
app_client = self._get_nsxlib_app_profile(nsxlib_lb, listener)
app_client.create_or_overwrite(
lb_app_profile_id=listener['id'], name=vs_name, tags=tags)
kwargs = self._get_virtual_server_kwargs(
context, listener, vs_name, tags, certificate)
context, listener, vs_name, tags, lb_service['id'],
certificate)
vs_client.create_or_overwrite(**kwargs)
except nsxlib_exc.ManagerError:
completor(success=False)
@ -255,10 +264,20 @@ class EdgeListenerManagerFromDict(base_mgr.NsxpLoadbalancerBaseManager):
new_listener['id'])
tags = self._get_listener_tags(context, new_listener)
lb_service = lb_utils.get_lb_nsx_lb_service(
self.core_plugin.nsxpolicy, new_listener['loadbalancer_id'],
try_old=True)
if not lb_service:
completor(success=False)
msg = (_('Cannot find loadbalancer %(lb_id)s service') %
{'lb_id': new_listener['loadbalancer_id']})
raise n_exc.BadRequest(resource='lbaas-listener', msg=msg)
try:
app_profile_id = new_listener['id']
updated_kwargs = self._get_virtual_server_kwargs(
context, new_listener, vs_name, tags, certificate)
context, new_listener, vs_name, tags, lb_service['id'],
certificate)
vs_client.update(**updated_kwargs)
if vs_name:
app_client.update(app_profile_id, name=vs_name,

View File

@ -67,7 +67,7 @@ class EdgeMemberManagerFromDict(base_mgr.NsxpLoadbalancerBaseManager):
# to the member subnet's router.
service_client = self.core_plugin.nsxpolicy.load_balancer.lb_service
service = p_utils.get_lb_nsx_lb_service(
self.core_plugin.nsxpolicy, lb['id'])
self.core_plugin.nsxpolicy, lb['id'], try_old=True)
if not service:
completor(success=False)
msg = (_('Cannot find loadbalancer %(lb_id)s service') %