Skip to content
Snippets Groups Projects
Commit ca80bc36 authored by Lluis Gifre Renom's avatar Lluis Gifre Renom
Browse files

Common - Settings:

- Upgraded methods wait_for_environment_variables() and find_missing_environment_variables() as they were failing in non-K8s environments.
- Migrated method find_missing_environment_variables() to find_environment_variables()
parent 08a77949
No related branches found
No related tags found
2 merge requests!142Release TeraFlowSDN 2.1,!119Migration of Interdomain component and OECC/PSC'22 test to Release 2
......@@ -13,7 +13,7 @@
# limitations under the License.
import logging, os, time
from typing import List
from typing import Dict, List
from common.Constants import (
DEFAULT_GRPC_BIND_ADDRESS, DEFAULT_GRPC_GRACE_PERIOD, DEFAULT_GRPC_MAX_WORKERS, DEFAULT_HTTP_BIND_ADDRESS,
DEFAULT_LOG_LEVEL, DEFAULT_METRICS_PORT, DEFAULT_SERVICE_GRPC_PORTS, DEFAULT_SERVICE_HTTP_BASEURLS,
......@@ -37,23 +37,24 @@ ENVVAR_SUFIX_SERVICE_HOST = 'SERVICE_HOST'
ENVVAR_SUFIX_SERVICE_PORT_GRPC = 'SERVICE_PORT_GRPC'
ENVVAR_SUFIX_SERVICE_PORT_HTTP = 'SERVICE_PORT_HTTP'
def find_missing_environment_variables(
required_environment_variables : List[str] = []
) -> List[str]:
if ENVVAR_KUBERNETES_PORT in os.environ:
missing_variables = set(required_environment_variables).difference(set(os.environ.keys()))
else:
# We're not running in Kubernetes, nothing to wait for
missing_variables = required_environment_variables
return missing_variables
def find_environment_variables(
environment_variable_names : List[str] = []
) -> Dict[str, str]:
environment_variable : Dict[str, str] = dict()
for name in environment_variable_names:
if name not in os.environ: continue
environment_variable[name] = os.environ[name]
return environment_variable
def wait_for_environment_variables(
required_environment_variables : List[str] = [], wait_delay_seconds : float = DEFAULT_RESTART_DELAY
):
missing_variables = find_missing_environment_variables(required_environment_variables)
if len(missing_variables) == 0: return # We have all environment variables defined
msg = 'Variables({:s}) are missing in Environment({:s}), restarting in {:f} seconds...'
LOGGER.error(msg.format(str(missing_variables), str(os.environ), wait_delay_seconds))
if ENVVAR_KUBERNETES_PORT not in os.environ: return # Not running in Kubernetes
found = find_environment_variables(required_environment_variables)
missing = set(required_environment_variables).difference(set(found.keys()))
if len(missing) == 0: return # We have all environment variables defined
MSG = 'Variables({:s}) are missing in Environment({:s}), restarting in {:f} seconds...'
LOGGER.error(MSG.format(str(missing), str(os.environ), wait_delay_seconds))
time.sleep(wait_delay_seconds)
raise Exception('Restarting...') # pylint: disable=broad-exception-raised
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment