fix: address release v2 reviews
This commit is contained in:
1 parent
4905ea6738
commit
32732680e5
55 files changed
+925
-294
No files matched your search
@@ -89,7 +89,7 @@ const parseTransport = (value: unknown, index: number): DockerEndpointTransport
|
||||
};
|
||||
|
||||
const parseCapabilities = (value: unknown, index: number): DockerEndpointCapability[] => {
|
||||
if (value === undefined) return allCapabilities;
|
||||
if (value === undefined) return [...allCapabilities];
|
||||
if (!Array.isArray(value) || value.some((capability) => !dockerEndpointCapabilities.includes(capability))) {
|
||||
throw new Error(`DOCKER_ENDPOINTS[${index}].capabilities contains an unsupported capability`);
|
||||
}
|
||||
@@ -136,7 +136,7 @@ export const createLegacySocketDescriptor = (path: string): DockerEndpointDescri
|
||||
name: path,
|
||||
kind: path.toLowerCase().includes("podman") ? "podman" : "docker",
|
||||
transport: { type: "socket", path },
|
||||
capabilities: allCapabilities,
|
||||
capabilities: [...allCapabilities],
|
||||
scope: "admin",
|
||||
source: "legacy",
|
||||
});
|
||||
@@ -146,7 +146,7 @@ export const createLegacyTcpDescriptor = (host: string, port: number): DockerEnd
|
||||
name: `${host}:${port}`,
|
||||
kind: "docker",
|
||||
transport: { type: "tcp", host, port, allowInsecure: true },
|
||||
capabilities: allCapabilities,
|
||||
capabilities: [...allCapabilities],
|
||||
scope: "admin",
|
||||
source: "legacy",
|
||||
});
|
||||
@@ -1,7 +1,7 @@
|
||||
import type Docker from "dockerode";
|
||||
import type { DockerEndpointCapability } from "./endpoint-descriptor";
|
||||
|
||||
export type { DockerInstance } from "./singleton";
|
||||
export type { DockerEndpointInitializationFailure, DockerInstance } from "./singleton";
|
||||
export { DockerSingleton } from "./singleton";
|
||||
export * from "./endpoint-descriptor";
|
||||
export type { ContainerInfo, Container, Port } from "dockerode";
|
||||
@@ -19,7 +19,7 @@ export interface DockerContainerTarget {
|
||||
export interface DockerEndpointStatus {
|
||||
id: string;
|
||||
name: string;
|
||||
status: "available" | "unavailable";
|
||||
status: "available" | "degraded" | "unavailable";
|
||||
kind?: "docker" | "podman";
|
||||
transport?: "socket" | "tcp" | "tls";
|
||||
capabilities?: DockerEndpointCapability[];
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import Docker from "dockerode";
|
||||
|
||||
import { createLogger } from "@homarr/core/infrastructure/logs";
|
||||
import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
||||
|
||||
import { env } from "./env";
|
||||
import type { DockerEndpointCapability, DockerEndpointDescriptor } from "./endpoint-descriptor";
|
||||
import {
|
||||
@@ -18,11 +21,40 @@ export interface DockerInstance {
|
||||
instance: Docker;
|
||||
}
|
||||
|
||||
export interface DockerEndpointInitializationFailure {
|
||||
descriptor: DockerEndpointDescriptor;
|
||||
host: string;
|
||||
}
|
||||
|
||||
const logger = createLogger({ module: "dockerSingleton" });
|
||||
|
||||
export class DockerSingleton {
|
||||
private static instances: DockerInstance[] | null = null;
|
||||
private static initializationFailures: DockerEndpointInitializationFailure[] = [];
|
||||
|
||||
private createInstanceSafely(descriptor: DockerEndpointDescriptor): DockerInstance[] {
|
||||
try {
|
||||
return [createInstance(descriptor)];
|
||||
} catch (error) {
|
||||
const host = getDockerEndpointHost(descriptor);
|
||||
DockerSingleton.initializationFailures.push({ descriptor, host });
|
||||
logger.warn(
|
||||
new ErrorWithMetadata(
|
||||
"Failed to initialize Docker endpoint",
|
||||
{ endpointId: descriptor.id, host },
|
||||
{ cause: error },
|
||||
),
|
||||
);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
private createInstances() {
|
||||
if (env.DOCKER_ENDPOINTS) return parseDockerEndpointDescriptors(env.DOCKER_ENDPOINTS).map(createInstance);
|
||||
if (env.DOCKER_ENDPOINTS) {
|
||||
return parseDockerEndpointDescriptors(env.DOCKER_ENDPOINTS).flatMap((descriptor) =>
|
||||
this.createInstanceSafely(descriptor),
|
||||
);
|
||||
}
|
||||
|
||||
const socketPaths = env.DOCKER_SOCKET_PATHS;
|
||||
const hostVariable = env.DOCKER_HOSTNAMES;
|
||||
@@ -30,7 +62,9 @@ export class DockerSingleton {
|
||||
|
||||
// Socket instances from DOCKER_SOCKET_PATHS
|
||||
const socketInstances: DockerInstance[] = socketPaths
|
||||
? socketPaths.split(",").map((socketPath) => createInstance(createLegacySocketDescriptor(socketPath)))
|
||||
? socketPaths
|
||||
.split(",")
|
||||
.flatMap((socketPath) => this.createInstanceSafely(createLegacySocketDescriptor(socketPath)))
|
||||
: [];
|
||||
|
||||
// TCP instances from existing DOCKER_HOSTNAMES/DOCKER_PORTS
|
||||
@@ -43,9 +77,9 @@ export class DockerSingleton {
|
||||
throw new Error("The number of hosts and ports must match");
|
||||
}
|
||||
|
||||
tcpInstances = hostnames.map((host, i) =>
|
||||
tcpInstances = hostnames.flatMap((host, i) =>
|
||||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
||||
createInstance(createLegacyTcpDescriptor(host, parseInt(ports[i]!, 10))),
|
||||
this.createInstanceSafely(createLegacyTcpDescriptor(host, parseInt(ports[i]!, 10))),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -59,7 +93,7 @@ export class DockerSingleton {
|
||||
name: "Local Docker",
|
||||
source: "default" as const,
|
||||
};
|
||||
return [{ ...createInstance(descriptor), host: "socket", instance: new Docker() }];
|
||||
return [createInstance(descriptor)];
|
||||
}
|
||||
|
||||
return instances;
|
||||
@@ -78,9 +112,15 @@ export class DockerSingleton {
|
||||
return this.instances;
|
||||
}
|
||||
|
||||
this.initializationFailures = [];
|
||||
this.instances = new DockerSingleton().createInstances();
|
||||
return this.instances;
|
||||
}
|
||||
|
||||
public static getInitializationFailures(): DockerEndpointInitializationFailure[] {
|
||||
this.getInstances();
|
||||
return [...this.initializationFailures];
|
||||
}
|
||||
}
|
||||
|
||||
const createInstance = (descriptor: DockerEndpointDescriptor): DockerInstance => ({
|
||||
|
||||
@@ -57,4 +57,15 @@ describe("parseDockerEndpointDescriptors", () => {
|
||||
),
|
||||
).toThrow("must include inventory");
|
||||
});
|
||||
|
||||
test("returns independent default capability arrays", () => {
|
||||
const [first, second] = parseDockerEndpointDescriptors(
|
||||
JSON.stringify([
|
||||
{ id: "first", name: "First", transport: { type: "socket", path: "/first.sock" } },
|
||||
{ id: "second", name: "Second", transport: { type: "socket", path: "/second.sock" } },
|
||||
]),
|
||||
);
|
||||
|
||||
expect(first?.capabilities).not.toBe(second?.capabilities);
|
||||
});
|
||||
});
|
||||
@@ -40,8 +40,10 @@ describe("DockerSingleton", () => {
|
||||
const instances = DockerSingleton.getInstances();
|
||||
|
||||
expect(instances).toHaveLength(1);
|
||||
expect(instances[0]?.host).toBe("socket");
|
||||
expect(instances[0]?.host).toBe("/var/run/docker.sock");
|
||||
expect(instances[0]).toMatchObject({ endpointId: "socket:default", endpointName: "Local Docker" });
|
||||
const options = (instances[0]?.instance as unknown as { opts: { socketPath: string } } | undefined)?.opts;
|
||||
expect(options?.socketPath).toBe("/var/run/docker.sock");
|
||||
});
|
||||
|
||||
test("should return TCP instances only when DOCKER_HOSTNAMES and DOCKER_PORTS set", () => {
|
||||
@@ -131,4 +133,20 @@ describe("DockerSingleton", () => {
|
||||
expect(DockerSingleton.hasCapability("readonly-podman", "logs")).toBe(true);
|
||||
expect(DockerSingleton.hasCapability("readonly-podman", "remove")).toBe(false);
|
||||
});
|
||||
|
||||
test("isolates an unreadable TLS endpoint from healthy endpoints", () => {
|
||||
mockEnv.DOCKER_ENDPOINTS = JSON.stringify([
|
||||
{ id: "local", name: "Local", transport: { type: "socket", path: "/var/run/docker.sock" } },
|
||||
{
|
||||
id: "broken-tls",
|
||||
name: "Broken TLS",
|
||||
transport: { type: "tls", host: "broken.example", port: 2376, caPath: "/missing/docker-ca.pem" },
|
||||
},
|
||||
]);
|
||||
|
||||
expect(DockerSingleton.getInstances().map(({ endpointId }) => endpointId)).toEqual(["local"]);
|
||||
expect(DockerSingleton.getInitializationFailures()).toEqual([
|
||||
expect.objectContaining({ descriptor: expect.objectContaining({ id: "broken-tls" }) }),
|
||||
]);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user