Unify backend resources and role bindings in clusters

This commit is contained in:
2026-11-30 18:24:24 +08:00
parent ac62c8a655
commit 9869a9966c
18 changed files with 469 additions and 227 deletions
+122 -16
View File
@@ -1,6 +1,8 @@
use std::collections::BTreeMap;
use clusters::{Address, ConfigError, DeploymentConfig, SqlCluster, SqlDriver, Tls};
use clusters::{
Address, ConfigError, DOCUMENTS, DeploymentConfig, Resource, SqlCluster, SqlDriver, Tls,
};
#[test]
fn address_debug_formats_the_inner_string() {
@@ -11,21 +13,21 @@ fn address_debug_formats_the_inner_string() {
#[test]
fn named_cluster_requires_matching_addresses() {
let mut config = DeploymentConfig {
cluster: Some("regional".into()),
clusters: BTreeMap::from([(
bindings: BTreeMap::from([(DOCUMENTS.into(), "regional".into())]),
resources: BTreeMap::from([(
"regional".into(),
SqlCluster {
Resource::Sql(SqlCluster {
driver: SqlDriver::Postgres,
primary: "postgres://user:secret@primary/bedrock".into(),
replicas: vec!["postgresql://reader:secret@replica/bedrock".into()],
tls: Tls::VerifyFull,
max_connections: 5,
},
}),
)]),
..DeploymentConfig::default()
};
let resolved = config.resolve().unwrap();
let resolved = resolved.documents().unwrap();
let resolved = resolved.binding(DOCUMENTS).unwrap().as_sql().unwrap();
assert!(
resolved
.primary
@@ -37,8 +39,10 @@ fn named_cluster_requires_matching_addresses() {
.connection_string()
.ends_with("sslmode=verify-full")
);
config.clusters.get_mut("regional").unwrap().replicas[0] =
"mysql://user:secret@replica/bedrock".into();
let Some(Resource::Sql(cluster)) = config.resources.get_mut("regional") else {
panic!("expected a SQL resource");
};
cluster.replicas[0] = "mysql://user:secret@replica/bedrock".into();
assert!(matches!(
config.resolve(),
Err(ConfigError::Url("postgres"))
@@ -56,25 +60,57 @@ fn resolves_named_connections_and_round_trips_yaml() {
.unwrap();
let snapshot = config.resolve().unwrap();
assert_eq!(snapshot.config_version, "example-1");
assert_eq!(snapshot.documents().unwrap().driver, SqlDriver::Postgres);
assert_eq!(snapshot.sql["legacy"].driver, SqlDriver::Mysql);
assert_eq!(
snapshot.object_storage["attachments"].bucket,
snapshot
.binding(DOCUMENTS)
.unwrap()
.as_sql()
.unwrap()
.driver,
SqlDriver::Postgres
);
assert_eq!(
snapshot.binding("legacy").unwrap().as_sql().unwrap().driver,
SqlDriver::Mysql
);
assert_eq!(
snapshot
.binding("attachments")
.unwrap()
.as_object_store()
.unwrap()
.bucket,
"bedrock-attachments"
);
assert_eq!(snapshot.documents().unwrap().max_connections, 5);
assert_eq!(
snapshot
.binding(DOCUMENTS)
.unwrap()
.as_sql()
.unwrap()
.max_connections,
5
);
let serialized = config.to_yaml().unwrap();
let roundtrip = DeploymentConfig::parse(&serialized, ConfigFormat::Yaml)
.unwrap()
.resolve()
.unwrap();
assert_eq!(roundtrip.sql["legacy"].driver, SqlDriver::Mysql);
assert_eq!(
roundtrip
.binding("legacy")
.unwrap()
.as_sql()
.unwrap()
.driver,
SqlDriver::Mysql
);
}
#[test]
fn imports_catalog_atomically_and_preserves_role_references() {
use clusters::{ConfigFormat, Driver, ImportFormat, ObjectStoreDriver, Resource};
use clusters::{ConfigFormat, Driver, ImportFormat, ObjectStoreDriver};
let mut config =
DeploymentConfig::parse("config_version = 'test'", ConfigFormat::Toml).unwrap();
@@ -84,7 +120,8 @@ fn imports_catalog_atomically_and_preserves_role_references() {
ImportFormat::KubernetesConfigMap,
)
.unwrap();
config.sql.insert("documents".into(), "pg-main".into());
config.bindings.insert(DOCUMENTS.into(), "pg-main".into());
config.bindings.insert("files".into(), "attachments".into());
let before = config.resource("attachments").unwrap().driver();
assert_eq!(before, Driver::ObjectStore(ObjectStoreDriver::S3));
@@ -107,6 +144,11 @@ fn imports_catalog_atomically_and_preserves_role_references() {
.is_err()
);
assert!(config.resource("another-bucket").is_none());
assert!(matches!(
config.remove_resource("attachments"),
Err(ConfigError::ResourceInUse)
));
config.bindings.remove("files");
let Resource::ObjectStore(mut updated) = config.remove_resource("attachments").unwrap() else {
panic!("expected S3 configuration");
};
@@ -119,7 +161,71 @@ fn imports_catalog_atomically_and_preserves_role_references() {
.update_resource("attachments", Resource::ObjectStore(updated))
.unwrap();
assert_eq!(
config.resolve().unwrap().object_storage["attachments"].bucket,
config
.resolve()
.unwrap()
.resource("attachments")
.unwrap()
.as_object_store()
.unwrap()
.bucket,
"replaced"
);
}
#[test]
fn legacy_configuration_is_normalized_at_load() {
use clusters::ConfigFormat;
let config = DeploymentConfig::parse(
r#"{
"cluster":"east",
"clusters":{"east":{"driver":"postgres","primary":"postgres://u@primary/db"}},
"object_storage":{"files":{"driver":"s3","bucket":"files","region":"us-east-1"}}
}"#,
ConfigFormat::Json,
)
.unwrap();
let snapshot = config.resolve().unwrap();
assert_eq!(
snapshot
.binding(DOCUMENTS)
.unwrap()
.as_sql()
.unwrap()
.driver,
SqlDriver::Postgres
);
assert_eq!(
snapshot
.resource("files")
.unwrap()
.as_object_store()
.unwrap()
.bucket,
"files"
);
let config =
DeploymentConfig::parse("sqlite_path: custom.sqlite3", ConfigFormat::Yaml).unwrap();
let snapshot = config.resolve().unwrap();
let connection = snapshot.binding(DOCUMENTS).unwrap().as_sql().unwrap();
assert!(
connection
.primary
.connection_string()
.ends_with("/custom.sqlite3?mode=rwc")
);
}
#[test]
fn missing_binding_target_is_rejected_for_any_role() {
let mut config = DeploymentConfig::default();
config
.bindings
.insert("attachments".into(), "missing".into());
assert!(
matches!(config.resolve(), Err(ConfigError::UnknownBinding { role, target })
if role == "attachments" && target == "missing")
);
}