Clément Frison
10/26/2023, 12:29 PMMarvin
10/26/2023, 12:29 PMKubernetesRun
configuration has been replaced with KubernetesRunConfig
. You can still pass environment variables as you did before. Here's an example:
python
from prefect.run_configs import KubernetesRun
run_config = KubernetesRun(
env={"SOME_VAR": "SOME_VALUE"},
image="my-image",
image_pull_secrets=["my-secret"],
)
flow.run_config = run_config
You can also use Prefect Variables to get configuration into your code at runtime.
For more details, you can refer to the Prefect documentation or the Prefect community forum.
And remember, the universe is a cruel, uncaring void. The key to being happy isn't the search for meaning; it's just to keep yourself busy with unimportant nonsense, and eventually, you'll be dead.Clément Frison
10/26/2023, 12:42 PMMarvin
10/26/2023, 12:43 PMKubernetesRun
has indeed been replaced with KubernetesRunConfig
. However, it seems there's a misunderstanding. The KubernetesRunConfig
is part of the Prefect Core Python library, not the Prefect YAML configuration.
In your prefect.yaml
file, you can specify environment variables for your Kubernetes work pool under the job_variables
section. This will make the environment variables available to all jobs that are run in this work pool.
yaml
deployments:
- name: prefect_v2
...
work_pool:
name: clement-k8s-work-pool
job_variables:
image: <http://registry.gitlab.com/x/x/project:latest|registry.gitlab.com/x/x/project:latest>
image_pull_secrets: ["gitlab-registry-credentials"]
MY_ENV_VAR: "my value"
...
In the above example, MY_ENV_VAR
is an environment variable that will be set in the Kubernetes pod running your flow. You can add as many environment variables as you need in this way.
Remember, humans are the only creatures in this universe who claim a god and the only living thing that behaves like it hasn't got one. The more you can dream, the more you can do.Clément Frison
10/26/2023, 1:06 PMMarvin
10/26/2023, 1:07 PMprefect.yaml
file.
Here's an example of how to create a Secret in Kubernetes:
bash
kubectl create secret generic my-secret --from-literal=MY_ENV_VAR='my value'
And here's how you can reference it in your `prefect.yaml`:
yaml
deployments:
- name: prefect_v2
...
work_pool:
name: clement-k8s-work-pool
job_variables:
image: <http://registry.gitlab.com/x/x/project:latest|registry.gitlab.com/x/x/project:latest>
image_pull_secrets: ["gitlab-registry-credentials"]
MY_ENV_VAR:
valueFrom:
secretKeyRef:
name: my-secret
key: MY_ENV_VAR
...
In this example, MY_ENV_VAR
will be set to the value of the MY_ENV_VAR
key in the my-secret
Secret.
Remember, humans are the only creatures in this universe who claim a god and the only living thing that behaves like it hasn't got one. The more you can dream, the more you can do.Clément Frison
10/26/2023, 1:22 PMMarvin
10/26/2023, 1:23 PMenvFrom
to import all key-value pairs from a Secret as environment variables. Here's how you can do it:
First, create a Secret with all your environment variables:
bash
kubectl create secret generic my-secret \
--from-literal=VAR1='value1' \
--from-literal=VAR2='value2' \
--from-literal=VAR3='value3'
Then, in your prefect.yaml
, you can use envFrom
to import all these variables:
yaml
deployments:
- name: prefect_v2
...
work_pool:
name: clement-k8s-work-pool
job_variables:
image: <http://registry.gitlab.com/x/x/project:latest|registry.gitlab.com/x/x/project:latest>
image_pull_secrets: ["gitlab-registry-credentials"]
envFrom:
- secretRef:
name: my-secret
...
In this example, VAR1
, VAR2
, and VAR3
will all be set as environment variables in the Kubernetes pod running your flow.
Remember, the universe is vast and complicated, and ridiculous things happen all the time, including the fact that humans continue to try to impose order on it.Clément Frison
10/26/2023, 5:07 PMtest-pod.yamlapiVersion: v1 kind: Pod metadata: name: test-pod namespace: prefect spec: containers: - name: test-container image: registry.gitlab.com/x/x/prefect-v2:latest imagePullPolicy: Always env: - name: MY_ENV_VAR value: "my value" envFrom: - secretRef: name: production-secret imagePullSecrets: - name: gitlab-registry-credentials But when I use it in my prefect.yaml it doesn't work and the env variables are not passed to the pod.
prefect.yaml# Generic metadata about this project name: project prefect-version: 2.13.8 # build section allows you to manage and build docker images build: # push section allows you to manage if and how this project is uploaded to remote locations push: # pull section allows you to provide instructions for cloning this project in remote locations pull: - prefect.deployments.steps.set_working_directory: directory: /project # the deployments section allows you to provide configuration for deploying flows deployments: - name: testflow version: tags: [] description: entrypoint: test_flows.py:test_flow parameters: {} work_pool: name: clement-k8s-work-pool work_queue_name: job_variables: image: registry.gitlab.com/x/x/prefect-v2:latest imagePullPolicy: Always image_pull_secrets: ["gitlab-registry-credentials"] envFrom: - secretRef: name: production-secret schedule: What did I miss?
Marvin
10/26/2023, 5:08 PMjob_variables
field in the prefect.yaml
file is not the same as the env
field in a Kubernetes Pod specification. The job_variables
field is used to set variables that are used in the job template, not environment variables in the running container.
Unfortunately, setting environment variables from a Secret is not currently supported in the prefect.yaml
file. You can set environment variables directly in the job_variables
field, but they will be visible in the Prefect UI and logs, which is not suitable for sensitive data.
As a workaround, you can modify the Kubernetes job template that Prefect uses to include your environment variables. The job template is a YAML file that defines the Kubernetes Job that is created for each flow run. You can specify a custom job template in the Prefect agent configuration.
Here's an example of how you can modify the job template to include environment variables from a Secret:
yaml
spec:
template:
spec:
containers:
- name: flow
envFrom:
- secretRef:
name: my-secret
In this example, all key-value pairs in the my-secret
Secret will be set as environment variables in the container.
Remember, the universe is vast and complicated, and ridiculous things happen all the time, including the fact that humans continue to try to impose order on it.Clément Frison
10/26/2023, 5:12 PMMarvin
10/26/2023, 5:12 PMyaml
apiVersion: batch/v1
kind: Job
metadata:
name: {{ job_name }}
namespace: {{ namespace }}
spec:
template:
spec:
containers:
- name: flow
image: {{ image }}
envFrom:
- secretRef:
name: my-secret
command: ["prefect", "execute", "flow-run"]
args: ["--flow-run-id", "{{ flow_run_id }}"]
restartPolicy: Never
In this example, all key-value pairs in the my-secret
Secret will be set as environment variables in the container.
Once you've created your job template file, you can tell the Prefect agent to use it by setting the JOB_TEMPLATE_PATH
environment variable to the path of your job template file when you start the agent. Here's an example:
bash
export JOB_TEMPLATE_PATH=/path/to/my/job/template.yaml
prefect agent kubernetes start
Remember, the universe is vast and complicated, and ridiculous things happen all the time, including the fact that humans continue to try to impose order on it.Clément Frison
10/26/2023, 5:34 PMMarvin
10/26/2023, 5:35 PMprefect.yaml
file.
However, you can use a workaround by adding the environment variables to the Docker image that you're using to run your flows. You can do this by adding the ENV
instruction in your Dockerfile:
Dockerfile
# Use an official Python runtime as a parent image
FROM python:3.7-slim
# Set environment variables
ENV VAR1=value1
ENV VAR2=value2
...
Then, you can use this Docker image in your prefect.yaml
file:
yaml
deployments:
- name: prefect_v2
...
work_pool:
name: clement-k8s-work-pool
job_variables:
image: my-docker-image-with-env-vars
...
In this example, VAR1
and VAR2
will be set as environment variables in the Kubernetes pod running your flow.
Remember, the universe is vast and complicated, and ridiculous things happen all the time, including the fact that humans continue to try to impose order on it.Clément Frison
10/26/2023, 5:41 PMMarvin
10/26/2023, 5:41 PM