@@ -4,40 +4,37 @@ use tokio::io::{AsyncRead, AsyncWrite};
44
55use crate :: { BytesBody , HttpForwarderService } ;
66
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) )
129}
1310
1411/// Check if the vault pod is sealed based on its labels
1512/// 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 > {
1714 match pod. metadata . labels . as_ref ( ) {
1815 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 ) ) {
2017 Some ( x) if x. as_str ( ) == "true" => Ok ( true ) ,
2118 Some ( x) if x. as_str ( ) == "false" => Ok ( false ) ,
2219 _ => Err ( anyhow:: anyhow!(
2320 "pod does not have a {} label" ,
24- LABEL_KEY_VAULT_SEALED
21+ & format! ( "{}-sealed" , flavor )
2522 ) ) ,
2623 } ,
2724 }
2825}
2926
3027/// Check if the vault pod is active based on its labels
3128/// 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 > {
3330 match pod. metadata . labels . as_ref ( ) {
3431 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 ) ) {
3633 Some ( x) if x. as_str ( ) == "true" => Ok ( true ) ,
3734 Some ( x) if x. as_str ( ) == "false" => Ok ( false ) ,
3835 _ => Err ( anyhow:: anyhow!(
3936 "pod does not have a {} label" ,
40- LABEL_KEY_VAULT_ACTIVE
37+ & format! ( "{}-active" , flavor )
4138 ) ) ,
4239 } ,
4340 }
@@ -49,11 +46,50 @@ pub struct PodApi {
4946 pub api : Api < Pod > ,
5047 tls : bool ,
5148 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+ }
5283}
5384
5485impl 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+ }
5793 }
5894}
5995
@@ -91,10 +127,11 @@ impl PodApi {
91127/// Wrapper around the kube::Api type for the Vault statefulset
92128pub struct StatefulSetApi {
93129 pub api : Api < StatefulSet > ,
130+ pub flavor : Flavor ,
94131}
95132
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 }
99136 }
100137}
0 commit comments