From 825a02c86acf35d9868363fddf5929a12a153ec2 Mon Sep 17 00:00:00 2001 From: Justin Downie <55856631+jdowni000@users.noreply.github.com> Date: Tue, 8 Oct 2024 15:29:57 -0500 Subject: [PATCH] Adding podAntiAffinity (#15578) --- awx/main/utils/execution_environments.py | 23 ++++++++++++++++++++++- awx/settings/defaults.py | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) 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"