From 87b319c3d51dea9abe10e01f052c496ae8babaec Mon Sep 17 00:00:00 2001 From: jxie Date: Wed, 24 Feb 2021 13:12:26 +0800 Subject: [PATCH] enable or disable snat --- .../lbaasv2/drivers/bigip/resource_manager.py | 59 ++++++++++++++++++- .../lbaasv2/drivers/bigip/service_adapter.py | 19 ++++++ 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/f5_openstack_agent/lbaasv2/drivers/bigip/resource_manager.py b/f5_openstack_agent/lbaasv2/drivers/bigip/resource_manager.py index 2bfe6693c..fed0fbccc 100644 --- a/f5_openstack_agent/lbaasv2/drivers/bigip/resource_manager.py +++ b/f5_openstack_agent/lbaasv2/drivers/bigip/resource_manager.py @@ -335,6 +335,28 @@ def _update_payload(self, old_listener, listener, service, **kwargs): if old_listener['default_pool_id'] != listener['default_pool_id']: payload['persist'] = create_payload['persist'] + snat_enable_or_not = self._check_snat_changed(old_listener, listener) + + LOG.debug('the create_payload:') + LOG.debug(create_payload) + if snat_enable_or_not == 'enable': + LOG.debug('inside enable') + payload['sourceAddressTranslation'] = {} + if "sourceAddressTranslation" in create_payload and \ + create_payload['sourceAddressTranslation'].get('type'): + payload['sourceAddressTranslation']['type'] = \ + create_payload['sourceAddressTranslation'].get('type') + + if "sourceAddressTranslation" in create_payload and \ + create_payload['sourceAddressTranslation'].get('pool'): + payload['sourceAddressTranslation']['pool'] = \ + create_payload['sourceAddressTranslation'].get('pool') + elif snat_enable_or_not == 'disable': + LOG.debug('inside disable') + payload['sourceAddressTranslation'] = {} + payload['sourceAddressTranslation']['type'] = None + payload['sourceAddressTranslation']['pool'] = None + return super(ListenerManager, self)._update_payload( old_listener, listener, service, payload=payload, create_payload=create_payload @@ -375,11 +397,46 @@ def _check_customized_changed(self, old_listener, listener): return True return False + def _check_snat_changed(self, old_listener, listener): + if not old_listener: + return '' + + if 'customized' in listener and listener['customized']: + try: + new_customized_json = json.loads(listener['customized']) + new_enable_snat = new_customized_json.get('enable_snat') + except ValueError: + LOG.error('Invalid json.') + LOG.error('Due to the error, not handle snat at all.') + return '' + + # might want to modify and compare with old one + # if 'customized' in old_listener and old_listener['customized']: + # try: + # old_customized_json = json.loads(old_listener['customized']) + # old_enable_snat = old_customized_json.get('enable_snat') + # except ValueError: + # LOG.error('Invalid json.') + # LOG.error('Due to the error, not handle snat at all.') + + # might want to test agaist other cases as well, e.g '', str, etc + if new_enable_snat == 'True': + LOG.info('enable snat') + return 'enable' + elif new_enable_snat == 'False': + LOG.info('disable snat') + return 'disable' + else: + LOG.info('seems nothing at all') + return '' + def _check_update_needed(self, payload, old_listener, listener): if not payload or len(payload.keys()) == 0: if self._check_customized_changed(old_listener, listener) \ is False and \ - self._check_tls_changed(old_listener, listener) is False: + self._check_tls_changed(old_listener, listener) is False and \ + not self._check_snat_changed(old_listener, listener): + LOG.info('seems not needed to update') return False return True diff --git a/f5_openstack_agent/lbaasv2/drivers/bigip/service_adapter.py b/f5_openstack_agent/lbaasv2/drivers/bigip/service_adapter.py index 659535cb5..1fa97dd0c 100644 --- a/f5_openstack_agent/lbaasv2/drivers/bigip/service_adapter.py +++ b/f5_openstack_agent/lbaasv2/drivers/bigip/service_adapter.py @@ -15,6 +15,7 @@ # import ast +import json import hashlib import netaddr @@ -116,6 +117,24 @@ def get_virtual(self, service): listener["use_snat"] = self.snat_mode() and not listener.get( "transparent") + + if 'customized' in listener and listener['customized']: + enable_snat_arg = None + try: + cust = json.loads(listener['customized']) + enable_snat_arg = cust.get('enable_snat') + LOG.info(enable_snat_arg) + except ValueError: + LOG.error("Skipped. Invalid json: %s", listener['customized']) + + # better to do more checks agaist value, 0,1,true,false,str,etc + if enable_snat_arg == 'True': + listener["use_snat"] = True + elif enable_snat_arg == 'False': + listener["use_snat"] = False + else: + LOG.debug('nothing, skip.') + if listener["use_snat"] and self.snat_count() > 0: listener["snat_pool_name"] = self.get_folder_name( loadbalancer["tenant_id"])