summaryrefslogtreecommitdiffstats
path: root/src/sandbox
diff options
context:
space:
mode:
Diffstat (limited to 'src/sandbox')
-rw-r--r--src/sandbox/kata.rs117
1 files changed, 109 insertions, 8 deletions
diff --git a/src/sandbox/kata.rs b/src/sandbox/kata.rs
index 26e4ba3..66f7cba 100644
--- a/src/sandbox/kata.rs
+++ b/src/sandbox/kata.rs
@@ -36,7 +36,7 @@ pub trait Provisioner: Send + Sync {
/// TTL even if the hub dies. The token rides in args for v1; a production
/// deployment should mount it from a Secret, and this comment is the honest
/// place that says so.
-pub fn sandbox_cr(req: &ProvisionRequest) -> serde_json::Value {
+pub(crate) fn sandbox_cr(req: &ProvisionRequest) -> serde_json::Value {
serde_json::json!({
"apiVersion": "agents.x-k8s.io/v1alpha1",
"kind": "Sandbox",
@@ -71,7 +71,7 @@ pub fn sandbox_cr(req: &ProvisionRequest) -> serde_json::Value {
}
impl K8sSandboxProvisioner {
- pub fn for_config(cfg: &Config) -> Option<Self> {
+ pub(crate) fn for_config(cfg: &Config) -> Option<Self> {
match &cfg.sandbox {
crate::config::Sandbox::AgentSandbox { api, namespace, bearer, .. } => Some(Self {
api: api.clone(),
@@ -174,7 +174,7 @@ pub struct KataBackend {
}
impl KataBackend {
- pub fn shared_for(cfg: &Config) -> Result<Arc<KataBackend>> {
+ pub(crate) fn shared_for(cfg: &Config) -> Result<Arc<KataBackend>> {
let crate::config::Sandbox::AgentSandbox { api, namespace, bearer, image, listen, token, .. } =
&cfg.sandbox
else {
@@ -199,7 +199,7 @@ impl KataBackend {
Ok(backend)
}
- pub fn provision_request(&self, spec: &SandboxSpec, id: &str) -> ProvisionRequest {
+ pub(crate) fn provision_request(&self, spec: &SandboxSpec, id: &str) -> ProvisionRequest {
ProvisionRequest {
sandbox_id: id.to_string(),
profile: spec.profile.clone(),
@@ -238,10 +238,15 @@ impl KataBackend {
})
}
- pub fn reclaim(&self, sandbox_id: &str) -> Result<()> {
- self.provisioner.reclaim(sandbox_id)
- }
-
+ /// NOTE: `KataBackend::reclaim` used to sit here and called nothing but
+ /// `self.provisioner.reclaim`, which the pool already calls itself at the
+ /// reaping site. Nothing — not a test — ever called the wrapper, so it is
+ /// gone rather than gated. The census had credited it to `tests/kata.rs`,
+ /// where the TTL test implements the *trait's* `reclaim` on a null
+ /// provisioner: same word, different function.
+ /// Test-tier in practice — `tests/remote_agent.rs` drives a real agent
+ /// through it — but it is a plain accessor on a public type, so it is
+ /// left public rather than gated behind a feature.
pub fn local_addr(&self) -> &str {
&self.addr
}
@@ -346,4 +351,100 @@ mod tests {
assert!(args.windows(2).any(|w| w[0] == "--hub" && w[1] == "10.0.0.5:9443"));
assert!(args.windows(2).any(|w| w[0] == "--token" && w[1] == "t0ken"));
}
+
+ // ------------------------------------------------------------------
+ // Moved verbatim from tests/kata.rs. Neither test builds a fixture or
+ // touches a database: one asserts the shape of the container record a
+ // lease provisions, the other that a sandbox which never dials in expires
+ // by TTL rather than hanging. Both are about this module's own machinery,
+ // which is what let `KataBackend`, `sandbox_cr`, `provision_request`,
+ // `shared_for` and `for_config` stop being part of the crate's interface.
+ //
+ // `Provisioner` and `ProvisionRequest` stayed public, and not by
+ // oversight: a `pub` implementor cannot implement a `pub(crate)` trait,
+ // and a `pub` trait method cannot take a `pub(crate)` parameter. Narrowing
+ // them would force `LocalAgentProvisioner` private, and the two tests that
+ // construct it drive a real sandbox agent through a fixture.
+ // ------------------------------------------------------------------
+
+ fn agent_sandbox_config() -> Config {
+ let tmp = tempfile::tempdir().unwrap();
+ std::fs::write(
+ tmp.path().join("config.json"),
+ r#"{
+ "sandbox": {
+ "kind": "agent-sandbox",
+ "api": "https://k8s.test:6443",
+ "namespace": "builds",
+ "bearer": "kube-bearer",
+ "image": "aur-build-sandbox:test",
+ "listen": "127.0.0.1:0",
+ "token": "kata-cfg-token",
+ "shared_workspace": true
+ }
+}"#,
+ )
+ .unwrap();
+ let cfg = Config::load(tmp.path()).unwrap();
+ cfg.validate().expect("the config must survive the startup guard");
+ cfg
+ }
+
+ #[test]
+ fn an_agent_sandbox_config_provisions_a_kata_vm_per_lease() {
+ let cfg = agent_sandbox_config();
+ let backend = KataBackend::shared_for(&cfg).unwrap();
+ let spec = SandboxSpec::new("extra-x86_64", "build-9", Duration::from_secs(77));
+ let cr = sandbox_cr(&backend.provision_request(&spec, "sbx-nonce-9"));
+
+ assert_eq!(cr["spec"]["runtimeClass"], "kata", "the VM boundary is the point");
+ assert_eq!(
+ cr["spec"]["activeDeadlineSeconds"], 77,
+ "the orchestrator enforces the lease TTL even if the hub dies"
+ );
+ assert_eq!(
+ cr["metadata"]["name"], "sbx-nonce-9",
+ "the nonce names the box, so a stray sandbox cannot be leased to the wrong build"
+ );
+ assert_eq!(cr["spec"]["template"]["spec"]["containers"][0]["image"], "aur-build-sandbox:test");
+ let args: Vec<String> = cr["spec"]["template"]["spec"]["containers"][0]["args"]
+ .as_array()
+ .unwrap()
+ .iter()
+ .map(|v| v.as_str().unwrap().to_string())
+ .collect();
+ assert!(args.windows(2).any(|w| w[0] == "--agent-id" && w[1] == "sbx-nonce-9"));
+ assert!(args.windows(2).any(|w| w[0] == "--ttl" && w[1] == "77"));
+
+ let prov = K8sSandboxProvisioner::for_config(&cfg).unwrap();
+ assert_eq!(prov.api, "https://k8s.test:6443");
+ assert_eq!(prov.namespace, "builds");
+ assert_eq!(prov.bearer, "kube-bearer");
+ }
+
+ #[test]
+ fn a_sandbox_that_never_dials_in_is_a_ttl_expiry_not_a_hang() {
+ struct NullProvisioner;
+ impl Provisioner for NullProvisioner {
+ fn provision(&self, _req: &ProvisionRequest) -> Result<()> {
+ Ok(())
+ }
+ fn reclaim(&self, _id: &str) -> Result<()> {
+ Ok(())
+ }
+ fn name(&self) -> &str {
+ "null"
+ }
+ }
+ let cfg = Config::default();
+ let backend =
+ KataBackend::bind(&cfg, "127.0.0.1:0", "tok", Arc::new(NullProvisioner)).unwrap();
+ let started = std::time::Instant::now();
+ let err = match backend.acquire(&SandboxSpec::new("extra-x86_64", "ghost", Duration::from_secs(1))) {
+ Ok(_) => panic!("nobody provisioned anything; the lease must not succeed"),
+ Err(e) => e,
+ };
+ assert!(started.elapsed() >= Duration::from_millis(900), "acquire gave up before the TTL");
+ assert!(err.to_string().contains("never dialed in"), "{err}");
+ }
}