talos-api-rs 0.2.0

A typed, async, idiomatic Rust client for the Talos Linux gRPC API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Cluster discovery helpers for working with Talos clusters.
//!
//! This module provides utilities for discovering cluster members and their health status.
//!
//! # Example
//!
//! ```no_run
//! use talos_api_rs::client::discovery::ClusterDiscovery;
//! use talos_api_rs::client::TalosClientConfig;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! // Discover cluster from a single known endpoint
//! let discovery = ClusterDiscovery::from_endpoint("https://192.168.1.100:50000")
//!     .with_ca_cert("/path/to/ca.crt")
//!     .with_client_cert("/path/to/client.crt", "/path/to/client.key")
//!     .build();
//!
//! let members = discovery.discover_members().await?;
//! for member in &members {
//!     println!("{}: {} ({:?})", member.name, member.endpoint, member.role);
//! }
//!
//! // Check health of all members
//! let health = discovery.check_cluster_health().await?;
//! println!("Healthy: {}/{}", health.healthy_count(), health.total_count());
//! # Ok(())
//! # }
//! ```

use crate::client::{TalosClient, TalosClientConfig};
use crate::error::Result;
use std::collections::HashMap;
use std::time::Duration;

/// Role of a node in the Talos cluster.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum NodeRole {
    /// Control plane node (runs etcd, API server, etc.)
    ControlPlane,
    /// Worker node (runs workloads only)
    Worker,
    /// Unknown role
    Unknown,
}

impl std::fmt::Display for NodeRole {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ControlPlane => write!(f, "controlplane"),
            Self::Worker => write!(f, "worker"),
            Self::Unknown => write!(f, "unknown"),
        }
    }
}

/// Information about a discovered cluster member.
#[derive(Debug, Clone)]
pub struct ClusterMember {
    /// Node name/hostname
    pub name: String,
    /// gRPC endpoint URL
    pub endpoint: String,
    /// Node role
    pub role: NodeRole,
    /// Whether this node is an etcd member
    pub is_etcd_member: bool,
}

impl ClusterMember {
    /// Create a new cluster member
    #[must_use]
    pub fn new(name: impl Into<String>, endpoint: impl Into<String>, role: NodeRole) -> Self {
        Self {
            name: name.into(),
            endpoint: endpoint.into(),
            role,
            is_etcd_member: role == NodeRole::ControlPlane,
        }
    }

    /// Check if this is a control plane node
    #[must_use]
    pub fn is_control_plane(&self) -> bool {
        self.role == NodeRole::ControlPlane
    }

    /// Check if this is a worker node
    #[must_use]
    pub fn is_worker(&self) -> bool {
        self.role == NodeRole::Worker
    }
}

/// Health status of a single node.
#[derive(Debug, Clone)]
pub struct NodeHealth {
    /// Node name
    pub name: String,
    /// Node endpoint
    pub endpoint: String,
    /// Whether the node is reachable and healthy
    pub is_healthy: bool,
    /// Talos version if healthy
    pub version: Option<String>,
    /// Error message if unhealthy
    pub error: Option<String>,
    /// Response time in milliseconds
    pub response_time_ms: Option<u64>,
}

impl NodeHealth {
    /// Create a healthy node health status
    #[must_use]
    pub fn healthy(
        name: impl Into<String>,
        endpoint: impl Into<String>,
        version: impl Into<String>,
        response_time_ms: u64,
    ) -> Self {
        Self {
            name: name.into(),
            endpoint: endpoint.into(),
            is_healthy: true,
            version: Some(version.into()),
            error: None,
            response_time_ms: Some(response_time_ms),
        }
    }

    /// Create an unhealthy node health status
    #[must_use]
    pub fn unhealthy(
        name: impl Into<String>,
        endpoint: impl Into<String>,
        error: impl Into<String>,
    ) -> Self {
        Self {
            name: name.into(),
            endpoint: endpoint.into(),
            is_healthy: false,
            version: None,
            error: Some(error.into()),
            response_time_ms: None,
        }
    }
}

/// Health status of the entire cluster.
#[derive(Debug, Clone)]
pub struct ClusterHealth {
    /// Health status of each node
    pub nodes: Vec<NodeHealth>,
    /// Overall cluster status
    pub is_healthy: bool,
}

impl ClusterHealth {
    /// Create cluster health from individual node health
    #[must_use]
    pub fn from_nodes(nodes: Vec<NodeHealth>) -> Self {
        let is_healthy = !nodes.is_empty() && nodes.iter().all(|n| n.is_healthy);
        Self { nodes, is_healthy }
    }

    /// Get the number of healthy nodes
    #[must_use]
    pub fn healthy_count(&self) -> usize {
        self.nodes.iter().filter(|n| n.is_healthy).count()
    }

