(
template: &SandboxTemplate,
gpu_requirements: Option<&GpuResourceRequirements>,
)
| 1884 | } |
| 1885 | |
| 1886 | fn container_resources( |
| 1887 | template: &SandboxTemplate, |
| 1888 | gpu_requirements: Option<&GpuResourceRequirements>, |
| 1889 | ) -> Option<serde_json::Value> { |
| 1890 | // Start from the raw resources passthrough in platform_config (preserves |
| 1891 | // custom resource types like GPU limits that users set via the public API |
| 1892 | // Struct), then overlay the typed DriverResourceRequirements on top. |
| 1893 | let mut resources = |
| 1894 | platform_config_struct(template, "resources_raw").unwrap_or_else(|| serde_json::json!({})); |
| 1895 | |
| 1896 | // Overlay typed CPU/memory from DriverResourceRequirements. |
| 1897 | if let Some(ref req) = template.resources { |
| 1898 | let obj = resources.as_object_mut().unwrap(); |
| 1899 | let mut apply = |section: &str, key: &str, value: &str| { |
| 1900 | if !value.is_empty() { |
| 1901 | let sec = obj.entry(section).or_insert_with(|| serde_json::json!({})); |
| 1902 | sec[key] = serde_json::json!(value); |
| 1903 | } |
| 1904 | }; |
| 1905 | apply("limits", "cpu", &req.cpu_limit); |
| 1906 | apply("limits", "memory", &req.memory_limit); |
| 1907 | |
| 1908 | let cpu_request = if req.cpu_request.is_empty() { |
| 1909 | &req.cpu_limit |
| 1910 | } else { |
| 1911 | &req.cpu_request |
| 1912 | }; |
| 1913 | let memory_request = if req.memory_request.is_empty() { |
| 1914 | &req.memory_limit |
| 1915 | } else { |
| 1916 | &req.memory_request |
| 1917 | }; |
| 1918 | apply("requests", "cpu", cpu_request); |
| 1919 | apply("requests", "memory", memory_request); |
| 1920 | } |
| 1921 | |
| 1922 | if let Some(gpu) = gpu_requirements { |
| 1923 | let quantity = gpu.count.unwrap_or(1).to_string(); |
| 1924 | apply_gpu_limit(&mut resources, &quantity); |
| 1925 | } |
| 1926 | if resources.as_object().is_some_and(serde_json::Map::is_empty) { |
| 1927 | None |
| 1928 | } else { |
| 1929 | Some(resources) |
| 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | fn apply_gpu_limit(resources: &mut serde_json::Value, quantity: &str) { |
| 1934 | let Some(resources_obj) = resources.as_object_mut() else { |
no test coverage detected