Prevent TypeError in get_active_endpoint function

Sometimes "endpoints_dict" var can be evaluated to None
resulting in "TypeError: 'NoneType' object is not iterable"
error. This patch catches the exception while getting
list of endpoints and checks the value of
endpoints_dict.  Also the amount of active endpoints is being logged
for debug purposes.

Change-Id: I79f6b0b5ced8129b9a28c120b61e3ee050af4336
This commit is contained in:
Oleksii Grudev 2020-03-27 17:39:48 +02:00 committed by Vasyl Saienko
parent 13a683b9c2
commit cb3afe6f85
3 changed files with 14 additions and 7 deletions

View File

@ -15,7 +15,7 @@ apiVersion: v1
appVersion: v10.6.7
description: OpenStack-Helm MariaDB
name: mariadb
version: 0.2.57
version: 0.2.58
home: https://mariadb.com/kb/en/
icon: http://badges.mariadb.org/mariadb-badge-180x60.png
sources:

View File

@ -616,13 +616,17 @@ def get_active_endpoints(endpoints_name=direct_svc_name,
(default direct_svc_name)
namespace -- namespace to check for endpoints (default pod_namespace)
"""
endpoints = k8s_api_instance.read_namespaced_endpoints(
name=endpoints_name, namespace=pod_namespace)
try:
endpoints = k8s_api_instance.read_namespaced_endpoints(
name=endpoints_name, namespace=pod_namespace)
except kubernetes.client.rest.ApiException as error:
logger.error("Failed to get mariadb service with error: {0}".format(error))
raise error
endpoints_dict = endpoints.to_dict()
addresses_index = [
i for i, s in enumerate(endpoints_dict['subsets']) if 'addresses' in s
][0]
active_endpoints = endpoints_dict['subsets'][addresses_index]['addresses']
active_endpoints = []
if endpoints_dict['subsets']:
active_endpoints = [s['addresses'] for s in endpoints_dict['subsets'] if 'addresses' in s
][0]
return active_endpoints
@ -638,8 +642,10 @@ def check_for_active_nodes(endpoints_name=direct_svc_name,
logger.info("Checking for active nodes")
active_endpoints = get_active_endpoints()
if active_endpoints and len(active_endpoints) >= 1:
logger.info("Amount of active endpoints: {0}".format(len(active_endpoints)))
return True
else:
logger.info("Amount of active endpoints: 0")
return False

View File

@ -73,4 +73,5 @@ mariadb:
- 0.2.55 Improve python3 compatibility
- 0.2.56 Stop running threads on sigkill
- 0.2.57 Remove useless retries on conflicts during cm update
- 0.2.58 Prevent TypeError in get_active_endpoint function
...