@@ -33,6 +33,49 @@ use tracing::{Instrument as _, debug, info, warn};
3333const STOP_COMPLETION_POLL_INTERVAL : Duration = Duration :: from_millis ( 50 ) ;
3434const STOP_COMPLETION_TIMEOUT_HEADROOM : Duration = Duration :: from_secs ( 5 ) ;
3535
36+ #[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
37+ enum PodmanEndpointEnvironment {
38+ LinuxHost ,
39+ PodmanMachine ,
40+ }
41+
42+ impl PodmanEndpointEnvironment {
43+ const fn current ( ) -> Self {
44+ if cfg ! ( target_os = "linux" ) {
45+ Self :: LinuxHost
46+ } else {
47+ Self :: PodmanMachine
48+ }
49+ }
50+
51+ const fn gateway_host ( self ) -> & ' static str {
52+ match self {
53+ Self :: LinuxHost => "127.0.0.1" ,
54+ Self :: PodmanMachine => "host.containers.internal" ,
55+ }
56+ }
57+ }
58+
59+ fn select_grpc_endpoint (
60+ config : & PodmanComputeConfig ,
61+ environment : PodmanEndpointEnvironment ,
62+ ) -> String {
63+ if !config. grpc_endpoint . is_empty ( ) {
64+ return config. grpc_endpoint . clone ( ) ;
65+ }
66+
67+ let scheme = if config. tls_enabled ( ) {
68+ "https"
69+ } else {
70+ "http"
71+ } ;
72+ format ! (
73+ "{scheme}://{}:{}" ,
74+ environment. gateway_host( ) ,
75+ config. gateway_port
76+ )
77+ }
78+
3679fn decode_launch_authentication (
3780 encoded : & [ u8 ] ,
3881) -> Result < openshell_core:: jwt:: SandboxLaunchAuthentication , ComputeDriverError > {
@@ -443,18 +486,9 @@ impl PodmanComputeDriver {
443486 // The supervisor shares the Podman host network. Linux supervisors can
444487 // therefore use gateway loopback directly; Podman Machine retains its
445488 // standard desktop-host alias.
446- if config. grpc_endpoint . is_empty ( ) {
447- let scheme = if config. tls_enabled ( ) {
448- "https"
449- } else {
450- "http"
451- } ;
452- let host = if cfg ! ( target_os = "linux" ) {
453- "127.0.0.1"
454- } else {
455- "host.containers.internal"
456- } ;
457- config. grpc_endpoint = format ! ( "{scheme}://{host}:{}" , config. gateway_port) ;
489+ let endpoint_was_selected = config. grpc_endpoint . is_empty ( ) ;
490+ config. grpc_endpoint = select_grpc_endpoint ( & config, PodmanEndpointEnvironment :: current ( ) ) ;
491+ if endpoint_was_selected {
458492 info ! (
459493 grpc_endpoint = %config. grpc_endpoint,
460494 tls = config. tls_enabled( ) ,
@@ -2375,37 +2409,34 @@ mod tests {
23752409
23762410 // ── grpc_endpoint auto-detection ───────────────────────────────────
23772411 //
2378- // PodmanComputeDriver::new() fills grpc_endpoint when it is empty.
2379- // The scheme (http vs https) depends on whether TLS client certs are
2380- // configured. These tests simulate the auto-detection logic.
2412+ // PodmanComputeDriver::new() fills grpc_endpoint through
2413+ // select_grpc_endpoint() when it is empty.
23812414
23822415 #[ test]
2383- fn grpc_endpoint_http_without_tls ( ) {
2384- let mut cfg = PodmanComputeConfig {
2416+ fn grpc_endpoint_uses_loopback_on_linux ( ) {
2417+ let cfg = PodmanComputeConfig {
23852418 gateway_port : 8081 ,
23862419 ..PodmanComputeConfig :: default ( )
23872420 } ;
2388- if cfg. grpc_endpoint . is_empty ( ) {
2389- let scheme = if cfg. tls_enabled ( ) { "https" } else { "http" } ;
2390- cfg. grpc_endpoint = format ! ( "{scheme}://host.containers.internal:{}" , cfg. gateway_port) ;
2391- }
2392- assert_eq ! ( cfg. grpc_endpoint, "http://host.containers.internal:8081" ) ;
2421+ assert_eq ! (
2422+ select_grpc_endpoint( & cfg, PodmanEndpointEnvironment :: LinuxHost ) ,
2423+ "http://127.0.0.1:8081"
2424+ ) ;
23932425 }
23942426
23952427 #[ test]
2396- fn grpc_endpoint_https_with_tls ( ) {
2397- let mut cfg = PodmanComputeConfig {
2428+ fn grpc_endpoint_uses_host_alias_on_podman_machine ( ) {
2429+ let cfg = PodmanComputeConfig {
23982430 gateway_port : 8080 ,
23992431 guest_tls_ca : Some ( PathBuf :: from ( "/tls/ca.crt" ) ) ,
24002432 guest_tls_cert : Some ( PathBuf :: from ( "/tls/tls.crt" ) ) ,
24012433 guest_tls_key : Some ( PathBuf :: from ( "/tls/tls.key" ) ) ,
24022434 ..PodmanComputeConfig :: default ( )
24032435 } ;
2404- if cfg. grpc_endpoint . is_empty ( ) {
2405- let scheme = if cfg. tls_enabled ( ) { "https" } else { "http" } ;
2406- cfg. grpc_endpoint = format ! ( "{scheme}://host.containers.internal:{}" , cfg. gateway_port) ;
2407- }
2408- assert_eq ! ( cfg. grpc_endpoint, "https://host.containers.internal:8080" ) ;
2436+ assert_eq ! (
2437+ select_grpc_endpoint( & cfg, PodmanEndpointEnvironment :: PodmanMachine ) ,
2438+ "https://host.containers.internal:8080"
2439+ ) ;
24092440 }
24102441
24112442 #[ test]
@@ -2433,16 +2464,15 @@ mod tests {
24332464
24342465 #[ test]
24352466 fn explicit_grpc_endpoint_takes_precedence ( ) {
2436- let mut cfg = PodmanComputeConfig {
2467+ let cfg = PodmanComputeConfig {
24372468 grpc_endpoint : "https://gateway.internal:9000" . to_string ( ) ,
24382469 gateway_port : 8081 ,
24392470 ..PodmanComputeConfig :: default ( )
24402471 } ;
2441- if cfg. grpc_endpoint . is_empty ( ) {
2442- let scheme = if cfg. tls_enabled ( ) { "https" } else { "http" } ;
2443- cfg. grpc_endpoint = format ! ( "{scheme}://host.containers.internal:{}" , cfg. gateway_port) ;
2444- }
2445- assert_eq ! ( cfg. grpc_endpoint, "https://gateway.internal:9000" ) ;
2472+ assert_eq ! (
2473+ select_grpc_endpoint( & cfg, PodmanEndpointEnvironment :: LinuxHost ) ,
2474+ "https://gateway.internal:9000"
2475+ ) ;
24462476 }
24472477
24482478 #[ test]
0 commit comments