@@ -4,40 +4,37 @@ use tokio::io::{AsyncRead, AsyncWrite};
4
4
5
5
use crate :: { BytesBody , HttpForwarderService } ;
6
6
7
- pub const LABEL_KEY_VAULT_ACTIVE : & str = "vault-active" ;
8
- pub const LABEL_KEY_VAULT_SEALED : & str = "vault-sealed" ;
9
-
10
- pub fn list_vault_pods ( ) -> ListParams {
11
- ListParams :: default ( ) . labels ( "app.kubernetes.io/name=vault" )
7
+ pub fn list_vault_pods ( flavor : & str ) -> ListParams {
8
+ ListParams :: default ( ) . labels ( & format ! ( "app.kubernetes.io/name={}" , flavor) )
12
9
}
13
10
14
11
/// Check if the vault pod is sealed based on its labels
15
12
/// Returns an error if the pod does not have the expected labels
16
- pub fn is_sealed ( pod : & Pod ) -> anyhow:: Result < bool > {
13
+ pub fn is_sealed ( pod : & Pod , flavor : & str ) -> anyhow:: Result < bool > {
17
14
match pod. metadata . labels . as_ref ( ) {
18
15
None => Err ( anyhow:: anyhow!( "pod does not have labels" ) ) ,
19
- Some ( labels) => match labels. get ( LABEL_KEY_VAULT_SEALED ) {
16
+ Some ( labels) => match labels. get ( & format ! ( "{}-sealed" , flavor ) ) {
20
17
Some ( x) if x. as_str ( ) == "true" => Ok ( true ) ,
21
18
Some ( x) if x. as_str ( ) == "false" => Ok ( false ) ,
22
19
_ => Err ( anyhow:: anyhow!(
23
20
"pod does not have a {} label" ,
24
- LABEL_KEY_VAULT_SEALED
21
+ & format! ( "{}-sealed" , flavor )
25
22
) ) ,
26
23
} ,
27
24
}
28
25
}
29
26
30
27
/// Check if the vault pod is active based on its labels
31
28
/// Returns an error if the pod does not have the expected labels
32
- pub fn is_active ( pod : & Pod ) -> anyhow:: Result < bool > {
29
+ pub fn is_active ( pod : & Pod , flavor : & str ) -> anyhow:: Result < bool > {
33
30
match pod. metadata . labels . as_ref ( ) {
34
31
None => Err ( anyhow:: anyhow!( "pod does not have labels" ) ) ,
35
- Some ( labels) => match labels. get ( LABEL_KEY_VAULT_ACTIVE ) {
32
+ Some ( labels) => match labels. get ( & format ! ( "{}-active" , flavor ) ) {
36
33
Some ( x) if x. as_str ( ) == "true" => Ok ( true ) ,
37
34
Some ( x) if x. as_str ( ) == "false" => Ok ( false ) ,
38
35
_ => Err ( anyhow:: anyhow!(
39
36
"pod does not have a {} label" ,
40
- LABEL_KEY_VAULT_ACTIVE
37
+ & format! ( "{}-active" , flavor )
41
38
) ) ,
42
39
} ,
43
40
}
@@ -49,11 +46,50 @@ pub struct PodApi {
49
46
pub api : Api < Pod > ,
50
47
tls : bool ,
51
48
domain : String ,
49
+ pub flavor : Flavor ,
50
+ }
51
+
52
+ #[ derive( Clone , Copy , Debug , PartialEq , Eq ) ]
53
+ pub enum Flavor {
54
+ OpenBao ,
55
+ Vault ,
56
+ }
57
+
58
+ impl std:: fmt:: Display for Flavor {
59
+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
60
+ match self {
61
+ Flavor :: OpenBao => write ! ( f, "openbao" ) ,
62
+ Flavor :: Vault => write ! ( f, "vault" ) ,
63
+ }
64
+ }
65
+ }
66
+
67
+ impl std:: str:: FromStr for Flavor {
68
+ type Err = anyhow:: Error ;
69
+
70
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
71
+ match s. to_lowercase ( ) . as_str ( ) {
72
+ "openbao" => Ok ( Flavor :: OpenBao ) ,
73
+ "vault" => Ok ( Flavor :: Vault ) ,
74
+ _ => Err ( anyhow:: anyhow!( "invalid flavor: {}" , s) ) ,
75
+ }
76
+ }
77
+ }
78
+
79
+ impl Flavor {
80
+ pub fn container_name ( & self ) -> String {
81
+ self . to_string ( )
82
+ }
52
83
}
53
84
54
85
impl PodApi {
55
- pub fn new ( api : Api < Pod > , tls : bool , domain : String ) -> Self {
56
- Self { api, tls, domain }
86
+ pub fn new ( api : Api < Pod > , tls : bool , domain : String , flavor : Flavor ) -> Self {
87
+ Self {
88
+ api,
89
+ tls,
90
+ domain,
91
+ flavor,
92
+ }
57
93
}
58
94
}
59
95
@@ -91,10 +127,11 @@ impl PodApi {
91
127
/// Wrapper around the kube::Api type for the Vault statefulset
92
128
pub struct StatefulSetApi {
93
129
pub api : Api < StatefulSet > ,
130
+ pub flavor : Flavor ,
94
131
}
95
132
96
- impl From < Api < StatefulSet > > for StatefulSetApi {
97
- fn from ( api : Api < StatefulSet > ) -> Self {
98
- Self { api }
133
+ impl StatefulSetApi {
134
+ pub fn new ( api : Api < StatefulSet > , flavor : Flavor ) -> Self {
135
+ Self { api, flavor }
99
136
}
100
137
}
0 commit comments