k8s_operator_raft/
config.rs1use serde::{Deserialize, Serialize};
2use std::time::Duration;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct RaftConfig {
6 pub node_id: u64,
7 pub cluster_name: String,
8 pub listen_addr: String,
9 pub advertise_addr: String,
10 pub peer_addresses: Vec<String>,
11 pub election_timeout: Duration,
12 pub heartbeat_interval: Duration,
13 pub snapshot_threshold: u64,
14 pub max_payload_entries: u64,
15 pub install_snapshot_timeout: Duration,
16 pub send_timeout: Duration,
17}
18
19impl Default for RaftConfig {
20 fn default() -> Self {
21 Self {
22 node_id: 0,
23 cluster_name: "operator".to_string(),
24 listen_addr: "0.0.0.0:5000".to_string(),
25 advertise_addr: "localhost:5000".to_string(),
26 peer_addresses: Vec::new(),
27 election_timeout: Duration::from_millis(1000),
28 heartbeat_interval: Duration::from_millis(300),
29 snapshot_threshold: 10000,
30 max_payload_entries: 100,
31 install_snapshot_timeout: Duration::from_secs(60),
32 send_timeout: Duration::from_secs(5),
33 }
34 }
35}
36
37impl RaftConfig {
38 pub fn builder() -> RaftConfigBuilder {
39 RaftConfigBuilder::default()
40 }
41
42 pub fn from_env() -> k8s_operator_core::Result<Self> {
43 let mut config = Self::default();
44
45 if let Ok(val) = std::env::var("RAFT_NODE_ID") {
46 config.node_id = val.parse().map_err(|_| {
47 k8s_operator_core::OperatorError::ConfigError("Invalid RAFT_NODE_ID".to_string())
48 })?;
49 }
50
51 if let Ok(val) = std::env::var("RAFT_CLUSTER_NAME") {
52 config.cluster_name = val;
53 }
54
55 if let Ok(val) = std::env::var("RAFT_LISTEN_ADDR") {
56 config.listen_addr = val;
57 }
58
59 if let Ok(val) = std::env::var("RAFT_ADVERTISE_ADDR") {
60 config.advertise_addr = val;
61 }
62
63 if let Ok(val) = std::env::var("RAFT_PEERS") {
64 config.peer_addresses = val.split(',').map(|s| s.trim().to_string()).collect();
65 }
66
67 Ok(config)
68 }
69}
70
71#[derive(Default)]
72pub struct RaftConfigBuilder {
73 config: RaftConfig,
74}
75
76impl RaftConfigBuilder {
77 pub fn node_id(mut self, id: u64) -> Self {
78 self.config.node_id = id;
79 self
80 }
81
82 pub fn cluster_name(mut self, name: impl Into<String>) -> Self {
83 self.config.cluster_name = name.into();
84 self
85 }
86
87 pub fn listen_addr(mut self, addr: impl Into<String>) -> Self {
88 self.config.listen_addr = addr.into();
89 self
90 }
91
92 pub fn advertise_addr(mut self, addr: impl Into<String>) -> Self {
93 self.config.advertise_addr = addr.into();
94 self
95 }
96
97 pub fn peer(mut self, addr: impl Into<String>) -> Self {
98 self.config.peer_addresses.push(addr.into());
99 self
100 }
101
102 pub fn peers(mut self, addrs: Vec<String>) -> Self {
103 self.config.peer_addresses = addrs;
104 self
105 }
106
107 pub fn election_timeout(mut self, timeout: Duration) -> Self {
108 self.config.election_timeout = timeout;
109 self
110 }
111
112 pub fn heartbeat_interval(mut self, interval: Duration) -> Self {
113 self.config.heartbeat_interval = interval;
114 self
115 }
116
117 pub fn snapshot_threshold(mut self, threshold: u64) -> Self {
118 self.config.snapshot_threshold = threshold;
119 self
120 }
121
122 pub fn build(self) -> RaftConfig {
123 self.config
124 }
125}