Skip to content

feat: passthrough extra args for adk deploy cloud_run as Cloud Run args #2544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 43 additions & 20 deletions src/google/adk/cli/cli_deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ def to_cloud_run(
artifact_service_uri: Optional[str] = None,
memory_service_uri: Optional[str] = None,
a2a: bool = False,
extra_gcloud_args: Optional[tuple[str, ...]] = None,
):
"""Deploys an agent to Google Cloud Run.

Expand Down Expand Up @@ -224,26 +225,48 @@ def to_cloud_run(
click.echo('Deploying to Cloud Run...')
region_options = ['--region', region] if region else []
project = _resolve_project(project)
subprocess.run(
[
'gcloud',
'run',
'deploy',
service_name,
'--source',
temp_folder,
'--project',
project,
*region_options,
'--port',
str(port),
'--verbosity',
log_level.lower() if log_level else verbosity,
'--labels',
'created-by=adk',
],
check=True,
)

# Build the command with extra gcloud args
gcloud_cmd = [
'gcloud',
'run',
'deploy',
service_name,
'--source',
temp_folder,
'--project',
project,
*region_options,
'--port',
str(port),
'--verbosity',
log_level.lower() if log_level else verbosity,
]

# Handle labels specially - merge user labels with ADK label
user_labels = []
extra_args_without_labels = []

if extra_gcloud_args:
for arg in extra_gcloud_args:
if arg.startswith('--labels='):
# Extract user-provided labels
user_labels_value = arg[9:] # Remove '--labels=' prefix
user_labels.append(user_labels_value)
else:
extra_args_without_labels.append(arg)

# Combine ADK label with user labels
all_labels = ['created-by=adk']
all_labels.extend(user_labels)
labels_arg = ','.join(all_labels)

gcloud_cmd.extend(['--labels', labels_arg])

# Add any remaining extra passthrough args
gcloud_cmd.extend(extra_args_without_labels)

subprocess.run(gcloud_cmd, check=True)
finally:
click.echo(f'Cleaning up the temp folder: {temp_folder}')
shutil.rmtree(temp_folder)
Expand Down
11 changes: 10 additions & 1 deletion src/google/adk/cli/cli_tools_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,13 @@ def cli_api_server(
server.run()


@deploy.command("cloud_run")
@deploy.command(
"cloud_run",
context_settings={
Copy link
Collaborator

@Jacksunwei Jacksunwei Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make it harder to maintain long-term.

I'd recommend adding a --gcloud_extra_arg option (with --multiple=True) to host.

Usage pattern could be --gcloud_extra_arg="--args1=val1" --gcloud_extra_arg="--args2=val2"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make it harder to maintain long-term.

@Jacksunwei can you explain why this makes it harder to maintain...?

Seems way easier to maintain than the current approach of adding supported flags one by one.

Copy link
Contributor Author

@jackwotherspoon jackwotherspoon Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usage pattern could be --gcloud_extra_arg="--args1=val1" --gcloud_extra_arg="--args2=val2"

From a DevX perspective I find this much worse than my current approach (in my opinion), you are asking folks to learn something new and have to do extra work to convert...

Versus telling folks to "take any of your existing Cloud Run deployments and copy your flags and just append them to your command".

The second option feels a bit more magical and it "just works"

Copy link
Collaborator

@Jacksunwei Jacksunwei Aug 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes things confusing. From user's perspective, when they do adk deploy cloud_run --arg1 val1 --arg2 val2.

There is no easy way to tell whether arg1 is for adk deploy cloud_run or for the underlying gcloud command.

User cannot have help text from adk deploy cloud_run --help, either.

It appears to be doing less things, but it will just confuse people and shut our door to provide any additional options in the future, because if gcloud also has an option of the same name, we may accidentally break users' existing setup.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Jacksunwei I think you may be mis-understanding...

Anything after AGENT_PATH is considered a gcloud arg, everything before is ADK flag...

# ADK flags
adk deploy cloud_run \
--project=$GOOGLE_CLOUD_PROJECT \
--region=$GOOGLE_CLOUD_LOCATION \
$AGENT_PATH \
# NEW gcloud run deploy flags
--min-instances=2 \
--no-allow-unauthenticated

There is no easy way to tell whether arg1 is for adk deploy cloud_run or for the underlying gcloud command.

Yes there is, anything before AGENT_PATH is an ADK arg, if I try and add a gcloud flag before AGENT_PATH it will error:

Try 'adk deploy cloud_run --help' for help.

Error: No such option: --allow-unauthenticated

User cannot have help text from adk deploy cloud_run --help, either.

Yes they can, adk deploy cloud_run --help, works fine. Only time it would potentially not work is --help after the AGENT_PATH.

because if gcloud also has an option of the same name, we may accidentally break users' existing setup.

This is a fair point, but do we really see this being the case...? Is there a plan to add a bunch more flags to adk deploy cloud_run? The benefits here seem to be huge.

"allow_extra_args": True,
"allow_interspersed_args": False,
},
)
@click.option(
"--project",
type=str,
Expand Down Expand Up @@ -971,7 +977,9 @@ def cli_api_server(
# TODO: Add eval_storage_uri option back when evals are supported in Cloud Run.
@adk_services_options()
@deprecated_adk_services_options()
@click.pass_context
def cli_deploy_cloud_run(
ctx,
agent: str,
project: Optional[str],
region: Optional[str],
Expand Down Expand Up @@ -1029,6 +1037,7 @@ def cli_deploy_cloud_run(
artifact_service_uri=artifact_service_uri,
memory_service_uri=memory_service_uri,
a2a=a2a,
extra_gcloud_args=ctx.args,
)
except Exception as e:
click.secho(f"Deploy failed: {e}", fg="red", err=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def test_adk_deploy_cloud_run():
cloud_run_command,
cli_deploy_cloud_run.callback,
"deploy cloud_run",
ignore_params={"verbose"},
ignore_params={"verbose", "ctx"},
)


Expand Down
43 changes: 43 additions & 0 deletions tests/unittests/cli/utils/test_cli_tools_click.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,49 @@ def _boom(*_a: Any, **_k: Any) -> None: # noqa: D401
assert "Deploy failed: boom" in result.output


def test_cli_deploy_cloud_run_passthrough_args(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
) -> None:
"""Extra args should be passed through to the gcloud command."""
rec = _Recorder()
monkeypatch.setattr(cli_tools_click.cli_deploy, "to_cloud_run", rec)

agent_dir = tmp_path / "agent_passthrough"
agent_dir.mkdir()
runner = CliRunner()
result = runner.invoke(
cli_tools_click.main,
[
"deploy",
"cloud_run",
"--project",
"test-project",
"--region",
"us-central1",
str(agent_dir),
"--labels=test-label=test",
"--memory=1Gi",
"--cpu=1",
],
)
# Print debug information if the test fails
if result.exit_code != 0:
print(f"Exit code: {result.exit_code}")
print(f"Output: {result.output}")
print(f"Exception: {result.exception}")

assert result.exit_code == 0
assert rec.calls, "cli_deploy.to_cloud_run must be invoked"

# Check that extra_gcloud_args were passed correctly
called_kwargs = rec.calls[0][1]
extra_args = called_kwargs.get("extra_gcloud_args")
assert extra_args is not None
assert "--labels=test-label=test" in extra_args
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is adk-labels? could you assert that?

assert "--memory=1Gi" in extra_args
assert "--cpu=1" in extra_args


# cli deploy agent_engine
def test_cli_deploy_agent_engine_success(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
Expand Down