shahar1 commented on code in PR #74236: URL: https://github.com/apache/airflow/pull/74236#discussion_r4500472147
########## providers/google/tests/system/google/resources_cleanup/airflow_google_provider_resource_cleanup/handlers/alloydb.py: ########## @@ -0,0 +1,85 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import asyncio + +from airflow_google_provider_resource_cleanup.handlers._base import BaseDeleteHandler +from airflow_google_provider_resource_cleanup.helpers import get_resource_path +from google.api_core.exceptions import NotFound +from google.cloud import alloydb_v1 + +CLUSTER_ASSET_TYPE = "alloydb.googleapis.com/Cluster" +# Not a Cloud Asset type: secondary clusters are re-labelled with it so that they get their own +# deletion pass. AlloyDB refuses to delete a primary cluster that still has secondaries, and +# force=True only cascades to instances. +SECONDARY_CLUSTER_ASSET_TYPE = "alloydb.googleapis.com/Cluster#secondary" + + +async def _delete_resource(resource: dict, request_type, delete_method_name: str, **request_kwargs): + client = alloydb_v1.AlloyDBAdminAsyncClient() + request = request_type(name=get_resource_path(resource), **request_kwargs) + operation = await getattr(client, delete_method_name)(request=request) + await operation.result() + + +async def _delete_backup(resource: dict, log_prefix: str): + await _delete_resource(resource, alloydb_v1.DeleteBackupRequest, "delete_backup") + + +async def _delete_instance(resource: dict, log_prefix: str): + await _delete_resource(resource, alloydb_v1.DeleteInstanceRequest, "delete_instance") + + +async def _delete_cluster(resource: dict, log_prefix: str): + await _delete_resource(resource, alloydb_v1.DeleteClusterRequest, "delete_cluster", force=True) Review Comment: **major:** `force=True` also deletes instances the tool decided to keep. `handle_asset_type` filters each resource separately, on `protected_resources`, protection labels and `--min-age-days`, so an instance can be filtered out while its cluster stays in the list. For example, with `--min-age-days 3`, a 10-day-old cluster with a 1-day-old instance: the instance is skipped, then this call deletes it anyway. The same happens to an instance listed in `protected_resources` or carrying a protected label. The README describes the Data Fusion handler avoiding exactly this: > Instance deletion does not use the API's `force` option, so an instance with remaining nested resources is left in place instead of bypassing resource protection or minimum-age filtering. Dropping `force` won't work here, because [a secondary instance can only be deleted with its cluster](https://github.com/hashicorp/terraform-provider-google/blob/main/website/docs/r/alloydb_cluster.html.markdown) and [a primary instance can't be deleted while read pools exist](https://cloud.google.com/alloydb/docs/instance-delete). Could `_delete_cluster` list the cluster's instances and skip the cluster, printing why, when one of them wasn't in the resources passed to `handle`? If you'd rather keep the cascade, please say in the README that it overrides protection and the age filter for instances. ########## providers/google/tests/system/google/resources_cleanup/airflow_google_provider_resource_cleanup/handlers/alloydb.py: ########## @@ -0,0 +1,85 @@ +# +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +import asyncio + +from airflow_google_provider_resource_cleanup.handlers._base import BaseDeleteHandler +from airflow_google_provider_resource_cleanup.helpers import get_resource_path +from google.api_core.exceptions import NotFound +from google.cloud import alloydb_v1 + +CLUSTER_ASSET_TYPE = "alloydb.googleapis.com/Cluster" +# Not a Cloud Asset type: secondary clusters are re-labelled with it so that they get their own +# deletion pass. AlloyDB refuses to delete a primary cluster that still has secondaries, and +# force=True only cascades to instances. +SECONDARY_CLUSTER_ASSET_TYPE = "alloydb.googleapis.com/Cluster#secondary" + + +async def _delete_resource(resource: dict, request_type, delete_method_name: str, **request_kwargs): + client = alloydb_v1.AlloyDBAdminAsyncClient() + request = request_type(name=get_resource_path(resource), **request_kwargs) + operation = await getattr(client, delete_method_name)(request=request) + await operation.result() + + +async def _delete_backup(resource: dict, log_prefix: str): + await _delete_resource(resource, alloydb_v1.DeleteBackupRequest, "delete_backup") + + +async def _delete_instance(resource: dict, log_prefix: str): + await _delete_resource(resource, alloydb_v1.DeleteInstanceRequest, "delete_instance") + + +async def _delete_cluster(resource: dict, log_prefix: str): + await _delete_resource(resource, alloydb_v1.DeleteClusterRequest, "delete_cluster", force=True) + + +async def _is_secondary_cluster(resource: dict) -> bool: + if resource.get("assetType") != CLUSTER_ASSET_TYPE: + return False + client = alloydb_v1.AlloyDBAdminAsyncClient() + try: + cluster = await client.get_cluster(name=get_resource_path(resource)) + except NotFound: Review Comment: **minor:** Only `NotFound` is handled, and the `gather` in `handle` has no `return_exceptions=True`. Any other API error from one lookup (`PermissionDenied`, `ResourceExhausted`, `InternalServerError`) leaves `handle` before anything is deleted. `BaseDeleteHandler.handle` prints deletion errors and carries on, but this error reaches the catch-all in `__main__`, which exits. In a full `delete` run, that also skips every asset type after `alloydb`, which is second in `ASSET_TYPES`. Could this catch `GoogleAPICallError` (the parent of `NotFound`), print it and return `False`? A secondary misread as a primary is safe: the primary's delete fails with a printed error, and the next run removes it. ########## providers/google/tests/system/google/resources_cleanup/airflow_google_provider_resource_cleanup/helpers.py: ########## @@ -139,7 +139,7 @@ def get_resource_name_for_compute(resource: dict) -> str: return resource_path.replace("//compute.googleapis.com/", "") -async def curl(url, method="DELETE", log_prefix=""): +async def curl(url, log_prefix="", method="DELETE"): Review Comment: **nit:** The Dataform bug is fixed twice. The call sites now pass `log_prefix=`, and that alone is enough, since `datafusion.py` and `sqladmin.py` already use the keyword. After this PR no caller passes `log_prefix` positionally, so the new `test_curl_accepts_log_prefix_as_second_positional_argument` pins an order nothing relies on. I'd keep the keyword calls, revert this line, and replace that test with one that checks a Dataform deleter issues `curl -X DELETE`. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
