Skip to content

Instantly share code, notes, and snippets.

@Dminor7
Created July 24, 2021 12:03
Show Gist options
  • Save Dminor7/87a0cf60dff5a18b7baa90b56521d606 to your computer and use it in GitHub Desktop.
Save Dminor7/87a0cf60dff5a18b7baa90b56521d606 to your computer and use it in GitHub Desktop.
KubernetesPodOperator with Volume mounted in read_only.
from datetime import datetime
from airflow import models
from kubernetes.client import models as k8s
from airflow.providers.cncf.kubernetes.operators.kubernetes_pod import (
KubernetesPodOperator,
)
volume_mount = k8s.V1VolumeMount(
name="my-volume",
mount_path="/mnt/disk",
sub_path=None,
read_only=True,
)
volume = k8s.V1Volume(
name="my-volume",
persistent_volume_claim=k8s.V1PersistentVolumeClaimVolumeSource(
claim_name="my-volume-claim",
read_only=True # Pod Claims for volume in read-only access mode.
),
)
with models.DAG(
dag_id="my-dag",
schedule_interval=None,
start_date=datetime.datetime(2021, 7, 19),
) as dag:
kubernetes_min_pod = KubernetesPodOperator(
task_id="my_task",
name="my_pod_name",
cmds=[...],
startup_timeout_seconds=300,
arguments=[...],
env_vars={
"PATH_TO_DATA_DIR": "/mnt/disk/data", # ENV will be populated in the container.
},
namespace=...,
image=...,
volumes=[volume],
volume_mounts=[volume_mount],
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment