@@ -12,11 +12,12 @@ mod resources;
12
12
13
13
use crate :: permissionables:: Sessions ;
14
14
use clap:: Parser ;
15
- use ldap3:: LdapConnAsync ;
16
- use resources:: update_resources;
17
- use sqlx:: mysql:: MySqlPoolOptions ;
15
+ use ldap3:: { Ldap , LdapConnAsync } ;
16
+ use resources:: { create_configmap, create_namespace, delete_namespace} ;
17
+ use sqlx:: { mysql:: MySqlPoolOptions , MySqlPool } ;
18
+ use std:: collections:: BTreeSet ;
18
19
use tokio:: time:: { sleep_until, Instant } ;
19
- use tracing:: warn;
20
+ use tracing:: { info , warn} ;
20
21
use url:: Url ;
21
22
22
23
/// SessionSpaces periodically polls the authorization bundle server and applies templates to the cluster accordingly
@@ -31,9 +32,6 @@ struct Cli {
31
32
/// The period to wait after a succesful bundle server request
32
33
#[ clap( long, env, default_value = "60s" ) ]
33
34
update_interval : humantime:: Duration ,
34
- /// The period to wait after an unsuccesful bundle server request
35
- #[ arg( long, env, default_value = "10s" ) ]
36
- retry_interval : humantime:: Duration ,
37
35
/// The [`tracing::Level`] to log at
38
36
#[ arg( long, env="LOG_LEVEL" , default_value_t=tracing:: Level :: INFO ) ]
39
37
log_level : tracing:: Level ,
@@ -61,17 +59,64 @@ async fn main() {
61
59
let mut request_at = Instant :: now ( ) ;
62
60
loop {
63
61
sleep_until ( request_at) . await ;
64
- if let Ok ( new_sessions) = async {
65
- let new_sessions = Sessions :: fetch ( & ispyb_pool, & mut ldap_connection) . await ?;
66
- update_resources ( k8s_client. clone ( ) , & current_sessions, & new_sessions) . await ?;
67
- Ok :: < _ , anyhow:: Error > ( new_sessions)
68
- }
62
+ if let Ok ( ( ) ) = perform_updates (
63
+ & mut current_sessions,
64
+ & ispyb_pool,
65
+ & mut ldap_connection,
66
+ & k8s_client,
67
+ )
69
68
. await
70
69
{
71
- current_sessions = new_sessions;
72
70
request_at = request_at. checked_add ( * args. update_interval ) . unwrap ( ) ;
73
- } else {
74
- request_at = request_at. checked_add ( * args. retry_interval ) . unwrap ( ) ;
75
71
} ;
76
72
}
77
73
}
74
+
75
+ /// Fetches new [`Sessions`] and updates k8s resources according to the observed changes.
76
+ async fn perform_updates (
77
+ current_sessions : & mut Sessions ,
78
+ ispyb_pool : & MySqlPool ,
79
+ ldap_connection : & mut Ldap ,
80
+ k8s_client : & kube:: Client ,
81
+ ) -> Result < ( ) , anyhow:: Error > {
82
+ let mut new_sessions = Sessions :: fetch ( ispyb_pool, ldap_connection) . await ?;
83
+
84
+ let current_session_names = current_sessions. keys ( ) . cloned ( ) . collect :: < BTreeSet < _ > > ( ) ;
85
+ let new_session_names = new_sessions. keys ( ) . cloned ( ) . collect :: < BTreeSet < _ > > ( ) ;
86
+ let to_update = current_session_names
87
+ . union ( & new_session_names)
88
+ . collect :: < BTreeSet < _ > > ( ) ;
89
+
90
+ info ! ( "Updating {} SessionSpaces" , to_update. len( ) ) ;
91
+ for namespace in to_update. into_iter ( ) {
92
+ match (
93
+ current_sessions. get ( namespace) ,
94
+ new_sessions. remove ( namespace) ,
95
+ ) {
96
+ ( Some ( _) , None ) => {
97
+ info ! ( "Deleting Namespace: {}" , namespace) ;
98
+ delete_namespace ( namespace, k8s_client. clone ( ) ) . await ?;
99
+ current_sessions. remove ( namespace) ;
100
+ }
101
+ ( None , Some ( new_session) ) => {
102
+ info ! (
103
+ "Creating Namespace, {}, with Config: {}" ,
104
+ namespace, new_session
105
+ ) ;
106
+ create_namespace ( namespace. clone ( ) , k8s_client. clone ( ) ) . await ?;
107
+ create_configmap ( namespace, new_session. clone ( ) , k8s_client. clone ( ) ) . await ?;
108
+ current_sessions. insert ( namespace. clone ( ) , new_session) ;
109
+ }
110
+ ( Some ( current_session) , Some ( new_session) ) if current_session != & new_session => {
111
+ info ! (
112
+ "Updating Namespace, {}, with Config: {}" ,
113
+ namespace, new_session
114
+ ) ;
115
+ create_configmap ( namespace, new_session. clone ( ) , k8s_client. clone ( ) ) . await ?;
116
+ current_sessions. insert ( namespace. clone ( ) , new_session) ;
117
+ }
118
+ ( _, _) => { }
119
+ }
120
+ }
121
+ Ok ( ( ) )
122
+ }
0 commit comments