Skip to content

Push to xcom before KubernetesPodOperator deferral - #34209

Merged
eladkal merged 1 commit into
apache:mainfrom
vchiapaikeo:vchiapaikeo/k8s-def-xcom-v1
Sep 9, 2023
Merged

Push to xcom before KubernetesPodOperator deferral#34209
eladkal merged 1 commit into
apache:mainfrom
vchiapaikeo:vchiapaikeo/k8s-def-xcom-v1

Conversation

@vchiapaikeo

@vchiapaikeo vchiapaikeo commented Sep 8, 2023

Copy link
Copy Markdown
Contributor

Currently, deferrable KPO will not allow users to see logs of the underlying base container until completion (even with get_logs=True). This is because logs are not streamed until the trigger event successfully fires and execute_complete is called. What's worse is that we do not know what the pod_name or the pod_namespace is until execute_complete is called as well. With synchronous KPO, this happens immediately after get_or_create_pod is called. We should do the same with the deferrable flow. Therefore, users can add extra_links to the KPO (retrieving pod_name and pod_namespace from xcom) and stream in-flight logs from their monitoring tool of choice (for us, it is Cloud Monitoring because we use GKE).

This moves the xcom_push calls up to execute_async. To test, I used this dag and observed xcom appear during deferral state.

from airflow import DAG

from airflow.providers.google.cloud.operators.kubernetes_engine import (
    GKEStartPodOperator,
)


DEFAULT_TASK_ARGS = {
    "owner": "gcp-data-platform",
    "start_date": "2021-04-20",
    "retries": 0,
    "retry_delay": 60,
}

with DAG(
    dag_id="test_gke_op",
    schedule_interval="@daily",
    max_active_runs=1,
    max_active_tasks=5,
    catchup=False,
    default_args=DEFAULT_TASK_ARGS,
) as dag:

    whoami = GKEStartPodOperator(
        task_id="whoami",
        name="whoami",
        cmds=["gcloud"],
        arguments=["auth", "list"],
        image="gcr.io/google.com/cloudsdktool/cloud-sdk:slim",
        project_id="redacted-project-id",
        namespace="airflow-default",
        location="us-central1",
        cluster_name="airflow-gke-cluster",
        service_account_name="default",
        is_delete_operator_pod=True,
    )

    sleep_3000 = GKEStartPodOperator(
        task_id="sleep_3000",
        name="sleep-3000",
        cmds=["sleep"],
        arguments=["3000"],
        image="gcr.io/google.com/cloudsdktool/cloud-sdk:slim",
        project_id="redacted-project-id",
        namespace="airflow-default",
        location="us-central1",
        cluster_name="airflow-gke-cluster",
        service_account_name="default",
        is_delete_operator_pod=True,
        deferrable=True,
    )

    sleep_3600 = GKEStartPodOperator(
        task_id="sleep_3600",
        name="sleep-3600",
        cmds=["sleep"],
        arguments=["3600"],
        image="gcr.io/google.com/cloudsdktool/cloud-sdk:slim",
        project_id="redacted-project-id",
        namespace="airflow-default",
        location="us-central1",
        cluster_name="airflow-gke-cluster",
        service_account_name="default",
        is_delete_operator_pod=True,
        deferrable=True,
    )

    sleep_4000 = GKEStartPodOperator(
        task_id="sleep_4000",
        name="sleep-4000",
        cmds=["sleep"],
        arguments=["4000"],
        image="gcr.io/google.com/cloudsdktool/cloud-sdk:slim",
        project_id="redacted-project-id",
        namespace="airflow-default",
        location="us-central1",
        cluster_name="airflow-gke-cluster",
        service_account_name="default",
        is_delete_operator_pod=True,
    )

    sleep_4800 = GKEStartPodOperator(
        task_id="sleep_4800",
        name="sleep-4800",
        cmds=["sleep"],
        arguments=["4800"],
        image="gcr.io/google.com/cloudsdktool/cloud-sdk:slim",
        project_id="redacted-project-id",
        namespace="airflow-default",
        location="us-central1",
        cluster_name="airflow-gke-cluster",
        service_account_name="default",
        is_delete_operator_pod=True,
    )
image image

^ Add meaningful description above
Read the Pull Request Guidelines for more information.
In case of fundamental code changes, an Airflow Improvement Proposal (AIP) is needed.
In case of a new dependency, check compliance with the ASF 3rd Party License Policy.
In case of backwards incompatible changes please leave a note in a newsfragment file, named {pr_number}.significant.rst or {issue_number}.significant.rst, in newsfragments.

@boring-cyborg boring-cyborg Bot added area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues labels Sep 8, 2023
@vchiapaikeo
vchiapaikeo force-pushed the vchiapaikeo/k8s-def-xcom-v1 branch from 66c0927 to 1efa1d3 Compare September 8, 2023 12:32
@vchiapaikeo
vchiapaikeo force-pushed the vchiapaikeo/k8s-def-xcom-v1 branch from 1efa1d3 to cc7e2ea Compare September 8, 2023 12:35
@vchiapaikeo

Copy link
Copy Markdown
Contributor Author

@hussein-awala , @eladkal - thoughts on this when you have a moment please. This would help us out a lot on the observability front when moving KPO to deferrable mode and streaming logs of in flight pods.

@hussein-awala hussein-awala left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for me!

@hussein-awala

Copy link
Copy Markdown
Member

FYI, I'm working on loading the logs in async mode without waiting for pod termination, it will be possible soon.

@vchiapaikeo

Copy link
Copy Markdown
Contributor Author

@eladkal , @potiuk - can you help merge when you have a moment please?

@eladkal eladkal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@eladkal eladkal changed the title Push to xcom before KPO deferral Push to xcom before KubernetesPodOperator deferral Sep 9, 2023
@eladkal
eladkal merged commit b435b8e into apache:main Sep 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:providers provider:cncf-kubernetes Kubernetes (k8s) provider related issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants