1
- use crate :: { postgres:: Postgres , Config , Error , Metrics , Result } ;
2
1
use futures:: StreamExt ;
3
2
use kube:: {
4
3
api:: { Patch , PatchParams } ,
@@ -17,6 +16,8 @@ use serde_json::json;
17
16
use std:: { sync:: Arc , time:: Duration } ;
18
17
use tracing:: error;
19
18
19
+ use crate :: { postgres:: Postgres , Config , Error , Metrics , State } ;
20
+
20
21
pub static DB_SYNC_PORT_FINALIZER : & str = "dbsyncports.demeter.run" ;
21
22
22
23
struct Context {
@@ -33,15 +34,6 @@ impl Context {
33
34
}
34
35
}
35
36
}
36
- #[ derive( Clone , Default ) ]
37
- pub struct State {
38
- registry : prometheus:: Registry ,
39
- }
40
- impl State {
41
- pub fn metrics ( & self ) -> Vec < prometheus:: proto:: MetricFamily > {
42
- self . registry . gather ( )
43
- }
44
- }
45
37
46
38
#[ derive( Debug , Clone , Deserialize , Serialize , JsonSchema ) ]
47
39
pub enum Network {
@@ -72,7 +64,7 @@ impl DbSyncPort {
72
64
. unwrap_or ( false )
73
65
}
74
66
75
- async fn reconcile ( & self , ctx : Arc < Context > , pg : & mut Postgres ) -> Result < Action > {
67
+ async fn reconcile ( & self , ctx : Arc < Context > , pg : & mut Postgres ) -> Result < Action , Error > {
76
68
let client = ctx. client . clone ( ) ;
77
69
let ns = self . namespace ( ) . unwrap ( ) ;
78
70
let name = self . name_any ( ) ;
@@ -107,15 +99,15 @@ impl DbSyncPort {
107
99
Ok ( Action :: requeue ( Duration :: from_secs ( 5 * 60 ) ) )
108
100
}
109
101
110
- async fn cleanup ( & self , ctx : Arc < Context > , pg : & mut Postgres ) -> Result < Action > {
102
+ async fn cleanup ( & self , ctx : Arc < Context > , pg : & mut Postgres ) -> Result < Action , Error > {
111
103
let username = self . status . as_ref ( ) . unwrap ( ) . username . clone ( ) ;
112
104
pg. user_disable ( & username) . await ?;
113
105
ctx. metrics . count_user_deactivated ( & username) ;
114
106
Ok ( Action :: await_change ( ) )
115
107
}
116
108
}
117
109
118
- async fn reconcile ( crd : Arc < DbSyncPort > , ctx : Arc < Context > ) -> Result < Action > {
110
+ async fn reconcile ( crd : Arc < DbSyncPort > , ctx : Arc < Context > ) -> Result < Action , Error > {
119
111
let url = match crd. spec . network {
120
112
Network :: Mainnet => & ctx. config . db_url_mainnet ,
121
113
Network :: Preprod => & ctx. config . db_url_preprod ,
@@ -143,11 +135,11 @@ fn error_policy(crd: Arc<DbSyncPort>, err: &Error, ctx: Arc<Context>) -> Action
143
135
Action :: requeue ( Duration :: from_secs ( 5 ) )
144
136
}
145
137
146
- pub async fn run ( state : State , config : Config ) -> Result < ( ) , Error > {
138
+ pub async fn run ( state : Arc < State > , config : Config ) -> Result < ( ) , Error > {
147
139
let client = Client :: try_default ( ) . await ?;
148
140
let crds = Api :: < DbSyncPort > :: all ( client. clone ( ) ) ;
149
- let metrics = Metrics :: default ( ) . register ( & state . registry ) . unwrap ( ) ;
150
- let ctx = Context :: new ( client, metrics, config) ;
141
+
142
+ let ctx = Context :: new ( client, state . metrics . clone ( ) , config) ;
151
143
152
144
Controller :: new ( crds, WatcherConfig :: default ( ) . any_semantic ( ) )
153
145
. shutdown_on_signal ( )
0 commit comments