1
- import { exec , ExecOptions } from './shell' ;
1
+ import { exec , ExecOptions , ExecResult } from './shell' ;
2
2
import { sleep } from './util' ;
3
3
import { getGlobalWerftInstance } from './werft' ;
4
4
@@ -195,32 +195,30 @@ export function findLastHostPort(namespace: string, name: string, kubeconfig: st
195
195
return Number . parseInt ( portStr )
196
196
}
197
197
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 [ ] > {
199
200
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 ) ) ;
206
213
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
- }
220
214
werft . log ( shellOpts . slice , `already reserved ports: ${ Array . from ( alreadyReservedPorts . values ( ) ) . map ( p => "" + p ) . join ( ", " ) } ` ) ;
221
215
222
216
const results : number [ ] = [ ] ;
223
217
for ( const range of ranges ) {
218
+ if ( results . length > 4 ) {
219
+ break ;
220
+ }
221
+
224
222
const r = range . end - range . start ;
225
223
while ( true ) {
226
224
const hostPort = range . start + Math . floor ( Math . random ( ) * r ) ;
@@ -234,7 +232,8 @@ export function findFreeHostPorts(ranges: PortRange[], kubeconfig: string, shell
234
232
break ;
235
233
}
236
234
}
237
- return results ;
235
+
236
+ return new Promise ( ( resolve ) => { resolve ( results ) } ) ;
238
237
}
239
238
240
239
export function waitForDeploymentToSucceed ( name : string , namespace : string , type : string , kubeconfig : string , shellOpts : ExecOptions ) {
0 commit comments