From cb3afe6f8581a7a1df33816b1265d35fc796bdfe Mon Sep 17 00:00:00 2001 From: Oleksii Grudev Date: Fri, 27 Mar 2020 17:39:48 +0200 Subject: [PATCH] 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 --- mariadb/Chart.yaml | 2 +- mariadb/templates/bin/_start.py.tpl | 18 ++++++++++++------ releasenotes/notes/mariadb.yaml | 1 + 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/mariadb/Chart.yaml b/mariadb/Chart.yaml index aa5173c4c..31edd4c66 100644 --- a/mariadb/Chart.yaml +++ b/mariadb/Chart.yaml @@ -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: diff --git a/mariadb/templates/bin/_start.py.tpl b/mariadb/templates/bin/_start.py.tpl index 59522748e..268731033 100644 --- a/mariadb/templates/bin/_start.py.tpl +++ b/mariadb/templates/bin/_start.py.tpl @@ -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 diff --git a/releasenotes/notes/mariadb.yaml b/releasenotes/notes/mariadb.yaml index ad7651d1e..2b87ac238 100644 --- a/releasenotes/notes/mariadb.yaml +++ b/releasenotes/notes/mariadb.yaml @@ -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 ...