    /// Get the total number of nodes
    #[must_use]
    pub fn total_count(&self) -> usize {
        self.nodes.len()
    }

    /// Get unhealthy nodes
    #[must_use]
    pub fn unhealthy_nodes(&self) -> Vec<&NodeHealth> {
        self.nodes.iter().filter(|n| !n.is_healthy).collect()
    }

    /// Get healthy nodes
    #[must_use]
    pub fn healthy_nodes(&self) -> Vec<&NodeHealth> {
        self.nodes.iter().filter(|n| n.is_healthy).collect()
    }

    /// Get the average response time of healthy nodes
    #[must_use]
    pub fn avg_response_time_ms(&self) -> Option<u64> {
        let times: Vec<u64> = self
            .nodes
            .iter()
            .filter_map(|n| n.response_time_ms)
            .collect();

        if times.is_empty() {
            None
        } else {
            Some(times.iter().sum::<u64>() / times.len() as u64)
        }
    }
}

/// Builder for cluster discovery operations.
#[derive(Debug, Clone)]
pub struct ClusterDiscoveryBuilder {
    /// Initial endpoint to connect to
    endpoint: String,
    /// CA certificate path
    ca_cert: Option<String>,
    /// Client certificate path
    client_cert: Option<String>,
    /// Client key path
    client_key: Option<String>,
    /// Connection timeout
    connect_timeout: Duration,
    /// Request timeout for health checks
    request_timeout: Duration,
    /// Skip TLS verification
    insecure: bool,
}

impl ClusterDiscoveryBuilder {
    /// Create a new builder with the given endpoint
    #[must_use]
    pub fn new(endpoint: impl Into<String>) -> Self {
        Self {
            endpoint: endpoint.into(),
            ca_cert: None,
            client_cert: None,
            client_key: None,
            connect_timeout: Duration::from_secs(10),
            request_timeout: Duration::from_secs(5),
            insecure: false,
        }
    }

    /// Set CA certificate path
    #[must_use]
    pub fn with_ca_cert(mut self, path: impl Into<String>) -> Self {
        self.ca_cert = Some(path.into());
        self
    }

    /// Set client certificate and key paths
    #[must_use]
    pub fn with_client_cert(
        mut self,
        cert_path: impl Into<String>,
        key_path: impl Into<String>,
    ) -> Self {
        self.client_cert = Some(cert_path.into());
        self.client_key = Some(key_path.into());
        self
    }

    /// Set connection timeout
    #[must_use]
    pub fn with_connect_timeout(mut self, timeout: Duration) -> Self {
        self.connect_timeout = timeout;
        self
    }

    /// Set request timeout for health checks
    #[must_use]
    pub fn with_request_timeout(mut self, timeout: Duration) -> Self {
        self.request_timeout = timeout;
        self
    }

    /// Skip TLS verification (insecure)
    #[must_use]
    pub fn insecure(mut self) -> Self {
        self.insecure = true;
        self
    }

    /// Build the cluster discovery instance
    #[must_use]
    pub fn build(self) -> ClusterDiscovery {
        ClusterDiscovery {
            endpoint: self.endpoint,
            ca_cert: self.ca_cert,
            client_cert: self.client_cert,
            client_key: self.client_key,
            connect_timeout: self.connect_timeout,
            request_timeout: self.request_timeout,
            insecure: self.insecure,
        }
    }
}

/// Cluster discovery helper for Talos clusters.
///
/// Provides utilities for discovering cluster members and checking their health.
#[derive(Debug, Clone)]
pub struct ClusterDiscovery {
    endpoint: String,
    ca_cert: Option<String>,
    client_cert: Option<String>,
    client_key: Option<String>,
    connect_timeout: Duration,
    request_timeout: Duration,
    insecure: bool,
}

impl ClusterDiscovery {
    /// Create a new discovery instance from an endpoint
    #[must_use]
    pub fn from_endpoint(endpoint: impl Into<String>) -> ClusterDiscoveryBuilder {
        ClusterDiscoveryBuilder::new(endpoint)
    }

    /// Create a client config for connecting to a specific endpoint
    fn create_config(&self, endpoint: &str) -> TalosClientConfig {
        let mut config = TalosClientConfig::new(endpoint)
            .with_connect_timeout(self.connect_timeout)
            .with_request_timeout(self.request_timeout);

        if let Some(ref ca) = self.ca_cert {
            config = config.with_ca(ca);
        }

        if let (Some(ref cert), Some(ref key)) = (&self.client_cert, &self.client_key) {
            config = config.with_client_cert(cert).with_client_key(key);
        }

        if self.insecure {
            config = config.insecure();
        }

        config
    }

