带有 KubernetesPodOperator 的 airflow2.0:TemplateNotFound

airflow2.0 with KubernetesPodOperator: TemplateNotFound

我正在使用 Airflow2.0 KubernetesPodOperator 想要 运行 一个命令,该命令使用操作员来自图像 运行 的文件作为参数。这是我用的:

KubernetesPodOperator(
    namespace=commons.kubernetes_namespace,
    labels=commons.labels,
    image=f"myregistry.io/myimage:{config['IMAGE_TAG']}",
    arguments=[
        "python",
        "run_module.py ",
        "-i",
        f'args/{config["INPUT_DIR"]}/{task_id}.json'
    ],
    name=dag_name + task_id,
    task_id=task_id,
    secrets=[secret_volume]
)

但这给了我错误:

raise TemplateNotFound(template)
jinja2.exceptions.TemplateNotFound: args/airflow2test/processing-pipeline.json

图像没有使用任何宏。

有人知道吗?我做错了什么?

这是一个以 PR released in version 2.0.0 of apache-airflow-providers-cncf-kubernetes 开头的错误。 更改的目的是允许对 .json 文件进行模板化。有一个 GitHub issue about the problems it created. The bug was eventually resolved by PR 在提供程序的 2.0.2 版本中发布。

解决方案:

  1. 升级到最新的apache-airflow-providers-cncf-kubernetes(目前是2.0.2)
  2. 如果升级不是一个选项,请使用自定义 KubernetesPodOperator

有两种方法可以解决该问题,一种是更改 template_fields,另一种是更改 template_ext:

第一个选项:正如 raphaelauv 在 issue 上发布的那样,不允许呈现 arguments 字段:

class MyKubernetesPodOperator(KubernetesPodOperator):
    template_fields = tuple(x for x in KubernetesPodOperator.template_fields if x != "arguments")

第二个选项:如果您不想渲染 .json 个文件:

class MyKubernetesPodOperator(KubernetesPodOperator):
    template_ext = ('.yaml', '.yml',)