Skip to content

Commit 091b26d

Browse files
authored
Fixing custom audience attribute for cloud run service. (#748)
* Fixing custom audience for cloud run service. * Added more comments. ---------
1 parent 9e729ca commit 091b26d

File tree

1 file changed

+97
-32
lines changed

1 file changed

+97
-32
lines changed

metamist_infrastructure/driver.py

+97-32
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,83 @@ def _private_repo_url(self):
602602
lambda args: f'https://{args[0]}-python.pkg.dev/{args[1]}/{args[2]}/simple/'
603603
)
604604

605+
def _etl_external_function(
606+
self,
607+
f_name: str,
608+
docker_image_url: str,
609+
sa: gcp.serviceaccount.Account,
610+
custom_audiences: list[str] | None,
611+
):
612+
"""
613+
Create External Function with custom audiences
614+
"""
615+
return gcp.cloudrunv2.Service(
616+
f'metamist-etl-{f_name}-external',
617+
name=f'metamist-etl-{f_name}-external',
618+
project=self.config.metamist.gcp.project,
619+
location=self.config.gcp.region,
620+
custom_audiences=custom_audiences,
621+
ingress='INGRESS_TRAFFIC_ALL',
622+
template=gcp.cloudrunv2.ServiceTemplateArgs(
623+
containers=[
624+
gcp.cloudrunv2.ServiceTemplateContainerArgs(
625+
image=docker_image_url,
626+
resources=gcp.cloudrunv2.ServiceTemplateContainerResourcesArgs(
627+
cpu_idle=True,
628+
startup_cpu_boost=True,
629+
limits={
630+
'cpu': '1',
631+
'memory': '2Gi',
632+
},
633+
),
634+
envs=[
635+
gcp.cloudrunv2.ServiceTemplateContainerEnvArgs(
636+
name=k,
637+
value=v,
638+
)
639+
for k, v in self._etl_get_env().items()
640+
],
641+
)
642+
],
643+
scaling=gcp.cloudrunv2.ServiceTemplateScalingArgs(
644+
max_instance_count=1,
645+
min_instance_count=0,
646+
),
647+
timeout='540s',
648+
service_account=sa.email,
649+
max_instance_request_concurrency=1,
650+
),
651+
)
652+
653+
def _etl_get_env(self) -> dict:
654+
"""
655+
Commnon environment to all the etl functions and services
656+
"""
657+
return {
658+
'BIGQUERY_TABLE': pulumi.Output.concat(
659+
self.etl_bigquery_table.project,
660+
'.',
661+
self.etl_bigquery_table.dataset_id,
662+
'.',
663+
self.etl_bigquery_table.table_id,
664+
),
665+
'BIGQUERY_LOG_TABLE': pulumi.Output.concat(
666+
self.etl_bigquery_log_table.project,
667+
'.',
668+
self.etl_bigquery_log_table.dataset_id,
669+
'.',
670+
self.etl_bigquery_log_table.table_id,
671+
),
672+
'PUBSUB_TOPIC': self.etl_pubsub_topic.id,
673+
'NOTIFICATION_PUBSUB_TOPIC': (
674+
self.etl_slack_notification_topic.id
675+
if self.etl_slack_notification_topic
676+
else ''
677+
),
678+
'SM_ENVIRONMENT': self.config.metamist.etl.environment,
679+
'CONFIGURATION_SECRET': self.etl_configuration_secret_version.id,
680+
}
681+
605682
def _etl_function(
606683
self,
607684
f_name: str,
@@ -655,8 +732,8 @@ def _etl_function(
655732
self.config.metamist.etl.custom_audience_list
656733
and self.config.metamist.etl.custom_audience_list.get(f_name)
657734
):
658-
custom_audience_list = json.dumps(
659-
self.config.metamist.etl.custom_audience_list.get(f_name)
735+
custom_audience_list = self.config.metamist.etl.custom_audience_list.get(
736+
f_name
660737
)
661738

662739
fxn = gcp.cloudfunctionsv2.Function(
@@ -682,36 +759,7 @@ def _etl_function(
682759
available_memory='2Gi',
683760
available_cpu='1',
684761
timeout_seconds=540,
685-
environment_variables={
686-
# format: 'project.dataset.table_id
687-
'BIGQUERY_TABLE': pulumi.Output.concat(
688-
self.etl_bigquery_table.project,
689-
'.',
690-
self.etl_bigquery_table.dataset_id,
691-
'.',
692-
self.etl_bigquery_table.table_id,
693-
),
694-
'BIGQUERY_LOG_TABLE': pulumi.Output.concat(
695-
self.etl_bigquery_log_table.project,
696-
'.',
697-
self.etl_bigquery_log_table.dataset_id,
698-
'.',
699-
self.etl_bigquery_log_table.table_id,
700-
),
701-
'PUBSUB_TOPIC': self.etl_pubsub_topic.id,
702-
'NOTIFICATION_PUBSUB_TOPIC': (
703-
self.etl_slack_notification_topic.id
704-
if self.etl_slack_notification_topic
705-
else ''
706-
),
707-
'SM_ENVIRONMENT': self.config.metamist.etl.environment,
708-
'CONFIGURATION_SECRET': self.etl_configuration_secret_version.id,
709-
}, # type: ignore
710-
annotations=(
711-
{'run.googleapis.com/custom-audiences': custom_audience_list}
712-
if custom_audience_list
713-
else None
714-
),
762+
environment_variables=self._etl_get_env(),
715763
ingress_settings='ALLOW_ALL',
716764
all_traffic_on_latest_revision=True,
717765
service_account_email=sa.email,
@@ -727,6 +775,23 @@ def _etl_function(
727775
),
728776
)
729777

778+
if custom_audience_list:
779+
# prepare docker image url
780+
docker_image_url = pulumi.Output.all(
781+
self.config.gcp.region,
782+
self.config.metamist.gcp.project,
783+
fxn.name,
784+
).apply(
785+
lambda args: f"{args[0]}-docker.pkg.dev/{args[1]}/gcf-artifacts/{args[2].replace('-','--')}:latest"
786+
)
787+
# create external cloud run with custom domain
788+
self._etl_external_function(
789+
f_name,
790+
docker_image_url,
791+
sa,
792+
custom_audience_list,
793+
)
794+
730795
return fxn
731796

732797
def _setup_metamist_etl_accessors(self):

0 commit comments

Comments
 (0)