add port/mount display
This commit is contained in:
1 parent
56153b38a5
commit
786c803847
21 files changed
+441
-24
No files matched your search
@@ -7295,6 +7295,55 @@
|
||||
},
|
||||
"startedAt": {
|
||||
"type": "string"
|
||||
},
|
||||
"ports": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"privatePort": {
|
||||
"type": "number"
|
||||
},
|
||||
"protocol": {
|
||||
"type": "string",
|
||||
"enum": ["tcp", "udp", "sctp"]
|
||||
},
|
||||
"publicPort": {
|
||||
"type": "number"
|
||||
},
|
||||
"hostIp": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["privatePort", "protocol"]
|
||||
}
|
||||
},
|
||||
"mounts": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"source": {
|
||||
"type": "string"
|
||||
},
|
||||
"destination": {
|
||||
"type": "string"
|
||||
},
|
||||
"mode": {
|
||||
"type": "string"
|
||||
},
|
||||
"rw": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"required": ["type", "source", "destination", "mode", "rw"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["id", "name", "image", "state", "status", "health"]
|
||||
@@ -7792,6 +7841,55 @@
|
||||
},
|
||||
"startedAt": {
|
||||
"type": "string"
|
||||
},
|
||||
"ports": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"privatePort": {
|
||||
"type": "number"
|
||||
},
|
||||
"protocol": {
|
||||
"type": "string",
|
||||
"enum": ["tcp", "udp", "sctp"]
|
||||
},
|
||||
"publicPort": {
|
||||
"type": "number"
|
||||
},
|
||||
"hostIp": {
|
||||
"type": "string"
|
||||
}
|
||||
},
|
||||
"required": ["privatePort", "protocol"]
|
||||
}
|
||||
},
|
||||
"mounts": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"type": {
|
||||
"type": "string"
|
||||
},
|
||||
"name": {
|
||||
"type": "string"
|
||||
},
|
||||
"source": {
|
||||
"type": "string"
|
||||
},
|
||||
"destination": {
|
||||
"type": "string"
|
||||
},
|
||||
"mode": {
|
||||
"type": "string"
|
||||
},
|
||||
"rw": {
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"required": ["type", "source", "destination", "mode", "rw"]
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": ["id", "name", "image", "state", "status", "health"]
|
||||
|
||||
@@ -13,7 +13,7 @@ import {
|
||||
ProxyModes,
|
||||
} from "@/domain/monitors/monitor.type.js";
|
||||
import { DateRanges, SortOrders } from "@/types/query.js";
|
||||
import { DockerContainerStates, DockerHealthStatuses } from "@/types/network.js";
|
||||
import { DockerContainerStates, DockerHealthStatuses, DockerPortProtocols } from "@/types/network.js";
|
||||
|
||||
const httpStatusCode = z.number().refine((code) => HttpStatusCodeSet.has(code), { message: "Must be a valid HTTP status code" });
|
||||
|
||||
@@ -430,6 +430,23 @@ export const uptimeDetailsResponseSchema = z.object({
|
||||
monitorStats: monitorStatsResponseSchema.nullable(),
|
||||
});
|
||||
|
||||
// Keep aligned with DockerContainerPort / DockerContainerMount in types/network.ts.
|
||||
export const dockerContainerPortResponseSchema = z.object({
|
||||
privatePort: z.number(),
|
||||
protocol: z.enum(DockerPortProtocols),
|
||||
publicPort: z.number().optional(),
|
||||
hostIp: z.string().optional(),
|
||||
});
|
||||
|
||||
export const dockerContainerMountResponseSchema = z.object({
|
||||
type: z.string(),
|
||||
name: z.string().optional(),
|
||||
source: z.string(),
|
||||
destination: z.string(),
|
||||
mode: z.string(),
|
||||
rw: z.boolean(),
|
||||
});
|
||||
|
||||
// Keep aligned with DockerContainerInfo / DockerContainerSummary in types/network.ts.
|
||||
export const dockerContainerResponseSchema = z.object({
|
||||
id: z.string(),
|
||||
@@ -444,6 +461,8 @@ export const dockerContainerResponseSchema = z.object({
|
||||
memoryPct: z.number().optional(),
|
||||
restartCount: z.number().optional(),
|
||||
startedAt: z.string().optional(),
|
||||
ports: z.array(dockerContainerPortResponseSchema).optional(),
|
||||
mounts: z.array(dockerContainerMountResponseSchema).optional(),
|
||||
});
|
||||
|
||||
export const containerSummaryResponseSchema = z.object({
|
||||
|
||||
@@ -14,7 +14,15 @@ import type {
|
||||
GotTimings,
|
||||
ILighthouseAudit,
|
||||
} from "@/domain/checks/check.type.js";
|
||||
import { DockerContainerInfo, DockerContainerStates, DockerContainerSummary, DockerHealthStatuses } from "@/types/network.js";
|
||||
import {
|
||||
DockerContainerInfo,
|
||||
DockerContainerMount,
|
||||
DockerContainerPort,
|
||||
DockerContainerStates,
|
||||
DockerContainerSummary,
|
||||
DockerHealthStatuses,
|
||||
DockerPortProtocols,
|
||||
} from "@/types/network.js";
|
||||
|
||||
type CheckMetadataDocument = Omit<CheckMetadata, "monitorId" | "teamId"> & {
|
||||
monitorId: Types.ObjectId;
|
||||
@@ -173,6 +181,28 @@ const auditsSchema = new Schema<CheckAudits>(
|
||||
{ _id: false }
|
||||
);
|
||||
|
||||
const dockerContainerPortSchema = new Schema<DockerContainerPort>(
|
||||
{
|
||||
privatePort: { type: Number, required: true },
|
||||
protocol: { type: String, enum: DockerPortProtocols, default: "tcp" },
|
||||
publicPort: { type: Number },
|
||||
hostIp: { type: String },
|
||||
},
|
||||
{ _id: false }
|
||||
);
|
||||
|
||||
const dockerContainerMountSchema = new Schema<DockerContainerMount>(
|
||||
{
|
||||
type: { type: String, default: "" },
|
||||
name: { type: String },
|
||||
source: { type: String, default: "" },
|
||||
destination: { type: String, default: "" },
|
||||
mode: { type: String, default: "" },
|
||||
rw: { type: Boolean, default: true },
|
||||
},
|
||||
{ _id: false }
|
||||
);
|
||||
|
||||
const dockerContainerSchema = new Schema<DockerContainerInfo>(
|
||||
{
|
||||
id: { type: String, required: true },
|
||||
@@ -187,6 +217,8 @@ const dockerContainerSchema = new Schema<DockerContainerInfo>(
|
||||
memoryPct: { type: Number },
|
||||
restartCount: { type: Number },
|
||||
startedAt: { type: String },
|
||||
ports: { type: [dockerContainerPortSchema], default: undefined },
|
||||
mounts: { type: [dockerContainerMountSchema], default: undefined },
|
||||
},
|
||||
{ _id: false }
|
||||
);
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
import { IStatusProvider } from "@/service/network/IStatusProvider.js";
|
||||
import {
|
||||
DockerContainerInfo,
|
||||
DockerContainerMount,
|
||||
DockerContainerPort,
|
||||
DockerContainerState,
|
||||
DockerContainerStates,
|
||||
DockerContainerSummary,
|
||||
DockerHealthStatus,
|
||||
DockerHealthStatuses,
|
||||
DockerPortProtocol,
|
||||
DockerPortProtocols,
|
||||
DockerStatusPayload,
|
||||
MonitorStatusResponse,
|
||||
} from "@/types/network.js";
|
||||
@@ -135,6 +139,8 @@ export class DockerProvider implements IStatusProvider<DockerStatusPayload> {
|
||||
info.restartCount = inspectResult.value.RestartCount;
|
||||
info.startedAt = inspectResult.value.State?.StartedAt;
|
||||
info.health = this.toHealthStatus(inspectResult.value.State?.Health?.Status);
|
||||
info.ports = this.toPorts(inspectResult.value.NetworkSettings?.Ports);
|
||||
info.mounts = this.toMounts(inspectResult.value.Mounts);
|
||||
} else {
|
||||
this.logger.warn({
|
||||
message: `Failed to inspect container ${info.name}`,
|
||||
@@ -152,6 +158,46 @@ export class DockerProvider implements IStatusProvider<DockerStatusPayload> {
|
||||
return info;
|
||||
}
|
||||
|
||||
private toPortProtocol(protocol: string): DockerPortProtocol {
|
||||
return (DockerPortProtocols as readonly string[]).includes(protocol) ? (protocol as DockerPortProtocol) : "tcp";
|
||||
}
|
||||
|
||||
private toPorts(ports: Dockerode.ContainerInspectInfo["NetworkSettings"]["Ports"] | undefined): DockerContainerPort[] {
|
||||
const result: DockerContainerPort[] = [];
|
||||
for (const [portAndProtocol, bindings] of Object.entries(ports ?? {})) {
|
||||
const [portString, protocolString = ""] = portAndProtocol.split("/");
|
||||
const privatePort = Number(portString);
|
||||
if (!Number.isInteger(privatePort)) continue;
|
||||
const protocol = this.toPortProtocol(protocolString);
|
||||
// Exposed but unpublished ports have no bindings
|
||||
if (!bindings || bindings.length === 0) {
|
||||
result.push({ privatePort, protocol });
|
||||
continue;
|
||||
}
|
||||
for (const binding of bindings) {
|
||||
const publicPort = Number(binding.HostPort);
|
||||
result.push({
|
||||
privatePort,
|
||||
protocol,
|
||||
publicPort: Number.isInteger(publicPort) ? publicPort : undefined,
|
||||
hostIp: binding.HostIp || undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private toMounts(mounts: Dockerode.ContainerInspectInfo["Mounts"] | undefined): DockerContainerMount[] {
|
||||
return (mounts ?? []).map((mount) => ({
|
||||
type: mount.Type ?? "",
|
||||
name: mount.Name,
|
||||
source: mount.Source ?? "",
|
||||
destination: mount.Destination ?? "",
|
||||
mode: mount.Mode ?? "",
|
||||
rw: mount.RW ?? true,
|
||||
}));
|
||||
}
|
||||
|
||||
handle = async (monitor: Monitor): Promise<MonitorStatusResponse<DockerStatusPayload>> => {
|
||||
try {
|
||||
const docker = new this.DockerLib(this.toDockerOptions(monitor));
|
||||
|
||||
@@ -123,6 +123,27 @@ export interface DockerContainerInfo {
|
||||
memoryPct?: number; // 0-1 fraction of the memory limit
|
||||
restartCount?: number;
|
||||
startedAt?: string; // ISO date
|
||||
ports?: DockerContainerPort[];
|
||||
mounts?: DockerContainerMount[];
|
||||
}
|
||||
|
||||
export const DockerPortProtocols = ["tcp", "udp", "sctp"] as const;
|
||||
export type DockerPortProtocol = (typeof DockerPortProtocols)[number];
|
||||
|
||||
export interface DockerContainerPort {
|
||||
privatePort: number;
|
||||
protocol: DockerPortProtocol;
|
||||
publicPort?: number;
|
||||
hostIp?: string;
|
||||
}
|
||||
|
||||
export interface DockerContainerMount {
|
||||
type: string; // open set: bind, volume, tmpfs, image, npipe, cluster, …
|
||||
name?: string;
|
||||
source: string;
|
||||
destination: string;
|
||||
mode: string;
|
||||
rw: boolean;
|
||||
}
|
||||
|
||||
export interface DockerStatusPayload {
|
||||
|
||||
@@ -375,6 +375,89 @@ describe("DockerProvider", () => {
|
||||
});
|
||||
});
|
||||
|
||||
// ── Ports and mounts ─────────────────────────────────────────────────
|
||||
|
||||
describe("ports and mounts", () => {
|
||||
it("maps published and unpublished ports and mounts from inspect", async () => {
|
||||
const inspect = jest.fn().mockResolvedValue(
|
||||
makeInspect({
|
||||
NetworkSettings: {
|
||||
Ports: {
|
||||
"80/tcp": [{ HostIp: "0.0.0.0", HostPort: "8080" }],
|
||||
"443/tcp": null,
|
||||
},
|
||||
},
|
||||
Mounts: [
|
||||
{
|
||||
Type: "volume",
|
||||
Name: "mongo_data",
|
||||
Source: "/var/lib/docker/volumes/mongo_data/_data",
|
||||
Destination: "/data/db",
|
||||
Mode: "z",
|
||||
RW: true,
|
||||
},
|
||||
{ Type: "bind", Source: "/srv/config", Destination: "/etc/app", Mode: "ro", RW: false },
|
||||
],
|
||||
})
|
||||
);
|
||||
const { provider } = setup({ inspect });
|
||||
|
||||
const result = await provider.handle(makeMonitor());
|
||||
|
||||
const container = result.payload?.containers[0];
|
||||
expect(container?.ports).toEqual([
|
||||
{ privatePort: 80, protocol: "tcp", publicPort: 8080, hostIp: "0.0.0.0" },
|
||||
{ privatePort: 443, protocol: "tcp" },
|
||||
]);
|
||||
expect(container?.mounts).toEqual([
|
||||
{ type: "volume", name: "mongo_data", source: "/var/lib/docker/volumes/mongo_data/_data", destination: "/data/db", mode: "z", rw: true },
|
||||
{ type: "bind", name: undefined, source: "/srv/config", destination: "/etc/app", mode: "ro", rw: false },
|
||||
]);
|
||||
});
|
||||
|
||||
it("emits one port entry per host binding", async () => {
|
||||
const inspect = jest.fn().mockResolvedValue(
|
||||
makeInspect({
|
||||
NetworkSettings: {
|
||||
Ports: {
|
||||
"80/tcp": [
|
||||
{ HostIp: "0.0.0.0", HostPort: "8080" },
|
||||
{ HostIp: "::", HostPort: "8080" },
|
||||
],
|
||||
},
|
||||
},
|
||||
})
|
||||
);
|
||||
const { provider } = setup({ inspect });
|
||||
|
||||
const result = await provider.handle(makeMonitor());
|
||||
|
||||
expect(result.payload?.containers[0]?.ports).toEqual([
|
||||
{ privatePort: 80, protocol: "tcp", publicPort: 8080, hostIp: "0.0.0.0" },
|
||||
{ privatePort: 80, protocol: "tcp", publicPort: 8080, hostIp: "::" },
|
||||
]);
|
||||
});
|
||||
|
||||
it("maps missing ports and mounts to empty arrays", async () => {
|
||||
const { provider } = setup();
|
||||
|
||||
const result = await provider.handle(makeMonitor());
|
||||
|
||||
expect(result.payload?.containers[0]?.ports).toEqual([]);
|
||||
expect(result.payload?.containers[0]?.mounts).toEqual([]);
|
||||
});
|
||||
|
||||
it("leaves ports and mounts undefined when inspect fails", async () => {
|
||||
const inspect = jest.fn().mockRejectedValue(new Error("inspect failed"));
|
||||
const { provider } = setup({ inspect });
|
||||
|
||||
const result = await provider.handle(makeMonitor());
|
||||
|
||||
expect(result.payload?.containers[0]?.ports).toBeUndefined();
|
||||
expect(result.payload?.containers[0]?.mounts).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
// ── Outer error handling ─────────────────────────────────────────────
|
||||
|
||||
describe("outer error handling", () => {
|
||||
|
||||
Reference in new issue
Block a user