-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathconfig.rs
More file actions
152 lines (133 loc) · 5.6 KB
/
Copy pathconfig.rs
File metadata and controls
152 lines (133 loc) · 5.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::collections::BTreeMap;
use stackable_operator::{
client::Client,
k8s_openapi::api::core::v1::{
ConfigMapKeySelector, EnvVar, EnvVarSource, SecretKeySelector, Volume, VolumeMount,
},
v2::types::kubernetes::NamespaceName,
};
use crate::{
catalog::{FromTrinoCatalogError, ToCatalogConfig},
crd::catalog::{TrinoCatalogConnector, TrinoCatalogName, v1alpha1},
};
#[derive(Clone, Debug)]
pub struct CatalogConfig {
/// Name of the catalog
pub name: TrinoCatalogName,
/// Properties of the catalog
pub properties: BTreeMap<String, String>,
/// List of EnvVar that will be added to every Trino container
pub env_bindings: Vec<EnvVar>,
/// Env-Vars that should be exported.
/// The value will be read from the file specified.
/// You can think of it like `export <key>="$(cat <value>)"`
pub load_env_from_files: BTreeMap<String, String>,
/// Additional commands that needs to be executed before starting Trino
pub init_container_extra_start_commands: Vec<String>,
/// Volumes that need to be added to the pod (e.g. for S3 credentials)
pub volumes: Vec<Volume>,
/// Volume mounts that need to be added to the Trino container (e.g. for S3 credentials)
pub volume_mounts: Vec<VolumeMount>,
}
impl CatalogConfig {
pub fn new(name: &TrinoCatalogName, connector_name: impl Into<String>) -> Self {
let mut config = CatalogConfig {
name: name.clone(),
properties: BTreeMap::new(),
env_bindings: Vec::new(),
load_env_from_files: BTreeMap::new(),
init_container_extra_start_commands: Vec::new(),
volumes: Vec::new(),
volume_mounts: Vec::new(),
};
config.add_property("connector.name", connector_name);
config
}
pub fn add_property(&mut self, property: impl Into<String>, value: impl Into<String>) {
self.properties.insert(property.into(), value.into());
}
pub fn add_env_property_from_file(
&mut self,
property: impl Into<String>,
file_name: impl Into<String>,
) {
let property = property.into();
let env_name = calculate_env_name(&self.name, &property);
self.add_property(&property, format!("${{ENV:{env_name}}}"));
self.load_env_from_files.insert(env_name, file_name.into());
}
pub fn add_env_property_from_secret(
&mut self,
property: impl Into<String>,
secret_key_selector: SecretKeySelector,
) {
let property = property.into();
let env_name = calculate_env_name(&self.name, &property);
self.env_bindings.push(EnvVar {
name: env_name.clone(),
value_from: Some(EnvVarSource {
secret_key_ref: Some(secret_key_selector),
..Default::default()
}),
..Default::default()
});
self.add_property(&property, format!("${{ENV:{env_name}}}"));
}
pub fn add_env_property_from_config_map(
&mut self,
property: impl Into<String>,
config_map_key_selector: ConfigMapKeySelector,
) {
let property = property.into();
let env_name = calculate_env_name(&self.name, &property);
self.env_bindings.push(EnvVar {
name: env_name.clone(),
value_from: Some(EnvVarSource {
config_map_key_ref: Some(config_map_key_selector),
..Default::default()
}),
..Default::default()
});
self.add_property(&property, format!("${{ENV:{env_name}}}"));
}
pub async fn from_catalog(
catalog_name: &TrinoCatalogName,
catalog: &v1alpha1::TrinoCatalog,
client: &Client,
catalog_namespace: &NamespaceName,
trino_version: u16,
) -> Result<CatalogConfig, FromTrinoCatalogError> {
let to_catalog_config: &dyn ToCatalogConfig = match &catalog.spec.connector {
TrinoCatalogConnector::BlackHole(black_hole_connector) => black_hole_connector,
TrinoCatalogConnector::DeltaLake(delta_lake_connector) => delta_lake_connector,
TrinoCatalogConnector::GoogleSheet(google_sheet_connector) => google_sheet_connector,
TrinoCatalogConnector::Generic(generic_connector) => generic_connector,
TrinoCatalogConnector::Hive(hive_connector) => hive_connector,
TrinoCatalogConnector::Iceberg(iceberg_connector) => iceberg_connector,
TrinoCatalogConnector::Postgresql(postgresql_connector) => postgresql_connector,
TrinoCatalogConnector::Tpcds(tpcds_connector) => tpcds_connector,
TrinoCatalogConnector::Tpch(tpch_connector) => tpch_connector,
};
let mut catalog_config = to_catalog_config
.to_catalog_config(catalog_name, catalog_namespace, client, trino_version)
.await?;
catalog_config
.properties
.extend(catalog.spec.config_overrides.clone());
for removal in &catalog.spec.config_removals {
if catalog_config.properties.remove(removal).is_none() {
tracing::warn!(
catalog.name = %catalog_name,
property = removal,
"You asked to remove a non-existing config property from a catalog"
);
}
}
Ok(catalog_config)
}
}
fn calculate_env_name(catalog_name: &TrinoCatalogName, property: impl Into<String>) -> String {
let catalog = catalog_name.to_string().replace(['.', '-'], "_");
let property = property.into().replace(['.', '-'], "_");
format!("CATALOG_{catalog}_{property}").to_uppercase()
}