-
-
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
Open
sbernauer
wants to merge
49
commits into
main
Choose a base branch
from
spike/generic-databases
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
49 commits
Select commit
Hold shift + click to select a range
df6084b
WIP
sbernauer f8a0b62
postgres -> postgresql
sbernauer 1dc73a1
Store username and password envs separately
sbernauer c581a5b
Better name modules
sbernauer 32c0ec9
Remove leftover code
sbernauer b9a16a9
Switch to config-utils templating
sbernauer b51f227
Add TODO marker
sbernauer 159faac
Add MySQL
sbernauer 9bb1e95
Improve Derby JDBC driver
sbernauer c2799fa
empty commit
sbernauer 69fbb2a
Auto-create Derby database
sbernauer 675dcc8
Merge remote-tracking branch 'origin/main' into spike/generic-databases
sbernauer f457c20
Update module structure
sbernauer 7c193c4
clippy
sbernauer 9e31680
cargo fmt
sbernauer 33ae519
Implement CeleryConnection for Postgresql
sbernauer cfa085d
Support specifying the templating mechanism
sbernauer 8e05224
fix: Use $VAR instead of ${VAR} (for Airflow)
sbernauer 49f8ba0
Revert "fix: Use $VAR instead of ${VAR} (for Airflow)"
sbernauer 3e391ff
Add lots of docs
sbernauer a21b8bd
Merge branch 'main' into spike/generic-databases
sbernauer 51cfff6
Merge branch 'main' into spike/generic-databases
sbernauer 43730e2
Improve docs
sbernauer 75efe83
Merge branch 'main' into spike/generic-databases
maltesander cb04eb0
Apply suggestions from code review
sbernauer 9fb9d8d
refactor!: Take IntoIterator<Item = EnvVar> instead
sbernauer 0f81de5
Remove too specific JDBC
sbernauer 357a288
JDBC -> Jdbc and SQLAlchemy -> SqlAlchemy
sbernauer 6242c31
Update crates/stackable-operator/src/databases/databases/derby.rs
sbernauer 62c019d
fix compilation
sbernauer 656346b
refactor: Let connection_parameters_as_url_query_parameters return Op…
sbernauer 83907d7
Put "org.postgresql.Driver" behind POSTGRES_JDBC_DRIVER_CLASS constant
sbernauer 31cc9f5
Merge remote-tracking branch 'origin/main' into spike/generic-databases
sbernauer c6bdce6
refactor: Switch derby location from String to PathBuf
sbernauer 205d9a5
Clarify what add_to_container does
sbernauer a3d1e6e
Remove bullet points
sbernauer 8618adf
Add docs on TemplatingMechanism
sbernauer d445d37
Rename "databases" module to "database_connections"
sbernauer 655e4ff
Explain context(false) and improve error messages
sbernauer 20ad3e6
Add TODO on connection param escaping
sbernauer 9887e7a
Add comment on airflow-op switching to config files
sbernauer e92a4ea
Merge remote-tracking branch 'origin/main' into spike/generic-databases
sbernauer f1afb15
Address field name feedback; URI -> URL
sbernauer cabe070
Merge branch 'main' into spike/generic-databases
sbernauer a884e67
Leftovers I forgot to commit
sbernauer 8c8615e
Rename env_var_from_secret to env_var_with_value_from_secret
sbernauer 5f988fc
Add comment on snafu error
sbernauer 106dba4
Move Derby JDBC driver into consant
sbernauer e9e9011
Merge remote-tracking branch 'origin/main' into spike/generic-databases
sbernauer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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_with_value_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() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
crates/stackable-operator/src/database_connections/databases/derby.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| 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 _, | ||
| }; | ||
|
|
||
| /// 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`. | ||
| pub const DERBY_JDBC_DRIVER_CLASS: &str = "org.apache.derby.jdbc.EmbeddedDriver"; | ||
|
|
||
| #[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_url = format!("jdbc:derby:{location};create=true",); | ||
| let connection_url = connection_url.parse().context(ParseConnectionUrlSnafu)?; | ||
|
|
||
| Ok(JdbcDatabaseConnectionDetails { | ||
| driver: DERBY_JDBC_DRIVER_CLASS.to_owned(), | ||
| connection_url, | ||
| username_env: None, | ||
| password_env: None, | ||
| }) | ||
| } | ||
| } |
4 changes: 4 additions & 0 deletions
4
crates/stackable-operator/src/database_connections/databases/mod.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.