@@ -176,7 +176,32 @@ def _create_pod_manifest(
176176 ],
177177 )
178178
179+ # Use iptables in an init container to drop all outbound traffic
180+ # before the main executor container starts. Since all containers
181+ # in a pod share a network namespace, rules set here apply to the
182+ # executor container as well. This eliminates the race condition
183+ # where the pod can send network requests before the Kubernetes
184+ # NetworkPolicy is enforced by the CNI.
185+ iptables_script = "set -e && iptables -A OUTPUT -j DROP && ip6tables -A OUTPUT -j DROP"
186+ network_lockdown_container = V1Container (
187+ name = "network-lockdown" ,
188+ image = self .image ,
189+ command = ["sh" , "-c" , iptables_script ],
190+ security_context = {
191+ "runAsUser" : 0 ,
192+ "runAsNonRoot" : False ,
193+ "allowPrivilegeEscalation" : False ,
194+ "readOnlyRootFilesystem" : True ,
195+ "capabilities" : {"drop" : ["ALL" ], "add" : ["NET_ADMIN" ]},
196+ },
197+ resources = {
198+ "limits" : {"cpu" : "100m" , "memory" : "32Mi" },
199+ "requests" : {"cpu" : "10m" , "memory" : "16Mi" },
200+ },
201+ )
202+
179203 spec = V1PodSpec (
204+ init_containers = [network_lockdown_container ],
180205 containers = [container ],
181206 restart_policy = "Never" ,
182207 service_account_name = self .service_account if self .service_account else None ,
0 commit comments