<@ULVA73B9P> i'm trying to run a sub flow as a kub...
# marvin-ai
j
@Marvin i'm trying to run a sub flow as a kubernetes job using the
from prefect_kubernetes.experimental.decorators import kubernetes
decorator. the problem i face is that prefect seems to ignore the job_variables dictionary i give through the decorator. i want to set an environment variable. it works when i define it a work-pool level, but not from the decorator. show me an exemple of how to do it from the decorator.
m
thought for 135.9 seconds
Yes — you can set env vars from the
@kubernetes(...)
decorator, but the key is that you should pass them as
env=...
, not as arbitrary nested Kubernetes manifest fields. Here’s a minimal example:
Copy code
python
from prefect import flow, get_run_logger
from prefect_kubernetes.experimental.decorators import kubernetes
import os


@flow
def child_flow():
    logger = get_run_logger()
    <http://logger.info|logger.info>(f"MY_ENV_VAR={os.getenv('MY_ENV_VAR')}")


@flow
def parent_flow():
    child_flow()


@kubernetes(
    work_pool="my-k8s-work-pool",
    env={
        "MY_ENV_VAR": "hello-from-decorator",
    },
)
@flow
def kubernetes_subflow():
    logger = get_run_logger()
    <http://logger.info|logger.info>(f"MY_ENV_VAR={os.getenv('MY_ENV_VAR')}")


if __name__ == "__main__":
    kubernetes_subflow()
A couple of important notes: * The decorator signature is
@kubernetes(work_pool=..., **job_variables)
, so
env={...}
is itself a job variable. * For Kubernetes workers, Prefect converts that
env
dict into the container env list in the generated Job spec. * This should behave similarly to setting env vars at the work-pool level, except it only applies to this decorated flow. You can also use dotted job variables syntax like this:
Copy code
python
@kubernetes(
    work_pool="my-k8s-work-pool",
    **{
        "env.MY_ENV_VAR": "hello-from-decorator",
    },
)
@flow
def kubernetes_subflow():
    import os
    print(os.getenv("MY_ENV_VAR"))
If your current version is ignoring this, the most likely causes are: * you’re passing the variable under the wrong key (for example, trying to modify raw manifest fields instead of using
env
) * the flow is not actually being run with that decorated infrastructure path * you may be hitting a bug/version mismatch in
prefect-kubernetes
A good known-working pattern is:
Copy code
python
@kubernetes(
    work_pool="my-k8s-work-pool",
    image="my-image:latest",
    namespace="default",
    env={"MY_ENV_VAR": "hello"},
)
@flow
def my_subflow():
    import os
    print(os.environ["MY_ENV_VAR"])
I verified the decorator accepts
**job_variables
, and
env
is the right job variable for Kubernetes worker configuration. If you want, I can also show you: * a full parent flow → subflow on Kubernetes Job example * how to inspect the rendered job configuration to confirm the env var is making it into the pod spec * or help debug the exact
job_variables
dict you’re currently using
a
@jusstol can you share a code snippet where you're seeing this issue?
j
@alex in fact i was setting job_variables as dictionnary `job_variables={}`which was a mistake. after exploding it as separate keyword arguments it worked.