1- import { exec , ExecOptions } from './shell' ;
1+ import { exec , ExecOptions , ExecResult } from './shell' ;
22import { sleep } from './util' ;
33import { 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
240239export function waitForDeploymentToSucceed ( name : string , namespace : string , type : string , kubeconfig : string , shellOpts : ExecOptions ) {
0 commit comments