Skip to content

Commit 4a1b5d8

Browse files
author
AFDudley
committed
Merge pull request 'fix(k8s): translate service names to localhost for sidecar containers' (#989) from fix-sidecar-localhost into main
Reviewed-on: https://git.vdb.to/cerc-io/stack-orchestrator/pulls/989
2 parents 0296da6 + 019225c commit 4a1b5d8

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

stack_orchestrator/deploy/k8s/cluster_info.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
envs_from_environment_variables_map,
3232
envs_from_compose_file,
3333
merge_envs,
34+
translate_sidecar_service_names,
3435
)
3536
from stack_orchestrator.deploy.deploy_util import (
3637
parsed_pod_files_map_from_file_names,
@@ -439,6 +440,12 @@ def get_deployment(self, image_pull_policy: Optional[str] = None):
439440
if "environment" in service_info
440441
else self.environment_variables.map
441442
)
443+
# Translate docker-compose service names to localhost for sidecars
444+
# All services in the same pod share the network namespace
445+
sibling_services = [s for s in services.keys() if s != service_name]
446+
merged_envs = translate_sidecar_service_names(
447+
merged_envs, sibling_services
448+
)
442449
envs = envs_from_environment_variables_map(merged_envs)
443450
if opts.o.debug:
444451
print(f"Merged envs: {envs}")

stack_orchestrator/deploy/k8s/helpers.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,41 @@ def envs_from_compose_file(
942942
return result
943943

944944

945+
def translate_sidecar_service_names(
946+
envs: Mapping[str, str], sibling_service_names: List[str]
947+
) -> Mapping[str, str]:
948+
"""Translate docker-compose service names to localhost for sidecar containers.
949+
950+
In docker-compose, services can reference each other by name (e.g., 'db:5432').
951+
In Kubernetes, when multiple containers are in the same pod (sidecars), they
952+
share the same network namespace and must use 'localhost' instead.
953+
954+
This function replaces service name references with 'localhost' in env values.
955+
"""
956+
import re
957+
958+
if not sibling_service_names:
959+
return envs
960+
961+
result = {}
962+
for env_var, env_val in envs.items():
963+
if env_val is None:
964+
result[env_var] = env_val
965+
continue
966+
967+
new_val = str(env_val)
968+
for service_name in sibling_service_names:
969+
# Match service name followed by optional port (e.g., 'db:5432', 'db')
970+
# Handle URLs like: postgres://user:pass@db:5432/dbname
971+
# and simple refs like: db:5432 or just db
972+
pattern = rf"\b{re.escape(service_name)}(:\d+)?\b"
973+
new_val = re.sub(pattern, lambda m: f'localhost{m.group(1) or ""}', new_val)
974+
975+
result[env_var] = new_val
976+
977+
return result
978+
979+
945980
def envs_from_environment_variables_map(
946981
map: Mapping[str, str]
947982
) -> List[client.V1EnvVar]:

0 commit comments

Comments
 (0)