Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/proxy healthcheck through host proxy #44

Open
wants to merge 3 commits into
base: dstewart/chore/avoid-bespoke-protocol
Choose a base branch
from
Open
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
51 changes: 44 additions & 7 deletions src/docker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface ContainerHandle {
stdin: NodeJS.WritableStream;
stdout: Promise<string>;
stderr: Promise<string>;
hostPortMapping: Record<string, string>;
}

export type ClientContainer = {
Expand Down Expand Up @@ -221,6 +222,24 @@ function stderrStreamToString(stream: NodeJS.WritableStream): Promise<string> {
});
}

async function getPortMapping(
container: Docker.Container,
): Promise<Record<string, string>> {
const inspectResult = await container.inspect();
return Object.entries(inspectResult.NetworkSettings.Ports).reduce(
(cur, [_port, bindings]) => {
const [port, proto] = _port.split('/', 2);
if (proto === 'tcp') {
for (const { HostPort } of bindings) {
cur[port] = HostPort;
}
}
return cur;
},
{} as Record<string, string>,
);
}

export async function setupContainer(
testId: string,
clientImpl: string,
Expand Down Expand Up @@ -259,6 +278,9 @@ export async function setupContainer(
HostConfig: {
NetworkMode: network.id,
AutoRemove: false,
PortBindings: {
'8080/tcp': [],
},
},
AttachStdin: true,
AttachStderr: true,
Expand All @@ -280,6 +302,9 @@ export async function setupContainer(
await container.start();
const [stdin, stdout, stderr] = await containerStreams(container);

const hostPortMapping: Record<string, string> =
await getPortMapping(container);

const removeContainerIfExists = async () => {
stdout.end();
stderr.end();
Expand Down Expand Up @@ -330,7 +355,7 @@ export async function setupContainer(

// warm up the container. wait 10s until we get at least one good result back.
if (type === 'server') {
healthCheck(container, network);
await healthCheck(container, network, hostPortMapping);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

}
const responses: Pushable<{
id: string;
Expand All @@ -349,10 +374,15 @@ export async function setupContainer(
responses.end();
},
syncBarriers: {},
hostPortMapping,
};
}

async function healthCheck(container: Container, network: Network) {
async function healthCheck(
container: Container,
network: Network,
hostPortMapping: Record<string, string>,
) {
for (let remaining = 100; remaining >= 0; remaining--) {
try {
const networkInfo: NetworkInspectInfo = await network.inspect();
Expand All @@ -367,10 +397,7 @@ async function healthCheck(container: Container, network: Network) {
throw new Error('Container not found in network');
}

const ipAddressWithSubnet = networkContainer.IPv4Address;
const [ip] = ipAddressWithSubnet.split('/');

const address = `http://${ip}:8080/healthz`;
const address = `http://localhost:${hostPortMapping['8080']}/healthz`;

// We just need for the fetch to give us something that looks like HTTP back.
await fetch(address, { headers: { Connection: 'close' } });
Expand Down Expand Up @@ -448,7 +475,17 @@ export async function applyActionServer(
) {
await applyActionCommon(network, containerHandle, action, log);
if (action.type == 'restart_container') {
healthCheck(containerHandle.container, network);
// After the server has been restarted, the port mapping will need to be rediscovered:
const hostPortMapping: Record<string, string> = await getPortMapping(
containerHandle.container,
);
containerHandle.hostPortMapping = hostPortMapping;

await healthCheck(
containerHandle.container,
network,
containerHandle.hostPortMapping,
);
}
}

Expand Down