Skip to content

Commit

Permalink
Add handling of null values in dictionary lists
Browse files Browse the repository at this point in the history
  • Loading branch information
jkupferer committed Aug 8, 2023
1 parent b3fdf7d commit 1ce0314
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
21 changes: 0 additions & 21 deletions operator/config.py

This file was deleted.

34 changes: 34 additions & 0 deletions operator/poolboy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import kubernetes_asyncio
import os

class Poolboy():
manage_claims_interval = int(os.environ.get('MANAGE_CLAIMS_INTERVAL', 60))
manage_handles_interval = int(os.environ.get('MANAGE_HANDLES_INTERVAL', 60))
operator_domain = os.environ.get('OPERATOR_DOMAIN', 'poolboy.gpte.redhat.com')
operator_version = os.environ.get('OPERATOR_VERSION', 'v1')
operator_api_version = f"{operator_domain}/{operator_version}"
resource_refresh_interval = int(os.environ.get('RESOURCE_REFRESH_INTERVAL', 600))

@classmethod
async def on_cleanup(cls):
await cls.api_client.close()

@classmethod
async def on_startup(cls):
if os.path.exists('/run/secrets/kubernetes.io/serviceaccount'):
kubernetes_asyncio.config.load_incluster_config()
with open('/run/secrets/kubernetes.io/serviceaccount/namespace') as f:
cls.namespace = f.read()
else:
await kubernetes_asyncio.config.load_kube_config()
if 'OPERATOR_NAMESPACE' in os.environ:
cls.namespace = os.environ['OPERATOR_NAMESPACE']
else:
raise Exception(
'Unable to determine operator namespace. '
'Please set OPERATOR_NAMESPACE environment variable.'
)

cls.api_client = kubernetes_asyncio.client.ApiClient()
cls.core_v1_api = kubernetes_asyncio.client.CoreV1Api(cls.api_client)
cls.custom_objects_api = kubernetes_asyncio.client.CustomObjectsApi(cls.api_client)
2 changes: 1 addition & 1 deletion operator/poolboy_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def seconds_to_interval(seconds:int) -> str:
}
jinja2envs['jinja2'].filters['bool'] = lambda x: bool(strtobool(x)) if isinstance(x, str) else bool(x)
jinja2envs['jinja2'].filters['json_query'] = lambda x, query: jmespath.search(query, x)
jinja2envs['jinja2'].filters['merge_list_of_dicts'] = lambda a: functools.reduce(lambda d1, d2: {**d1, **d2}, a)
jinja2envs['jinja2'].filters['merge_list_of_dicts'] = lambda a: functools.reduce(lambda d1, d2: {**(d1 or {}), **(d2 or {})}, a)
jinja2envs['jinja2'].filters['object'] = lambda x: json.dumps(x)
jinja2envs['jinja2'].filters['parse_time_interval'] = lambda x: timedelta(seconds=pytimeparse.parse(x))
jinja2envs['jinja2'].filters['strgen'] = lambda x: StringGenerator(x).render()
Expand Down
18 changes: 18 additions & 0 deletions test/unittest-poolboy_templating.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,15 @@ def test_26(self):
)

def test_27(self):
template = "{{ l | json_query('from_items(zip([].keys(@)[], [].values(@)[]))') | object }}"
template_vars = {
"l": [{"a": "A", "b": "X"}, {"b": "B", "c": "C"}, {"d": "D"}]
}
self.assertEqual(
recursive_process_template_strings(template, 'jinja2', template_vars), {"a": "A", "b": "B", "c": "C", "d": "D"}
)

def test_28(self):
template = "{{ l | merge_list_of_dicts | object }}"
template_vars = {
"l": [{"a": "A", "b": "X"}, {"b": "B", "c": "C"}, {"d": "D"}]
Expand All @@ -399,5 +408,14 @@ def test_27(self):
recursive_process_template_strings(template, 'jinja2', template_vars), {"a": "A", "b": "B", "c": "C", "d": "D"}
)

def test_29(self):
template = "{{ l | merge_list_of_dicts | object }}"
template_vars = {
"l": [{"a": "A", "b": "X"}, None, {"b": "B", "c": "C"}, {}, {"d": "D"}]
}
self.assertEqual(
recursive_process_template_strings(template, 'jinja2', template_vars), {"a": "A", "b": "B", "c": "C", "d": "D"}
)

if __name__ == '__main__':
unittest.main()

0 comments on commit 1ce0314

Please sign in to comment.