Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Fixed VPC Connector usage in Cloud Run Worker v2 (#252)
Browse files Browse the repository at this point in the history
* added vpcAccess to job body, and function to fill it in

* πŸ€– TEST: added tests for new vpc function

* πŸ› FIX: fix failing pre-commit checks
  • Loading branch information
japerry911 committed Mar 5, 2024
1 parent 0898eeb commit f742dd4
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions prefect_gcp/workers/cloud_run_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def _get_default_job_body_template() -> Dict[str, Any]:
"serviceAccount": "{{ service_account_name }}",
"maxRetries": "{{ max_retries }}",
"timeout": "{{ timeout }}",
"vpcAccess": "{{ vpc_connector_name }}",
"containers": [
{
"env": [],
Expand Down Expand Up @@ -183,6 +184,7 @@ def prepare_for_flow_run(
self._format_args_if_present()
self._populate_image_if_not_present()
self._populate_timeout()
self._populate_vpc_if_present()

def _populate_timeout(self):
"""
Expand Down Expand Up @@ -233,6 +235,15 @@ def _format_args_if_present(self):
"args"
] = shlex.split(args)

def _populate_vpc_if_present(self):
"""
Populates the job body with the VPC connector if present.
"""
if self.job_body["template"]["template"].get("vpcAccess") is not None:
self.job_body["template"]["template"]["vpcAccess"] = {
"connector": self.job_body["template"]["template"]["vpcAccess"],
}

# noinspection PyMethodParameters
@validator("job_body")
def _ensure_job_includes_all_required_components(cls, value: Dict[str, Any]):
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cloud_run_worker_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def job_body():
"template": {
"maxRetries": None,
"timeout": None,
"vpcAccess": "projects/my_project/locations/us-central1/connectors/my-connector", # noqa: E501
"containers": [
{
"env": [],
Expand Down Expand Up @@ -120,3 +121,13 @@ def test_format_args_if_present(self, cloud_run_worker_v2_job_config):
assert cloud_run_worker_v2_job_config.job_body["template"]["template"][
"containers"
][0]["args"] == ["-m", "prefect.engine"]

def test_populate_vpc_if_present(self, cloud_run_worker_v2_job_config):
cloud_run_worker_v2_job_config._populate_vpc_if_present()

assert (
cloud_run_worker_v2_job_config.job_body["template"]["template"][
"vpcAccess"
]["connector"]
== "projects/my_project/locations/us-central1/connectors/my-connector"
)

0 comments on commit f742dd4

Please sign in to comment.