@@ -362,6 +362,40 @@ func (c *Client) createIngressContainer(ctx context.Context, containerName strin
362
362
363
363
}
364
364
365
+ func extractFirstPort (options * runtime.DeployWorkloadOptions ) (int , error ) {
366
+ var firstPort string
367
+ if len (options .ExposedPorts ) == 0 {
368
+ return 0 , fmt .Errorf ("no exposed ports specified in options.ExposedPorts" )
369
+ }
370
+ for port := range options .ExposedPorts {
371
+ firstPort = port
372
+
373
+ // need to strip the protocol
374
+ firstPort = strings .Split (firstPort , "/" )[0 ]
375
+ break // take only the first one
376
+ }
377
+ firstPortInt , err := strconv .Atoi (firstPort )
378
+ if err != nil {
379
+ return 0 , fmt .Errorf ("failed to convert port %s to int: %v" , firstPort , err )
380
+ }
381
+ return firstPortInt , nil
382
+ }
383
+
384
+ func extractFirstPortBinding (portBindings map [string ][]runtime.PortBinding ) (int , error ) {
385
+ var firstHostPort string
386
+ for _ , bindings := range portBindings {
387
+ if len (bindings ) > 0 {
388
+ firstHostPort = bindings [0 ].HostPort
389
+ break // we only want the first item in the map
390
+ }
391
+ }
392
+ hostPortInt , err := strconv .Atoi (firstHostPort )
393
+ if err != nil {
394
+ return 0 , fmt .Errorf ("failed to convert host port %s to int: %v" , firstHostPort , err )
395
+ }
396
+ return hostPortInt , nil
397
+ }
398
+
365
399
// DeployWorkload creates and starts a workload.
366
400
// It configures the workload based on the provided permission profile and transport type.
367
401
// If options is nil, default options will be used.
@@ -465,38 +499,24 @@ func (c *Client) DeployWorkload(
465
499
return containerId , 0 , nil
466
500
}
467
501
468
- firstPortInt , err := extractFirstPort (options )
502
+ // extract the first port binding
503
+ firstHostPort , err := extractFirstPortBinding (options .PortBindings )
469
504
if err != nil {
470
- return "" , 0 , err // extractFirstPort already wraps the error with context.
505
+ return "" , 0 , err
471
506
}
472
-
473
507
if isolateNetwork {
474
- firstPortInt , err = c .createIngressContainer (ctx , name , firstPortInt , attachStdio , externalEndpointsConfig )
508
+ // just extract the first exposed port
509
+ firstPortInt , err := extractFirstPort (options )
510
+ if err != nil {
511
+ return "" , 0 , err // extractFirstPort already wraps the error with context.
512
+ }
513
+ firstHostPort , err = c .createIngressContainer (ctx , name , firstPortInt , attachStdio , externalEndpointsConfig )
475
514
if err != nil {
476
515
return "" , 0 , fmt .Errorf ("failed to create ingress container: %v" , err )
477
516
}
478
517
}
479
518
480
- return containerId , firstPortInt , nil
481
- }
482
-
483
- func extractFirstPort (options * runtime.DeployWorkloadOptions ) (int , error ) {
484
- var firstPort string
485
- if len (options .ExposedPorts ) == 0 {
486
- return 0 , fmt .Errorf ("no exposed ports specified in options.ExposedPorts" )
487
- }
488
- for port := range options .ExposedPorts {
489
- firstPort = port
490
-
491
- // need to strip the protocol
492
- firstPort = strings .Split (firstPort , "/" )[0 ]
493
- break // take only the first one
494
- }
495
- firstPortInt , err := strconv .Atoi (firstPort )
496
- if err != nil {
497
- return 0 , fmt .Errorf ("failed to convert port %s to int: %v" , firstPort , err )
498
- }
499
- return firstPortInt , nil
519
+ return containerId , firstHostPort , nil
500
520
}
501
521
502
522
// ListWorkloads lists workloads
0 commit comments