redup_bootstrap/
bootstrap_config.rs1use redup_config::Config;
4
5use crate::control_config::ControlConfig;
6use crate::grpc_config::GrpcConfig;
7use crate::rest_config::RestConfig;
8use crate::telemetry::{LogsConfig, MetricsConfig, TracingConfig};
9use crate::watchers_config::WatchersConfig;
10
11#[derive(Debug, Clone, Config)]
15pub struct BootstrapConfig {
16 watchers: WatchersConfig,
18 control: ControlConfig,
20 rest: Option<RestConfig>,
22 grpc: Option<GrpcConfig>,
24 logs: LogsConfig,
26 metrics: MetricsConfig,
28 tracing: TracingConfig,
30}
31
32impl Default for BootstrapConfig {
33 fn default() -> Self {
34 Self {
35 watchers: WatchersConfig::default(),
36 control: ControlConfig::default(),
37 rest: Some(RestConfig::default()),
38 grpc: None,
39 logs: LogsConfig::default(),
40 metrics: MetricsConfig::default(),
41 tracing: TracingConfig::default(),
42 }
43 }
44}
45
46impl BootstrapConfig {
47 pub fn watchers(&self) -> WatchersConfig {
49 self.watchers
50 }
51
52 pub fn control(&self) -> ControlConfig {
54 self.control
55 }
56
57 pub fn rest(&self) -> Option<&RestConfig> {
59 self.rest.as_ref()
60 }
61
62 pub fn grpc(&self) -> Option<&GrpcConfig> {
64 self.grpc.as_ref()
65 }
66
67 pub fn logs(&self) -> LogsConfig {
69 self.logs
70 }
71
72 pub fn metrics(&self) -> &MetricsConfig {
74 &self.metrics
75 }
76
77 pub fn tracing(&self) -> &TracingConfig {
79 &self.tracing
80 }
81}
82
83#[cfg(test)]
84mod tests {
85 use redup_config::{Source, load};
86
87 use super::BootstrapConfig;
88 use crate::detection::Detection;
89 use crate::watch_mode::WatchMode;
90
91 #[test]
92 fn defaults_apply_without_a_section() {
93 let config = BootstrapConfig::default();
94 assert_eq!(config.watchers().mode(), WatchMode::Once);
95 assert_eq!(config.control().drain_timeout().as_secs(), 5);
96 assert!(config.control().oom().is_none());
97 let rest = config.rest().expect("the listener is on by default");
98 assert_eq!(rest.port(), 8080);
99 }
100
101 #[test]
102 fn section_is_read_from_the_common_file() {
103 let directory = tempfile::tempdir().expect("a temporary directory");
104 std::fs::write(
105 directory.path().join("config.yaml"),
106 "bootstrap:\n\
107 \x20 watchers:\n\
108 \x20 mode: startupThenObserve\n\
109 \x20 pullDelay: 3s\n\
110 \x20 control:\n\
111 \x20 drainTimeout: 12s\n\
112 \x20 oom:\n\
113 \x20 detection: boundary\n\
114 \x20 rest: ~\n",
115 )
116 .expect("the configuration file");
117 let source = Source::directory(directory.path());
118 let loaded = load::<BootstrapConfig>(&source).expect("the configuration is accepted");
119 let value = loaded.value;
120 assert_eq!(value.watchers().mode(), WatchMode::StartupThenObserve);
121 assert_eq!(value.watchers().pull_delay().as_secs(), 3);
122 assert_eq!(value.control().drain_timeout().as_secs(), 12);
123 assert_eq!(
124 value.control().oom().expect("oom is on").detection(),
125 Detection::Boundary
126 );
127 assert!(value.rest().is_none());
128 }
129}