public inbox for pdm-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [PATCH datacenter-manager 1/4] server: remote mapping: remember whether a pve remote is a cluster
Date: Tue,  1 Dec 2026 16:12:05 +0100	[thread overview]
Message-ID: <20261201151531.3348382-2-d.csapak@proxmox.com> (raw)
In-Reply-To: <20261201151531.3348382-1-d.csapak@proxmox.com>

so that we can tell standalone nodes and clusters apart without
having to query the remote, e.g. for showing a fitting icon in the
UI.

The mapping task already queries '/cluster/status' of every endpoint,
which only contains an entry of type 'cluster' when the node is part
of one. The cluster ID can't be used for that, since standalone nodes
report one too.

Like the cluster ID, the info belongs to the old endpoints when they
change, so reset it then.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 .../tasks/remote_node_mapping.rs              | 47 ++++++++++++-------
 server/src/remote_cache/mod.rs                | 33 +++++++++++--
 2 files changed, 58 insertions(+), 22 deletions(-)

diff --git a/server/src/bin/proxmox-datacenter-api/tasks/remote_node_mapping.rs b/server/src/bin/proxmox-datacenter-api/tasks/remote_node_mapping.rs
index 23422008..27749a04 100644
--- a/server/src/bin/proxmox-datacenter-api/tasks/remote_node_mapping.rs
+++ b/server/src/bin/proxmox-datacenter-api/tasks/remote_node_mapping.rs
@@ -167,9 +167,10 @@ impl CachingTask {
             }
         }
 
-        // The cluster ID belongs to the old endpoints until one of the new ones reports it.
+        // The cluster info belongs to the old endpoints until one of the new ones reports it.
         if changed {
             entry.cluster_id = None;
+            entry.cluster = None;
         }
     }
 
