|
25 | 25 | from app.app_configs import ( |
26 | 26 | KUBERNETES_EXECUTOR_IMAGE, |
27 | 27 | KUBERNETES_EXECUTOR_NAMESPACE, |
| 28 | + KUBERNETES_EXECUTOR_NET_ADMIN_LOCKDOWN, |
28 | 29 | KUBERNETES_EXECUTOR_SERVICE_ACCOUNT, |
29 | 30 | ) |
30 | 31 | from app.services.executor_base import ( |
@@ -95,6 +96,7 @@ def __init__(self) -> None: |
95 | 96 | self.namespace = KUBERNETES_EXECUTOR_NAMESPACE |
96 | 97 | self.image = KUBERNETES_EXECUTOR_IMAGE |
97 | 98 | self.service_account = KUBERNETES_EXECUTOR_SERVICE_ACCOUNT |
| 99 | + self.net_admin_lockdown = KUBERNETES_EXECUTOR_NET_ADMIN_LOCKDOWN |
98 | 100 |
|
99 | 101 | def check_health(self) -> HealthCheck: |
100 | 102 | """Verify Kubernetes API is reachable and we can create pods in the namespace.""" |
@@ -196,26 +198,34 @@ def _create_pod_manifest( |
196 | 198 | # executor container as well. This eliminates the race condition |
197 | 199 | # where the pod can send network requests before the Kubernetes |
198 | 200 | # NetworkPolicy is enforced by the CNI. |
199 | | - iptables_script = "set -e && iptables -A OUTPUT -j DROP && ip6tables -A OUTPUT -j DROP" |
200 | | - network_lockdown_container = V1Container( |
201 | | - name="network-lockdown", |
202 | | - image=self.image, |
203 | | - command=["sh", "-c", iptables_script], |
204 | | - security_context={ |
205 | | - "runAsUser": 0, |
206 | | - "runAsNonRoot": False, |
207 | | - "allowPrivilegeEscalation": False, |
208 | | - "readOnlyRootFilesystem": True, |
209 | | - "capabilities": {"drop": ["ALL"], "add": ["NET_ADMIN"]}, |
210 | | - }, |
211 | | - resources={ |
212 | | - "limits": {"cpu": "100m", "memory": "32Mi"}, |
213 | | - "requests": {"cpu": "10m", "memory": "16Mi"}, |
214 | | - }, |
215 | | - ) |
| 201 | + # |
| 202 | + # This requires the NET_ADMIN capability. Environments whose CNI |
| 203 | + # enforces NetworkPolicies without that race (or that disallow |
| 204 | + # NET_ADMIN) can disable this and rely on a NetworkPolicy instead. |
| 205 | + init_containers: list[V1Container] = [] |
| 206 | + if self.net_admin_lockdown: |
| 207 | + iptables_script = "set -e && iptables -A OUTPUT -j DROP && ip6tables -A OUTPUT -j DROP" |
| 208 | + init_containers.append( |
| 209 | + V1Container( |
| 210 | + name="network-lockdown", |
| 211 | + image=self.image, |
| 212 | + command=["sh", "-c", iptables_script], |
| 213 | + security_context={ |
| 214 | + "runAsUser": 0, |
| 215 | + "runAsNonRoot": False, |
| 216 | + "allowPrivilegeEscalation": False, |
| 217 | + "readOnlyRootFilesystem": True, |
| 218 | + "capabilities": {"drop": ["ALL"], "add": ["NET_ADMIN"]}, |
| 219 | + }, |
| 220 | + resources={ |
| 221 | + "limits": {"cpu": "100m", "memory": "32Mi"}, |
| 222 | + "requests": {"cpu": "10m", "memory": "16Mi"}, |
| 223 | + }, |
| 224 | + ) |
| 225 | + ) |
216 | 226 |
|
217 | 227 | spec = V1PodSpec( |
218 | | - init_containers=[network_lockdown_container], |
| 228 | + init_containers=init_containers or None, |
219 | 229 | containers=[container], |
220 | 230 | restart_policy="Never", |
221 | 231 | active_deadline_seconds=active_deadline_seconds, |
|
0 commit comments