Vugar Yagubov
07/30/2025, 2:36 PMjson
{
"job_configuration": {
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": {
"name": "{{ labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>'] }}"
},
"spec": {
"template": {
"metadata": {
"name": "{{ labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>'] }}"
},
"spec": {
"containers": [
{
"name": "prefect-job"
}
],
"restartPolicy": "Never"
}
}
}
}
}
I get an error:
File "/usr/local/lib/python3.12/site-packages/prefect_kubernetes/worker.py", line 929, in _create_job
raise InfrastructureError(
prefect.exceptions.InfrastructureError: Unable to create Kubernetes job: Unprocessable Entity: Job.batch "prefect-flow-run-{{ labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>'] }}-svm4f" is invalid: [metadata.generateName: Invalid value: "prefect-flow-run-{{ labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>'] }}-": a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. '<http://example.com|example.com>', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*'), metadata.name: Invalid value: "prefect-flow-run-{{ labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>'] }}-svm4f": a lowercase RFC 1123 subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. '<http://example.com|example.com>', regex used for validation is '[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*'), spec.template.labels: Invalid value: "prefect-flow-run-{{ labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>'] }}-svm4f": a valid label must be an empty string or consist of alphanumeric characters, '-', '_' or '.', and must start and end with an alphanumeric character (e.g. 'MyValue', or 'my_value', or '12345', regex used for validation is '(([A-Za-z0-9][-A-Za-z0-9_.]*)?[A-Za-z0-9])?')]
could you help me to integrate flow-run-name from labels and use it as pod name in K8s cluster?Marvin
07/30/2025, 2:36 PMMarvin
07/30/2025, 2:37 PMjson
{
"job_configuration": {
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": {
"name": "{{ flow_run_name }}"
},
"spec": {
"template": {
"metadata": {
"name": "{{ flow_run_name }}"
},
"spec": {
"containers": [
{
"name": "prefect-job"
}
],
"restartPolicy": "Never"
}
}
}
}
}
Key changes made:
1. Replaced labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>']
with flow_run_name
2. The flow_run_name
is a built-in template variable that Prefect automatically provides
Some important notes:
1. The name will automatically be made Kubernetes-compliant (lowercase alphanumeric characters with hyphens)
2. If you need additional labels, you can use the labels
template variable which will include all Prefect-specific labels
3. Kubernetes has strict naming requirements (as shown in your error message), but the template system will handle this automatically
If you want to include additional labels, you could do:
json
{
"job_configuration": {
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": {
"name": "{{ flow_run_name }}",
"labels": {{ labels }}
},
"spec": {
"template": {
"metadata": {
"name": "{{ flow_run_name }}",
"labels": {{ labels }}
},
"spec": {
"containers": [
{
"name": "prefect-job"
}
],
"restartPolicy": "Never"
}
}
}
}
}
This will automatically include all Prefect-specific labels in a Kubernetes-compliant format.Vugar Yagubov
07/30/2025, 2:40 PMValue error, The variables specified in the job configuration template must be present as properties in the variables schema. Your job configuration uses the following undeclared variable(s): flow_run_name.
We need to approach it a little bit differently:
we have BaseJobConfiguration in source code. it has labels.
self.labels = {
**self._base_flow_run_labels(flow_run),
**self._base_work_pool_labels(work_pool),
**self._base_worker_name_label(worker_name),
**self._base_flow_labels(flow),
**self._base_deployment_labels(deployment),
**self.labels,
}
and
@staticmethod
def _base_flow_run_labels(flow_run: "FlowRun") -> dict[str, str]:
"""
Generate a dictionary of labels for a flow run job.
"""
return {
"<http://prefect.io/flow-run-id|prefect.io/flow-run-id>": str(flow_run.id),
"<http://prefect.io/flow-run-name|prefect.io/flow-run-name>": flow_run.name,
"<http://prefect.io/version|prefect.io/version>": prefect.__version__,
}
Now I want to use it and get flow_run.name as pod nameMarvin
07/30/2025, 2:40 PMMarvin
07/30/2025, 2:40 PMjson
{
"job_configuration": {
"apiVersion": "batch/v1",
"kind": "Job",
"metadata": {
"name": "{{ labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>'] }}"
},
"spec": {
"template": {
"metadata": {
"name": "{{ labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>'] }}"
},
"spec": {
"containers": [
{
"name": "prefect-job"
}
],
"restartPolicy": "Never"
}
}
}
}
}
Key changes:
1. Use labels['<http://prefect.io/flow-run-name|prefect.io/flow-run-name>']
instead of the full URL format
2. The label key is <http://prefect.io/flow-run-name|prefect.io/flow-run-name>
(not the URL-encoded version)
This should work because:
1. The labels
variable is available in the template context
2. The flow run name is automatically included in these labels by the _base_flow_run_labels
method you showed
3. When accessing the labels in the template, we use the simplified form of the label key
If you need to access other built-in labels, here are the available keys:
- <http://prefect.io/flow-run-id|prefect.io/flow-run-id>
- <http://prefect.io/flow-run-name|prefect.io/flow-run-name>
- <http://prefect.io/version|prefect.io/version>
Let me know if you still encounter any issues with this syntax!