Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable or disable snat (should probably into another branch first, this branch can hold a little) #1657

Open
wants to merge 1 commit into
base: 9.8-stable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 59 additions & 1 deletion f5_openstack_agent/lbaasv2/drivers/bigip/resource_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -375,11 +397,47 @@ 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 values as well, if needed.
# seems using 'in' has to consider None, might not want to use it
if new_enable_snat == 'True' or new_enable_snat == 'true':
LOG.info('enable snat')
return 'enable'
elif new_enable_snat == 'False' or 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

Expand Down
19 changes: 19 additions & 0 deletions f5_openstack_agent/lbaasv2/drivers/bigip/service_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#

import ast
import json

import hashlib
import netaddr
Expand Down Expand Up @@ -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"])
Expand Down