Skip to content

Commit 081b19b

Browse files
Private Service Connect integration (GoogleCloudPlatform#7162)
Co-authored-by: Leah E. Cole <6719667+leahecole@users.noreply.github.com>
1 parent 66476e5 commit 081b19b

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

composer/tools/composer_db_transfer.py

+27-12
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import typing
2727
import uuid
2828

29-
SCRIPT_VERSION = "1.2"
29+
SCRIPT_VERSION = "1.3"
3030

3131
USAGE = r"""This script handles database transfer for Cloud Composer
3232
(Airflow 1.10.14/15 -> Airflow 2.0.1+).
@@ -3084,7 +3084,7 @@ def _get_environment_variable_from_container_description(
30843084
variable_name: str,
30853085
container_description: typing.Dict[typing.Any, typing.Any],
30863086
) -> str:
3087-
"""Reads environment variable from monitoring"""
3087+
"""Reads environment variable from container description"""
30883088
for variable in container_description["env"]:
30893089
if variable["name"] == variable_name:
30903090
logger.info(
@@ -3093,7 +3093,7 @@ def _get_environment_variable_from_container_description(
30933093
variable["value"],
30943094
)
30953095
return variable["value"]
3096-
raise Exception(
3096+
raise KeyError(
30973097
f"Environment variable {variable} could not be found in the container."
30983098
)
30993099

@@ -3138,6 +3138,13 @@ def _read_environment_variables_from_monitoring_pod(self: typing.Any) -> None:
31383138
monitoring_container_description,
31393139
)
31403140
)
3141+
try:
3142+
self.sql_proxy = self._get_environment_variable_from_container_description(
3143+
"SQL_HOST",
3144+
monitoring_container_description,
3145+
)
3146+
except KeyError:
3147+
self.sql_proxy = None
31413148

31423149
def _read_fernet_key(self: typing.Any) -> None:
31433150
self.fernet_key = EnvironmentUtils.read_env_variable_from_container(
@@ -3172,15 +3179,23 @@ def _check_composer_system_namespace(self: typing.Any) -> None:
31723179

31733180
def _check_cloud_sql_proxy(self: typing.Any) -> None:
31743181
"""Sets sql proxy."""
3175-
namespace = (
3176-
"composer-system" if self.composer_system_namespace_exists else "default"
3177-
)
3178-
self.sql_proxy = f"airflow-sqlproxy-service.{namespace}.svc.cluster.local"
3179-
logger.info(
3180-
"composer-system %s -> sql proxy: %s",
3181-
"exists" if self.composer_system_namespace_exists else "does not exist",
3182-
self.sql_proxy,
3183-
)
3182+
if self.sql_proxy is not None:
3183+
logger.info(
3184+
"sql proxy as provided by SQL_HOST: %s",
3185+
self.sql_proxy,
3186+
)
3187+
else:
3188+
namespace = (
3189+
"composer-system"
3190+
if self.composer_system_namespace_exists
3191+
else "default"
3192+
)
3193+
self.sql_proxy = f"airflow-sqlproxy-service.{namespace}.svc.cluster.local"
3194+
logger.info(
3195+
"composer-system %s -> sql proxy: %s",
3196+
"exists" if self.composer_system_namespace_exists else "does not exist",
3197+
self.sql_proxy,
3198+
)
31843199

31853200
def _check_drs_and_select_db_storage_bucket(self: typing.Any) -> None:
31863201
"""Checks if the environment is DRS-compliant."""

0 commit comments

Comments
 (0)