enhance release v2 experience

This commit is contained in:
ajnart committed 2026-08-14 02:18:46 +02:00
1 parent 9b4970583e
commit ef2ed6c602
152 files changed
+7236 -764

No files matched your search

+152
View File
@@ -0,0 +1,152 @@
export const dockerEndpointCapabilities = ["inventory", "logs", "lifecycle", "remove"] as const;
export type DockerEndpointCapability = (typeof dockerEndpointCapabilities)[number];
export type DockerEndpointTransport =
| { type: "socket"; path: string }
| { type: "tcp"; host: string; port: number; allowInsecure: true }
| {
type: "tls";
host: string;
port: number;
caPath: string;
certPath?: string;
keyPath?: string;
};
export interface DockerEndpointDescriptor {
id: string;
name: string;
kind: "docker" | "podman";
transport: DockerEndpointTransport;
capabilities: DockerEndpointCapability[];
scope: "admin";
source: "environment" | "legacy" | "default";
}
const allCapabilities = [...dockerEndpointCapabilities];
export const parseDockerEndpointDescriptors = (value: string): DockerEndpointDescriptor[] => {
let input: unknown;
try {
input = JSON.parse(value);
} catch (error) {
throw new Error("DOCKER_ENDPOINTS must be valid JSON", { cause: error });
}
if (!Array.isArray(input) || input.length === 0) {
throw new Error("DOCKER_ENDPOINTS must be a non-empty JSON array");
}
const descriptors = input.map((entry, index) => parseDescriptor(entry, index));
const ids = new Set(descriptors.map(({ id }) => id));
if (ids.size !== descriptors.length) throw new Error("DOCKER_ENDPOINTS ids must be unique");
return descriptors;
};
const parseDescriptor = (value: unknown, index: number): DockerEndpointDescriptor => {
const entry = getRecord(value, `DOCKER_ENDPOINTS[${index}]`);
const id = getNonEmptyString(entry.id, `DOCKER_ENDPOINTS[${index}].id`);
const name = getNonEmptyString(entry.name, `DOCKER_ENDPOINTS[${index}].name`);
const kind =
entry.kind === "podman" ? "podman" : entry.kind === "docker" || entry.kind === undefined ? "docker" : null;
if (!kind) throw new Error(`DOCKER_ENDPOINTS[${index}].kind must be docker or podman`);
const transport = parseTransport(entry.transport, index);
const capabilities = parseCapabilities(entry.capabilities, index);
return { id, name, kind, transport, capabilities, scope: "admin", source: "environment" };
};
const parseTransport = (value: unknown, index: number): DockerEndpointTransport => {
const transport = getRecord(value, `DOCKER_ENDPOINTS[${index}].transport`);
if (transport.type === "socket") {
return { type: "socket", path: getAbsolutePath(transport.path, `DOCKER_ENDPOINTS[${index}].transport.path`) };
}
if (transport.type === "tcp") {
if (transport.allowInsecure !== true) {
throw new Error(`DOCKER_ENDPOINTS[${index}] plaintext TCP requires allowInsecure: true`);
}
return {
type: "tcp",
host: getNonEmptyString(transport.host, `DOCKER_ENDPOINTS[${index}].transport.host`),
port: getPort(transport.port, index),
allowInsecure: true,
};
}
if (transport.type === "tls") {
const certPath = getOptionalAbsolutePath(transport.certPath, `DOCKER_ENDPOINTS[${index}].transport.certPath`);
const keyPath = getOptionalAbsolutePath(transport.keyPath, `DOCKER_ENDPOINTS[${index}].transport.keyPath`);
if (Boolean(certPath) !== Boolean(keyPath)) {
throw new Error(`DOCKER_ENDPOINTS[${index}] TLS certPath and keyPath must be provided together`);
}
return {
type: "tls",
host: getNonEmptyString(transport.host, `DOCKER_ENDPOINTS[${index}].transport.host`),
port: getPort(transport.port, index),
caPath: getAbsolutePath(transport.caPath, `DOCKER_ENDPOINTS[${index}].transport.caPath`),
...(certPath && keyPath ? { certPath, keyPath } : {}),
};
}
throw new Error(`DOCKER_ENDPOINTS[${index}].transport.type must be socket, tcp, or tls`);
};
const parseCapabilities = (value: unknown, index: number): DockerEndpointCapability[] => {
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`);
}
const capabilities = [...new Set(value)] as DockerEndpointCapability[];
if (!capabilities.includes("inventory")) {
throw new Error(`DOCKER_ENDPOINTS[${index}].capabilities must include inventory`);
}
return capabilities;
};
const getRecord = (value: unknown, path: string): Record<string, unknown> => {
if (!value || typeof value !== "object" || Array.isArray(value)) throw new Error(`${path} must be an object`);
return value as Record<string, unknown>;
};
const getNonEmptyString = (value: unknown, path: string) => {
if (typeof value !== "string" || value.trim().length === 0) throw new Error(`${path} must be a non-empty string`);
return value.trim();
};
const getAbsolutePath = (value: unknown, path: string) => {
const result = getNonEmptyString(value, path);
if (!result.startsWith("/")) throw new Error(`${path} must be an absolute path`);
return result;
};
const getOptionalAbsolutePath = (value: unknown, path: string) =>
value === undefined ? undefined : getAbsolutePath(value, path);
const getPort = (value: unknown, index: number) => {
if (!Number.isInteger(value) || Number(value) < 1 || Number(value) > 65_535) {
throw new Error(`DOCKER_ENDPOINTS[${index}].transport.port must be an integer from 1 to 65535`);
}
return Number(value);
};
export const getDockerEndpointHost = (descriptor: DockerEndpointDescriptor) =>
descriptor.transport.type === "socket"
? descriptor.transport.path
: `${descriptor.transport.host}:${descriptor.transport.port}`;
export const createLegacySocketDescriptor = (path: string): DockerEndpointDescriptor => ({
id: `socket:${path}`,
name: path,
kind: path.toLowerCase().includes("podman") ? "podman" : "docker",
transport: { type: "socket", path },
capabilities: allCapabilities,
scope: "admin",
source: "legacy",
});
export const createLegacyTcpDescriptor = (host: string, port: number): DockerEndpointDescriptor => ({
id: `tcp:${host}:${port}`,
name: `${host}:${port}`,
kind: "docker",
transport: { type: "tcp", host, port, allowInsecure: true },
capabilities: allCapabilities,
scope: "admin",
source: "legacy",
});
+2
View File
@@ -9,6 +9,8 @@ export const env = createEnv({
DOCKER_PORTS: z.string().optional(),
// Comma separated paths to Docker-compatible Unix sockets (e.g., /var/run/docker.sock,/run/user/1000/podman/podman.sock)
DOCKER_SOCKET_PATHS: z.string().optional(),
// JSON descriptors for friendly names, TLS, Podman identity, and per-endpoint capabilities.
DOCKER_ENDPOINTS: z.string().optional(),
ENABLE_DOCKER: createBooleanSchema(true),
ENABLE_KUBERNETES: createBooleanSchema(false),
},
+19
View File
@@ -1,12 +1,31 @@
import type Docker from "dockerode";
import type { DockerEndpointCapability } from "./endpoint-descriptor";
export type { DockerInstance } from "./singleton";
export { DockerSingleton } from "./singleton";
export * from "./endpoint-descriptor";
export type { ContainerInfo, Container, Port } from "dockerode";
export type { Docker };
export const containerStates = ["created", "running", "paused", "restarting", "exited", "removing", "dead"] as const;
export type ContainerState = (typeof containerStates)[number];
export interface DockerContainerTarget {
endpointId: string;
id: string;
}
export interface DockerEndpointStatus {
id: string;
name: string;
status: "available" | "unavailable";
kind?: "docker" | "podman";
transport?: "socket" | "tcp" | "tls";
capabilities?: DockerEndpointCapability[];
source?: "environment" | "legacy" | "default";
scope?: "admin";
}
export * from "./labels";
export * from "./discovery";
+53 -11
View File
@@ -1,9 +1,20 @@
import { readFileSync } from "node:fs";
import Docker from "dockerode";
import { env } from "./env";
import type { DockerEndpointCapability, DockerEndpointDescriptor } from "./endpoint-descriptor";
import {
createLegacySocketDescriptor,
createLegacyTcpDescriptor,
getDockerEndpointHost,
parseDockerEndpointDescriptors,
} from "./endpoint-descriptor";
export interface DockerInstance {
endpointId: string;
endpointName: string;
host: string;
descriptor: DockerEndpointDescriptor;
instance: Docker;
}
@@ -11,16 +22,15 @@ export class DockerSingleton {
private static instances: DockerInstance[] | null = null;
private createInstances() {
if (env.DOCKER_ENDPOINTS) return parseDockerEndpointDescriptors(env.DOCKER_ENDPOINTS).map(createInstance);
const socketPaths = env.DOCKER_SOCKET_PATHS;
const hostVariable = env.DOCKER_HOSTNAMES;
const portVariable = env.DOCKER_PORTS;
// Socket instances from DOCKER_SOCKET_PATHS
const socketInstances: DockerInstance[] = socketPaths
? socketPaths.split(",").map((socketPath) => ({
host: socketPath,
instance: new Docker({ socketPath }),
}))
? socketPaths.split(",").map((socketPath) => createInstance(createLegacySocketDescriptor(socketPath)))
: [];
// TCP instances from existing DOCKER_HOSTNAMES/DOCKER_PORTS
@@ -33,25 +43,34 @@ export class DockerSingleton {
throw new Error("The number of hosts and ports must match");
}
tcpInstances = hostnames.map((host, i) => ({
host: `${host}:${ports[i]}`,
tcpInstances = hostnames.map((host, i) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
instance: new Docker({ host, port: parseInt(ports[i]!, 10) }),
}));
createInstance(createLegacyTcpDescriptor(host, parseInt(ports[i]!, 10))),
);
}
const instances = [...socketInstances, ...tcpInstances];
// Default: local socket if nothing else configured
if (instances.length === 0) {
return [{ host: "socket", instance: new Docker() }];
const descriptor = {
...createLegacySocketDescriptor("/var/run/docker.sock"),
id: "socket:default",
name: "Local Docker",
source: "default" as const,
};
return [{ ...createInstance(descriptor), host: "socket", instance: new Docker() }];
}
return instances;
}
public static findInstance(host: string): DockerInstance | undefined {
return this.instances?.find((instance) => instance.host === host);
public static findInstance(endpointId: string): DockerInstance | undefined {
return this.getInstances().find((instance) => instance.endpointId === endpointId);
}
public static hasCapability(endpointId: string, capability: DockerEndpointCapability): boolean {
return this.findInstance(endpointId)?.descriptor.capabilities.includes(capability) ?? false;
}
public static getInstances(): DockerInstance[] {
@@ -63,3 +82,26 @@ export class DockerSingleton {
return this.instances;
}
}
const createInstance = (descriptor: DockerEndpointDescriptor): DockerInstance => ({
endpointId: descriptor.id,
endpointName: descriptor.name,
host: getDockerEndpointHost(descriptor),
descriptor,
instance: new Docker(createDockerOptions(descriptor)),
});
const createDockerOptions = (descriptor: DockerEndpointDescriptor) => {
const transport = descriptor.transport;
if (transport.type === "socket") return { socketPath: transport.path };
if (transport.type === "tcp") return { host: transport.host, port: transport.port, protocol: "http" as const };
return {
host: transport.host,
port: transport.port,
protocol: "https" as const,
ca: readFileSync(transport.caPath),
...(transport.certPath && transport.keyPath
? { cert: readFileSync(transport.certPath), key: readFileSync(transport.keyPath) }
: {}),
};
};
@@ -0,0 +1,60 @@
import { describe, expect, test } from "vitest";
import { parseDockerEndpointDescriptors } from "../endpoint-descriptor";
describe("parseDockerEndpointDescriptors", () => {
test("parses friendly TLS and read-only Podman endpoints", () => {
const descriptors = parseDockerEndpointDescriptors(
JSON.stringify([
{
id: "nas",
name: "NAS Docker",
transport: { type: "tls", host: "nas.internal", port: 2376, caPath: "/run/secrets/docker-ca" },
},
{
id: "podman",
name: "Build host",
kind: "podman",
transport: { type: "socket", path: "/run/podman/podman.sock" },
capabilities: ["inventory", "logs"],
},
]),
);
expect(descriptors).toEqual([
expect.objectContaining({ id: "nas", name: "NAS Docker", kind: "docker", source: "environment" }),
expect.objectContaining({ id: "podman", kind: "podman", capabilities: ["inventory", "logs"] }),
]);
});
test("requires an explicit opt-in for plaintext TCP", () => {
expect(() =>
parseDockerEndpointDescriptors(
JSON.stringify([{ id: "nas", name: "NAS", transport: { type: "tcp", host: "nas", port: 2375 } }]),
),
).toThrow("allowInsecure: true");
});
test("rejects duplicate identities and capabilities without inventory", () => {
expect(() =>
parseDockerEndpointDescriptors(
JSON.stringify([
{ id: "same", name: "A", transport: { type: "socket", path: "/a" } },
{ id: "same", name: "B", transport: { type: "socket", path: "/b" } },
]),
),
).toThrow("ids must be unique");
expect(() =>
parseDockerEndpointDescriptors(
JSON.stringify([
{
id: "limited",
name: "Limited",
transport: { type: "socket", path: "/run/docker.sock" },
capabilities: ["logs"],
},
]),
),
).toThrow("must include inventory");
});
});
@@ -6,6 +6,7 @@ const mockEnv = vi.hoisted(() => ({
DOCKER_SOCKET_PATHS: undefined as string | undefined,
DOCKER_HOSTNAMES: undefined as string | undefined,
DOCKER_PORTS: undefined as string | undefined,
DOCKER_ENDPOINTS: undefined as string | undefined,
ENABLE_DOCKER: true,
ENABLE_KUBERNETES: false,
}));
@@ -32,6 +33,7 @@ describe("DockerSingleton", () => {
mockEnv.DOCKER_SOCKET_PATHS = undefined;
mockEnv.DOCKER_HOSTNAMES = undefined;
mockEnv.DOCKER_PORTS = undefined;
mockEnv.DOCKER_ENDPOINTS = undefined;
});
test("should return default socket when no env vars set", () => {
@@ -39,6 +41,7 @@ describe("DockerSingleton", () => {
expect(instances).toHaveLength(1);
expect(instances[0]?.host).toBe("socket");
expect(instances[0]).toMatchObject({ endpointId: "socket:default", endpointName: "Local Docker" });
});
test("should return TCP instances only when DOCKER_HOSTNAMES and DOCKER_PORTS set", () => {
@@ -60,6 +63,7 @@ describe("DockerSingleton", () => {
expect(instances).toHaveLength(2);
expect(instances[0]?.host).toBe("host1:2375");
expect(instances[1]?.host).toBe("host2:2376");
expect(instances.map(({ endpointId }) => endpointId)).toEqual(["tcp:host1:2375", "tcp:host2:2376"]);
});
test("should throw when hostname and port counts do not match", () => {
@@ -86,6 +90,7 @@ describe("DockerSingleton", () => {
expect(instances).toHaveLength(2);
expect(instances[0]?.host).toBe("/var/run/docker.sock");
expect(instances[1]?.host).toBe("/run/user/1000/podman/podman.sock");
expect(DockerSingleton.findInstance("socket:/run/user/1000/podman/podman.sock")).toBe(instances[1]);
});
test("should combine socket and TCP instances", () => {
@@ -107,4 +112,23 @@ describe("DockerSingleton", () => {
expect(first).toBe(second);
});
test("uses the structured endpoint descriptor and exposes scoped capabilities", () => {
mockEnv.DOCKER_ENDPOINTS = JSON.stringify([
{
id: "readonly-podman",
name: "Read-only Podman",
kind: "podman",
transport: { type: "socket", path: "/run/podman/podman.sock" },
capabilities: ["inventory", "logs"],
},
]);
const [endpoint] = DockerSingleton.getInstances();
expect(endpoint).toMatchObject({ endpointId: "readonly-podman", endpointName: "Read-only Podman" });
expect(endpoint?.descriptor).toMatchObject({ kind: "podman", source: "environment" });
expect(DockerSingleton.hasCapability("readonly-podman", "logs")).toBe(true);
expect(DockerSingleton.hasCapability("readonly-podman", "remove")).toBe(false);
});
});