Airflow KubernetesPodOperator:如何访问传递给Pod的秘密?

时间:2021-03-24 19:09:48

标签: python kubernetes airflow kubernetes-secrets kubernetespodoperator

我试图在气流中将秘密变量传递给我的 KubernetesPodOperator

这是我所做的:

  1. 创建一个如下所示的 secret.yaml 文件
apiVersion: v1
kind: Secret
metadata:
  name: my-secret
type: Opaque
data:
  SECRET_1: blabla 
  SECRET_2: blibli 
  1. 应用秘密:
kubectl apply -f ./secret.yaml
  1. 从我的 DAG 文件中检索机密:
from airflow.contrib.kubernetes.secret import Secret
from airflow.contrib.operators.kubernetes_pod_operator import KubernetesPodOperator
from airflow.models import DAG

SECRET_1 = Secret(
   deploy_type="env", deploy_target="SECRET_1", secret="ai-controller-object-storage", key="SECRET_1"
)
SECRET_2 = Secret(
   deploy_type="env", deploy_target="SECRET_2", secret="ai-controller-object-storage", key="SECRET_2"
)

with DAG(...) as dag:
   KubernetesPodOperator(
           task_id=..,
           trigger_rule="all_success",
           namespace="default",
           image=IMAGE,
           startup_timeout_seconds=600,
           secrets=[
               SECRET_1,
               SECRET_2], ...)

所以现在据我所知,我应该从 SECRET_1

访问 KubernetesPodOperator 作为容器中的环境变量

但是,我从 python 脚本(使用 os.environ["SECRET_1"])执行的第一个任务返回一个错误,表明该环境变量不存在:

KeyError: 'SECRET_1'

我怎样才能从我的 python 脚本访问这个变量?

0 个答案:

没有答案