我正在使用KubernetesPodOperator
. 在容器中运行的 Python 进程必须打开包含敏感数据的文件:
with open('credentials/jira_credentials.json', 'r') as f:
creds = json.load(f)
并且必须对 CloudStorage 客户端进行身份验证:
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "credentials/cloud_storage_credentials.json"
根据最佳安全实践,我不会将容器的图像与敏感数据打包在一起。相反,我使用Kubernetes Secrets。使用适用于 Kubernetes 的 Python API我正在尝试将它们安装为一个卷,但没有成功。该credentials/
目录存在于容器中,但它是空的。我应该怎么做才能使文件jira_credentials.json和cloud_storage_credentials.json在容器中可访问?
我的 DAG 代码:
from airflow import DAG
from datetime import datetime, timedelta
from airflow.contrib.operators.kubernetes_pod_operator import KubernetesPodOperator
from airflow.kubernetes.secret import Secret
from airflow.kubernetes.volume import Volume
from airflow.kubernetes.volume_mount import VolumeMount
from airflow.operators.dummy_operator import DummyOperator
from kubernetes.client import models as k8s
default_args = {
'owner': 'airflow',
'depends_on_past': False,
'start_date': datetime.utcnow(),
'email': ['airflow@example.com'],
'email_on_failure': False,
'email_on_retry': False,
'retry_delay': timedelta(minutes=5)
}
volume = Volume(name="volume-credentials", configs={})
volume_mnt = VolumeMount(mount_path="/credentials", name="volume-credentials", sub_path="", read_only=True)
secret_jira_user = Secret(deploy_type="volume",
deploy_target="/credentials",
secret="jira-user-secret",
key="jira_credentials.json")
secret_storage_credentials = Secret(deploy_type="volume",
deploy_target="/credentials",
secret="jira-trans-projects-cloud-storage-creds",
key="cloud_storage_credentials.json")
dag = DAG(
dag_id="jira_translations_project",
schedule_interval="0 1 * * MON",
start_date=datetime(2021, 9, 5, 0, 0, 0),
max_active_runs=1,
default_args=default_args
)
start = DummyOperator(task_id='START', dag=dag)
passing = KubernetesPodOperator(namespace='default',
image="eu.gcr.io/data-engineering/jira_downloader:v0.18",
cmds=["/usr/local/bin/run_process.sh"],
name="jira-translation-projects-01",
task_id="jira-translation-projects-01",
get_logs=True,
dag=dag,
volumes=[volume],
volume_mounts=[volume_mnt],
secrets=[
secret_jira_user,
secret_storage_credentials],
env_vars={'MIGRATION_DATETIME': '2021-01-02T03:04:05'},
)
start >> passing