Skip to content
This repository was archived by the owner on Aug 4, 2023. It is now read-only.

Commit 85ccb18

Browse files
committed
Add support to Scheduled invocations
1 parent 478bd73 commit 85ccb18

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

flamingo/models/__init__.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,16 @@
22
from models.buildpack import BuildPack, Target
33
from models.deployment import Deployment, Event, Source
44
from models.environment import Environment
5-
from models.app import EnvVar, ServiceAccount, Database, Bucket, App, Project, Repository # import this last
5+
from models.app import (
6+
EnvVar,
7+
ServiceAccount,
8+
Database,
9+
Bucket,
10+
App,
11+
Project,
12+
Repository,
13+
ScheduledInvocation,
14+
) # import this last
615

716

817
__all__ = (
@@ -18,4 +27,5 @@
1827
'Event',
1928
'Source',
2029
'Target',
30+
'ScheduledInvocation',
2131
)

flamingo/models/app.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,17 @@ def get_tags(self) -> List[str]:
348348
]
349349

350350

351+
@dataclass
352+
class ScheduledInvocation(EmbeddedDocument):
353+
name: str
354+
cron: str
355+
path: str = '/'
356+
method: str = 'GET'
357+
body: str = None
358+
cloud_scheduler_id: str = None
359+
content_type: str = 'application/json'
360+
361+
351362
@dataclass
352363
class App(Document):
353364
name: str
@@ -357,6 +368,7 @@ class App(Document):
357368
identifier: str = None
358369
domains: List[str] = field(default_factory=list)
359370
vars: List[EnvVar] = field(default_factory=list)
371+
scheduled_invocations: List[ScheduledInvocation] = field(default_factory=list)
360372
database: Database = None
361373
bucket: Bucket = None
362374
region: str = None

flamingo/utils/build_engine.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from google.cloud.devtools import cloudbuild_v1
1010

1111
import settings
12-
from models import App, Target
12+
from models import App, Target, ScheduledInvocation
1313
from utils.alias_engine import AliasEngine
1414

1515
logger = logging.getLogger()
@@ -109,10 +109,44 @@ def _get_description(self) -> str:
109109
_event_str = f'tagged {self._build_setup.deploy_tag}'
110110
return f'🦩 Deploy to {self._build_setup.build_pack.target} when {_event_str}'
111111

112+
def _add_scheduled_invocation_step(self, scheduled_invocation: ScheduledInvocation, wait_for: str):
113+
schedule_name = f"{self.app.identifier}--{scheduled_invocation.name}"
114+
115+
auth_params = []
116+
if self._build_setup.is_authenticated:
117+
auth_params = [
118+
'--oidc-token-audience', f"{self.app.endpoint}",
119+
'--oidc-service-account-email', f"{self._substitution.SERVICE_ACCOUNT}",
120+
]
121+
122+
scheduler = self._service.make_build_step(
123+
identifier=f"Schedule {scheduled_invocation.name}",
124+
name="gcr.io/google.com/cloudsdktool/cloud-sdk",
125+
entrypoint='gcloud',
126+
args=[
127+
"beta", "scheduler", "jobs", "create", "http", "deploy", f"{schedule_name}",
128+
'--uri', f"{self.app.endpoint}{scheduled_invocation.path}",
129+
'--schedule', f'{scheduled_invocation.cron}',
130+
'--http-method', f'{scheduled_invocation.method}',
131+
'--headers', f'Content-Type={scheduled_invocation.content_type}',
132+
'--region', f"{self._substitution.REGION}",
133+
*auth_params,
134+
],
135+
wait_for=[wait_for],
136+
)
137+
self.steps.append(scheduler)
138+
112139
async def build(self) -> str:
113140
self.init()
114141
self._add_steps()
115142

143+
last_step_id = self.steps[-1].id
144+
for scheduled_invocation in self.app.scheduled_invocations:
145+
self._add_scheduled_invocation_step(
146+
scheduled_invocation=scheduled_invocation,
147+
wait_for=last_step_id,
148+
)
149+
116150
event = self.app.repository.as_event(
117151
branch_name=self._build_setup.deploy_branch,
118152
tag_name=self._build_setup.deploy_tag,
@@ -329,6 +363,7 @@ def _create_placeholder(self):
329363

330364
@dataclass
331365
class CloudFunctionsFactory(BuildTriggerFactory):
366+
# TODO: setup this <https://cloud.google.com/functions/docs/reference/iam/roles#additional-configuration>
332367
def _get_setup_params(self) -> KeyValue:
333368
from gcp_pilot.functions import CloudFunctions
334369

0 commit comments

Comments
 (0)