question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Templated Variables being treated as String by KubernetesPodOperator in Airflow 2.0

See original GitHub issue

I’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:closed
  • Created 3 years ago
  • Reactions:10
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
dimbermancommented, Feb 9, 2021

@phlope yes when we release the next backport provider it will also have this fix.

1reaction
dimbermancommented, Feb 4, 2021

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 😃 )

Read more comments on GitHub >

github_iconTop Results From Across the Web

[GitHub] [airflow] andreychernih commented on issue #13522 ...
[GitHub] [airflow] andreychernih commented on issue #13522: Templated Variables being treated as String by KubernetesPodOperator in Airflow 2.0.
Read more >
apache-airflow-providers-cncf-kubernetes Documentation
Previously KubernetesPodOperator considered some settings from the Airflow config's kubernetes section. Such consideration was deprecated in 4.1.0 and is ...
Read more >
Use the KubernetesPodOperator | Cloud Composer
The var template variable allows you to access variables defined in # Airflow UI. In this case we are getting the value of...
Read more >
Construct list using jinja2 for KubernetesPodOperator in ...
Of course the variable were not available in the example (which had been ... env = NativeEnvironment() template = env.from_string(arguments) ...
Read more >
org.apache.airflow.commits - 2021 January - 5819 messages
[GitHub] [airflow] SamWheating edited a comment on issue #13522: Templated Variables being treated as String by KubernetesPodOperator in Airflow 2.0 - ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found