diff --git a/crates/kiri/src/daemon/api.rs b/crates/kiri/src/daemon/api.rs index 03ebb0e5..234f018d 100644 --- a/crates/kiri/src/daemon/api.rs +++ b/crates/kiri/src/daemon/api.rs @@ -461,8 +461,42 @@ pub(super) fn reference_matches(pattern: &str, name: &str) -> bool { } } +pub(super) fn image_config_json(engine: &Engine, digest: &str) -> Value { + engine.store.read_config(digest).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or(Value::Null) +} + +pub(super) fn image_labels_from(cfg: &Value) -> Value { + match &cfg["config"]["Labels"] { + Value::Object(m) if !m.is_empty() => Value::Object(m.clone()), + _ => Value::Null, + } +} + +pub(super) fn image_labels(engine: &Engine, i: &kiri_image::Image) -> Value { + image_labels_from(&image_config_json(engine, &i.config_digest)) +} + +fn merge_labels(image_cfg: &Value, body_labels: &Value) -> Value { + let mut merged = serde_json::Map::new(); + if let Value::Object(img) = &image_cfg["config"]["Labels"] { + for (k, v) in img { + merged.insert(k.clone(), v.clone()); + } + } + if let Value::Object(req) = body_labels { + for (k, v) in req { + merged.insert(k.clone(), v.clone()); + } + } + if merged.is_empty() && !body_labels.is_object() { + Value::Null + } else { + Value::Object(merged) + } +} + fn images(req: &Request, engine: &Engine) -> Result { - let f = filters(req, &["reference", "dangling", "label"])?; + let f = filters(req, &["reference", "dangling", "label", "label!"])?; let list = engine.store.images()?; // Every stored image is tagged, so there are no dangling ones. if f.bool("dangling") == Some(true) { @@ -470,9 +504,15 @@ fn images(req: &Request, engine: &Engine) -> Result { } Ok(Value::Array( list.iter() - .filter(|i| f.any("reference", |p| reference_matches(p, &i.name)) && f.labels(&Value::Null)) - .map(|i| { - json!({ + .filter_map(|i| { + if !f.any("reference", |p| reference_matches(p, &i.name)) { + return None; + } + let labels = image_labels(engine, i); + if !f.labels(&labels) { + return None; + } + Some(json!({ "Id": i.config_digest, "ParentId": "", "RepoTags": [i.name], @@ -481,11 +521,11 @@ fn images(req: &Request, engine: &Engine) -> Result { "Size": i.size, "SharedSize": -1, "VirtualSize": i.size, - "Labels": null, + "Labels": labels, "Containers": -1, // Not Docker's: when this image arrived here, for the window. "Pulled": i.pulled_at, - }) + })) }) .collect(), )) @@ -493,6 +533,7 @@ fn images(req: &Request, engine: &Engine) -> Result { fn image_inspect(engine: &Engine, name: &str) -> Result { let i = engine.image(name)?.ok_or_else(|| Error::NotFound(format!("No such image: {name}")))?; + let cfg = image_config_json(engine, &i.config_digest); Ok(json!({ "Id": i.config_digest, "RepoTags": [i.name], @@ -507,7 +548,8 @@ fn image_inspect(engine: &Engine, name: &str) -> Result { "Entrypoint": if i.config.entrypoint.is_empty() { Value::Null } else { json!(i.config.entrypoint) }, "WorkingDir": i.config.working_dir, "User": i.config.user, - "Healthcheck": image_healthcheck(engine, &i), + "Labels": image_labels_from(&cfg), + "Healthcheck": cfg["config"]["Healthcheck"].clone(), }, "RootFS": {"Type": "layers", "Layers": i.layers.iter().map(|l| &l.digest).collect::>()}, })) @@ -1559,7 +1601,7 @@ pub(super) fn create(req: &Request, engine: &Engine) -> Result { } } // The image's own config, for what Kiri's image model does not keep. - let image_cfg: Value = engine.store.read_config(&image.config_digest).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or(Value::Null); + let image_cfg = image_config_json(engine, &image.config_digest); // Checked before mounts, which may create volumes. let healthcheck = healthcheck_from(&body, &image_cfg)?; let mounts = mounts(&body, engine, &image)?; @@ -1604,9 +1646,7 @@ pub(super) fn create(req: &Request, engine: &Engine) -> Result { readonly_rootfs: host["ReadonlyRootfs"].as_bool().unwrap_or(false), hostname: body["Hostname"].as_str().unwrap_or_default().to_owned(), stop_signal: body["StopSignal"].as_str().filter(|s| !s.is_empty()).map(signal).or_else(|| { - // The image's STOPSIGNAL. - let cfg: Value = serde_json::from_slice(&engine.store.read_config(&image.config_digest).ok()?).ok()?; - cfg["config"]["StopSignal"].as_str().filter(|s| !s.is_empty()).map(signal) + image_cfg["config"]["StopSignal"].as_str().filter(|s| !s.is_empty()).map(signal) }), stop_timeout: body["StopTimeout"].as_i64(), healthcheck, @@ -1623,7 +1663,7 @@ pub(super) fn create(req: &Request, engine: &Engine) -> Result { dns_options: strings(&host["DnsOptions"]).unwrap_or_default(), domainname: body["Domainname"].as_str().unwrap_or_default().to_owned(), links, - labels: body["Labels"].clone(), + labels: merge_labels(&image_cfg, &body["Labels"]), tty: body["Tty"].as_bool().unwrap_or(false), open_stdin: body["OpenStdin"].as_bool().unwrap_or(false), console_size: console_size(&host["ConsoleSize"]), @@ -1773,12 +1813,6 @@ fn healthcheck_from(body: &Value, image_cfg: &Value) -> Result Value { - let cfg: Value = engine.store.read_config(&i.config_digest).ok().and_then(|b| serde_json::from_slice(&b).ok()).unwrap_or(Value::Null); - cfg["config"]["Healthcheck"].clone() -} - /// `docker ps`'s Status: "Up 2 minutes (healthy)" while running unpaused /// with a healthcheck. fn ps_status(c: &Container, health: Option<&super::health::Health>) -> String { @@ -3107,4 +3141,89 @@ mod network_option_tests { assert_eq!(super::pull_hint(&status(500), "docker.io", None), None); assert_eq!(super::pull_hint(&Error::NotFound("no such image".into()), "docker.io", p), None); } + + #[test] + fn image_and_container_labels_follow_docker() { + let dir = std::env::temp_dir().join(format!("kiri-labels-test-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let store = kiri_image::Store::open(&dir).unwrap(); + let put = |name: &str, cfg: Value| { + let bytes = serde_json::to_vec(&cfg).unwrap(); + let mut w = store.ingest().unwrap(); + io::Write::write_all(&mut w, &bytes).unwrap(); + let (digest, _) = w.finish().unwrap(); + let img = kiri_image::archive::from_config(name.into(), &digest, &cfg, Vec::new()); + store.put_image(img).unwrap(); + }; + put( + "demo:labeled", + json!({ + "created": "2026-11-16T00:00:00Z", + "architecture": "arm64", + "os": "linux", + "config": { + "Labels": { + "cn.shopcider.safeguards.revision": "0a5b41cadacdafe94742fc8401d682347a392739", + "keep": "image" + } + } + }), + ); + put( + "demo:plain", + json!({"created": "2026-11-16T00:00:00Z", "architecture": "arm64", "os": "linux", "config": {}}), + ); + + let engine = Engine::boot(store).unwrap(); + let labeled = super::image_inspect(&engine, "demo:labeled").unwrap(); + assert_eq!( + labeled["Config"]["Labels"], + json!({"cn.shopcider.safeguards.revision": "0a5b41cadacdafe94742fc8401d682347a392739", "keep": "image"}) + ); + let plain = super::image_inspect(&engine, "demo:plain").unwrap(); + assert!(plain["Config"].as_object().unwrap().contains_key("Labels")); + assert!(plain["Config"]["Labels"].is_null()); + + let filtered = super::images( + &Request::internal("GET", "/images/json", &[("filters", r#"{"label":["keep=image"]}"#)], Vec::new()), + &engine, + ) + .unwrap(); + let rows = filtered.as_array().unwrap(); + assert_eq!(rows.len(), 1); + assert_eq!(rows[0]["RepoTags"], json!(["demo:labeled"])); + assert_eq!(rows[0]["Labels"]["keep"], "image"); + + let negated = super::images( + &Request::internal("GET", "/images/json", &[("filters", r#"{"label!":["keep=image"]}"#)], Vec::new()), + &engine, + ) + .unwrap(); + assert_eq!(negated.as_array().unwrap().len(), 1); + assert_eq!(negated[0]["RepoTags"], json!(["demo:plain"])); + + let body = json!({ + "Image": "demo:labeled", + "Labels": {"keep": "container", "extra": "1"}, + "HostConfig": {"NetworkMode": "none"} + }); + let created = super::create( + &Request::internal("POST", "/containers/create", &[("name", "c1")], serde_json::to_vec(&body).unwrap()), + &engine, + ) + .unwrap(); + let id = created["Id"].as_str().unwrap(); + let ctr = super::container_inspect(&engine, id).unwrap(); + assert_eq!( + ctr["Config"]["Labels"], + json!({ + "cn.shopcider.safeguards.revision": "0a5b41cadacdafe94742fc8401d682347a392739", + "keep": "container", + "extra": "1" + }) + ); + + engine.shutdown(); + let _ = std::fs::remove_dir_all(&dir); + } } diff --git a/crates/kiri/src/daemon/ops.rs b/crates/kiri/src/daemon/ops.rs index 3bd31575..6ba03a74 100644 --- a/crates/kiri/src/daemon/ops.rs +++ b/crates/kiri/src/daemon/ops.rs @@ -278,6 +278,9 @@ fn commit(req: &Request, engine: &Engine) -> Result { cfg["config"]["Env"] = json!(spec.env); cfg["config"]["WorkingDir"] = spec.cwd.clone().into(); cfg["config"]["User"] = spec.user.clone().into(); + if spec.labels.is_object() { + cfg["config"]["Labels"] = spec.labels.clone(); + } if let Some(h) = &spec.healthcheck { cfg["config"]["Healthcheck"] = h.to_json(); } @@ -472,7 +475,7 @@ fn system_df(engine: &Engine) -> Result { "Size": size, "SharedSize": shared, "VirtualSize": size, - "Labels": null, + "Labels": super::api::image_labels(engine, i), "Containers": used.iter().filter(|u| **u == i.config_digest).count(), }) })