Skip to content

Commit

Permalink
Allow setting default execution group pod spec
Browse files Browse the repository at this point in the history
This will allow us to control the default container group created via settings, meaning
we could set this in the operator and the default container group would get created with it applied.

We need this for ansible/awx-operator#242

Deepmerge the default podspec and the override

With out this, providing the `spec` for the podspec would override everything
contained, which ends up including the container used, which is not desired

Also, use the same deepmerge function def, as the code seems to be copypasted from
the utils
  • Loading branch information
kdelee committed Dec 10, 2021
1 parent cdf1415 commit e10030b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 22 deletions.
7 changes: 6 additions & 1 deletion awx/main/management/commands/register_queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ def __init__(self, message, changed, *args, **kwargs):


class RegisterQueue:
def __init__(self, queuename, instance_percent, inst_min, hostname_list, is_container_group=None):
def __init__(self, queuename, instance_percent, inst_min, hostname_list, is_container_group=None, pod_spec_override=None):
self.instance_not_found_err = None
self.queuename = queuename
self.instance_percent = instance_percent
self.instance_min = inst_min
self.hostname_list = hostname_list
self.is_container_group = is_container_group
self.pod_spec_override = pod_spec_override

def get_create_update_instance_group(self):
created = False
Expand All @@ -40,6 +41,10 @@ def get_create_update_instance_group(self):
ig.is_container_group = self.is_container_group
changed = True

if self.pod_spec_override and (ig.pod_spec_override != self.pod_spec_override):
ig.pod_spec_override = self.pod_spec_override
changed = True

if changed:
ig.save()

Expand Down
4 changes: 3 additions & 1 deletion awx/main/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def get_or_register(self):
else:
registered = self.register(ip_address=pod_ip, uuid=settings.SYSTEM_UUID)
RegisterQueue(settings.DEFAULT_CONTROL_PLANE_QUEUE_NAME, 100, 0, [], is_container_group=False).register()
RegisterQueue(settings.DEFAULT_EXECUTION_QUEUE_NAME, 100, 0, [], is_container_group=True).register()
RegisterQueue(
settings.DEFAULT_EXECUTION_QUEUE_NAME, 100, 0, [], is_container_group=True, pod_spec_override=settings.DEFAULT_EXECUTION_QUEUE_POD_SPEC_OVERRIDE
).register()
return registered
else:
return (False, self.me())
Expand Down
21 changes: 2 additions & 19 deletions awx/main/scheduler/kubernetes.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,12 @@
from django.utils.functional import cached_property
from django.utils.translation import ugettext_lazy as _

from awx.main.utils.common import parse_yaml_or_json
from awx.main.utils.common import parse_yaml_or_json, deepmerge
from awx.main.utils.execution_environments import get_default_pod_spec

logger = logging.getLogger('awx.main.scheduler')


def deepmerge(a, b):
"""
Merge dict structures and return the result.
>>> a = {'first': {'all_rows': {'pass': 'dog', 'number': '1'}}}
>>> b = {'first': {'all_rows': {'fail': 'cat', 'number': '5'}}}
>>> import pprint; pprint.pprint(deepmerge(a, b))
{'first': {'all_rows': {'fail': 'cat', 'number': '5', 'pass': 'dog'}}}
"""
if isinstance(a, dict) and isinstance(b, dict):
return dict([(k, deepmerge(a.get(k), b.get(k))) for k in set(a.keys()).union(b.keys())])
elif b is None:
return a
else:
return b


class PodManager(object):
def __init__(self, task=None):
self.task = task
Expand Down Expand Up @@ -183,7 +166,7 @@ def pod_definition(self):
pod_spec_override = {}
if self.task and self.task.instance_group.pod_spec_override:
pod_spec_override = parse_yaml_or_json(self.task.instance_group.pod_spec_override)
pod_spec = {**default_pod_spec, **pod_spec_override}
pod_spec = deepmerge(default_pod_spec, pod_spec_override)

if self.task:
pod_spec['metadata'] = deepmerge(
Expand Down
6 changes: 5 additions & 1 deletion awx/main/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3286,7 +3286,11 @@ def pod_definition(self):
pod_spec_override = {}
if self.task and self.task.instance.instance_group.pod_spec_override:
pod_spec_override = parse_yaml_or_json(self.task.instance.instance_group.pod_spec_override)
pod_spec = {**default_pod_spec, **pod_spec_override}
# According to the deepmerge docstring, the second dictionary will override when
# they share keys, which is the desired behavior.
# This allows user to only provide elements they want to override, and for us to still provide any
# defaults they don't want to change
pod_spec = deepmerge(default_pod_spec, pod_spec_override)

pod_spec['spec']['containers'][0]['image'] = ee.image
pod_spec['spec']['containers'][0]['args'] = ['ansible-runner', 'worker', '--private-data-dir=/runner']
Expand Down
2 changes: 2 additions & 0 deletions awx/settings/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,5 +986,7 @@ def IS_TESTING(argv=None):

# Name of the default task queue
DEFAULT_EXECUTION_QUEUE_NAME = 'default'
# pod spec used when the default execution queue is a container group, e.g. when deploying on k8s/ocp with the operator
DEFAULT_EXECUTION_QUEUE_POD_SPEC_OVERRIDE = ''
# Name of the default controlplane queue
DEFAULT_CONTROL_PLANE_QUEUE_NAME = 'controlplane'

0 comments on commit e10030b

Please sign in to comment.