Skip to content
Merged
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
27 changes: 24 additions & 3 deletions .github/workflows/build-images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)"
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion deploy/ecs/render_task_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand Down Expand Up @@ -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


Expand Down
2 changes: 1 addition & 1 deletion deploy/ecs/task-definition-api.staging.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
2 changes: 1 addition & 1 deletion deploy/ecs/task-definition-worker.staging.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
34 changes: 34 additions & 0 deletions deploy/ecs/test_render_task_definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
Loading