@@ -242,7 +242,86 @@ def _submit_sge_job(
242
242
def _submit_k8s_job (
243
243
self , func_data : Dict [str , Any ], job_config : Dict [str , Any ]
244
244
) -> str :
245
- """Submit job via Kubernetes."""
245
+ """
246
+ Submit a job to Kubernetes cluster using containerized Python execution.
247
+
248
+ This method implements a sophisticated Kubernetes job submission strategy that
249
+ packages Python functions and data into self-contained container jobs without
250
+ requiring custom Docker images or persistent storage.
251
+
252
+ **Architecture:**
253
+
254
+ 1. **Function Serialization**: Uses cloudpickle to serialize the function and all data
255
+ 2. **Base64 Encoding**: Encodes serialized data for safe embedding in container args
256
+ 3. **Container Execution**: Creates a Job with inline Python code that:
257
+ - Decodes the base64 data
258
+ - Deserializes the function and arguments
259
+ - Executes the function
260
+ - Captures results or errors
261
+ 4. **Resource Management**: Applies CPU and memory limits from job_config
262
+
263
+ **Key Features:**
264
+ - **No Custom Images**: Uses standard `python:3.11-slim` image
265
+ - **Self-Contained**: All code and data embedded in Job manifest
266
+ - **Resource Aware**: Respects CPU/memory requirements
267
+ - **Error Handling**: Captures exceptions with full tracebacks
268
+ - **Cloud Native**: Leverages Kubernetes Job semantics for reliability
269
+
270
+ **Job Manifest Structure:**
271
+ ```yaml
272
+ apiVersion: batch/v1
273
+ kind: Job
274
+ metadata:
275
+ name: clustrix-job-{timestamp}
276
+ spec:
277
+ template:
278
+ spec:
279
+ containers:
280
+ - name: clustrix-worker
281
+ image: python:3.11-slim
282
+ command: ["python", "-c"]
283
+ args: ["<embedded Python code>"]
284
+ resources:
285
+ requests/limits: {cpu, memory from job_config}
286
+ restartPolicy: Never
287
+ ```
288
+
289
+ Args:
290
+ func_data: Serialized function data containing:
291
+ - 'func': The function to execute
292
+ - 'args': Positional arguments
293
+ - 'kwargs': Keyword arguments
294
+ - 'requirements': Package dependencies (not used for K8s)
295
+ job_config: Job configuration including:
296
+ - 'cores': CPU request/limit (default: 1)
297
+ - 'memory': Memory request/limit (default: "1Gi")
298
+ - Additional K8s-specific settings
299
+
300
+ Returns:
301
+ str: Kubernetes Job name that can be used for status tracking
302
+
303
+ Raises:
304
+ ImportError: If kubernetes package is not installed
305
+ Exception: If Kubernetes API calls fail
306
+
307
+ Examples:
308
+ >>> func_data = {
309
+ ... 'func': lambda x: x**2,
310
+ ... 'args': (5,),
311
+ ... 'kwargs': {},
312
+ ... 'requirements': {}
313
+ ... }
314
+ >>> job_config = {'cores': 2, 'memory': '4Gi'}
315
+ >>> job_id = executor._submit_k8s_job(func_data, job_config)
316
+ >>> print(job_id) # "clustrix-job-1234567890"
317
+
318
+ Note:
319
+ - Requires kubernetes package: `pip install kubernetes`
320
+ - Assumes kubectl is configured with cluster access
321
+ - Jobs are created in the "default" namespace
322
+ - Cloudpickle is used for function serialization
323
+ - Results are captured via stdout parsing (CLUSTRIX_RESULT: prefix)
324
+ """
246
325
try :
247
326
from kubernetes import client , config as k8s_config
248
327
except ImportError :
0 commit comments