Skip to content

fix: cap generated container name to GCP Cloud Run's service-id limit#787

Open
Mohak-Agrawal wants to merge 1 commit into
StructuredLabs:mainfrom
Mohak-Agrawal:fix/659-gcp-service-name-length
Open

fix: cap generated container name to GCP Cloud Run's service-id limit#787
Mohak-Agrawal wants to merge 1 commit into
StructuredLabs:mainfrom
Mohak-Agrawal:fix/659-gcp-service-name-length

Conversation

@Mohak-Agrawal

Copy link
Copy Markdown

What was the bug

Deploying to GCP fails with a 500, and the underlying API response is:

{"error": "400 Violation in CreateServiceRequest.service_id: only lowercase, digits, and hyphens; must begin with letter, and cannot end with hyphen; must be less than 50 characters. ..."}

Root cause

get_container_name() in preswald/deploy.py builds the Cloud Run service name as preswald-app-{slug}, sanitizes the character set, and then hands it straight to gcloud run deploy <container_name> / Cloud Run's CreateService API — with no length cap.

Google Cloud Run rejects service_id values of 50 characters or more. The "preswald-app-" prefix alone is 13 characters, so any project slug that pushes the combined name to 50+ characters triggers exactly the quoted violation. This surfaces to the user as an opaque 500 during deploy, with the real cause several layers removed from the actual GCP validation error.

What the fix does

Truncates the sanitized container name to 49 characters (Cloud Run's limit is "less than 50"), stripping any trailing hyphen left by the cut. Short slugs are completely unaffected.

GCP_SERVICE_NAME_MAX_LENGTH = 49
...
container_name = container_name[:GCP_SERVICE_NAME_MAX_LENGTH].rstrip("-")

How to reproduce / verify

import re

def build_container_name(slug, cap):
    name = f"preswald-app-{slug}".lower()
    name = re.sub(r"[^a-z0-9-]", "", name).strip("-")
    if cap:
        name = name[:49].rstrip("-")
    return name

long_slug = "a-very-descriptive-project-slug-for-an-org-1744321134"

before = build_container_name(long_slug, cap=False)
after = build_container_name(long_slug, cap=True)

assert len(before) == 66   # >= 50 -> violates GCP's constraint, reproduces #659
assert len(after) == 48    # now within the limit

Added tests/test_deploy.py with regression tests covering:

  • a long project slug, asserting the generated container name stays at or under 49 characters and doesn't end in a hyphen
  • a short project slug (zwbq, matching the slug format in the original report), asserting it's byte-for-byte unaffected by the change

Note

The report in #659 was against the hosted app.preswald.com deploy flow rather than the local preswald deploy --target gcp path, and I don't have visibility into that hosted backend's source. However, the error message is GCP Cloud Run's literal CreateServiceRequest.service_id validation response, and this is the only place in the OSS codebase that constructs a Cloud Run service name from a project slug with no length bound — the exact same violation reproduces from this code with a sufficiently long slug. If the hosted backend uses different naming logic, happy to adjust scope once that's clarified.

Fixes #659

get_container_name() built "preswald-app-{slug}" with no length cap
before handing it to `gcloud run deploy`/Cloud Run's CreateService API
as the service name. Google Cloud Run rejects service_id values of 50
characters or more:

  400 Violation in CreateServiceRequest.service_id: only lowercase,
  digits, and hyphens; must begin with letter, and cannot end with
  hyphen; must be less than 50 characters.

Any project slug long enough to push the prefixed name past 49
characters triggered exactly this error, surfacing as an opaque 500
during deploy with no indication of the actual cause.

Truncate the sanitized container name to 49 characters (stripping any
trailing hyphen left by the cut) so it always satisfies Cloud Run's
constraint, and add regression tests covering both a long slug (now
capped) and a short slug (unaffected).

Fixes StructuredLabs#659
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Deploy Error on app.preswald.com

1 participant