Skip to content

feat: Add GCP_GKE and WATCHER_GCP_GKE execution modes#2488

Open
vricciardulli wants to merge 40 commits into
astronomer:mainfrom
vricciardulli:gcp-modes
Open

feat: Add GCP_GKE and WATCHER_GCP_GKE execution modes#2488
vricciardulli wants to merge 40 commits into
astronomer:mainfrom
vricciardulli:gcp-modes

Conversation

@vricciardulli
Copy link
Copy Markdown
Contributor

@vricciardulli vricciardulli commented Mar 21, 2026

Description

Added GCP_GKE and WATCHER_GCP_GKE execution modes.

It's the same as KUBERNETES and WATCHER_KUBERNETES modes, but uses GKE operators (i.e. GKEStartPodOperator).

This enables a user to use cosmos in Cloud Composer 3 with a self-managed custom GKE cluster.

Tests:

  • Added unit tests
  • Tested in GCP Cloud Composer 3 with a custom GKE cluster. It works.
  • Did not add any integration test.

Implementation details:

  • Given the amount of duplication between the k8s modes and the new GKE ones, I've extracted all common code and placed it in helper functions in cosmos/operators/_k8s_common.py.
  • I've attempted to type hint as best as possible, but I had to ignore some type-checker errors. I've marked those with comments.
  • An alternative approach would have been to use a factory to generate the classes based on the pod operator provided, but I think that this approach would have been too opaque.

Docs:

  • I did not add separate pages documenting these modes.
  • I concisely added details of GKE modes in the k8s and watcher k8s pages.
  • Also added to the mermaid graph.

Co-Authored-By: Claude Opus 4.6 (1M context) noreply@anthropic.com

Related Issue(s)

closes #2487
closes #2379

Breaking Change?

There should not be a breaking change.

Checklist

  • I have made corresponding changes to the documentation (if required)
  • I have added tests that prove my fix is effective or that my feature works

vricciardulli and others added 3 commits March 19, 2026 11:02
Add two new execution modes enabling dbt execution on GKE clusters:
- GCP_GKE: standard mode using GKEStartPodOperator
- WATCHER_GCP_GKE: watcher mode with producer/consumer pattern

Includes all operators, watcher callbacks, graph.py watcher mappings,
and comprehensive unit/integration tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds new execution modes so Cosmos can run dbt tasks on self-managed GKE clusters (e.g., Cloud Composer 3 on custom GKE) by using GKEStartPodOperator and a watcher variant.

Changes:

  • Introduce ExecutionMode.GCP_GKE and ExecutionMode.WATCHER_GCP_GKE.
  • Add GKE-backed dbt operators (cosmos/operators/gcp_gke.py) and watcher counterparts (cosmos/operators/watcher_gcp_gke.py).
  • Wire new watcher mode into graph-building logic and add unit/integration tests.

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
cosmos/constants.py Adds the two new execution modes.
cosmos/airflow/graph.py Ensures watcher graph building and AFTER_ALL test behavior work with WATCHER_GCP_GKE.
cosmos/operators/gcp_gke.py Implements dbt operators backed by GKEStartPodOperator.
cosmos/operators/watcher_gcp_gke.py Implements watcher producer/consumer behavior for GKE.
tests/operators/test_gcp_gke.py Unit tests for GCP GKE operator command construction and execute path.
tests/operators/test_watcher_gcp_gke_unit.py Unit tests for watcher GCP GKE retry/callback behavior.
tests/operators/test_watcher_gcp_gke_integration.py Integration test for DbtDag with WATCHER_GCP_GKE.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/operators/test_gcp_gke.py
Comment thread cosmos/operators/gcp_gke.py Outdated
Comment thread cosmos/operators/watcher_gcp_gke.py Outdated
Copilot AI review requested due to automatic review settings March 21, 2026 10:41
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tests/operators/test_watcher_gcp_gke_integration.py Outdated
Comment thread cosmos/operators/gcp_gke.py Outdated
Comment thread cosmos/operators/watcher_gcp_gke.py Outdated
Copilot AI review requested due to automatic review settings March 21, 2026 12:47

# Get the logs from the pod
logs = []
for log in task.pod_manager.read_pod_logs(pod, "base"): # type: ignore[attr-defined]
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Type checker error:

error: "BaseOperator" has no attribute "pod_manager"  [attr-defined]

was not getting this error before the refactoring

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 18 out of 18 changed files in this pull request and generated 6 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/guides/run_dbt/container/kubernetes.rst Outdated
Comment thread docs/guides/run_dbt/container/kubernetes.rst Outdated
Comment thread docs/guides/run_dbt/container/watcher-kubernetes-execution-mode.rst Outdated
Comment thread tests/operators/test_watcher_gcp_gke_integration.py Outdated
Comment thread tests/operators/test_watcher_gcp_gke_integration.py Outdated
Comment thread cosmos/operators/_k8s_common.py
Copilot AI review requested due to automatic review settings April 1, 2026 16:52
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cosmos/operators/_k8s_common.py
Comment thread tests/operators/test_k8s_common.py
Comment on lines 7 to +13
.. versionadded:: 1.13.0

