Templated Variables being treated as String by KubernetesPodOperator in Airflow 2.0
See original GitHub issueI’m encountering an issue that I believe is similar to #13348, but am opening a new issue to expand on what is happening in more detail.
Apache Airflow version: 2.0.0
Kubernetes version (if you are using kubernetes) (use kubectl version
): 1.19.4
Environment:
- Cloud provider or hardware configuration: GCP, using GKE
What happened:
My DAG in Airflow 1.10.13 was using a couple of built-in variables and Jinja templating to pass information between KubernetesPodOperator
tasks (e.g. file paths, execution dates); it looked similar to this:
from airflow import DAG
from airflow.contrib.operators.kubernetes_pod_operator import KubernetesPodOperator
default_args = {'owner': 'airflow'}
example_workflow = DAG('my_dag',
default_args=default_args,
default_view='graph',
schedule_interval='@daily')
with example_workflow:
example_k8s_task = KubernetesPodOperator(namespace='my_namespace',
image="ubuntu:latest",
name="pod1",
task_id='my_task_id',
env_vars={'TASK_ID': '{{ task.task_id }}'},
cmds=["echo $TASK_ID"]
)
example_k8s_task
which successfully echoed 'my_task_id'
in the logs when the DAG was triggered and ran successfully.
With the changes to the KubernetesPodOperator
in Airflow 2.0, I followed the guidelines here and refactored the above code to look like this:
from airflow import DAG
from airflow.providers.cncf.kubernetes.operators.kubernetes_pod import KubernetesPodOperator
from kubernetes.client import models as k8s
default_args = {'owner': 'airflow'}
my_env_vars = [
k8s.V1EnvVar(
name='TASK_ID',
value='{{ task.task_id }}'
)]
example_workflow = DAG(dag_id='my_dag',
default_args=default_args,
default_view='graph',
schedule_interval='@daily')
with example_workflow:
example_k8s_task = KubernetesPodOperator(namespace='my_namespace',
image="ubuntu:latest",
name="pod1",
task_id='my_task_id',
env_vars=my_env_vars,
cmds=["echo $TASK_ID"]
)
example_k8s_task
However, this echoed '{{ task.task_id }}'
in the logs, meaning the templated variable did not render properly.
What I expected to happen:
I expected to have the templated variable rendered properly and return the variable value, e.g. 'my_task_id'
.
What I think actually happened:
While KubernetesPodOperator
env_var
parameter is marked as “(templated)” in the source code, when a string is passed in the value
parameter of kubernetes.client
V1EnvVar
object (source code), it does not render the template, but rather treats it as a literal string.
How to reproduce it:
Set up Airflow on minikube or use your cloud provider’s Airflow instance (so easy to set up, I know /s) and run the second Python code snippet above (using Airflow 2.0).
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Reactions:10
- Comments:15 (6 by maintainers)
@phlope yes when we release the next backport provider it will also have this fix.
Hi @shihabcsedu09
I’ve created and linked a PR. Because the KPO lives in a provider once this is merged we can release and you can upgrade for this fix without needing to upgrade your airflow version (just pull the newest provider 😃 )