|
| 1 | +// Consolidate Trino S3 properties in a single reusable struct. |
| 2 | + |
| 3 | +use std::collections::BTreeMap; |
| 4 | + |
| 5 | +use snafu::{self, ResultExt, Snafu}; |
| 6 | +use stackable_operator::{ |
| 7 | + client::Client, |
| 8 | + k8s_openapi::api::core::v1::{Volume, VolumeMount}, |
| 9 | +}; |
| 10 | + |
| 11 | +use crate::{ |
| 12 | + config, |
| 13 | + crd::{ |
| 14 | + ENV_SPOOLING_SECRET, |
| 15 | + client_protocol::{ClientProtocolConfig, SpoolingFileSystemConfig}, |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +#[derive(Snafu, Debug)] |
| 20 | +pub enum Error { |
| 21 | + #[snafu(display("Failed to resolve S3 connection"))] |
| 22 | + ResolveS3Connection { source: config::s3::Error }, |
| 23 | + |
| 24 | + #[snafu(display("trino does not support disabling the TLS verification of S3 servers"))] |
| 25 | + S3TlsNoVerificationNotSupported, |
| 26 | + |
| 27 | + #[snafu(display("failed to convert data size for [{field}] to bytes"))] |
| 28 | + QuantityConversion { |
| 29 | + source: stackable_operator::memory::Error, |
| 30 | + field: &'static str, |
| 31 | + }, |
| 32 | +} |
| 33 | + |
| 34 | +pub struct ResolvedClientProtocolConfig { |
| 35 | + /// Properties to add to config.properties |
| 36 | + pub config_properties: BTreeMap<String, String>, |
| 37 | + |
| 38 | + // Properties for spooling-manager.properties |
| 39 | + pub spooling_manager_properties: BTreeMap<String, String>, |
| 40 | + |
| 41 | + /// Volumes required for the configuration (e.g., for S3 credentials) |
| 42 | + pub volumes: Vec<Volume>, |
| 43 | + |
| 44 | + /// Volume mounts required for the configuration |
| 45 | + pub volume_mounts: Vec<VolumeMount>, |
| 46 | + |
| 47 | + /// Additional commands that need to be executed before starting Trino |
| 48 | + /// Used to add TLS certificates to the client's trust store. |
| 49 | + pub init_container_extra_start_commands: Vec<String>, |
| 50 | +} |
| 51 | + |
| 52 | +impl ResolvedClientProtocolConfig { |
| 53 | + /// Resolve S3 connection properties from Kubernetes resources |
| 54 | + /// and prepare spooling filesystem configuration. |
| 55 | + pub async fn from_config( |
| 56 | + config: &ClientProtocolConfig, |
| 57 | + client: Option<&Client>, |
| 58 | + namespace: &str, |
| 59 | + ) -> Result<Self, Error> { |
| 60 | + let mut resolved_config = Self { |
| 61 | + config_properties: BTreeMap::new(), |
| 62 | + spooling_manager_properties: BTreeMap::new(), |
| 63 | + volumes: Vec::new(), |
| 64 | + volume_mounts: Vec::new(), |
| 65 | + init_container_extra_start_commands: Vec::new(), |
| 66 | + }; |
| 67 | + |
| 68 | + match config { |
| 69 | + ClientProtocolConfig::Spooling(spooling_config) => { |
| 70 | + // Resolve external resources if Kubernetes client is available |
| 71 | + // This should always be the case, except for when this function is called during unit tests |
| 72 | + if let Some(client) = client { |
| 73 | + match &spooling_config.filesystem { |
| 74 | + SpoolingFileSystemConfig::S3(s3_config) => { |
| 75 | + let resolved_s3_config = config::s3::ResolvedS3Config::from_config( |
| 76 | + s3_config, client, namespace, |
| 77 | + ) |
| 78 | + .await |
| 79 | + .context(ResolveS3ConnectionSnafu)?; |
| 80 | + |
| 81 | + // Enable S3 filesystem after successful resolution |
| 82 | + resolved_config |
| 83 | + .spooling_manager_properties |
| 84 | + .insert("fs.s3.enabled".to_string(), "true".to_string()); |
| 85 | + |
| 86 | + // Copy the S3 configuration over |
| 87 | + resolved_config |
| 88 | + .spooling_manager_properties |
| 89 | + .extend(resolved_s3_config.properties); |
| 90 | + resolved_config.volumes.extend(resolved_s3_config.volumes); |
| 91 | + resolved_config |
| 92 | + .volume_mounts |
| 93 | + .extend(resolved_s3_config.volume_mounts); |
| 94 | + resolved_config |
| 95 | + .init_container_extra_start_commands |
| 96 | + .extend(resolved_s3_config.init_container_extra_start_commands); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + resolved_config.spooling_manager_properties.extend([ |
| 102 | + ("fs.location".to_string(), spooling_config.location.clone()), |
| 103 | + ( |
| 104 | + "spooling-manager.name".to_string(), |
| 105 | + "filesystem".to_string(), |
| 106 | + ), |
| 107 | + ]); |
| 108 | + |
| 109 | + // Enable spooling protocol |
| 110 | + resolved_config.config_properties.extend([ |
| 111 | + ("protocol.spooling.enabled".to_string(), "true".to_string()), |
| 112 | + ( |
| 113 | + "protocol.spooling.shared-secret-key".to_string(), |
| 114 | + format!("${{ENV:{secret}}}", secret = ENV_SPOOLING_SECRET), |
| 115 | + ), |
| 116 | + ]); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + Ok(resolved_config) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +#[cfg(test)] |
| 125 | +mod tests { |
| 126 | + use super::*; |
| 127 | + use crate::crd::{client_protocol::ClientSpoolingProtocolConfig, s3::S3Config}; |
| 128 | + |
| 129 | + #[tokio::test] |
| 130 | + async fn test_spooling_config() { |
| 131 | + let config = ClientProtocolConfig::Spooling(ClientSpoolingProtocolConfig { |
| 132 | + location: "s3://my-bucket/spooling".to_string(), |
| 133 | + filesystem: SpoolingFileSystemConfig::S3(S3Config { |
| 134 | + connection: |
| 135 | + stackable_operator::crd::s3::v1alpha1::InlineConnectionOrReference::Reference( |
| 136 | + "test-s3-connection".to_string(), |
| 137 | + ), |
| 138 | + iam_role: None, |
| 139 | + external_id: None, |
| 140 | + max_error_retries: None, |
| 141 | + upload_part_size: None, |
| 142 | + }), |
| 143 | + }); |
| 144 | + |
| 145 | + let resolved_spooling_config = ResolvedClientProtocolConfig::from_config( |
| 146 | + &config, None, // No client, so no external resolution |
| 147 | + "default", |
| 148 | + ) |
| 149 | + .await |
| 150 | + .unwrap(); |
| 151 | + |
| 152 | + let expected_props = BTreeMap::from([ |
| 153 | + ( |
| 154 | + "fs.location".to_string(), |
| 155 | + "s3://my-bucket/spooling".to_string(), |
| 156 | + ), |
| 157 | + ( |
| 158 | + "spooling-manager.name".to_string(), |
| 159 | + "filesystem".to_string(), |
| 160 | + ), |
| 161 | + ]); |
| 162 | + assert_eq!( |
| 163 | + expected_props, |
| 164 | + resolved_spooling_config.spooling_manager_properties |
| 165 | + ); |
| 166 | + } |
| 167 | +} |
0 commit comments