From 6a134c45e272b3435c1be6ef192420c2f0ddec22 Mon Sep 17 00:00:00 2001 From: Paul Nettleton Date: Sun, 29 Sep 2024 19:07:14 -0500 Subject: [PATCH] fix(compose): prepend container dependencies with pod name When the `podlet compose --pod` option is used, the names of the services from the Compose file are prepended with the pod name. If a service had a dependency via the `depends_on` attribute, the name of the dependency was not similarly prepended with the pod name when added to the `[Unit]` section of the Quadlet file. Changed `podlet::cli::compose::service_try_into_quadlet_file()` to handle all service name changes when the `--pod` option is used. Fixes: #114 Signed-off-by: Paul Nettleton --- src/cli/compose.rs | 44 ++++++++++++++++++++++++++------------------ src/cli/unit.rs | 4 ++-- 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/cli/compose.rs b/src/cli/compose.rs index 8211521..814b6e0 100644 --- a/src/cli/compose.rs +++ b/src/cli/compose.rs @@ -249,27 +249,15 @@ fn parts_try_into_files( let mut files = services .into_iter() .map(|(name, service)| { - let mut file = service_try_into_quadlet_file( + service_try_into_quadlet_file( service, name, unit.clone(), install.clone(), &volume_has_options, - )?; - if let ( - Some(pod_name), - quadlet::File { - name, - resource: quadlet::Resource::Container(container), - .. - }, - ) = (&pod_name, &mut file) - { - *name = format!("{pod_name}-{name}"); - pod_ports.extend(mem::take(&mut container.publish_port)); - container.pod = Some(format!("{pod_name}.pod")); - } - Ok(file) + pod_name.as_deref(), + &mut pod_ports, + ) }) .chain(networks_try_into_quadlet_files( networks, @@ -309,6 +297,9 @@ fn parts_try_into_files( /// options set. It is used to determine whether to link to a [`quadlet::Volume`] in the created /// [`quadlet::Container`]. /// +/// If `pod_name` is [`Some`] and the `service` has any published ports, they are taken from the +/// created [`quadlet::Container`] and added to `pod_ports`. +/// /// # Errors /// /// Returns an error if there was an error [adding](Unit::add_dependency()) a service @@ -320,13 +311,22 @@ fn service_try_into_quadlet_file( mut unit: Option, install: Option, volume_has_options: &HashMap, + pod_name: Option<&str>, + pod_ports: &mut Vec, ) -> color_eyre::Result { // Add any service dependencies to the [Unit] section of the Quadlet file. let dependencies = mem::take(&mut service.depends_on).into_long(); if !dependencies.is_empty() { let unit = unit.get_or_insert_with(Unit::default); for (ident, dependency) in dependencies { - unit.add_dependency(&ident, dependency).wrap_err_with(|| { + unit.add_dependency( + pod_name.map_or_else( + || ident.to_string(), + |pod_name| format!("{pod_name}-{ident}"), + ), + dependency, + ) + .wrap_err_with(|| { format!("error adding dependency on `{ident}` to service `{name}`") })?; } @@ -355,8 +355,16 @@ fn service_try_into_quadlet_file( } } + let name = if let Some(pod_name) = pod_name { + container.pod = Some(format!("{pod_name}.pod")); + pod_ports.extend(mem::take(&mut container.publish_port)); + format!("{pod_name}-{name}") + } else { + name.into() + }; + Ok(quadlet::File { - name: name.into(), + name, unit, resource: container.into(), globals: global_args.into(), diff --git a/src/cli/unit.rs b/src/cli/unit.rs index d2ba595..e02baf5 100644 --- a/src/cli/unit.rs +++ b/src/cli/unit.rs @@ -113,7 +113,7 @@ impl Unit { /// or the [`Dependency`] is set to `restart` but is not `required`. pub fn add_dependency( &mut self, - name: impl Display, + mut name: String, Dependency { condition, restart, @@ -141,7 +141,7 @@ impl Unit { (false, false) => &mut self.wants, }; - let name = format!("{name}.service"); + name.push_str(".service"); list.push(name.clone()); self.after.push(name);