MCPcopy Create free account
hub / github.com/NVIDIA/OpenShell / resolve_sandbox_identity

Method resolve_sandbox_identity

crates/openshell-driver-kubernetes/src/driver.rs:345–406  ·  view source on GitHub ↗

Resolve sandbox UID/GID from config or `OpenShift` SCC namespace annotations. Returns `(uid, gid, ns_annotations_map)`: - If `sandbox_uid` is set in config, returns that (with fallback GID) - Otherwise fetches the target namespace and checks for `openshift.io/sa.scc.uid-range` / `openshift.io/sa.scc.supplemental-groups` annotations. - If neither config nor `OpenShift` is found, returns `(1000, 10

(&self)

Source from the content-addressed store, hash-verified

343 /// annotations.
344 /// - If neither config nor `OpenShift` is found, returns `(1000, 1000, {})` as defaults.
345 async fn resolve_sandbox_identity(&self) -> (u32, u32, BTreeMap<String, String>) {
346 // Explicit config takes priority — skip namespace lookup entirely.
347 if self.config.sandbox_uid.is_some() {
348 let uid = self.config.resolve_sandbox_uid(None);
349 let gid = self.config.resolve_sandbox_gid(uid, None);
350 return (uid, gid, BTreeMap::new());
351 }
352
353 // Try to read namespace annotations for OpenShift SCC.
354 // Namespace is namespaced so Api::all works (it's cluster-scoped but
355 // can list all namespaces) and we filter by name, or use Api::namespaced.
356 let ns_api: Api<Namespace> = Api::all(self.client.clone());
357 match tokio::time::timeout(KUBE_API_TIMEOUT, ns_api.get(self.config.namespace.as_str()))
358 .await
359 {
360 Ok(Ok(ns)) => {
361 let anns = ns.metadata.annotations.unwrap_or_default();
362 tracing::info!(
363 namespace = %self.config.namespace,
364 uid_range = ?anns.get(crate::config::ANNOTATION_SCC_UID_RANGE),
365 sup_groups = ?anns.get(crate::config::ANNOTATION_SCC_SUPPLEMENTAL_GROUPS),
366 "Resolved namespace annotations for sandbox identity"
367 );
368 let uid = self.config.resolve_sandbox_uid(Some(&anns));
369 // Explicit sandbox_gid config wins; SCC annotation only applies when not set.
370 let baseline_gid = self.config.resolve_sandbox_gid(uid, None);
371 let gid = self.config.sandbox_gid.map_or_else(
372 || {
373 anns.get(crate::config::ANNOTATION_SCC_SUPPLEMENTAL_GROUPS)
374 .and_then(|sup_range| {
375 KubernetesComputeConfig::from_open_shift_supplemental_groups(
376 sup_range,
377 )
378 })
379 .unwrap_or(baseline_gid)
380 },
381 |_| baseline_gid,
382 );
383 tracing::info!(uid, gid, "Resolved sandbox identity");
384 (uid, gid, anns)
385 }
386 Ok(Err(e)) => {
387 tracing::warn!(
388 namespace = %self.config.namespace,
389 error = %e,
390 "Failed to fetch namespace for SCC annotations, falling back to defaults"
391 );
392 let uid = DEFAULT_SANDBOX_UID;
393 let gid = self.config.resolve_sandbox_gid(uid, None);
394 (uid, gid, BTreeMap::new())
395 }
396 Err(_) => {
397 tracing::warn!(
398 namespace = %self.config.namespace,
399 "Namespace fetch timed out, falling back to defaults"
400 );
401 let uid = DEFAULT_SANDBOX_UID;
402 let gid = self.config.resolve_sandbox_gid(uid, None);

Callers 1

create_sandboxMethod · 0.80

Calls 4

resolve_sandbox_uidMethod · 0.45
resolve_sandbox_gidMethod · 0.45
getMethod · 0.45
as_strMethod · 0.45

Tested by

no test coverage detected