what's the purpose of `RunNamespacedJob`? do I add...
# prefect-server
d
what's the purpose of
RunNamespacedJob
? do I add it inside a task, so a task can create a Job during the course of a Flow?
m
Hello Dana! You can use
RunNamespacedJob
task to run a namespaced job. No need to add it inside the task. You can initialize your
RunNamespacedJob
instance, pass namespace, job template, etc. Here is a small example
Copy code
from prefect.tasks.kubernetes.job import RunNamespacedJob
from prefect import Flow, task

body = {
    "apiVersion": "batch/v1",
    "kind": "Job",
    "metadata": {
        "name": "pi"
    },
    "spec": {
        "template": {
            "spec": {
                "containers": [
                    {
                        "name": "pi",
                        "image": "perl",
                        "command": [
                            "perl",
                            "-Mbignum=bpi",
                            "-wle",
                            "print bpi(2000)"
                        ]
                    }
                ],
                "restartPolicy": "Never"
            }
        },
        "backoffLimit": 4
    }
}

your_task_name = RunNamespacedJob(body=body, namespace="test")


with Flow("Test") as flow:
    your_task_name(kubernetes_api_key_secret=None)

if __name__ == "__main__":
    flow.run()
d
I see, so it's like a helper method that can be used for general purposes
it uses the underlying Prefect k8s library to instantiate a k8s Job instead of using the k8s client API directly
💯 1