@@ -9,18 +9,66 @@ use tokio::{
99 time:: interval,
1010} ;
1111
12- use super :: { EndpointConfig , FactConfig , GrpcConfig , CONFIG_FILES } ;
12+ use super :: { builder:: FactConfigBuilder , EndpointConfig , FactConfig , GrpcConfig } ;
13+
14+ const CONFIG_FILES : [ & str ; 4 ] = [
15+ "/etc/stackrox/fact.yml" ,
16+ "/etc/stackrox/fact.yaml" ,
17+ "fact.yml" ,
18+ "fact.yaml" ,
19+ ] ;
1320
1421pub struct Reloader {
1522 config : FactConfig ,
23+ builder : FactConfigBuilder ,
1624 endpoint : watch:: Sender < EndpointConfig > ,
1725 grpc : watch:: Sender < GrpcConfig > ,
1826 paths : watch:: Sender < Vec < PathBuf > > ,
19- files : HashMap < & ' static str , i64 > ,
27+ files : HashMap < PathBuf , i64 > ,
2028 trigger : Arc < Notify > ,
2129}
2230
2331impl Reloader {
32+ pub fn new ( ) -> anyhow:: Result < Self > {
33+ let builder = FactConfigBuilder :: new ( ) . add_files ( CONFIG_FILES . as_slice ( ) ) ;
34+ let config = builder. build ( ) ?;
35+ info ! ( "Startup configuration: {config:#?}" ) ;
36+
37+ let ( endpoint, _) = watch:: channel ( config. endpoint . clone ( ) ) ;
38+ let ( grpc, _) = watch:: channel ( config. grpc . clone ( ) ) ;
39+ let ( paths, _) = watch:: channel ( config. paths ( ) . to_vec ( ) ) ;
40+ let trigger = Arc :: new ( Notify :: new ( ) ) ;
41+ let files = builder
42+ . files ( )
43+ . iter ( )
44+ . filter_map ( |path| {
45+ if path. exists ( ) {
46+ let mtime = match path. metadata ( ) {
47+ Ok ( m) => m. mtime ( ) ,
48+ Err ( e) => {
49+ warn ! ( "Failed to stat {}: {e}" , path. display( ) ) ;
50+ warn ! ( "Configuration reloading may not work" ) ;
51+ return None ;
52+ }
53+ } ;
54+ Some ( ( path. clone ( ) , mtime) )
55+ } else {
56+ None
57+ }
58+ } )
59+ . collect ( ) ;
60+
61+ Ok ( Reloader {
62+ config,
63+ builder,
64+ endpoint,
65+ grpc,
66+ paths,
67+ files,
68+ trigger,
69+ } )
70+ }
71+
2472 /// Consume the reloader into a task
2573 ///
2674 /// The resulting task will handle reloading the configuration and
@@ -91,34 +139,33 @@ impl Reloader {
91139 fn update_cache ( & mut self ) -> bool {
92140 let mut res = false ;
93141
94- for file in CONFIG_FILES {
95- let path = PathBuf :: from ( file) ;
96- if path. exists ( ) {
97- let mtime = match path. metadata ( ) {
142+ for file in self . builder . files ( ) {
143+ if file. exists ( ) {
144+ let mtime = match file. metadata ( ) {
98145 Ok ( m) => m. mtime ( ) ,
99146 Err ( e) => {
100- warn ! ( "Failed to stat {file }: {e}" ) ;
147+ warn ! ( "Failed to stat {}: {e}" , file . display ( ) ) ;
101148 warn ! ( "Configuration reloading may not work" ) ;
102149 continue ;
103150 }
104151 } ;
105- match self . files . get_mut ( & file) {
152+ match self . files . get_mut ( file) {
106153 Some ( old) if * old == mtime => { }
107154 Some ( old) => {
108- debug ! ( "Updating '{file }'" ) ;
155+ debug ! ( "Updating '{}'" , file . display ( ) ) ;
109156 res = true ;
110157 * old = mtime;
111158 }
112159 None => {
113- debug ! ( "New configuration file '{file }'" ) ;
160+ debug ! ( "New configuration file '{}'" , file . display ( ) ) ;
114161 res = true ;
115- self . files . insert ( file, mtime) ;
162+ self . files . insert ( file. clone ( ) , mtime) ;
116163 }
117164 }
118- } else if self . files . contains_key ( & file) {
119- debug ! ( "'{file }' no longer exists, removing from cache" ) ;
165+ } else if self . files . contains_key ( file) {
166+ debug ! ( "'{}' no longer exists, removing from cache" , file . display ( ) ) ;
120167 res = true ;
121- self . files . remove ( & file) ;
168+ self . files . remove ( file) ;
122169 }
123170 }
124171 res
@@ -131,7 +178,7 @@ impl Reloader {
131178 return ;
132179 }
133180
134- let new = match FactConfig :: build ( ) {
181+ let new = match self . builder . build ( ) {
135182 Ok ( config) => config,
136183 Err ( e) => {
137184 warn ! ( "Configuration reloading failed: {e}" ) ;
@@ -178,40 +225,3 @@ impl Reloader {
178225 self . config = new;
179226 }
180227}
181-
182- impl From < FactConfig > for Reloader {
183- fn from ( config : FactConfig ) -> Self {
184- let files = CONFIG_FILES
185- . iter ( )
186- . filter_map ( |path| {
187- let p = PathBuf :: from ( path) ;
188- if p. exists ( ) {
189- let mtime = match p. metadata ( ) {
190- Ok ( m) => m. mtime ( ) ,
191- Err ( e) => {
192- warn ! ( "Failed to stat {path}: {e}" ) ;
193- warn ! ( "Configuration reloading may not work" ) ;
194- return None ;
195- }
196- } ;
197- Some ( ( * path, mtime) )
198- } else {
199- None
200- }
201- } )
202- . collect ( ) ;
203- let ( endpoint, _) = watch:: channel ( config. endpoint . clone ( ) ) ;
204- let ( grpc, _) = watch:: channel ( config. grpc . clone ( ) ) ;
205- let ( paths, _) = watch:: channel ( config. paths ( ) . to_vec ( ) ) ;
206- let trigger = Arc :: new ( Notify :: new ( ) ) ;
207-
208- Reloader {
209- config,
210- endpoint,
211- grpc,
212- paths,
213- files,
214- trigger,
215- }
216- }
217- }
0 commit comments