You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "VladaZakharova (via GitHub)" <gi...@apache.org> on 2026/04/09 11:20:17 UTC

[PR] Fix Cloud Composer Сreate environment operator to validate final env state after creation [airflow]

VladaZakharova opened a new pull request, #64956:
URL: https://github.com/apache/airflow/pull/64956

   Fix Cloud Composer environment creation to succeed only when the environment is actually usable.
   
   Previously, create could rely on stale state from the create long-running operation, and the AlreadyExists path could incorrectly treat an environment in a bad state as success. This PR makes the create operator verify the current environment state and fail unless it is RUNNING.
   
   The system test retry flow is also updated to delete environments left in ERROR before retrying creation. 
   
   <!-- SPDX-License-Identifier: Apache-2.0
         https://www.apache.org/licenses/LICENSE-2.0 -->
   
   <!--
   Thank you for contributing!
   
   Please provide above a brief description of the changes made in this pull request.
   Write a good git commit message following this guide: http://chris.beams.io/posts/git-commit/
   
   Please make sure that your code changes are covered with tests.
   And in case of new features or big changes remember to adjust the documentation.
   
   Feel free to ping (in general) for the review if you do not see reaction for a few days
   (72 Hours is the minimum reaction time you can expect from volunteers) - we sometimes miss notifications.
   
   In case of an existing issue, reference it using one of the following:
   
   * closes: #ISSUE
   * related: #ISSUE
   -->
   
   ---
   
   ##### Was generative AI tooling used to co-author this PR?
   
   <!--
   If generative AI tooling has been used in the process of authoring this PR, please
   change below checkbox to `[X]` followed by the name of the tool, uncomment the "Generated-by".
   -->
   
   - [ ] Yes (please specify the tool below)
   
   <!--
   Generated-by: [Tool Name] following [the guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
   -->
   
   ---
   
   * Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information. Note: commit author/co-author name and email in commits become permanently public when merged.
   * For fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   * When adding dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   * For significant user-facing changes create newsfragment: `{pr_number}.significant.rst`, in [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments). You can add this file in a follow-up commit after the PR is created so you know the PR number.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix Cloud Composer Сreate environment operator to validate final env state after creation [airflow]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #64956:
URL: https://github.com/apache/airflow/pull/64956#discussion_r3066483323


##########
providers/google/tests/unit/google/cloud/operators/test_cloud_composer.py:
##########
@@ -77,9 +79,18 @@
 
 
 class TestCloudComposerCreateEnvironmentOperator:
+    @staticmethod
+    def _mock_environment(state: int) -> mock.MagicMock:
+        environment = mock.MagicMock()
+        environment.state = state
+        return environment

Review Comment:
   New helper `_mock_environment` creates a `MagicMock()` without `spec`/`autospec`, which can hide attribute/typing mistakes in tests. Consider using a real `Environment` instance or `mock.MagicMock(spec=Environment)` (or `spec_set`) so the test double matches the production object more closely.
   



##########
providers/google/src/airflow/providers/google/cloud/operators/cloud_composer.py:
##########
@@ -137,6 +137,23 @@ def extra_links_params(self) -> dict[str, Any]:
             "environment_id": self.environment_id,
         }
 
+    def _raise_if_environment_not_running(self, environment: Environment) -> None:
+        env_state = Environment.State(environment.state)
+        self.log.info("Environment state: %s", env_state.name)
+
+        self.log.info(
+            "Composer environment final state: raw=%s, name=%s, value=%s",
+            environment.state,
+            env_state.name,
+            env_state.value,
+        )
+
+        if env_state != Environment.State.RUNNING:
+            raise AirflowException(
+                "Create environment operation completed, but the Composer environment is not in RUNNING state. "

Review Comment:
   `_raise_if_environment_not_running` is also called from the `AlreadyExists` path, but the raised message says "Create environment operation completed". This makes the error misleading when the environment pre-exists; consider rephrasing the exception text to be context-agnostic (e.g., "Composer environment is not in RUNNING state").
   



##########
providers/google/tests/system/google/cloud/composer/example_cloud_composer.py:
##########
@@ -104,6 +110,61 @@ def get_project_number():
         raise exc
 
 
+# [START howto_operator_composer_retry_cleanup]
+def cleanup_failed_environment_before_retry(context: dict[str, Any]) -> None:
+    task = context["task"]
+
+    hook = CloudComposerHook(
+        gcp_conn_id=task.gcp_conn_id,
+        impersonation_chain=task.impersonation_chain,
+    )
+
+    environment_id = task.environment_id
+
+    log.info(
+        "Retry cleanup started for Composer env. project_id=%s region=%s environment_id=%s",
+        PROJECT_ID,
+        REGION,
+        environment_id,
+    )
+
+    try:
+        environment = hook.get_environment(
+            project_id=PROJECT_ID,
+            region=REGION,
+            environment_id=environment_id,
+        )

Review Comment:
   `cleanup_failed_environment_before_retry` uses module-level `PROJECT_ID`/`REGION` instead of the retried task's `project_id`/`region`. If the operator is configured differently (templating, different region), this callback could query/delete the wrong environment. Consider reading `project_id`/`region` from `context['task']` for correctness and reusability.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix Cloud Composer Сreate environment operator to validate final env state after creation [airflow]

Posted by "shahar1 (via GitHub)" <gi...@apache.org>.
shahar1 commented on PR #64956:
URL: https://github.com/apache/airflow/pull/64956#issuecomment-4297465020

   @VladaZakharova Could you please fix failing static checks? (direct usage of `AirflowException` is deprecated and should be replaced with native Python exception) 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix Cloud Composer Сreate environment operator to validate final env state after creation [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk merged PR #64956:
URL: https://github.com/apache/airflow/pull/64956


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix Cloud Composer Сreate environment operator to validate final env state after creation [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk commented on PR #64956:
URL: https://github.com/apache/airflow/pull/64956#issuecomment-4301025473

   @VladaZakharova Thanks for your contribution. This PR has a small deterministic issue to address before review — please see our [Pull Request quality criteria](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-quality-criteria).
   
   **Issues found:**
   - :x: **Failing CI**: `CI image checks / Static checks`. Please investigate and fix.
   
   Leaving the PR open (not converting to draft) since the fix is small and deterministic. Push the fix and CI will re-run automatically.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Fix Cloud Composer Сreate environment operator to validate final env state after creation [airflow]

Posted by "VladaZakharova (via GitHub)" <gi...@apache.org>.
VladaZakharova commented on PR #64956:
URL: https://github.com/apache/airflow/pull/64956#issuecomment-4295648712

   @shahar1 Hello there! can you please check this PR?
   The comments from Copilot were adrressed 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org