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_bootstrap_grpc::Exposure;
86 use redup_config::{Source, load};
87
88 use super::BootstrapConfig;
89 use crate::detection::Detection;
90 use crate::watch_mode::WatchMode;
91
92 #[test]
93 fn defaults_apply_without_a_section() {
94 let config = BootstrapConfig::default();
95 assert_eq!(config.watchers().mode(), WatchMode::Once);
96 assert_eq!(config.control().drain_timeout().as_secs(), 5);
97 assert!(config.control().oom().is_none());
98 let rest = config.rest().expect("the listener is on by default");
99 assert_eq!(rest.port(), 8080);
100 assert_eq!(rest.concurrent_requests_limit(), 128);
101 assert_eq!(rest.compression_threshold(), 1024);
102 }
103
104 #[test]
105 fn transcoding_section_is_read() {
106 let directory = tempfile::tempdir().expect("a temporary directory");
107 std::fs::write(
108 directory.path().join("config.yaml"),
109 "bootstrap:\n\
110 \x20 grpc:\n\
111 \x20 host: 127.0.0.1\n\
112 \x20 port: 14200\n\
113 \x20 transcoding:\n\
114 \x20 port: 18081\n\
115 \x20 enableOpenApi: false\n\
116 \x20 removeEnumPrefix: false\n\
117 \x20 errorExposure: full\n",
118 )
119 .expect("the configuration file");
120 let source = Source::directory(directory.path());
121 let loaded = load::<BootstrapConfig>(&source).expect("the configuration is accepted");
122 let grpc = loaded.value.grpc().expect("the gRPC listener");
123 let transcoding = grpc.transcoding().expect("transcoding is on");
124 assert_eq!(transcoding.port(), 18081);
125 assert!(!transcoding.enable_open_api());
126 assert!(!transcoding.remove_enum_prefix());
127 assert_eq!(transcoding.error_exposure(), Exposure::Full);
128 }
129
130 #[test]
131 fn section_is_read_from_the_common_file() {
132 let directory = tempfile::tempdir().expect("a temporary directory");
133 std::fs::write(
134 directory.path().join("config.yaml"),
135 "bootstrap:\n\
136 \x20 watchers:\n\
137 \x20 mode: startupThenObserve\n\
138 \x20 pullDelay: 3s\n\
139 \x20 control:\n\
140 \x20 drainTimeout: 12s\n\
141 \x20 oom:\n\
142 \x20 detection: boundary\n\
143 \x20 rest: ~\n",
144 )
145 .expect("the configuration file");
146 let source = Source::directory(directory.path());
147 let loaded = load::<BootstrapConfig>(&source).expect("the configuration is accepted");
148 let value = loaded.value;
149 assert_eq!(value.watchers().mode(), WatchMode::StartupThenObserve);
150 assert_eq!(value.watchers().pull_delay().as_secs(), 3);
151 assert_eq!(value.control().drain_timeout().as_secs(), 12);
152 assert_eq!(
153 value.control().oom().expect("oom is on").detection(),
154 Detection::Boundary
155 );
156 assert!(value.rest().is_none());
157 }
158}