| Giorgi Lekveishvili | 285ab62 | 2023-11-22 13:50:45 +0400 | [diff] [blame^] | 1 | import abc |
| 2 | import random |
| 3 | import re |
| 4 | import string |
| 5 | |
| 6 | from time import time |
| 7 | |
| 8 | from kubernetes import client |
| 9 | |
| 10 | |
| 11 | class AbstractDeployment(abc.ABC): |
| 12 | def __init__(self, tmp_dir): |
| 13 | self.tmp_dir = tmp_dir |
| 14 | self.namespace = "".join( |
| 15 | [random.choice(string.ascii_letters) for n in range(8)] |
| 16 | ).lower() |
| 17 | self.values_file = self._set_values_file() |
| 18 | self.chart_opts = {} |
| 19 | |
| 20 | @abc.abstractmethod |
| 21 | def install(self, wait=True): |
| 22 | pass |
| 23 | |
| 24 | @abc.abstractmethod |
| 25 | def update(self): |
| 26 | pass |
| 27 | |
| 28 | @abc.abstractmethod |
| 29 | def uninstall(self): |
| 30 | pass |
| 31 | |
| 32 | @abc.abstractmethod |
| 33 | def _set_values_file(self): |
| 34 | pass |
| 35 | |
| 36 | def set_helm_value(self, combined_key, value): |
| 37 | nested_keys = re.split(r"(?<!\\)\.", combined_key) |
| 38 | dct_pointer = self.chart_opts |
| 39 | for key in nested_keys[:-1]: |
| 40 | # pylint: disable=W1401 |
| 41 | key.replace("\.", ".") |
| 42 | dct_pointer = dct_pointer.setdefault(key, {}) |
| 43 | # pylint: disable=W1401 |
| 44 | dct_pointer[nested_keys[-1].replace("\.", ".")] = value |
| 45 | |
| 46 | def _wait_for_pod_readiness(self, pod_labels, timeout=180): |
| 47 | """Helper function that can be used to wait for all pods with a given set of |
| 48 | labels to be ready. |
| 49 | |
| 50 | Arguments: |
| 51 | pod_labels {str} -- Label selector string to be used to select pods. |
| 52 | (https://kubernetes.io/docs/concepts/overview/working-with-objects/\ |
| 53 | labels/#label-selectors) |
| 54 | |
| 55 | Keyword Arguments: |
| 56 | timeout {int} -- Time in seconds to wait for the pod status to become ready. |
| 57 | (default: {180}) |
| 58 | |
| 59 | Returns: |
| 60 | boolean -- Whether pods were ready in time. |
| 61 | """ |
| 62 | |
| 63 | def check_pod_readiness(): |
| 64 | core_v1 = client.CoreV1Api() |
| 65 | pod_list = core_v1.list_pod_for_all_namespaces( |
| 66 | watch=False, label_selector=pod_labels |
| 67 | ) |
| 68 | for pod in pod_list.items: |
| 69 | for condition in pod.status.conditions: |
| 70 | if condition.type != "Ready" and condition.status != "True": |
| 71 | return False |
| 72 | return True |
| 73 | |
| 74 | return self._exec_fn_with_timeout(check_pod_readiness, limit=timeout) |
| 75 | |
| 76 | def _exec_fn_with_timeout(self, func, limit=60): |
| 77 | """Helper function that executes a given function until it returns True or a |
| 78 | given time limit is reached. |
| 79 | |
| 80 | Arguments: |
| 81 | func {function} -- Function to execute. The function can return some output |
| 82 | (or None) and as a second return value a boolean indicating, |
| 83 | whether the event the function was waiting for has happened. |
| 84 | |
| 85 | Keyword Arguments: |
| 86 | limit {int} -- Maximum time in seconds to wait for a positive response of |
| 87 | the function (default: {60}) |
| 88 | |
| 89 | Returns: |
| 90 | boolean -- False, if the timeout was reached |
| 91 | any -- Last output of fn |
| 92 | """ |
| 93 | |
| 94 | timeout = time() + limit |
| 95 | while time() < timeout: |
| 96 | is_finished = func() |
| 97 | if is_finished: |
| 98 | return True |
| 99 | return False |