@@ -227,12 +228,12 @@ impl CachingTask {
             if let Some(connection_state) = connection_state {
                 cache.mark_host_reachable(&remote.id, &node.hostname, connection_state);
             }
-            if let Ok((node_name, cluster_id)) = query_result {
-                if node_name.is_some() {
-                    cache.set_node_name(&remote.id, &node.hostname, node_name);
+            if let Ok(status) = query_result {
+                if status.node_name.is_some() {
+                    cache.set_node_name(&remote.id, &node.hostname, status.node_name);
                 }
                 if let Some(entry) = cache.remotes.get_mut(&remote.id) {
-                    entry.set_cluster_id(&node.hostname, cluster_id);
+                    entry.set_cluster_info(&node.hostname, status.cluster_id, status.cluster);
                 }
             }
             cache.save()?;
@@ -242,27 +243,36 @@ impl CachingTask {
     }
 }
 
-/// Calls `/cluster/status` directly on a specific node to find its name, which the status lacks if
-/// it does not mark one node as the local one, and the ID of its cluster.
-async fn query_node(
-    remote: &Remote,
-    hostname: &str,
-) -> Result<(Option<String>, Option<String>), Error> {
+/// What the `/cluster/status` of a node tells about it.
+struct NodeClusterStatus {
+    /// The name of the node, which the status lacks if it does not mark one node as the local one.
+    node_name: Option<String>,
+    cluster_id: Option<String>,
+    cluster: bool,
+}
+
+/// Calls `/cluster/status` directly on a specific node.
+async fn query_node(remote: &Remote, hostname: &str) -> Result<NodeClusterStatus, Error> {
     log::trace!("querying node name {hostname:?} for remote {:?}", remote.id);
     let client = server::connection::make_pve_client_with_endpoint(remote, Some(hostname))?;
     let node_status_list = client.cluster_status().await?;
     let cluster_id = remote_cache::cluster_id(&node_status_list);
-    let name = node_status_list
+    let cluster = remote_cache::is_cluster(&node_status_list);
+    let node_name = node_status_list
         .into_iter()
         .find(|node| node.local == Some(true))
         .map(|node| node.name);
-    if name.is_none() {
+    if node_name.is_none() {
         log::warn!(
             "the cluster status of remote '{}' node '{hostname}' names no local node",
             remote.id
         );
     }
-    Ok((name, cluster_id))
+    Ok(NodeClusterStatus {
+        node_name,
+        cluster_id,
+        cluster,
+    })
 }
 
 #[cfg(test)]
@@ -272,7 +282,7 @@ mod tests {
     use super::*;
 
     #[test]
-    fn changed_endpoints_drop_the_cluster_id() -> Result<(), Error> {
+    fn changed_endpoints_drop_the_cluster_info() -> Result<(), Error> {
         let remote = |hostname: &str| -> Result<Remote, Error> {
             let raw = format!("pve: pve-a\n\tauthid root@pam!pdm\n\tnodes {hostname}\n\ttoken -\n");
             Ok(Remote::parse_section_config("remotes.cfg", &raw)?
@@ -285,17 +295,20 @@ mod tests {
         let old = remote("x1.example")?;
         task.prune_remote_nodes(&mut cache, "pve-a", &old);
         let entry = cache.remotes.get_mut("pve-a").unwrap();
-        entry.set_cluster_id("x1.example", Some("x".into()));
+        entry.set_cluster_info("x1.example", Some("x".into()), true);
         task.prune_remote_nodes(&mut cache, "pve-a", &old);
         assert_eq!(cache.cluster_id("pve-a"), Some("x"));
+        assert_eq!(cache.is_cluster("pve-a"), Some(true));
 
         // Repointed to another cluster that does not answer: the old cluster is free to be added.
         task.prune_remote_nodes(&mut cache, "pve-a", &remote("y1.example")?);
         assert_eq!(cache.cluster_id("pve-a"), None);
+        assert_eq!(cache.is_cluster("pve-a"), None);
         // An answer of the old endpoint that arrives late does not count either.
         let entry = cache.remotes.get_mut("pve-a").unwrap();
-        entry.set_cluster_id("x1.example", Some("x".into()));
+        entry.set_cluster_info("x1.example", Some("x".into()), true);
         assert_eq!(cache.cluster_id("pve-a"), None);
+        assert_eq!(cache.is_cluster("pve-a"), None);
 
         Ok(())
     }
diff --git a/server/src/remote_cache/mod.rs b/server/src/remote_cache/mod.rs
index a532adc0..a78328ae 100644
--- a/server/src/remote_cache/mod.rs
+++ b/server/src/remote_cache/mod.rs
@@ -28,7 +28,7 @@ use proxmox_time::{epoch_i64, epoch_to_rfc2822};
 
 use pdm_api_types::remotes::RemoteType;
 use pdm_config::ConfigVersionCache;
-use pve_api_types::ClusterNodeStatus;
+use pve_api_types::{ClusterNodeStatus, ClusterNodeStatusType};
 
 mod back_off;
 use back_off::BackOffState;
@@ -208,6 +208,11 @@ impl RemoteMappingCache {
         self.remotes.get(remote)?.cluster_id.as_deref()
     }
 
+    /// Whether a remote was a cluster when it answered last.
+    pub fn is_cluster(&self, remote: &str) -> Option<bool> {
+        self.remotes.get(remote)?.cluster
+    }
+
     /// Attempt to retrieve the host name from a node name.
     pub fn node_name_to_hostname(&self, remote: &str, node_name: &str) -> Option<&str> {
         Some(self.remotes.get(remote)?.node_to_host.get(node_name)?)
@@ -432,6 +437,14 @@ pub fn cluster_id(status: &[ClusterNodeStatus]) -> Option<String> {
     status.iter().find_map(|entry| entry.cluster_id.clone())
 }
 
+/// Whether a `/cluster/status` of Proxmox VE is that of a cluster, a standalone node lists no
+/// cluster entry.
+pub fn is_cluster(status: &[ClusterNodeStatus]) -> bool {
+    status
+        .iter()
+        .any(|entry| entry.ty == ClusterNodeStatusType::Cluster)
+}
+
 /// If a remote is reachable or not
 pub enum ConnectionState {
     /// The host/remote/etc. is reachable
@@ -455,6 +468,10 @@ pub struct RemoteMapping {
     /// The ID of the cluster, as last reported by an endpoint, see [`cluster_id`].
     #[serde(default, skip_serializing_if = "Option::is_none")]
     pub cluster_id: Option<String>,
+
+    /// Whether the remote is a cluster, as last reported by an endpoint, see [`is_cluster`].
+    #[serde(default, skip_serializing_if = "Option::is_none")]
+    pub cluster: Option<bool>,
 }
 
 impl RemoteMapping {
@@ -464,14 +481,17 @@ impl RemoteMapping {
             hosts: HashMap::new(),
             node_to_host: HashMap::new(),
             cluster_id: None,
+            cluster: None,
         }
     }
 
-    /// Update the cluster ID that a host reported, if the host exists (otherwise this does
-    /// nothing), so that the answer of an endpoint that was removed meanwhile does not count.
-    pub fn set_cluster_id(&mut self, hostname: &str, cluster_id: Option<String>) {
+    /// Update the cluster ID and whether the remote is a cluster, as a host reported them, if the
+    /// host exists (otherwise this does nothing), so that the answer of an endpoint that was
+    /// removed meanwhile does not count.
+    pub fn set_cluster_info(&mut self, hostname: &str, cluster_id: Option<String>, cluster: bool) {
         if self.hosts.contains_key(hostname) {
             self.cluster_id = cluster_id;
+            self.cluster = Some(cluster);
         }
     }
 
@@ -569,7 +589,7 @@ mod tests {
     use super::*;
 
     #[test]
-    fn cluster_id_of_clusters_and_standalone_nodes() {
+    fn cluster_info_of_clusters_and_standalone_nodes() {
         let status = |entries| serde_json::from_value::<Vec<ClusterNodeStatus>>(entries).unwrap();
 
         let cluster = status(json!([
@@ -581,6 +601,7 @@ mod tests {
             cluster_id(&cluster).as_deref(),
             Some("abcdefghij2345672345")
         );
+        assert!(is_cluster(&cluster));
 
         let standalone = status(json!([
             { "type": "node", "id": "node/a", "name": "a", "cluster-id": "zyxwvutsrq7654327654" },
@@ -589,11 +610,13 @@ mod tests {
             cluster_id(&standalone).as_deref(),
             Some("zyxwvutsrq7654327654")
         );
+        assert!(!is_cluster(&standalone));
 
         let older = status(json!([
             { "type": "cluster", "id": "cluster", "name": "dc1" },
             { "type": "node", "id": "node/a", "name": "a", "local": 1 },
         ]));
         assert_eq!(cluster_id(&older), None);
+        assert!(is_cluster(&older));
     }
 }
-- 
2.47.3





  reply	other threads:[~2026-12-01 15:15 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 15:12 [PATCH datacenter-manager 0/4] api/ui: show consistent remote icons Dominik Csapak
2026-12-01 15:12 ` Dominik Csapak [this message]
2026-12-01 15:12 ` [PATCH datacenter-manager 2/4] api: remotes: report whether a remote is a cluster in the list Dominik Csapak
2026-12-01 15:12 ` [PATCH datacenter-manager 3/4] pdm-client: return the cluster flag with the remote list Dominik Csapak
2026-12-01 15:12 ` [PATCH datacenter-manager 4/4] ui: use consistent icons for the types of remotes Dominik Csapak
2026-12-01 18:29 ` applied: [PATCH datacenter-manager 0/4] api/ui: show consistent remote icons Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201151531.3348382-2-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pdm-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal