-
-
Notifications
You must be signed in to change notification settings - Fork 17
feat: Add generic database connection mechanism #1163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
df6084b
f8a0b62
1dc73a1
c581a5b
32c0ec9
b9a16a9
b51f227
159faac
9bb1e95
c2799fa
69fbb2a
675dcc8
f457c20
7c193c4
9e31680
33ae519
cfa085d
8e05224
49f8ba0
3e391ff
a21b8bd
51cfff6
43730e2
75efe83
cb04eb0
9fb9d8d
0f81de5
357a288
6242c31
62c019d
656346b
83907d7
31cc9f5
c6bdce6
205d9a5
a3d1e6e
8618adf
d445d37
655e4ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| use k8s_openapi::api::core::v1::{EnvVar, EnvVarSource, SecretKeySelector}; | ||
|
|
||
| pub fn env_var_from_secret( | ||
| env_var_name: impl Into<String>, | ||
| secret_name: impl Into<String>, | ||
| secret_key: impl Into<String>, | ||
| ) -> EnvVar { | ||
| EnvVar { | ||
| name: env_var_name.into(), | ||
| value_from: Some(EnvVarSource { | ||
| secret_key_ref: Some(SecretKeySelector { | ||
| name: secret_name.into(), | ||
| key: secret_key.into(), | ||
| ..Default::default() | ||
| }), | ||
| ..Default::default() | ||
| }), | ||
| ..Default::default() | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ use crate::{ | |
| }; | ||
|
|
||
| pub mod container; | ||
| pub mod env; | ||
| pub mod probe; | ||
| pub mod resources; | ||
| pub mod security; | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: I find it awkward that we have
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at the list of derby, mysql, postgres and redis I think |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| use std::path::PathBuf; | ||
|
|
||
| use schemars::JsonSchema; | ||
| use serde::{Deserialize, Serialize}; | ||
| use snafu::{OptionExt, ResultExt, Snafu}; | ||
|
|
||
| use crate::{ | ||
| database_connections::{ | ||
| TemplatingMechanism, | ||
| drivers::jdbc::{JdbcDatabaseConnection, JdbcDatabaseConnectionDetails}, | ||
| }, | ||
| utils::OptionExt as _, | ||
| }; | ||
|
|
||
| #[derive(Debug, Snafu)] | ||
| pub enum Error { | ||
| #[snafu(display("failed to parse connection URL"))] | ||
| ParseConnectionUrl { source: url::ParseError }, | ||
|
|
||
| #[snafu(display("invalid derby database location, likely as it contains non-utf8 characters"))] | ||
| NonUtf8Location { location: PathBuf }, | ||
| } | ||
|
|
||
| /// Connection settings for an embedded [Apache Derby](https://db.apache.org/derby/) database. | ||
| /// | ||
| /// Derby is an embedded, file-based Java database engine that requires no separate server process. | ||
| /// It is typically used for development, testing, or as a lightweight metastore backend (e.g. for | ||
| /// Apache Hive). | ||
| #[derive(Clone, Debug, Deserialize, JsonSchema, PartialEq, Serialize)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct DerbyConnection { | ||
| /// Path on the filesystem where Derby stores its database files. | ||
| /// | ||
| /// If not specified, defaults to `/tmp/derby/{unique_database_name}/derby.db`. | ||
| /// The `{unique_database_name}` part is automatically handled by the operator and is added to | ||
| /// prevent clashing database files. The `create=true` flag is always appended to the JDBC URL, | ||
| /// so the database is created automatically if it does not yet exist at this location. | ||
| pub location: Option<PathBuf>, | ||
| } | ||
|
|
||
| impl JdbcDatabaseConnection for DerbyConnection { | ||
| fn jdbc_connection_details_with_templating( | ||
| &self, | ||
| unique_database_name: &str, | ||
| _templating_mechanism: &TemplatingMechanism, | ||
| ) -> Result<JdbcDatabaseConnectionDetails, crate::database_connections::Error> { | ||
| let location = self.location.as_ref_or_else(|| { | ||
| PathBuf::from(format!("/tmp/derby/{unique_database_name}/derby.db")) | ||
| }); | ||
| let location = location.to_str().with_context(|| NonUtf8LocationSnafu { | ||
| location: location.to_path_buf(), | ||
| })?; | ||
| let connection_uri = format!("jdbc:derby:{location};create=true",); | ||
| let connection_uri = connection_uri.parse().context(ParseConnectionUrlSnafu)?; | ||
|
|
||
| Ok(JdbcDatabaseConnectionDetails { | ||
| // Sadly the Derby driver class name is a bit complicated, e.g. for HMS up to 4.1.x we used | ||
| // "org.apache.derby.jdbc.EmbeddedDriver", | ||
| // for HMS 4.2.x we used "org.apache.derby.iapi.jdbc.AutoloadedDriver". | ||
| driver: "org.apache.derby.jdbc.EmbeddedDriver".to_owned(), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: These well-known driver names (for JDBC) should live in (associated) constants instead. |
||
| connection_uri, | ||
| username_env: None, | ||
| password_env: None, | ||
| }) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| pub mod derby; | ||
| pub mod mysql; | ||
| pub mod postgresql; | ||
| pub mod redis; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: I think this is not an appropriate name. We are not constructing an env var from a Kubernetes secret (which would need to be looked up), but we instead prepare the env var in such a way that it will source its value from a Secret (which the Kubernetes apiserver or the kubelet is responsible for).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WDYT of
env_var_with_value_from_secret? That matches thevalueFromattribute Kubernetes uses