diff --git a/awx/main/utils/execution_environments.py b/awx/main/utils/execution_environments.py index f1f7faa34f7e..7b498d50a172 100644 --- a/awx/main/utils/execution_environments.py +++ b/awx/main/utils/execution_environments.py @@ -28,6 +28,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.") @@ -35,10 +36,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 d51818d7222e..bf1e6e57270d 100644 --- a/awx/settings/defaults.py +++ b/awx/settings/defaults.py @@ -58,6 +58,7 @@ 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"