Debian Codegraph @

src/plugins/fd/kubernetes-backend/baculak8s/util/lambda_util.py

raw
1 # Bacula(R) - The Network Backup Solution
2 #
3 # Copyright (C) 2000-2022 Kern Sibbald
4 #
5 # The original author of Bacula is Kern Sibbald, with contributions
6 # from many others, a complete list can be found in the file AUTHORS.
7 #
8 # You may use this file and others of this release according to the
9 # license defined in the LICENSE file, which includes the Affero General
10 # Public License, v3.0 ("AGPLv3") and some additional permissions and
11 # terms pursuant to its AGPLv3 Section 7.
12 #
13 # This notice must be preserved when any source code is
14 # conveyed and/or propagated.
15 #
16 # Bacula(R) is a registered trademark of Kern Sibbald.
17
18 import logging
19 import time
20
21 K8S_NUM_MAX_TRIES_ERROR = "Reached maximum number of tries! Message: {message}"
22 NUM_MAX_TRIES = 20
23
24 def apply(condition, iterable):
25 # Helper method to map lambdas on iterables
26 # Created just for readability
27 return list(map(condition, iterable))
28
29
30 def wait_until_resource_is_ready(action, error_msg = 'Reached num maximum tries.', sleep = 3):
31 logging.debug('Waiting until resource is ready')
32 tries = 0
33 is_ready = False
34 while not is_ready:
35 is_ready = action()
36 if tries >= NUM_MAX_TRIES:
37 logging.debug("Reached num maximum tries.")
38 return { 'error': K8S_NUM_MAX_TRIES_ERROR.format(message=error_msg)}
39 time.sleep(sleep)
40 return {}