Skip to content

Commit 2fd8feb

Browse files
DeanChensjcopybara-github
authored andcommitted
chore: Support allow_origins in cloud_run deployment
Also reorganize the fast_api_common_options. This resolves #1444. PiperOrigin-RevId: 773890111
1 parent 8677d5c commit 2fd8feb

File tree

3 files changed

+59
-47
lines changed

3 files changed

+59
-47
lines changed

src/google/adk/cli/cli_deploy.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
5656
EXPOSE {port}
5757
58-
CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_option} "/app/agents"
58+
CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_option} {allow_origins_option} "/app/agents"
5959
"""
6060

6161
_AGENT_ENGINE_APP_TEMPLATE = """
@@ -121,8 +121,10 @@ def to_cloud_run(
121121
port: int,
122122
trace_to_cloud: bool,
123123
with_ui: bool,
124+
log_level: str,
124125
verbosity: str,
125126
adk_version: str,
127+
allow_origins: Optional[list[str]] = None,
126128
session_service_uri: Optional[str] = None,
127129
artifact_service_uri: Optional[str] = None,
128130
memory_service_uri: Optional[str] = None,
@@ -150,6 +152,7 @@ def to_cloud_run(
150152
app_name: The name of the app, by default, it's basename of `agent_folder`.
151153
temp_folder: The temp folder for the generated Cloud Run source files.
152154
port: The port of the ADK api server.
155+
allow_origins: The list of allowed origins for the ADK api server.
153156
trace_to_cloud: Whether to enable Cloud Trace.
154157
with_ui: Whether to deploy with UI.
155158
verbosity: The verbosity level of the CLI.
@@ -183,6 +186,9 @@ def to_cloud_run(
183186
# create Dockerfile
184187
click.echo('Creating Dockerfile...')
185188
host_option = '--host=0.0.0.0' if adk_version > '0.5.0' else ''
189+
allow_origins_option = (
190+
f'--allow_origins={",".join(allow_origins)}' if allow_origins else ''
191+
)
186192
dockerfile_content = _DOCKERFILE_TEMPLATE.format(
187193
gcp_project_id=project,
188194
gcp_region=region,
@@ -197,6 +203,7 @@ def to_cloud_run(
197203
memory_service_uri,
198204
),
199205
trace_to_cloud_option='--trace_to_cloud' if trace_to_cloud else '',
206+
allow_origins_option=allow_origins_option,
200207
adk_version=adk_version,
201208
host_option=host_option,
202209
)
@@ -226,7 +233,7 @@ def to_cloud_run(
226233
'--port',
227234
str(port),
228235
'--verbosity',
229-
verbosity,
236+
log_level.lower() if log_level else verbosity,
230237
'--labels',
231238
'created-by=adk',
232239
],

src/google/adk/cli/cli_tools_click.py

Lines changed: 48 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@
3939
from .utils import envs
4040
from .utils import logs
4141

42+
LOG_LEVELS = click.Choice(
43+
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
44+
case_sensitive=False,
45+
)
46+
4247

4348
class HelpfulCommand(click.Command):
4449
"""Command that shows full help on error instead of just the error message.
@@ -498,13 +503,6 @@ def fast_api_common_options():
498503
"""Decorator to add common fast api options to click commands."""
499504

500505
def decorator(func):
501-
@click.option(
502-
"--host",
503-
type=str,
504-
help="Optional. The binding host of the server",
505-
default="127.0.0.1",
506-
show_default=True,
507-
)
508506
@click.option(
509507
"--port",
510508
type=int,
@@ -518,10 +516,7 @@ def decorator(func):
518516
)
519517
@click.option(
520518
"--log_level",
521-
type=click.Choice(
522-
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
523-
case_sensitive=False,
524-
),
519+
type=LOG_LEVELS,
525520
default="INFO",
526521
help="Optional. Set the logging level",
527522
)
@@ -535,7 +530,10 @@ def decorator(func):
535530
@click.option(
536531
"--reload/--no-reload",
537532
default=True,
538-
help="Optional. Whether to enable auto reload for server.",
533+
help=(
534+
"Optional. Whether to enable auto reload for server. Not supported"
535+
" for Cloud Run."
536+
),
539537
)
540538
@functools.wraps(func)
541539
def wrapper(*args, **kwargs):
@@ -547,6 +545,13 @@ def wrapper(*args, **kwargs):
547545

548546

549547
@main.command("web")
548+
@click.option(
549+
"--host",
550+
type=str,
551+
help="Optional. The binding host of the server",
552+
default="127.0.0.1",
553+
show_default=True,
554+
)
550555
@fast_api_common_options()
551556
@adk_services_options()
552557
@deprecated_adk_services_options()
@@ -578,7 +583,7 @@ def cli_web(
578583
579584
Example:
580585
581-
adk web --session_service_uri=[uri] --port=[port] path/to/agents_dir
586+
adk web --port=[port] path/to/agents_dir
582587
"""
583588
logs.setup_adk_logger(getattr(logging, log_level.upper()))
584589

@@ -628,6 +633,16 @@ async def _lifespan(app: FastAPI):
628633

629634

630635
@main.command("api_server")
636+
@click.option(
637+
"--host",
638+
type=str,
639+
help="Optional. The binding host of the server",
640+
default="127.0.0.1",
641+
show_default=True,
642+
)
643+
@fast_api_common_options()
644+
@adk_services_options()
645+
@deprecated_adk_services_options()
631646
# The directory of agents, where each sub-directory is a single agent.
632647
# By default, it is the current working directory
633648
@click.argument(
@@ -637,9 +652,6 @@ async def _lifespan(app: FastAPI):
637652
),
638653
default=os.getcwd(),
639654
)
640-
@fast_api_common_options()
641-
@adk_services_options()
642-
@deprecated_adk_services_options()
643655
def cli_api_server(
644656
agents_dir: str,
645657
log_level: str = "INFO",
@@ -661,7 +673,7 @@ def cli_api_server(
661673
662674
Example:
663675
664-
adk api_server --session_service_uri=[uri] --port=[port] path/to/agents_dir
676+
adk api_server --port=[port] path/to/agents_dir
665677
"""
666678
logs.setup_adk_logger(getattr(logging, log_level.upper()))
667679

@@ -720,19 +732,7 @@ def cli_api_server(
720732
" of the AGENT source code)."
721733
),
722734
)
723-
@click.option(
724-
"--port",
725-
type=int,
726-
default=8000,
727-
help="Optional. The port of the ADK API server (default: 8000).",
728-
)
729-
@click.option(
730-
"--trace_to_cloud",
731-
is_flag=True,
732-
show_default=True,
733-
default=False,
734-
help="Optional. Whether to enable Cloud Trace for cloud run.",
735-
)
735+
@fast_api_common_options()
736736
@click.option(
737737
"--with_ui",
738738
is_flag=True,
@@ -743,6 +743,11 @@ def cli_api_server(
743743
" only)"
744744
),
745745
)
746+
@click.option(
747+
"--verbosity",
748+
type=LOG_LEVELS,
749+
help="Deprecated. Use --log_level instead.",
750+
)
746751
@click.option(
747752
"--temp_folder",
748753
type=str,
@@ -756,20 +761,6 @@ def cli_api_server(
756761
" (default: a timestamped folder in the system temp directory)."
757762
),
758763
)
759-
@click.option(
760-
"--verbosity",
761-
type=click.Choice(
762-
["debug", "info", "warning", "error", "critical"], case_sensitive=False
763-
),
764-
default="WARNING",
765-
help="Optional. Override the default verbosity level.",
766-
)
767-
@click.argument(
768-
"agent",
769-
type=click.Path(
770-
exists=True, dir_okay=True, file_okay=False, resolve_path=True
771-
),
772-
)
773764
@click.option(
774765
"--adk_version",
775766
type=str,
@@ -782,6 +773,12 @@ def cli_api_server(
782773
)
783774
@adk_services_options()
784775
@deprecated_adk_services_options()
776+
@click.argument(
777+
"agent",
778+
type=click.Path(
779+
exists=True, dir_okay=True, file_okay=False, resolve_path=True
780+
),
781+
)
785782
def cli_deploy_cloud_run(
786783
agent: str,
787784
project: Optional[str],
@@ -792,8 +789,11 @@ def cli_deploy_cloud_run(
792789
port: int,
793790
trace_to_cloud: bool,
794791
with_ui: bool,
795-
verbosity: str,
796792
adk_version: str,
793+
log_level: Optional[str] = None,
794+
verbosity: str = "WARNING",
795+
reload: bool = True,
796+
allow_origins: Optional[list[str]] = None,
797797
session_service_uri: Optional[str] = None,
798798
artifact_service_uri: Optional[str] = None,
799799
memory_service_uri: Optional[str] = None,
@@ -808,6 +808,7 @@ def cli_deploy_cloud_run(
808808
809809
adk deploy cloud_run --project=[project] --region=[region] path/to/my_agent
810810
"""
811+
log_level = log_level or verbosity
811812
session_service_uri = session_service_uri or session_db_url
812813
artifact_service_uri = artifact_service_uri or artifact_storage_uri
813814
try:
@@ -820,7 +821,9 @@ def cli_deploy_cloud_run(
820821
temp_folder=temp_folder,
821822
port=port,
822823
trace_to_cloud=trace_to_cloud,
824+
allow_origins=allow_origins,
823825
with_ui=with_ui,
826+
log_level=log_level,
824827
verbosity=verbosity,
825828
adk_version=adk_version,
826829
session_service_uri=session_service_uri,

tests/unittests/cli/utils/test_cli_deploy.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ def _recording_copytree(*args: Any, **kwargs: Any):
162162
trace_to_cloud=True,
163163
with_ui=True,
164164
verbosity="info",
165+
log_level="info",
165166
session_service_uri="sqlite://",
166167
artifact_service_uri="gs://bucket",
167168
memory_service_uri="rag://",
@@ -206,6 +207,7 @@ def _fake_rmtree(path: str | Path, *a: Any, **k: Any) -> None:
206207
trace_to_cloud=False,
207208
with_ui=False,
208209
verbosity="info",
210+
log_level="info",
209211
adk_version="1.0.0",
210212
session_service_uri=None,
211213
artifact_service_uri=None,

0 commit comments

Comments
 (0)