Skip to main content

isoloom_compose/
labels.rs

1//! What Isoloom marks an environment's Docker resources with: whose they are (so leftovers can
2//! be told apart and collected without guessing), which volumes `down` keeps, and what each
3//! machine serves.
4
5use isoloom_plan::Environment;
6use isoloom_spec::model;
7
8use crate::file::ComposeFile;
9
10/// On everything an environment creates on Docker: `isoloom gc` collects only what has it.
11pub const MANAGED_LABEL: &str = "isoloom.managed";
12/// The environment a Docker resource belongs to.
13pub const ENVIRONMENT_LABEL: &str = "isoloom.environment";
14/// On a volume `isoloom down` keeps (`down --volumes` removes it too).
15pub(crate) const PERSISTENT_LABEL: &str = "isoloom.persistent";
16
17/// Labels everything the environment creates (containers, built images, networks, volumes) as
18/// Isoloom's and the environment's; an existing network or volume isn't the environment's and
19/// stays unlabelled.
20pub(crate) fn mark(env: &Environment, file: &mut ComposeFile) {
21    let ours = [(MANAGED_LABEL.to_owned(), "true".to_owned()), (ENVIRONMENT_LABEL.to_owned(), env.name.clone())];
22    for svc in file.services.values_mut() {
23        svc.labels.extend(ours.clone());
24        if let Some(b) = &mut svc.build {
25            b.labels.extend(ours.clone());
26        }
27    }
28    for n in file.networks.values_mut() {
29        n.labels.extend(ours.clone());
30    }
31    for v in file.volumes.values_mut().filter(|v| !v.external) {
32        v.labels.extend(ours.clone());
33    }
34}
35
36/// What a machine serves, for tools reading the running containers (dashboards, runners):
37/// `isoloom.port.<name>: "<http|tcp|udp>:<port>"` (an unnamed UDP port's key is `<port>-udp`,
38/// so a port served over both keeps both).
39pub(crate) fn ports(m: &model::Machine) -> impl Iterator<Item = (String, String)> + '_ {
40    m.ports.iter().map(|p| {
41        let kind = if p.http { "http" } else { p.protocol.id() };
42        (format!("isoloom.port.{}", m.port_label(p)), format!("{kind}:{}", p.port))
43    })
44}