    /// Connect to the primary endpoint and get a client
    async fn connect_primary(&self) -> Result<TalosClient> {
        let config = self.create_config(&self.endpoint);
        TalosClient::new(config).await
    }

    /// Discover cluster members via etcd member list
    ///
    /// This connects to the initial endpoint and queries the etcd member list
    /// to discover all control plane nodes.
    pub async fn discover_members(&self) -> Result<Vec<ClusterMember>> {
        let client = self.connect_primary().await?;

        // Use EtcdMemberList to discover control plane nodes
        let etcd_response = client
            .etcd_member_list(crate::resources::EtcdMemberListRequest::new())
            .await?;

        let mut members = Vec::new();

        for result in &etcd_response.results {
            for member in &result.members {
                // Extract endpoint from member's client URLs
                let endpoint = member
                    .client_urls
                    .first()
                    .map(|url| {
                        // Convert etcd client URL to Talos API endpoint
                        // etcd typically uses port 2379, Talos API uses 50000
                        url.replace(":2379", ":50000")
                            .replace("http://", "https://")
                    })
                    .unwrap_or_else(|| self.endpoint.clone());

                members.push(ClusterMember {
                    name: member.hostname.clone(),
                    endpoint,
                    role: NodeRole::ControlPlane,
                    is_etcd_member: true,
                });
            }
        }

        Ok(members)
    }

    /// Check health of a single endpoint
    ///
    /// Tries the Version API first, falls back to Hostname API if unavailable.
    /// This is necessary because Docker-based Talos clusters don't implement
    /// the Version API.
    async fn check_endpoint_health(&self, name: &str, endpoint: &str) -> NodeHealth {
        let config = self.create_config(endpoint);
        let start = std::time::Instant::now();

        match TalosClient::new(config).await {
            Ok(client) => {
                // Try Version API first
                let mut version_client = client.version();
                let version_req = crate::api::version::VersionRequest { client: false };

                match version_client.version(version_req).await {
                    Ok(response) => {
                        let elapsed = start.elapsed().as_millis() as u64;
                        NodeHealth::healthy(name, endpoint, &response.get_ref().tag, elapsed)
                    }
                    Err(version_err) => {
                        // Version API failed - try Hostname API as fallback
                        // This is common in Docker-based clusters where Version is unimplemented
                        let mut machine_client = client.machine();
                        match machine_client.hostname(()).await {
                            Ok(response) => {
                                let elapsed = start.elapsed().as_millis() as u64;
                                // Extract hostname from response
                                let hostname = response
                                    .get_ref()
                                    .messages
                                    .first()
                                    .map(|m| m.hostname.as_str())
                                    .unwrap_or("unknown");
                                // Mark as healthy with hostname instead of version
                                NodeHealth::healthy(
                                    name,
                                    endpoint,
                                    format!("(hostname: {})", hostname),
                                    elapsed,
                                )
                            }
                            Err(_) => {
                                // Both APIs failed - report the version error
                                NodeHealth::unhealthy(name, endpoint, version_err.to_string())
                            }
                        }
                    }
                }
            }
            Err(e) => NodeHealth::unhealthy(name, endpoint, e.to_string()),
        }
    }

    /// Check health of all discovered cluster members
    ///
    /// This first discovers members, then checks health of each one.
    pub async fn check_cluster_health(&self) -> Result<ClusterHealth> {
        let members = self.discover_members().await?;
        self.check_members_health(&members).await
    }

    /// Check health of specific cluster members
    ///
    /// Useful when you already have a list of members.
    pub async fn check_members_health(&self, members: &[ClusterMember]) -> Result<ClusterHealth> {
        let mut health_results = Vec::with_capacity(members.len());

        for member in members {
            let health = self
                .check_endpoint_health(&member.name, &member.endpoint)
                .await;
            health_results.push(health);
        }

        Ok(ClusterHealth::from_nodes(health_results))
    }

    /// Check health of multiple endpoints directly
    ///
    /// Useful when you have a list of endpoint URLs but not member info.
    pub async fn check_endpoints_health(&self, endpoints: &[String]) -> Result<ClusterHealth> {
        let mut health_results = Vec::with_capacity(endpoints.len());

        for endpoint in endpoints {
            let health = self.check_endpoint_health(endpoint, endpoint).await;
            health_results.push(health);
        }

        Ok(ClusterHealth::from_nodes(health_results))
    }

