Skip to content

Commit

Permalink
Allows overriding the BACKEND_ENDPOINT via env (#116)
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
codefromthecrypt authored Feb 21, 2024
1 parent 0267b61 commit 612b1e0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 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(Frontend.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
8 changes: 6 additions & 2 deletions docker/bin/install-example
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ 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.
cat >> start-frontend <<-EOF
export PORT=${frontend_port}
export EXAMPLE=${version}
export EXAMPLE_SERVICE=frontend
DEFAULT_BACKEND_ENDPOINT=http://backend:${backend_port}/api
EOF

cat >> start-backend <<-EOF
Expand All @@ -40,7 +42,8 @@ export EXAMPLE=${version}
export 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 +58,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
2 changes: 1 addition & 1 deletion docker/bin/post-install-example-cassandra
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -eux
sed -i 's~JAVA_OPTS=~sed -i "s/127.0.0.1/$IP/g" classes/cassandra.yaml\n\nJAVA_OPTS=~' start-backend

# Communication isn't HTTP
sed -i 's~endpoint=http://\(.*\)/api~contactPoint=\1~' start-frontend
sed -i 's~DEFAULT_BACKEND_ENDPOINT=http://\(.*\)/api~DEFAULT_BACKEND_ENDPOINT=\1~' start-frontend
sed -i 's/HEALTHCHECK_KIND=http/HEALTHCHECK_KIND=tcp/g' start-backend

# Cassandra needs more than 32m memory
Expand Down
8 changes: 7 additions & 1 deletion jersey2-cassandra3/src/main/java/brave/example/Frontend.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
import javax.ws.rs.Path;
import javax.ws.rs.ext.RuntimeDelegate;
import org.glassfish.jersey.server.ResourceConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static java.nio.charset.StandardCharsets.UTF_8;

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

@Path("")
public static class Resource {
final Session session;
Expand Down Expand Up @@ -50,8 +54,10 @@ public static class HealthCheck implements HttpHandler {
}

public static void main(String[] args) throws Exception {
String contactPointString = System.getProperty("backend.contactPoint", "127.0.0.1:9042");
String contactPointString = System.getProperty("backend.endpoint", "127.0.0.1:9042");
HostAndPort parsed = HostAndPort.fromString(contactPointString).withDefaultPort(9042);
LOGGER.info("Using contact point: {}", parsed);

Cluster cluster = Cluster.builder()
.addContactPointsWithPorts(new InetSocketAddress(parsed.getHost(), parsed.getPort()))
.build();
Expand Down

0 comments on commit 612b1e0

Please sign in to comment.