@mesh-tech/mesh-cli
Version:
CLI for Mesh platform development utilities
28 lines (27 loc) • 1.15 kB
JavaScript
export function clusterHostPattern(serviceName) {
const name = serviceName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
return new RegExp(`https?://${name}\\.[a-z0-9-]+\\.svc\\.cluster\\.local(?::\\d+)?`, "g");
}
export function rewriteClusterHostsToLocal(devOutput) {
const localPorts = new Map();
for (const [name, service] of Object.entries(devOutput.services ?? {})) {
if (!service.command || service.command.length === 0)
continue;
if (typeof service.port !== "number" || service.port <= 0)
continue;
localPorts.set(name, service.port);
}
if (localPorts.size === 0)
return;
for (const service of Object.values(devOutput.services ?? {})) {
if (!service.env)
continue;
for (const envVar of Object.values(service.env)) {
if (typeof envVar.value !== "string" || !envVar.value.includes(".svc.cluster.local"))
continue;
for (const [name, port] of localPorts) {
envVar.value = envVar.value.replace(clusterHostPattern(name), `http://localhost:${port}`);
}
}
}
}