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
+93 -2
View File
@@ -4,7 +4,9 @@ use serde::Deserialize;
use serde::de::DeserializeOwned;
use thiserror::Error;
use crate::{ConfigError, DeploymentConfig, Resource};
use crate::{
Address, ConfigError, DOCUMENTS, DeploymentConfig, ObjectStorage, Resource, SqlCluster,
};
#[derive(Clone, Copy, Debug)]
pub enum ConfigFormat {
@@ -52,7 +54,10 @@ impl ConfigFormat {
impl DeploymentConfig {
pub fn parse(input: &str, format: ConfigFormat) -> Result<Self, FormatError> {
format.parse(input)
format
.parse::<DeploymentInput>(input)?
.try_into()
.map_err(Into::into)
}
pub fn load(path: impl AsRef<Path>) -> Result<Self, FormatError> {
@@ -108,6 +113,92 @@ impl DeploymentConfig {
}
}
// Compatibility for the older, category-specific on-disk layout. Normalize once on load.
#[derive(Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub(crate) struct DeploymentInput {
deployment: Option<String>,
config_version: Option<String>,
resources: BTreeMap<String, Resource>,
bindings: BTreeMap<String, String>,
read_only: bool,
cluster: Option<String>,
clusters: BTreeMap<String, SqlCluster>,
sql: BTreeMap<String, String>,
object_storage: BTreeMap<String, ObjectStorage>,
sqlite_path: Option<Address>,
}
impl TryFrom<DeploymentInput> for DeploymentConfig {
type Error = ConfigError;
fn try_from(input: DeploymentInput) -> Result<Self, Self::Error> {
let defaults = Self::default();
let mut config = Self {
deployment: input.deployment.unwrap_or(defaults.deployment),
config_version: input.config_version.unwrap_or(defaults.config_version),
resources: input.resources,
bindings: input.bindings,
read_only: input.read_only,
};
let has_canonical_config = !config.resources.is_empty() || !config.bindings.is_empty();
let has_legacy_sql_targets = !input.clusters.is_empty();
for (name, resource) in input
.clusters
.into_iter()
.map(|(name, cluster)| (name, Resource::Sql(cluster)))
.chain(
input
.object_storage
.into_iter()
.map(|(name, store)| (name, Resource::ObjectStore(store))),
)
{
if config.resources.insert(name, resource).is_some() {
return Err(ConfigError::DuplicateResource);
}
}
let mut sql = input.sql;
if let Some(name) = input.cluster {
if sql.get(DOCUMENTS).is_some_and(|target| target != &name) {
return Err(ConfigError::ConflictingDocuments);
}
sql.insert(DOCUMENTS.into(), name);
}
if !has_canonical_config && has_legacy_sql_targets && sql.is_empty() {
return Err(ConfigError::ClusterRequired);
}
for (role, target) in sql {
if !matches!(config.resources.get(&target), Some(Resource::Sql(_))) {
return Err(ConfigError::UnknownCluster);
}
if config
.bindings
.get(&role)
.is_some_and(|existing| existing != &target)
{
return Err(ConfigError::ConflictingBinding(role));
}
config.bindings.insert(role, target);
}
if !has_canonical_config
&& config.bindings.is_empty()
&& (input.sqlite_path.is_some() || !config.resources.is_empty())
{
let mut name = String::from("local");
while config.resources.contains_key(&name) {
name.push('-');
}
let local = input
.sqlite_path
.map_or_else(Resource::default_documents, Resource::local_documents);
config.resources.insert(name.clone(), local);
config.bindings.insert(DOCUMENTS.into(), name);
}
Ok(config)
}
}
#[derive(Deserialize)]
struct TerraformOutputs {
bedrock_resources: Option<TerraformOutput>,