chore: Support allow_origins in cloud_run deployment

Also reorganize the fast_api_common_options. This resolves https://github.com/google/adk-python/issues/1444.

PiperOrigin-RevId: 773890111
This commit is contained in:
Shangjie Chen
2025-06-20 16:54:08 -07:00
committed by Copybara-Service
parent 8677d5c8dc
commit 2fd8feb65d
3 changed files with 59 additions and 47 deletions
+9 -2
View File
@@ -55,7 +55,7 @@ COPY "agents/{app_name}/" "/app/agents/{app_name}/"
EXPOSE {port}
CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_option} "/app/agents"
CMD adk {command} --port={port} {host_option} {service_option} {trace_to_cloud_option} {allow_origins_option} "/app/agents"
"""
_AGENT_ENGINE_APP_TEMPLATE = """
@@ -121,8 +121,10 @@ def to_cloud_run(
port: int,
trace_to_cloud: bool,
with_ui: bool,
log_level: str,
verbosity: str,
adk_version: str,
allow_origins: Optional[list[str]] = None,
session_service_uri: Optional[str] = None,
artifact_service_uri: Optional[str] = None,
memory_service_uri: Optional[str] = None,
@@ -150,6 +152,7 @@ def to_cloud_run(
app_name: The name of the app, by default, it's basename of `agent_folder`.
temp_folder: The temp folder for the generated Cloud Run source files.
port: The port of the ADK api server.
allow_origins: The list of allowed origins for the ADK api server.
trace_to_cloud: Whether to enable Cloud Trace.
with_ui: Whether to deploy with UI.
verbosity: The verbosity level of the CLI.
@@ -183,6 +186,9 @@ def to_cloud_run(
# create Dockerfile
click.echo('Creating Dockerfile...')
host_option = '--host=0.0.0.0' if adk_version > '0.5.0' else ''
allow_origins_option = (
f'--allow_origins={",".join(allow_origins)}' if allow_origins else ''
)
dockerfile_content = _DOCKERFILE_TEMPLATE.format(
gcp_project_id=project,
gcp_region=region,
@@ -197,6 +203,7 @@ def to_cloud_run(
memory_service_uri,
),
trace_to_cloud_option='--trace_to_cloud' if trace_to_cloud else '',
allow_origins_option=allow_origins_option,
adk_version=adk_version,
host_option=host_option,
)
@@ -226,7 +233,7 @@ def to_cloud_run(
'--port',
str(port),
'--verbosity',
verbosity,
log_level.lower() if log_level else verbosity,
'--labels',
'created-by=adk',
],
+48 -45
View File
@@ -39,6 +39,11 @@ from .fast_api import get_fast_api_app
from .utils import envs
from .utils import logs
LOG_LEVELS = click.Choice(
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
case_sensitive=False,
)
class HelpfulCommand(click.Command):
"""Command that shows full help on error instead of just the error message.
@@ -498,13 +503,6 @@ def fast_api_common_options():
"""Decorator to add common fast api options to click commands."""
def decorator(func):
@click.option(
"--host",
type=str,
help="Optional. The binding host of the server",
default="127.0.0.1",
show_default=True,
)
@click.option(
"--port",
type=int,
@@ -518,10 +516,7 @@ def fast_api_common_options():
)
@click.option(
"--log_level",
type=click.Choice(
["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
case_sensitive=False,
),
type=LOG_LEVELS,
default="INFO",
help="Optional. Set the logging level",
)
@@ -535,7 +530,10 @@ def fast_api_common_options():
@click.option(
"--reload/--no-reload",
default=True,
help="Optional. Whether to enable auto reload for server.",
help=(
"Optional. Whether to enable auto reload for server. Not supported"
" for Cloud Run."
),
)
@functools.wraps(func)
def wrapper(*args, **kwargs):
@@ -547,6 +545,13 @@ def fast_api_common_options():
@main.command("web")
@click.option(
"--host",
type=str,
help="Optional. The binding host of the server",
default="127.0.0.1",
show_default=True,
)
@fast_api_common_options()
@adk_services_options()
@deprecated_adk_services_options()
@@ -578,7 +583,7 @@ def cli_web(
Example:
adk web --session_service_uri=[uri] --port=[port] path/to/agents_dir
adk web --port=[port] path/to/agents_dir
"""
logs.setup_adk_logger(getattr(logging, log_level.upper()))
@@ -628,6 +633,16 @@ def cli_web(
@main.command("api_server")
@click.option(
"--host",
type=str,
help="Optional. The binding host of the server",
default="127.0.0.1",
show_default=True,
)
@fast_api_common_options()
@adk_services_options()
@deprecated_adk_services_options()
# The directory of agents, where each sub-directory is a single agent.
# By default, it is the current working directory
@click.argument(
@@ -637,9 +652,6 @@ def cli_web(
),
default=os.getcwd(),
)
@fast_api_common_options()
@adk_services_options()
@deprecated_adk_services_options()
def cli_api_server(
agents_dir: str,
log_level: str = "INFO",
@@ -661,7 +673,7 @@ def cli_api_server(
Example:
adk api_server --session_service_uri=[uri] --port=[port] path/to/agents_dir
adk api_server --port=[port] path/to/agents_dir
"""
logs.setup_adk_logger(getattr(logging, log_level.upper()))
@@ -720,19 +732,7 @@ def cli_api_server(
" of the AGENT source code)."
),
)
@click.option(
"--port",
type=int,
default=8000,
help="Optional. The port of the ADK API server (default: 8000).",
)
@click.option(
"--trace_to_cloud",
is_flag=True,
show_default=True,
default=False,
help="Optional. Whether to enable Cloud Trace for cloud run.",
)
@fast_api_common_options()
@click.option(
"--with_ui",
is_flag=True,
@@ -743,6 +743,11 @@ def cli_api_server(
" only)"
),
)
@click.option(
"--verbosity",
type=LOG_LEVELS,
help="Deprecated. Use --log_level instead.",
)
@click.option(
"--temp_folder",
type=str,
@@ -756,20 +761,6 @@ def cli_api_server(
" (default: a timestamped folder in the system temp directory)."
),
)
@click.option(
"--verbosity",
type=click.Choice(
["debug", "info", "warning", "error", "critical"], case_sensitive=False
),
default="WARNING",
help="Optional. Override the default verbosity level.",
)
@click.argument(
"agent",
type=click.Path(
exists=True, dir_okay=True, file_okay=False, resolve_path=True
),
)
@click.option(
"--adk_version",
type=str,
@@ -782,6 +773,12 @@ def cli_api_server(
)
@adk_services_options()
@deprecated_adk_services_options()
@click.argument(
"agent",
type=click.Path(
exists=True, dir_okay=True, file_okay=False, resolve_path=True
),
)
def cli_deploy_cloud_run(
agent: str,
project: Optional[str],
@@ -792,8 +789,11 @@ def cli_deploy_cloud_run(
port: int,
trace_to_cloud: bool,
with_ui: bool,
verbosity: str,
adk_version: str,
log_level: Optional[str] = None,
verbosity: str = "WARNING",
reload: bool = True,
allow_origins: Optional[list[str]] = None,
session_service_uri: Optional[str] = None,
artifact_service_uri: Optional[str] = None,
memory_service_uri: Optional[str] = None,
@@ -808,6 +808,7 @@ def cli_deploy_cloud_run(
adk deploy cloud_run --project=[project] --region=[region] path/to/my_agent
"""
log_level = log_level or verbosity
session_service_uri = session_service_uri or session_db_url
artifact_service_uri = artifact_service_uri or artifact_storage_uri
try:
@@ -820,7 +821,9 @@ def cli_deploy_cloud_run(
temp_folder=temp_folder,
port=port,
trace_to_cloud=trace_to_cloud,
allow_origins=allow_origins,
with_ui=with_ui,
log_level=log_level,
verbosity=verbosity,
adk_version=adk_version,
session_service_uri=session_service_uri,
@@ -162,6 +162,7 @@ def test_to_cloud_run_happy_path(
trace_to_cloud=True,
with_ui=True,
verbosity="info",
log_level="info",
session_service_uri="sqlite://",
artifact_service_uri="gs://bucket",
memory_service_uri="rag://",
@@ -206,6 +207,7 @@ def test_to_cloud_run_cleans_temp_dir(
trace_to_cloud=False,
with_ui=False,
verbosity="info",
log_level="info",
adk_version="1.0.0",
session_service_uri=None,
artifact_service_uri=None,