@@ -12,9 +12,11 @@ use crate::naming::{
1212 AgentIdentity , ANNOTATION_CREATE_INTENT , ANNOTATION_IMAGE , ANNOTATION_PUBKEY_FULL ,
1313} ;
1414use k8s_openapi:: api:: core:: v1:: {
15- Capabilities , Container , EmptyDirVolumeSource , EnvFromSource , EnvVar , EnvVarSource ,
16- ObjectFieldSelector , Pod , PodSecurityContext , PodSpec , ResourceRequirements , SeccompProfile ,
17- Secret , SecretEnvSource , SecurityContext , Volume , VolumeMount ,
15+ Capabilities , ConfigMapProjection , Container , DownwardAPIProjection , DownwardAPIVolumeFile ,
16+ EmptyDirVolumeSource , EnvFromSource , EnvVar , EnvVarSource , KeyToPath , ObjectFieldSelector , Pod ,
17+ PodSecurityContext , PodSpec , ProjectedVolumeSource , ResourceRequirements , SeccompProfile ,
18+ Secret , SecretEnvSource , SecurityContext , ServiceAccountTokenProjection , Volume , VolumeMount ,
19+ VolumeProjection ,
1820} ;
1921use k8s_openapi:: apimachinery:: pkg:: api:: resource:: Quantity ;
2022use k8s_openapi:: apimachinery:: pkg:: apis:: meta:: v1:: ObjectMeta ;
@@ -26,6 +28,61 @@ const WORKSPACE_VOLUME: &str = "workspace";
2628/// The container name. Fixed: log and exec tooling addresses it by name.
2729const CONTAINER_NAME : & str = "agent" ;
2830
31+ /// Where an orchestrating agent finds its sub-agent credentials (§Sub-agents):
32+ /// `token`, `ca.crt`, and `namespace`. Deliberately not the default
33+ /// ServiceAccount path, so tools do not pick the token up implicitly.
34+ pub const SUBAGENT_TOKEN_PATH : & str = "/var/run/secrets/buzz-subagents" ;
35+ /// Lifetime of the projected token; the kubelet rotates it before expiry.
36+ pub const SUBAGENT_TOKEN_SECONDS : i64 = 600 ;
37+ const SUBAGENT_TOKEN_VOLUME : & str = "subagent-token" ;
38+
39+ /// Short-lived API credentials for claiming sub-agent Sandboxes. RBAC in the
40+ /// developer namespace limits the token to SandboxClaims there.
41+ fn subagent_token_volume ( ) -> Volume {
42+ Volume {
43+ name : SUBAGENT_TOKEN_VOLUME . to_string ( ) ,
44+ projected : Some ( ProjectedVolumeSource {
45+ sources : Some ( vec ! [
46+ VolumeProjection {
47+ service_account_token: Some ( ServiceAccountTokenProjection {
48+ path: "token" . to_string( ) ,
49+ expiration_seconds: Some ( SUBAGENT_TOKEN_SECONDS ) ,
50+ ..Default :: default ( )
51+ } ) ,
52+ ..Default :: default ( )
53+ } ,
54+ VolumeProjection {
55+ config_map: Some ( ConfigMapProjection {
56+ name: "kube-root-ca.crt" . to_string( ) ,
57+ items: Some ( vec![ KeyToPath {
58+ key: "ca.crt" . to_string( ) ,
59+ path: "ca.crt" . to_string( ) ,
60+ ..Default :: default ( )
61+ } ] ) ,
62+ ..Default :: default ( )
63+ } ) ,
64+ ..Default :: default ( )
65+ } ,
66+ VolumeProjection {
67+ downward_api: Some ( DownwardAPIProjection {
68+ items: Some ( vec![ DownwardAPIVolumeFile {
69+ path: "namespace" . to_string( ) ,
70+ field_ref: Some ( ObjectFieldSelector {
71+ field_path: "metadata.namespace" . to_string( ) ,
72+ ..Default :: default ( )
73+ } ) ,
74+ ..Default :: default ( )
75+ } ] ) ,
76+ } ) ,
77+ ..Default :: default ( )
78+ } ,
79+ ] ) ,
80+ ..Default :: default ( )
81+ } ) ,
82+ ..Default :: default ( )
83+ }
84+ }
85+
2986/// Build the per-attempt Secret holding the resolved environment.
3087///
3188/// `immutable: true` — the Secret is written once per attempt and never
@@ -144,11 +201,22 @@ pub fn build_pod(
144201 limits : Some ( limits) ,
145202 ..Default :: default ( )
146203 } ) ,
147- volume_mounts : Some ( vec ! [ VolumeMount {
148- name: WORKSPACE_VOLUME . to_string( ) ,
149- mount_path: WORKSPACE_PATH . to_string( ) ,
150- ..Default :: default ( )
151- } ] ) ,
204+ volume_mounts : Some ( {
205+ let mut mounts = vec ! [ VolumeMount {
206+ name: WORKSPACE_VOLUME . to_string( ) ,
207+ mount_path: WORKSPACE_PATH . to_string( ) ,
208+ ..Default :: default ( )
209+ } ] ;
210+ if cfg. pod_options . enable_subagents {
211+ mounts. push ( VolumeMount {
212+ name : SUBAGENT_TOKEN_VOLUME . to_string ( ) ,
213+ mount_path : SUBAGENT_TOKEN_PATH . to_string ( ) ,
214+ read_only : Some ( true ) ,
215+ ..Default :: default ( )
216+ } ) ;
217+ }
218+ mounts
219+ } ) ,
152220 security_context : Some ( SecurityContext {
153221 allow_privilege_escalation : Some ( false ) ,
154222 capabilities : Some ( Capabilities {
@@ -177,6 +245,7 @@ pub fn build_pod(
177245 tolerations : ( !cfg. pod_options . tolerations . is_empty ( ) )
178246 . then ( || cfg. pod_options . tolerations . iter ( ) . map ( Into :: into) . collect ( ) ) ,
179247 active_deadline_seconds : cfg. pod_options . active_deadline_seconds ,
248+ priority_class_name : cfg. pod_options . priority_class_name . clone ( ) ,
180249 restart_policy : Some ( RESTART_POLICY . to_string ( ) ) ,
181250 termination_grace_period_seconds : Some ( TERMINATION_GRACE_SECONDS ) ,
182251 // The agent runs prompted, untrusted code while holding an nsec;
@@ -197,14 +266,20 @@ pub fn build_pod(
197266 } ) ,
198267 ..Default :: default ( )
199268 } ) ,
200- volumes : Some ( vec ! [ Volume {
201- name: WORKSPACE_VOLUME . to_string( ) ,
202- empty_dir: Some ( EmptyDirVolumeSource {
203- size_limit: cfg. pod_options. workspace_size_limit. clone( ) . map( Quantity ) ,
269+ volumes : Some ( {
270+ let mut volumes = vec ! [ Volume {
271+ name: WORKSPACE_VOLUME . to_string( ) ,
272+ empty_dir: Some ( EmptyDirVolumeSource {
273+ size_limit: cfg. pod_options. workspace_size_limit. clone( ) . map( Quantity ) ,
274+ ..Default :: default ( )
275+ } ) ,
204276 ..Default :: default ( )
205- } ) ,
206- ..Default :: default ( )
207- } ] ) ,
277+ } ] ;
278+ if cfg. pod_options . enable_subagents {
279+ volumes. push ( subagent_token_volume ( ) ) ;
280+ }
281+ volumes
282+ } ) ,
208283 ..Default :: default ( )
209284 } ) ,
210285 ..Default :: default ( )
@@ -376,6 +451,42 @@ mod tests {
376451 ) ;
377452 }
378453
454+ #[ test]
455+ fn subagent_token_is_opt_in_short_lived_and_off_the_default_path ( ) {
456+ let mut cfg = provider_config ( ) ;
457+ let fp = intent_template ( & cfg, [ "A" . into ( ) ] ) . fingerprint ( ) ;
458+ let plain = build_pod ( & identity ( ) , & cfg, "g" , & fp) ;
459+ assert_eq ! ( spec( & plain) . volumes. as_ref( ) . unwrap( ) . len( ) , 1 ) ;
460+ assert_eq ! (
461+ spec( & plain) . containers[ 0 ]
462+ . volume_mounts
463+ . as_ref( )
464+ . unwrap( )
465+ . len( ) ,
466+ 1
467+ ) ;
468+
469+ cfg. pod_options . enable_subagents = true ;
470+ let changed = intent_template ( & cfg, [ "A" . into ( ) ] ) . fingerprint ( ) ;
471+ let pod = build_pod ( & identity ( ) , & cfg, "g" , & changed) ;
472+ let spec = spec ( & pod) ;
473+ // Still no ambient token at the default ServiceAccount path.
474+ assert_eq ! ( spec. automount_service_account_token, Some ( false ) ) ;
475+ let volume = & spec. volumes . as_ref ( ) . unwrap ( ) [ 1 ] ;
476+ let sources = volume. projected . as_ref ( ) . unwrap ( ) . sources . as_ref ( ) . unwrap ( ) ;
477+ let token = sources[ 0 ] . service_account_token . as_ref ( ) . unwrap ( ) ;
478+ assert_eq ! ( token. expiration_seconds, Some ( SUBAGENT_TOKEN_SECONDS ) ) ;
479+ assert ! ( token. audience. is_none( ) ) ;
480+ let mount = & spec. containers [ 0 ] . volume_mounts . as_ref ( ) . unwrap ( ) [ 1 ] ;
481+ assert_eq ! ( mount. name, volume. name) ;
482+ assert_eq ! ( mount. mount_path, SUBAGENT_TOKEN_PATH ) ;
483+ assert_eq ! ( mount. read_only, Some ( true ) ) ;
484+ assert_ne ! (
485+ SUBAGENT_TOKEN_PATH ,
486+ "/var/run/secrets/kubernetes.io/serviceaccount"
487+ ) ;
488+ }
489+
379490 #[ test]
380491 fn each_optional_pod_control_changes_intent ( ) {
381492 let cfg = provider_config ( ) ;
@@ -388,6 +499,8 @@ mod tests {
388499 serde_json:: json!( { "ephemeral_storage_limit" : "4Gi" } ) ,
389500 serde_json:: json!( { "active_deadline_seconds" : 28800 } ) ,
390501 serde_json:: json!( { "setup_pending" : true } ) ,
502+ serde_json:: json!( { "enable_subagents" : true } ) ,
503+ serde_json:: json!( { "priority_class_name" : "agent-session" } ) ,
391504 ] {
392505 let mut changed = cfg. clone ( ) ;
393506 changed. pod_options = serde_json:: from_value ( value) . unwrap ( ) ;
@@ -400,6 +513,23 @@ mod tests {
400513 assert ! ( original. get( "pod_options" ) . is_none( ) ) ;
401514 }
402515
516+ #[ test]
517+ fn priority_class_reaches_the_pod_without_changing_default_intent ( ) {
518+ let mut cfg = provider_config ( ) ;
519+ assert ! ( serde_json:: to_value( & cfg. pod_options)
520+ . unwrap( )
521+ . get( "priority_class_name" )
522+ . is_none( ) ) ;
523+ cfg. pod_options . priority_class_name = Some ( "agent-session" . into ( ) ) ;
524+ let identity = identity ( ) ;
525+ let fingerprint = intent_template ( & cfg, [ "A" . into ( ) ] ) . fingerprint ( ) ;
526+ let pod = build_pod ( & identity, & cfg, "generation" , & fingerprint) ;
527+ assert_eq ! (
528+ pod. spec. unwrap( ) . priority_class_name. as_deref( ) ,
529+ Some ( "agent-session" )
530+ ) ;
531+ }
532+
403533 #[ test]
404534 fn optional_placement_storage_and_lifetime_reach_pod_and_intent ( ) {
405535 let mut cfg = provider_config ( ) ;
0 commit comments