Diffinery

homeflare/celldpublic

fix(containers): an idle container keeps the privileged engine's network namespace

Jason Schwarzenberger authored 1 parent 6f2fbc5commit 46ee9a6fbca86c2c01687ed993b3d82f77faa93cBrowse files
1 file changed+82−0
modifiedcrates/celld/container.rs+82−0View file
@@ -935,6 +935,84 @@
935935 /// without. Docker implements an internal network by omitting the
936936 /// masquerade rule and the default route; the node still reaches the
937937 /// bridge address, which is all ingress needs.
938+ /// homeflare: rootless Podman keeps a user's rootless-netns, where the
939+ /// bridges and their fence live, only while a container on one of its
940+ /// networks runs. The privileged engine's user runs nothing else, so
941+ /// the namespace would not exist for the fence to load into, and would
942+ /// go (with the rules) whenever its last runner stopped. An idle
943+ /// container of the fence image on the internal bridge keeps it. Returns
944+ /// whether it had to be started, which makes a new namespace.
945+ async fn keep_netns(&self) -> anyhow::Result<bool> {
946+ let name: String = format!("celld-netns-{}", self.node)
947+ .chars()
948+ .map(|c| {
949+ if c.is_ascii_alphanumeric() || c == '-' {
950+ c
951+ } else {
952+ '_'
953+ }
954+ })
955+ .collect();
956+ let reply = self
957+ .docker
958+ .call("GET", &format!("/containers/{name}/json"), None)
959+ .await?;
960+ if reply.status.is_success()
961+ && reply.json()?.pointer("/State/Running") == Some(&Value::Bool(true))
962+ {
963+ return Ok(false);
964+ }
965+ let image = FENCE_IMAGE.read().unwrap().clone().ok_or_else(|| {
966+ anyhow!(
967+ "this deployment has no fence image; deploy it again with this celld, which \
968+ saves the celld-fence image beside the container images"
969+ )
970+ })?;
971+ self.ensure_image(&image).await?;
972+ let (_, internal) = self.networks().await?;
973+ let _ = self
974+ .docker
975+ .call(
976+ "DELETE",
977+ &format!("/containers/{name}?force=true&v=true"),
978+ None,
979+ )
980+ .await;
981+ let body = json!({
982+ "Image": image,
983+ "Cmd": ["sleep", "infinity"],
984+ "Labels": { "celld.node": self.node, "celld.netns": "1" },
985+ "HostConfig": {
986+ "NetworkMode": internal,
987+ "CapDrop": ["ALL"],
988+ "SecurityOpt": ["no-new-privileges"],
989+ "Init": true,
990+ },
991+ });
992+ self.docker
993+ .expect(
994+ "POST",
995+ &format!("/containers/create?name={name}"),
996+ Some(body),
997+ "create netns container",
998+ )
999+ .await?;
1000+ self.docker
1001+ .expect(
1002+ "POST",
1003+ &format!("/containers/{name}/start"),
1004+ None,
1005+ "start netns container",
1006+ )
1007+ .await?;
1008+ tracing::info!(
1009+ event = "container_netns_kept",
1010+ container = %name,
1011+ "an idle container keeps the privileged engine's network namespace"
1012+ );
1013+ Ok(true)
1014+ }
1015+
9381016 async fn networks(&self) -> anyhow::Result<(String, String)> {
9391017 let mut guard = self.networks.lock().await;
9401018 if let Some(names) = guard.as_ref() {
@@ -1020,6 +1098,10 @@
10201098 /// process; a restart re-applies them, which is idempotent.
10211099 pub async fn ensure_fence(&self) -> anyhow::Result<()> {
10221100 let mut fenced = self.fenced.lock().await;
1101+ // homeflare: a new rootless-netns has none of the rules.
1102+ if self.privileged && self.keep_netns().await? {
1103+ *fenced = None;
1104+ }
10231105 let netns = fence_netns(self.privileged);
10241106 let (rules, network_mode) = match &netns {
10251107 Some(path) => (FENCE_RULES_NETNS, format!("ns:{path}")),