From f7ca9ade8879349173bfa059ef17b649c8d8c68c Mon Sep 17 00:00:00 2001 From: suguanYang Date: Sun, 29 Nov 2026 00:38:24 -0700 Subject: [PATCH] Make Knowhere production S3 placement configurable --- .github/workflows/build-images.yml | 27 +++++++++++++-- deploy/ecs/render_task_definitions.py | 3 +- deploy/ecs/task-definition-api.staging.json | 2 +- .../ecs/task-definition-worker.staging.json | 2 +- deploy/ecs/test_render_task_definitions.py | 34 +++++++++++++++++++ 5 files changed, 62 insertions(+), 6 deletions(-) diff --git a/.github/workflows/build-images.yml b/.github/workflows/build-images.yml index 62d0c5ee8..876326b18 100644 --- a/.github/workflows/build-images.yml +++ b/.github/workflows/build-images.yml @@ -823,6 +823,24 @@ jobs: resolve_image knowhere-backend api_image resolve_image knowhere-worker worker_image + - name: Resolve production S3 storage + id: production_storage + env: + STORAGE_CONFIGURATION: ${{ vars.PROD_S3_STORAGE || '{"bucket":"knowhere-storage-prod","region":"us-east-1","sns_topic_arn":"arn:aws:sns:us-east-1:107424103509:knowhere-prod-s3-events"}' }} + shell: bash + run: | + set -euo pipefail + bucket="$(jq -er '.bucket | select(test("^[a-z0-9][a-z0-9.-]{1,61}[a-z0-9]$"))' <<< "$STORAGE_CONFIGURATION")" + region="$(jq -er '.region | select(test("^[a-z]{2}(-[a-z]+)+-[0-9]+$"))' <<< "$STORAGE_CONFIGURATION")" + topic="$(jq -er '.sns_topic_arn | select(test("^arn:aws:sns:[a-z0-9-]+:107424103509:[A-Za-z0-9_-]+$"))' <<< "$STORAGE_CONFIGURATION")" + if [[ "$topic" != "arn:aws:sns:$region:107424103509:"* ]]; then + echo "::error::Production S3 storage and upload notifications must use the same region." + exit 1 + fi + echo "bucket=$bucket" >> "$GITHUB_OUTPUT" + echo "region=$region" >> "$GITHUB_OUTPUT" + echo "sns_topic_arn=$topic" >> "$GITHUB_OUTPUT" + - name: Read lexical backend from the database control row id: lexical_backend # Render the backend the database serves. A gated control row means an @@ -831,7 +849,8 @@ jobs: DATABASE_URL: ${{ secrets.PRODUCTION_MIGRATION_DATABASE_URL }} API_IMAGE: ${{ steps.images.outputs.api_image }} ENVIRONMENT: production - S3_BUCKET_NAME: knowhere-storage-prod + S3_BUCKET_NAME: ${{ steps.production_storage.outputs.bucket }} + S3_REGION: ${{ steps.production_storage.outputs.region }} S3_TEMP_PATH: /tmp TMP_PATH: /tmp/aismart_bid shell: bash @@ -846,6 +865,7 @@ jobs: --env TMP_PATH \ --env "DB_SSL_MODE=${PROD_DB_SSL_MODE}" \ --env ENVIRONMENT \ + --env S3_REGION \ --entrypoint python \ "${API_IMAGE}" \ -m shared.services.retrieval.lexical.deployment_backend)" @@ -877,11 +897,12 @@ jobs: API_DB_MAX_OVERFLOW: "50" WORKER_DB_SYNC_POOL_SIZE: "5" WORKER_DB_SYNC_MAX_OVERFLOW: "5" - S3_BUCKET_NAME: knowhere-storage-prod + S3_BUCKET_NAME: ${{ steps.production_storage.outputs.bucket }} + S3_REGION: ${{ steps.production_storage.outputs.region }} INTERNAL_DASHBOARD_ENDPOINT: https://knowhereto.ai FRONTEND_URL: https://knowhereto.ai API_WEBHOOK_ENDPOINT: https://api.knowhereto.ai/v1/internal/s3-events - SNS_TOPIC_ARN: arn:aws:sns:us-east-1:107424103509:knowhere-prod-s3-events + SNS_TOPIC_ARN: ${{ steps.production_storage.outputs.sns_topic_arn }} QSTASH_CALLBACK_BASE_URL: https://api.knowhereto.ai/api/v1 QSTASH_BASE_URL: https://qstash-us-east-1.upstash.io API_CPU: "512" diff --git a/deploy/ecs/render_task_definitions.py b/deploy/ecs/render_task_definitions.py index 9c914c107..130185803 100644 --- a/deploy/ecs/render_task_definitions.py +++ b/deploy/ecs/render_task_definitions.py @@ -227,7 +227,7 @@ def attach_opensearch( def render_template(template_path: Path, output_path: Path, variables: dict[str, str]) -> None: """Render one JSON template to a deployment-only output path.""" definition = json.loads(template_path.read_text(encoding="utf-8")) - rendered = replace_placeholders(definition, variables) + rendered = replace_placeholders(definition, {"S3_REGION": "us-east-1", **variables}) configure_staging_opensearch(rendered, variables) configure_production_opensearch(rendered, variables) qstash_base_url: str = variables.get("QSTASH_BASE_URL", "") @@ -280,6 +280,7 @@ def load_variables() -> dict[str, str]: # Preserve the SDK default when no override is supplied, while production # explicitly selects the region that owns its existing QStash token. variables["QSTASH_BASE_URL"] = os.environ.get("QSTASH_BASE_URL", "") + variables["S3_REGION"] = os.environ.get("S3_REGION", "us-east-1") return variables diff --git a/deploy/ecs/task-definition-api.staging.json b/deploy/ecs/task-definition-api.staging.json index a2f129950..3cc8bce75 100644 --- a/deploy/ecs/task-definition-api.staging.json +++ b/deploy/ecs/task-definition-api.staging.json @@ -37,7 +37,7 @@ {"name": "S3_BUCKET_NAME", "value": "${S3_BUCKET_NAME}"}, {"name": "S3_RESULTS_BUCKET", "value": "${S3_BUCKET_NAME}"}, {"name": "S3_TEMP_PATH", "value": "/tmp"}, - {"name": "S3_REGION", "value": "us-east-1"}, + {"name": "S3_REGION", "value": "${S3_REGION}"}, {"name": "S3_USE_SSL", "value": "true"}, {"name": "S3_ADDRESSING_STYLE", "value": "virtual"}, {"name": "SUPPORTED_EXTENSIONS", "value": ".doc,.docx,.pdf,.txt,.xls,.xlsx,.csv,.tsv,.pptx,.jpg,.jpeg,.png,.gif,.webp,.bmp,.tif,.tiff,.heic,.heif,.md,.html,.htm,.mp3,.wav,.m4a,.aac,.flac,.ogg,.opus,.mp4,.mov,.webm,.mkv"}, diff --git a/deploy/ecs/task-definition-worker.staging.json b/deploy/ecs/task-definition-worker.staging.json index 3073791d4..f0a647615 100644 --- a/deploy/ecs/task-definition-worker.staging.json +++ b/deploy/ecs/task-definition-worker.staging.json @@ -32,7 +32,7 @@ {"name": "S3_BUCKET_NAME", "value": "${S3_BUCKET_NAME}"}, {"name": "S3_RESULTS_BUCKET", "value": "${S3_BUCKET_NAME}"}, {"name": "S3_TEMP_PATH", "value": "/tmp"}, - {"name": "S3_REGION", "value": "us-east-1"}, + {"name": "S3_REGION", "value": "${S3_REGION}"}, {"name": "S3_USE_SSL", "value": "true"}, {"name": "S3_ADDRESSING_STYLE", "value": "virtual"}, {"name": "SUPPORTED_EXTENSIONS", "value": ".doc,.docx,.pdf,.txt,.xls,.xlsx,.csv,.tsv,.pptx,.jpg,.jpeg,.png,.gif,.webp,.bmp,.tif,.tiff,.heic,.heif,.md,.html,.htm,.mp3,.wav,.m4a,.aac,.flac,.ogg,.opus,.mp4,.mov,.webm,.mkv"}, diff --git a/deploy/ecs/test_render_task_definitions.py b/deploy/ecs/test_render_task_definitions.py index 987fb0ad0..c1e304c41 100644 --- a/deploy/ecs/test_render_task_definitions.py +++ b/deploy/ecs/test_render_task_definitions.py @@ -50,6 +50,40 @@ "TMP_PATH": "/tmp/aismart_bid", } +@pytest.mark.parametrize( + "template_name", + ("task-definition-api.staging.json", "task-definition-worker.staging.json"), +) +def test_production_storage_can_use_a_different_region( + tmp_path: Path, template_name: str +) -> None: + output_path = tmp_path / "production.json" + bucket = "knowhere-test-singapore" + topic = "arn:aws:sns:ap-southeast-1:107424103509:knowhere-test" + render_template( + TEMPLATE_DIRECTORY / template_name, + output_path, + { + **RENDER_VARIABLES, + "DEPLOYMENT_ENVIRONMENT": "prod", + "RUNTIME_ENVIRONMENT": "production", + "S3_BUCKET_NAME": bucket, + "S3_REGION": "ap-southeast-1", + "SNS_TOPIC_ARN": topic, + }, + ) + definition = json.loads(output_path.read_text(encoding="utf-8")) + environment = { + entry["name"]: entry["value"] + for entry in definition["containerDefinitions"][0]["environment"] + } + assert environment["S3_BUCKET_NAME"] == bucket + assert environment["S3_RESULTS_BUCKET"] == bucket + assert environment["S3_REGION"] == "ap-southeast-1" + assert environment["SNS_TOPIC_ARN"] == topic + assert environment["AWS_REGION"] == "us-east-1" + + API_STAGING_ENVIRONMENT: dict[str, str] = { **SHARED_STAGING_ENVIRONMENT, "DB_MAX_OVERFLOW": "5",