Skip to content

Commit

Permalink
remove support for port ranges (#521)
Browse files Browse the repository at this point in the history
Signed-off-by: Philippe Martin <[email protected]>
  • Loading branch information
feloy authored Mar 13, 2024
1 parent 6678068 commit 9803d1e
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 70 deletions.
1 change: 0 additions & 1 deletion packages/backend/src/managers/applicationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,6 @@ export class ApplicationManager extends Publisher<ApplicationState[]> {
}

const portmappings: PodCreatePortOptions[] = [];
// N.B: it may not work with ranges
// we expose all ports so we can check the model service if it is actually running
for (const image of images) {
for (const exposed of image.ports) {
Expand Down
72 changes: 3 additions & 69 deletions packages/backend/src/utils/ports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,6 @@ export async function getFreePort(port = 0): Promise<number> {
return port;
}

/**
* Find a free port range
*/
export async function getFreePortRange(rangeSize: number): Promise<string> {
let port = 9000;
let startPort = port;

do {
if (await isFreePort(port)) {
++port;
} else {
++port;
startPort = port;
}
} while (port + 1 - startPort <= rangeSize);

return `${startPort}-${port - 1}`;
}

function isFreeAddressPort(address: string, port: number): Promise<boolean> {
const server = net.createServer();
return new Promise((resolve, reject) =>
Expand All @@ -70,37 +51,11 @@ export async function isFreePort(port: number): Promise<boolean> {
}

export async function getPortsInfo(portDescriptor: string): Promise<string | undefined> {
// check if portDescriptor is a range of ports
if (portDescriptor.includes('-')) {
return await getPortRange(portDescriptor);
} else {
const localPort = await getPort(portDescriptor);
if (!localPort) {
return undefined;
}
return `${localPort}`;
}
}

/**
* return a range of the same length as portDescriptor containing free ports
* undefined if the portDescriptor range is not valid
* e.g 5000:5001 -> 9000:9001
*/
async function getPortRange(portDescriptor: string): Promise<string | undefined> {
const rangeValues = getStartEndRange(portDescriptor);
if (!rangeValues) {
return Promise.resolve(undefined);
}

const rangeSize = rangeValues.endRange + 1 - rangeValues.startRange;
try {
// if free port range fails, return undefined
return await getFreePortRange(rangeSize);
} catch (e) {
console.error(e);
const localPort = await getPort(portDescriptor);
if (!localPort) {
return undefined;
}
return `${localPort}`;
}

async function getPort(portDescriptor: string): Promise<number | undefined> {
Expand All @@ -122,24 +77,3 @@ async function getPort(portDescriptor: string): Promise<number | undefined> {
return undefined;
}
}

function getStartEndRange(range: string) {
if (range.endsWith('/tcp') || range.endsWith('/udp')) {
range = range.substring(0, range.length - 4);
}

const rangeValues = range.split('-');
if (rangeValues.length !== 2) {
return undefined;
}
const startRange = parseInt(rangeValues[0]);
const endRange = parseInt(rangeValues[1]);

if (isNaN(startRange) || isNaN(endRange)) {
return undefined;
}
return {
startRange,
endRange,
};
}

0 comments on commit 9803d1e

Please sign in to comment.