11// SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22// SPDX-License-Identifier: Apache-2.0
33
4- //! Shared image-name resolution for community sandbox images .
4+ //! Default sandbox image .
55//!
6- //! Both the CLI and TUI need to expand bare sandbox names (e.g. `"base"`) into
7- //! fully-qualified container image references. This module centralises that
8- //! logic so every client resolves names identically.
9-
10- /// Default registry prefix for community sandbox images.
11- ///
12- /// Bare sandbox names are expanded to `{prefix}/{name}:latest`.
13- /// Override at runtime with the `OPENSHELL_COMMUNITY_REGISTRY` env var.
14- pub const DEFAULT_COMMUNITY_REGISTRY : & str = "ghcr.io/nvidia/openshell-community/sandboxes" ;
6+ //! Provides the fallback image used by all compute drivers when a sandbox spec
7+ //! does not specify one. User-supplied `--from` values are explicit OCI image
8+ //! references passed through unchanged by the CLI and TUI.
159
1610/// Default sandbox base image reference.
1711///
@@ -27,104 +21,3 @@ pub const DEFAULT_SANDBOX_BASE_IMAGE: &str = "docker.io/library/alpine:3.22";
2721pub fn default_sandbox_image ( ) -> String {
2822 DEFAULT_SANDBOX_BASE_IMAGE . to_string ( )
2923}
30-
31- /// Resolve a user-supplied image string into a fully-qualified reference.
32- ///
33- /// Resolution rules (applied in order):
34- /// 1. If the value contains `/`, `:`, or `.` it is treated as a complete image
35- /// reference and returned as-is.
36- /// 2. Otherwise it is treated as a community sandbox name and expanded to
37- /// `{registry}/{value}:latest` where `{registry}` defaults to
38- /// [`DEFAULT_COMMUNITY_REGISTRY`] but can be overridden via the
39- /// `OPENSHELL_COMMUNITY_REGISTRY` environment variable.
40- ///
41- /// This function only handles image-name resolution. Dockerfile detection is
42- /// the responsibility of the caller (e.g. the CLI's `resolve_from()`).
43- pub fn resolve_community_image ( value : & str ) -> String {
44- // Already a fully-qualified reference.
45- if value. contains ( '/' ) || value. contains ( ':' ) || value. contains ( '.' ) {
46- return value. to_string ( ) ;
47- }
48-
49- // Community sandbox shorthand → expand with registry prefix.
50- let prefix = std:: env:: var ( "OPENSHELL_COMMUNITY_REGISTRY" )
51- . unwrap_or_else ( |_| DEFAULT_COMMUNITY_REGISTRY . to_string ( ) ) ;
52- let prefix = prefix. trim_end_matches ( '/' ) ;
53- format ! ( "{prefix}/{value}:latest" )
54- }
55-
56- #[ cfg( test) ]
57- #[ allow( unsafe_code) ]
58- mod tests {
59- use super :: * ;
60- use std:: sync:: { Mutex , OnceLock } ;
61-
62- fn env_lock ( ) -> & ' static Mutex < ( ) > {
63- static ENV_LOCK : OnceLock < Mutex < ( ) > > = OnceLock :: new ( ) ;
64- ENV_LOCK . get_or_init ( || Mutex :: new ( ( ) ) )
65- }
66-
67- #[ test]
68- fn bare_name_expands_to_community_registry ( ) {
69- let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
70- let result = resolve_community_image ( "base" ) ;
71- assert_eq ! (
72- result,
73- "ghcr.io/nvidia/openshell-community/sandboxes/base:latest"
74- ) ;
75- }
76-
77- #[ test]
78- fn bare_name_with_env_override ( ) {
79- let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
80- // Use a temp env override. Safety: test-only, and these env-var tests
81- // are not run concurrently with other tests reading the same var.
82- let key = "OPENSHELL_COMMUNITY_REGISTRY" ;
83- let prev = std:: env:: var ( key) . ok ( ) ;
84- // SAFETY: single-threaded test context; no other thread reads this var.
85- unsafe { std:: env:: set_var ( key, "my-registry.example.com/sandboxes" ) } ;
86- let result = resolve_community_image ( "python" ) ;
87- assert_eq ! ( result, "my-registry.example.com/sandboxes/python:latest" ) ;
88- // Restore.
89- match prev {
90- Some ( v) => unsafe { std:: env:: set_var ( key, v) } ,
91- None => unsafe { std:: env:: remove_var ( key) } ,
92- }
93- }
94-
95- #[ test]
96- fn full_reference_with_slash_passes_through ( ) {
97- let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
98- let input = "ghcr.io/myorg/myimage:v1" ;
99- assert_eq ! ( resolve_community_image( input) , input) ;
100- }
101-
102- #[ test]
103- fn reference_with_colon_passes_through ( ) {
104- let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
105- let input = "myimage:latest" ;
106- assert_eq ! ( resolve_community_image( input) , input) ;
107- }
108-
109- #[ test]
110- fn reference_with_dot_passes_through ( ) {
111- let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
112- let input = "registry.example.com" ;
113- assert_eq ! ( resolve_community_image( input) , input) ;
114- }
115-
116- #[ test]
117- fn trailing_slash_in_env_is_trimmed ( ) {
118- let _guard = env_lock ( ) . lock ( ) . unwrap ( ) ;
119- let key = "OPENSHELL_COMMUNITY_REGISTRY" ;
120- let prev = std:: env:: var ( key) . ok ( ) ;
121- // SAFETY: single-threaded test context; no other thread reads this var.
122- unsafe { std:: env:: set_var ( key, "my-registry.example.com/sandboxes/" ) } ;
123- let result = resolve_community_image ( "base" ) ;
124- assert_eq ! ( result, "my-registry.example.com/sandboxes/base:latest" ) ;
125- match prev {
126- Some ( v) => unsafe { std:: env:: set_var ( key, v) } ,
127- None => unsafe { std:: env:: remove_var ( key) } ,
128- }
129- }
130- }
0 commit comments