Skip to content

Commit 1998a98

Browse files
aledbfroboquat
authored andcommitted
[werft] Improve findFreeHostPorts speed
1 parent 660b593 commit 1998a98

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

.werft/jobs/build/deploy-to-preview-environment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ async function deployToDevWithInstaller(werft: Werft, jobConfig: JobConfig, depl
208208

209209
if (isNaN(wsdaemonPortMeta) || isNaN(wsdaemonPortMeta) || (isNaN(nodeExporterPort) && !withVM && withObservability)) {
210210
werft.log(installerSlices.FIND_FREE_HOST_PORTS, "Can't reuse, check for some free ports.");
211-
[wsdaemonPortMeta, registryNodePortMeta, nodeExporterPort] = findFreeHostPorts([
211+
[wsdaemonPortMeta, registryNodePortMeta, nodeExporterPort] = await findFreeHostPorts([
212212
{ start: 10000, end: 11000 },
213213
{ start: 30000, end: 31000 },
214214
{ start: 31001, end: 32000 },
@@ -374,7 +374,7 @@ async function deployToDevWithHelm(werft: Werft, jobConfig: JobConfig, deploymen
374374
const { version, destname, namespace, domain, monitoringDomain, url } = deploymentConfig;
375375
// find free ports
376376
werft.log("find free ports", "Check for some free ports.");
377-
const [wsdaemonPortMeta, registryNodePortMeta, nodeExporterPort] = findFreeHostPorts([
377+
const [wsdaemonPortMeta, registryNodePortMeta, nodeExporterPort] = await findFreeHostPorts([
378378
{ start: 10000, end: 11000 },
379379
{ start: 30000, end: 31000 },
380380
{ start: 31001, end: 32000 },

.werft/util/kubectl.ts

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { exec, ExecOptions } from './shell';
1+
import { exec, ExecOptions, ExecResult } from './shell';
22
import { sleep } from './util';
33
import { getGlobalWerftInstance } from './werft';
44

@@ -195,32 +195,30 @@ export function findLastHostPort(namespace: string, name: string, kubeconfig: st
195195
return Number.parseInt(portStr)
196196
}
197197

198-
export function findFreeHostPorts(ranges: PortRange[], kubeconfig: string, shellOpts: ExecOptions): number[] {
198+
199+
export async function findFreeHostPorts(ranges: PortRange[], kubeconfig: string, shellOpts: ExecOptions): Promise<number[]> {
199200
const werft = getGlobalWerftInstance()
200-
const hostPorts: number[] = exec(`kubectl --kubeconfig ${kubeconfig} get pods --all-namespaces -o yaml | yq r - 'items.*.spec.containers.*.ports.*.hostPort'`, { ...shellOpts, silent: true, async: false })
201-
.stdout
202-
.split("\n")
203-
.map(l => l.trim())
204-
.filter(l => l.length > 0)
205-
.map(l => Number.parseInt(l));
201+
var hostPorts: Array<number> = [];
202+
var nodePorts: Array<number> = [];
203+
204+
const hostPortsPromise = exec(`kubectl --kubeconfig ${kubeconfig} get pods --all-namespaces -o yaml | yq r - 'items.*.spec.containers.*.ports.*.hostPort | grep -v null | sort | uniq'`, { ...shellOpts, silent: true, async: true }) as Promise<ExecResult>
205+
const nodePortsPromise = exec(`kubectl --kubeconfig ${kubeconfig} get services --all-namespaces -o yaml | yq r - 'items.*.spec.ports.*.nodePort | grep -v null | sort | uniq'`, { ...shellOpts, silent: true, async: true }) as Promise<ExecResult>
206+
207+
hostPortsPromise.then(res => hostPorts = res.stdout.split("\n").map(line => line.trim()).map(line => Number.parseInt(line)));
208+
nodePortsPromise.then(res => nodePorts = res.stdout.split("\n").map(line => line.trim()).map(line => Number.parseInt(line)));
209+
210+
await Promise.all([hostPortsPromise, nodePortsPromise]);
211+
212+
const alreadyReservedPorts: Set<number> = new Set([].concat(hostPorts, nodePorts));
206213

207-
const nodePorts: number[] = exec(`kubectl --kubeconfig ${kubeconfig} get services --all-namespaces -o yaml | yq r - 'items.*.spec.ports.*.nodePort'`, { ...shellOpts, silent: true, async: false })
208-
.stdout
209-
.split("\n")
210-
.map(l => l.trim())
211-
.filter(l => l.length > 0)
212-
.map(l => Number.parseInt(l));
213-
const alreadyReservedPorts: Set<number> = new Set();
214-
for (const port of hostPorts) {
215-
alreadyReservedPorts.add(port);
216-
}
217-
for (const port of nodePorts) {
218-
alreadyReservedPorts.add(port);
219-
}
220214
werft.log(shellOpts.slice, `already reserved ports: ${Array.from(alreadyReservedPorts.values()).map(p => "" + p).join(", ")}`);
221215

222216
const results: number[] = [];
223217
for (const range of ranges) {
218+
if (results.length > 4) {
219+
break;
220+
}
221+
224222
const r = range.end - range.start;
225223
while (true) {
226224
const hostPort = range.start + Math.floor(Math.random() * r);
@@ -234,7 +232,8 @@ export function findFreeHostPorts(ranges: PortRange[], kubeconfig: string, shell
234232
break;
235233
}
236234
}
237-
return results;
235+
236+
return new Promise((resolve) => { resolve(results) });
238237
}
239238

240239
export function waitForDeploymentToSucceed(name: string, namespace: string, type: string, kubeconfig: string, shellOpts: ExecOptions) {

0 commit comments

Comments
 (0)