diff --git a/awx/main/utils/execution_environments.py b/awx/main/utils/execution_environments.py index 7b197287b3..1bd2a4ca64 100644 --- a/awx/main/utils/execution_environments.py +++ b/awx/main/utils/execution_environments.py @@ -30,6 +30,7 @@ def get_default_execution_environment(): def get_default_pod_spec(): + job_label: str = settings.AWX_CONTAINER_GROUP_DEFAULT_JOB_LABEL ee = get_default_execution_environment() if ee is None: raise RuntimeError("Unable to find an execution environment.") @@ -37,10 +38,30 @@ def get_default_pod_spec(): return { "apiVersion": "v1", "kind": "Pod", - "metadata": {"namespace": settings.AWX_CONTAINER_GROUP_DEFAULT_NAMESPACE}, + "metadata": {"namespace": settings.AWX_CONTAINER_GROUP_DEFAULT_NAMESPACE, "labels": {job_label: ""}}, "spec": { "serviceAccountName": "default", "automountServiceAccountToken": False, + "affinity": { + "podAntiAffinity": { + "preferredDuringSchedulingIgnoredDuringExecution": [ + { + "weight": 100, + "podAffinityTerm": { + "labelSelector": { + "matchExpressions": [ + { + "key": job_label, + "operator": "Exists", + } + ] + }, + "topologyKey": "kubernetes.io/hostname", + }, + } + ] + } + }, "containers": [ { "image": ee.image, diff --git a/awx/settings/defaults.py b/awx/settings/defaults.py index 97a6cc3ce9..c2fd50a6bf 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -58,6 +58,7 @@ IS_K8S = False AWX_CONTAINER_GROUP_K8S_API_TIMEOUT = 10 AWX_CONTAINER_GROUP_DEFAULT_NAMESPACE = os.getenv('MY_POD_NAMESPACE', 'default') +AWX_CONTAINER_GROUP_DEFAULT_JOB_LABEL = os.getenv('AWX_CONTAINER_GROUP_DEFAULT_JOB_LABEL', 'ansible_job') # Timeout when waiting for pod to enter running state. If the pod is still in pending state , it will be terminated. Valid time units are "s", "m", "h". Example : "5m" , "10s". AWX_CONTAINER_GROUP_POD_PENDING_TIMEOUT = "2h"