The ``ExecutionMode.WATCHER_KUBERNETES`` combines the **speed of the** :ref:`watcher-execution-mode` **with the isolation of** :ref:`kubernetes`.

A GCP GKE variant is also available as ``ExecutionMode.WATCHER_GCP_GKE``, which uses
``GKEStartPodOperator`` instead of ``KubernetesPodOperator``. See :ref:`watcher-gcp-gke` below.

Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

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

The page-level .. versionadded:: 1.13.0 now applies to the newly documented ExecutionMode.WATCHER_GCP_GKE as well, but this mode is introduced in this PR (later than 1.13.0). Consider adding a separate .. versionadded:: <new version> note for WATCHER_GCP_GKE (or rewording the existing directive) to avoid implying it has been available since 1.13.0.

Copilot uses AI. Check for mistakes.
@tatiana
Copy link
Copy Markdown
Collaborator

tatiana commented Apr 7, 2026

Thank you so much for these improvements, @vricciardulli ! They look really valuable and will definitely enhance the project.

We’ve had a heavier workload than expected over the past few weeks, so we haven’t been able to review everything immediately. We’ll be reviewing this PR thoroughly in the upcoming month and are considering including these changes in the upcoming 1.15.0 release.

Really appreciate your patience and contribution!

In the meantime, please, could you address Copilot's feedback and also rebase with the latest changes on main?

@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 7, 2026

Codecov Report

❌ Patch coverage is 95.45455% with 14 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.89%. Comparing base (0ba7fde) to head (eb9c56e).
⚠️ Report is 59 commits behind head on main.

Files with missing lines Patch % Lines
cosmos/operators/watcher_gcp_gke.py 88.67% 6 Missing ⚠️
cosmos/operators/gcp_gke.py 91.52% 5 Missing ⚠️
cosmos/operators/_k8s_common.py 98.35% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2488      +/-   ##
==========================================
- Coverage   97.92%   97.89%   -0.03%     
==========================================
  Files         103      106       +3     
  Lines        7312     7460     +148     
==========================================
+ Hits         7160     7303     +143     
- Misses        152      157       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@vricciardulli
Copy link
Copy Markdown
Contributor Author

Thank you so much for these improvements, @vricciardulli ! They look really valuable and will definitely enhance the project.

We’ve had a heavier workload than expected over the past few weeks, so we haven’t been able to review everything immediately. We’ll be reviewing this PR thoroughly in the upcoming month and are considering including these changes in the upcoming 1.15.0 release.

Really appreciate your patience and contribution!

In the meantime, please, could you address Copilot's feedback and also rebase with the latest changes on main?

@tatiana I'll rebase and clean up once #2543 is merged to main

Copilot AI review requested due to automatic review settings May 12, 2026 22:34
@vricciardulli vricciardulli review requested due to automatic review settings May 12, 2026 22:34
Copilot AI review requested due to automatic review settings May 12, 2026 22:55
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 16 out of 16 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

cosmos/operators/kubernetes.py:253

  • DbtDocsCloudKubernetesOperator.build_and_run_cmd builds the shell command from self.arguments only. After the refactor that splits the executable into self.cmds + self.arguments, this drops the dbt executable entirely (e.g., produces docs generate ...), which will fail at runtime. Build the command string from self.cmds + self.arguments (or otherwise ensure dbt is included) before overriding self.cmds/self.arguments for the bash wrapper.

Comment on lines +29 to +65
from cosmos.log import get_logger
from cosmos.operators._watcher.xcom import (
_backup_xcom_to_variable,
_delete_xcom_backup_variable,
_init_xcom_backup,
_restore_xcom_from_variable,
)

if TYPE_CHECKING: # pragma: no cover
from pendulum import DateTime

if TYPE_CHECKING: # pragma: no cover
try:
from airflow.sdk.definitions.context import Context
except ImportError:
from airflow.utils.context import Context # type: ignore[attr-defined]
try:
# apache-airflow-providers-cncf-kubernetes >= 7.14.0
from airflow.providers.cncf.kubernetes.callbacks import KubernetesPodOperatorCallback
except ImportError:

class KubernetesPodOperatorCallback: # type: ignore[no-redef]
"""Mock fallback for older versions. Should not be used in practice."""

pass


from cosmos.dbt.parser.output import extract_log_issues
from cosmos.operators._watcher.base import store_dbt_resource_status_from_log
from cosmos.operators.base import AbstractDbtBase

try:
from airflow.sdk.bases.operator import BaseOperator # Airflow 3
except ImportError:
from airflow.models import BaseOperator # Airflow 2

logger = get_logger(__name__)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Kept because it was here also before the refactoring

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add GCP_GKE and WATCHER_GCP_GKE execution modes [Feature] Add support for GKEStartPodOperator (again)

4 participants