11use indexmap:: IndexMap ;
22use kube:: { ResourceExt , core:: GroupVersionKind } ;
33use serde:: Serialize ;
4- use snafu:: { ResultExt , Snafu } ;
4+ use snafu:: { OptionExt , ResultExt , Snafu } ;
55use stackable_operator:: status:: condition:: ClusterCondition ;
66use tracing:: info;
77#[ cfg( feature = "openapi" ) ]
88use utoipa:: ToSchema ;
99
1010use crate :: {
11- constants:: PRODUCT_NAMES ,
11+ constants:: PRODUCTS ,
1212 platform:: { credentials, service} ,
13- utils:: {
14- k8s:: { self , Client , ConditionsExt } ,
15- string:: Casing ,
16- } ,
13+ utils:: k8s:: { self , Client , ConditionsExt } ,
1714} ;
1815
1916mod grafana;
@@ -58,6 +55,9 @@ pub enum Error {
5855
5956 #[ snafu( display( "failed to receive service information" ) ) ]
6057 ServiceFetch { source : service:: Error } ,
58+
59+ #[ snafu( display( "product name {product_name:?} not found" ) ) ]
60+ GetProduct { product_name : String } ,
6161}
6262
6363/// Lists all installed stacklets. If `namespace` is [`None`], stacklets from ALL
@@ -83,7 +83,7 @@ pub async fn get_credentials_for_product(
8383 object_name : & str ,
8484 product_name : & str ,
8585) -> Result < Option < credentials:: Credentials > , Error > {
86- let product_gvk = gvk_from_product_name ( product_name) ;
86+ let product_gvk = gvk_from_product_name ( product_name) ? ;
8787 let product_cluster = match client
8888 . get_namespaced_object ( namespace, object_name, & product_gvk)
8989 . await
@@ -113,10 +113,14 @@ async fn list_stackable_stacklets(
113113 client : & Client ,
114114 namespace : Option < & str > ,
115115) -> Result < Vec < Stacklet > , Error > {
116- let product_list = build_products_gvk_list ( PRODUCT_NAMES ) ;
117116 let mut stacklets = Vec :: new ( ) ;
118117
119- for ( product_name, product_gvk) in product_list {
118+ for ( product_name, group, version, kind) in PRODUCTS {
119+ let product_gvk = GroupVersionKind {
120+ group : group. to_string ( ) ,
121+ version : version. to_string ( ) ,
122+ kind : kind. to_string ( ) ,
123+ } ;
120124 let objects = match client
121125 . list_objects ( & product_gvk, namespace)
122126 . await
@@ -164,42 +168,15 @@ async fn list_stackable_stacklets(
164168 Ok ( stacklets)
165169}
166170
167- fn build_products_gvk_list < ' a > ( product_names : & [ & ' a str ] ) -> IndexMap < & ' a str , GroupVersionKind > {
168- let mut map = IndexMap :: new ( ) ;
169-
170- for product_name in product_names {
171- // Note(techassi): Why? Just why? Can we please make this consistent?
172- // Note(sbernauer): I think it's legit that SparkHistoryServer and SparkConnectServer are in
173- // the api group spark.stackable.tech. All of this will probably be rewritten any as soon as
174- // we have versions different than v1alpha1.
175- if * product_name == "spark-history" {
176- map. insert ( * product_name, GroupVersionKind {
177- group : "spark.stackable.tech" . into ( ) ,
178- version : "v1alpha1" . into ( ) ,
179- kind : "SparkHistoryServer" . into ( ) ,
180- } ) ;
181- } else if * product_name == "spark-connect" {
182- map. insert ( * product_name, GroupVersionKind {
183- group : "spark.stackable.tech" . into ( ) ,
184- version : "v1alpha1" . into ( ) ,
185- kind : "SparkConnectServer" . into ( ) ,
186- } ) ;
187- } else {
188- map. insert ( * product_name, gvk_from_product_name ( product_name) ) ;
189- }
190- }
191-
192- map
193- }
194-
195- // FIXME: Support SparkApplication and SparkConnectServer
196- fn gvk_from_product_name ( product_name : & str ) -> GroupVersionKind {
197- GroupVersionKind {
198- group : format ! ( "{product_name}.stackable.tech" ) ,
199- version : "v1alpha1" . into ( ) ,
200- kind : format ! (
201- "{product_name}Cluster" ,
202- product_name = product_name. capitalize( )
203- ) ,
204- }
171+ fn gvk_from_product_name ( product_name : & str ) -> Result < GroupVersionKind , Error > {
172+ let ( _, group, version, kind) = PRODUCTS
173+ . iter ( )
174+ . find ( |( other_product_name, _, _, _) | product_name == * other_product_name)
175+ . context ( GetProductSnafu { product_name } ) ?;
176+
177+ Ok ( GroupVersionKind {
178+ group : group. to_string ( ) ,
179+ version : version. to_string ( ) ,
180+ kind : kind. to_string ( ) ,
181+ } )
205182}
0 commit comments