@@ -23,6 +23,14 @@ pub use sqlite::SqliteStore;
2323pub const POLICY_OBJECT_TYPE : & str = "sandbox_policy" ;
2424/// Object type string for draft policy chunk records.
2525pub const DRAFT_CHUNK_OBJECT_TYPE : & str = "draft_policy_chunk" ;
26+ /// Object type string for warm-pool `SandboxClaim` → sandbox-id mappings.
27+ ///
28+ /// Stored in the shared `objects` table so any HA gateway replica can serve the
29+ /// `IssueSandboxToken` bootstrap. The record `id` is keyed on
30+ /// `(namespace, claim_name, claim_uid)` (so a lookup miss on a uid mismatch
31+ /// fails closed) and the record `name` is the sandbox-id (so a sandbox can be
32+ /// torn down by id without re-deriving the claim key).
33+ pub const CLAIM_MAPPING_OBJECT_TYPE : & str = "k8s_claim_mapping" ;
2634
2735pub type PersistenceResult < T > = Result < T , PersistenceError > ;
2836
@@ -507,6 +515,130 @@ impl Store {
507515 updated. set_resource_version ( result. resource_version ) ;
508516 Ok ( updated)
509517 }
518+
519+ // -----------------------------------------------------------------------
520+ // Warm-pool claim mappings
521+ // -----------------------------------------------------------------------
522+
523+ /// Persist a durable `(namespace, claim_name, claim_uid) -> sandbox_id`
524+ /// mapping for a gateway-created warm-pool `SandboxClaim`.
525+ ///
526+ /// Idempotent: a re-write of the identical mapping (same key + same
527+ /// sandbox-id) is accepted so create, bootstrap, and reconcile back-fill
528+ /// retries converge. A write that would rebind an existing claim key to a
529+ /// *different* sandbox-id is rejected — that must never happen because the
530+ /// claim UID is apiserver-assigned and unique.
531+ pub async fn put_claim_mapping ( & self , mapping : & ClaimMapping ) -> PersistenceResult < ( ) > {
532+ let id = claim_mapping_id ( & mapping. namespace , & mapping. claim_name , & mapping. claim_uid ) ;
533+
534+ if let Some ( existing) = self . get_claim_mapping_by_id ( & id) . await ? {
535+ return if existing. sandbox_id == mapping. sandbox_id {
536+ Ok ( ( ) )
537+ } else {
538+ Err ( claim_mapping_rebind_error ( & id) )
539+ } ;
540+ }
541+
542+ let payload = serde_json:: to_vec ( mapping)
543+ . map_err ( |e| PersistenceError :: Encode ( format ! ( "claim mapping encode error: {e}" ) ) ) ?;
544+
545+ match self
546+ . put_if (
547+ CLAIM_MAPPING_OBJECT_TYPE ,
548+ & id,
549+ & mapping. sandbox_id ,
550+ & payload,
551+ None ,
552+ WriteCondition :: MustCreate ,
553+ )
554+ . await
555+ {
556+ Ok ( _) => Ok ( ( ) ) ,
557+ // Lost a concurrent MustCreate race: accept iff the winner stored
558+ // the same binding, otherwise surface the conflict.
559+ Err ( PersistenceError :: UniqueViolation { .. } ) => {
560+ match self . get_claim_mapping_by_id ( & id) . await ? {
561+ Some ( existing) if existing. sandbox_id == mapping. sandbox_id => Ok ( ( ) ) ,
562+ _ => Err ( claim_mapping_rebind_error ( & id) ) ,
563+ }
564+ }
565+ Err ( e) => Err ( e) ,
566+ }
567+ }
568+
569+ /// Look up the sandbox-id bound to a warm-pool claim. A miss (including a
570+ /// `claim_uid` that does not match a stored record) returns `Ok(None)` so
571+ /// callers fail closed.
572+ pub async fn get_claim_mapping (
573+ & self ,
574+ namespace : & str ,
575+ claim_name : & str ,
576+ claim_uid : & str ,
577+ ) -> PersistenceResult < Option < ClaimMapping > > {
578+ self . get_claim_mapping_by_id ( & claim_mapping_id ( namespace, claim_name, claim_uid) )
579+ . await
580+ }
581+
582+ async fn get_claim_mapping_by_id ( & self , id : & str ) -> PersistenceResult < Option < ClaimMapping > > {
583+ let Some ( record) = self . get ( CLAIM_MAPPING_OBJECT_TYPE , id) . await ? else {
584+ return Ok ( None ) ;
585+ } ;
586+ let mapping: ClaimMapping = serde_json:: from_slice ( & record. payload )
587+ . map_err ( |e| PersistenceError :: Decode ( format ! ( "claim mapping decode error: {e}" ) ) ) ?;
588+ Ok ( Some ( mapping) )
589+ }
590+
591+ /// Delete a warm-pool claim mapping by its sandbox-id (the record name).
592+ /// Returns `true` when a mapping was removed.
593+ pub async fn delete_claim_mapping_for_sandbox (
594+ & self ,
595+ sandbox_id : & str ,
596+ ) -> PersistenceResult < bool > {
597+ self . delete_by_name ( CLAIM_MAPPING_OBJECT_TYPE , sandbox_id)
598+ . await
599+ }
600+
601+ /// List all warm-pool claim mappings (used by reconcile GC). Bounded by
602+ /// `limit`/`offset`.
603+ pub async fn list_claim_mappings (
604+ & self ,
605+ limit : u32 ,
606+ offset : u32 ,
607+ ) -> PersistenceResult < Vec < ClaimMapping > > {
608+ let records = self . list ( CLAIM_MAPPING_OBJECT_TYPE , limit, offset) . await ?;
609+ let mut mappings = Vec :: with_capacity ( records. len ( ) ) ;
610+ for record in records {
611+ let mapping: ClaimMapping = serde_json:: from_slice ( & record. payload ) . map_err ( |e| {
612+ PersistenceError :: Decode ( format ! ( "claim mapping decode error: {e}" ) )
613+ } ) ?;
614+ mappings. push ( mapping) ;
615+ }
616+ Ok ( mappings)
617+ }
618+ }
619+
620+ /// Durable mapping that binds a gateway-created warm-pool `SandboxClaim` to the
621+ /// sandbox identity the gateway assigned. The auth bootstrap re-anchors a warm
622+ /// sandbox pod's identity to this record (see `auth::k8s_sa`).
623+ #[ derive( Debug , Clone , PartialEq , Eq , serde:: Serialize , serde:: Deserialize ) ]
624+ pub struct ClaimMapping {
625+ pub namespace : String ,
626+ pub claim_name : String ,
627+ pub claim_uid : String ,
628+ pub sandbox_id : String ,
629+ }
630+
631+ fn claim_mapping_id ( namespace : & str , claim_name : & str , claim_uid : & str ) -> String {
632+ format ! ( "{namespace}/{claim_name}/{claim_uid}" )
633+ }
634+
635+ fn claim_mapping_rebind_error ( id : & str ) -> PersistenceError {
636+ PersistenceError :: unique_violation (
637+ Some ( CLAIM_MAPPING_OBJECT_TYPE . to_string ( ) ) ,
638+ Some ( format ! (
639+ "claim mapping {id} is already bound to a different sandbox"
640+ ) ) ,
641+ )
510642}
511643
512644pub fn current_time_ms ( ) -> i64 {
0 commit comments