    /// Get a map of endpoint to version for healthy nodes
    pub async fn get_cluster_versions(&self) -> Result<HashMap<String, String>> {
        let health = self.check_cluster_health().await?;

        Ok(health
            .nodes
            .into_iter()
            .filter_map(|n| n.version.map(|v| (n.endpoint, v)))
            .collect())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_node_role_display() {
        assert_eq!(format!("{}", NodeRole::ControlPlane), "controlplane");
        assert_eq!(format!("{}", NodeRole::Worker), "worker");
        assert_eq!(format!("{}", NodeRole::Unknown), "unknown");
    }

    #[test]
    fn test_cluster_member_new() {
        let member = ClusterMember::new(
            "node1",
            "https://192.168.1.100:50000",
            NodeRole::ControlPlane,
        );
        assert_eq!(member.name, "node1");
        assert_eq!(member.endpoint, "https://192.168.1.100:50000");
        assert!(member.is_control_plane());
        assert!(!member.is_worker());
        assert!(member.is_etcd_member);
    }

    #[test]
    fn test_cluster_member_worker() {
        let member = ClusterMember::new("worker1", "https://192.168.1.200:50000", NodeRole::Worker);
        assert!(!member.is_control_plane());
        assert!(member.is_worker());
        assert!(!member.is_etcd_member);
    }

    #[test]
    fn test_node_health_healthy() {
        let health = NodeHealth::healthy("node1", "https://192.168.1.100:50000", "v1.9.0", 42);
        assert!(health.is_healthy);
        assert_eq!(health.version, Some("v1.9.0".to_string()));
        assert_eq!(health.response_time_ms, Some(42));
        assert!(health.error.is_none());
    }

    #[test]
    fn test_node_health_unhealthy() {
        let health =
            NodeHealth::unhealthy("node1", "https://192.168.1.100:50000", "connection refused");
        assert!(!health.is_healthy);
        assert!(health.version.is_none());
        assert!(health.response_time_ms.is_none());
        assert_eq!(health.error, Some("connection refused".to_string()));
    }

    #[test]
    fn test_cluster_health_all_healthy() {
        let nodes = vec![
            NodeHealth::healthy("node1", "endpoint1", "v1.9.0", 10),
            NodeHealth::healthy("node2", "endpoint2", "v1.9.0", 20),
        ];
        let health = ClusterHealth::from_nodes(nodes);

        assert!(health.is_healthy);
        assert_eq!(health.healthy_count(), 2);
        assert_eq!(health.total_count(), 2);
        assert_eq!(health.unhealthy_nodes().len(), 0);
    }

    #[test]
    fn test_cluster_health_partial_healthy() {
        let nodes = vec![
            NodeHealth::healthy("node1", "endpoint1", "v1.9.0", 10),
            NodeHealth::unhealthy("node2", "endpoint2", "timeout"),
        ];
        let health = ClusterHealth::from_nodes(nodes);

        assert!(!health.is_healthy);
        assert_eq!(health.healthy_count(), 1);
        assert_eq!(health.total_count(), 2);
        assert_eq!(health.unhealthy_nodes().len(), 1);
    }

    #[test]
    fn test_cluster_health_avg_response_time() {
        let nodes = vec![
            NodeHealth::healthy("node1", "endpoint1", "v1.9.0", 10),
            NodeHealth::healthy("node2", "endpoint2", "v1.9.0", 20),
            NodeHealth::healthy("node3", "endpoint3", "v1.9.0", 30),
        ];
        let health = ClusterHealth::from_nodes(nodes);

        assert_eq!(health.avg_response_time_ms(), Some(20));
    }

    #[test]
    fn test_cluster_health_no_response_time() {
        let nodes = vec![NodeHealth::unhealthy("node1", "endpoint1", "error")];
        let health = ClusterHealth::from_nodes(nodes);

        assert_eq!(health.avg_response_time_ms(), None);
    }

    #[test]
    fn test_cluster_discovery_builder() {
        let discovery = ClusterDiscovery::from_endpoint("https://192.168.1.100:50000")
            .with_ca_cert("/path/to/ca.crt")
            .with_client_cert("/path/to/client.crt", "/path/to/client.key")
            .with_connect_timeout(Duration::from_secs(5))
            .with_request_timeout(Duration::from_secs(3))
            .build();

        assert_eq!(discovery.endpoint, "https://192.168.1.100:50000");
        assert_eq!(discovery.ca_cert, Some("/path/to/ca.crt".to_string()));
        assert_eq!(
            discovery.client_cert,
            Some("/path/to/client.crt".to_string())
        );
        assert_eq!(
            discovery.client_key,
            Some("/path/to/client.key".to_string())
        );
        assert_eq!(discovery.connect_timeout, Duration::from_secs(5));
        assert_eq!(discovery.request_timeout, Duration::from_secs(3));
        assert!(!discovery.insecure);
    }

    #[test]
    fn test_cluster_discovery_builder_insecure() {
        let discovery = ClusterDiscovery::from_endpoint("https://192.168.1.100:50000")
            .insecure()
            .build();

        assert!(discovery.insecure);
    }
}