Skip to content

Commit

Permalink
Allows overriding the BACKEND_ENDPOINT via env
Browse files Browse the repository at this point in the history
When deployed in the same k8s pod, services communicate by localhost
despite being in different docker containers. By adding
`BACKEND_ENDPOINT`, we can set this to "http://localhost:9000/api" in
a k8s "extra container', so that we can do "helm test" on it.

See https://kubernetes.io/docs/concepts/workloads/pods/#pod-networking

Signed-off-by: Adrian Cole <[email protected]>
  • Loading branch information
Adrian Cole committed Feb 21, 2024
1 parent 9fe8f0d commit 5fc1065
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
9 changes: 8 additions & 1 deletion armeria/src/main/java/brave/example/Frontend.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
import com.linecorp.armeria.server.healthcheck.HealthCheckService;
import com.linecorp.armeria.server.logging.AccessLogWriter;
import com.linecorp.armeria.server.logging.LoggingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class Frontend {
static final Logger LOGGER = LoggerFactory.getLogger(HttpTracingFactory.class);

public static void main(String[] args) {
final HttpTracing httpTracing = HttpTracingFactory.create("frontend");

final String backendEndpoint =
System.getProperty("backend.endpoint", "http://127.0.0.1:9000/api");
LOGGER.info("Using backend endpoint: {}", backendEndpoint);

final WebClient backendClient =
WebClient.builder(System.getProperty("backend.endpoint", "http://127.0.0.1:9000/api"))
WebClient.builder(backendEndpoint)
.decorator(BraveClient.newDecorator(httpTracing.clientOf("backend")))
.build();

Expand Down
21 changes: 13 additions & 8 deletions docker/bin/install-example
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,23 @@ tee -a start-frontend start-backend <<-'EOF'
export IP="$(hostname -i || echo '127.0.0.1')"
EOF

# Declare variables that expand variables set in this shell script/
# These don't need to be exported as they are converted to java properties.
cat >> start-frontend <<-EOF
export PORT=${frontend_port}
export EXAMPLE=${version}
export EXAMPLE_SERVICE=frontend
PORT=${frontend_port}
EXAMPLE=${version}
EXAMPLE_SERVICE=frontend
DEFAULT_BACKEND_ENDPOINT=http://backend:${backend_port}/api
EOF

cat >> start-backend <<-EOF
export PORT=${backend_port}
export EXAMPLE=${version}
export EXAMPLE_SERVICE=backend
PORT=${backend_port}
EXAMPLE=${version}
EXAMPLE_SERVICE=backend
EOF

# Add common elements for Java applications
# Add common elements for Java applications.
# HEALTHCHECK variables need to be exported as they are read via proc info.
tee -a start-frontend start-backend <<-'EOF'
export HEALTHCHECK_IP=${IP}
export HEALTHCHECK_PORT=${PORT}
Expand All @@ -55,7 +59,8 @@ exec java ${JAVA_OPTS} -cp 'classes:lib/*' \
-Dzipkin.baseUrl=${ZIPKIN_BASEURL:-http://zipkin:9411/} \
EOF

echo "-Dbackend.endpoint=http://backend:${backend_port}/api \\" >> start-frontend
# Allow override of the backend endpoint, for a multi-container pod.
echo '-Dbackend.endpoint=${BACKEND_ENDPOINT:-${DEFAULT_BACKEND_ENDPOINT}} \' >> start-frontend

# Add the main class
echo 'brave.example.Frontend "$@"' >> start-frontend
Expand Down

0 comments on commit 5fc1065

Please sign in to comment.