163 lines
7.9 KiB
Markdown
Raw Normal View History

# cfgsync
`cfgsync` is a small library stack for node registration and config artifact delivery.
2026-03-12 07:57:27 +01:00
It is meant for distributed bootstrap flows where nodes register themselves with a config service, wait until artifacts are ready, fetch one payload containing the files they need, and then write those files locally before continuing startup.
2026-03-12 07:57:27 +01:00
The boundary is simple. `cfgsync` owns transport, registration storage, polling, and artifact serving. The application adapter owns readiness policy and artifact generation. That keeps the library reusable without forcing application-specific bootstrap logic into core crates.
2026-03-12 07:55:08 +01:00
## The model
There is one main way to use `cfgsync`: nodes register, the server evaluates the current registration snapshot, and the application materializer decides whether artifacts are ready yet. Once ready, cfgsync serves a single payload containing both node-local and shared files.
2026-03-12 07:55:08 +01:00
Precomputed artifacts still fit this model. They are just a special case where the materializer already knows the final outputs and uses registration only as an identity and readiness gate.
2026-03-12 07:55:08 +01:00
## Crate roles
### `cfgsync-artifacts`
2026-03-12 07:57:27 +01:00
This crate defines the file-level data model. `ArtifactFile` represents one file and `ArtifactSet` represents a group of files delivered together. If you only need to talk about files and file groups, this is the crate you use.
### `cfgsync-core`
2026-03-12 07:57:27 +01:00
This crate defines the protocol and the low-level server/client pieces. The central types are `NodeRegistration`, `RegistrationPayload`, `NodeArtifactsPayload`, `CfgsyncClient`, and the `NodeConfigSource` implementations used by the server.
2026-03-12 07:57:27 +01:00
It also defines the generic HTTP contract: nodes `POST /register`, then `POST /node` to fetch artifacts. The server responds with either a payload, `NotReady`, or `Missing`.
### `cfgsync-adapter`
2026-03-12 07:57:27 +01:00
This crate is the application-facing integration layer. The main concepts are `RegistrationSnapshot`, `RegistrationSnapshotMaterializer`, `MaterializedArtifacts`, and `MaterializationResult`.
2026-03-12 07:57:27 +01:00
The adapter answers one question: given the current registration snapshot, are artifacts ready yet, and if so, what should be served?
2026-03-12 07:55:08 +01:00
The crate also includes reusable wrappers such as `CachedSnapshotMaterializer`, `PersistingSnapshotMaterializer`, and `RegistrationConfigSource`. Static deployment-driven rendering still exists for current testing-framework consumers, but it is intentionally a secondary helper path. The main cfgsync model is registration-backed materialization.
### `cfgsync-runtime`
This crate provides the operational entrypoints. It includes client-side fetch/write helpers, server config loading, and the default `serve_cfgsync(...)` path for snapshot materializers. Use this crate when you want to run cfgsync rather than define its protocol or adapter contracts.
2026-03-12 07:55:08 +01:00
## Artifact model
2026-03-12 07:55:08 +01:00
`cfgsync` serves one node request at a time, but the adapter usually thinks in snapshots.
2026-03-12 07:57:27 +01:00
The adapter produces `MaterializedArtifacts`, which contain node-local artifacts keyed by node identifier plus optional shared artifacts delivered alongside every node. When one node requests config, cfgsync resolves that nodes local files, merges in the shared files, and returns a single payload.
2026-03-12 07:55:08 +01:00
This is why applications do not need separate “node config” and “shared config” endpoints unless they want legacy compatibility.
2026-03-12 07:55:08 +01:00
## Registration-backed flow
2026-03-12 07:55:08 +01:00
This is the main integration path.
2026-03-12 07:57:27 +01:00
The node sends a `NodeRegistration` containing a stable identifier, an IP address, and optional typed application metadata. That metadata is opaque to cfgsync itself and is only interpreted by the application adapter.
2026-03-12 07:57:27 +01:00
The server stores registrations and builds a `RegistrationSnapshot`. The application implements `RegistrationSnapshotMaterializer` and decides whether the current snapshot is ready, which node-local artifacts should be produced, and which shared artifacts should accompany them.
2026-03-12 07:55:08 +01:00
If the materializer returns `NotReady`, cfgsync responds accordingly and the client can retry later. If it returns `Ready`, cfgsync serves the resolved artifact payload.
## Precomputed artifacts
2026-03-12 07:55:08 +01:00
Some consumers know the full artifact set ahead of time. That case still fits the same registration-backed model: the server starts with precomputed `MaterializedArtifacts`, nodes register, and cfgsync serves the right payload once the registration is acceptable.
2026-03-12 07:55:08 +01:00
The important point is that precomputed artifacts are not a separate public workflow anymore. They are one way to back the same registration/materialization protocol.
2026-03-12 07:55:08 +01:00
## Example: typed registration metadata
```rust
use cfgsync_core::NodeRegistration;
#[derive(serde::Serialize)]
struct MyNodeMetadata {
network_port: u16,
api_port: u16,
}
let registration = NodeRegistration::new("node-1", "127.0.0.1".parse().unwrap())
.with_metadata(&MyNodeMetadata {
network_port: 3000,
api_port: 18080,
})?;
```
2026-03-12 07:55:08 +01:00
## Example: snapshot materializer
```rust
use cfgsync_adapter::{
2026-03-12 07:44:20 +01:00
DynCfgsyncError, MaterializationResult, MaterializedArtifacts, RegistrationSnapshot,
RegistrationSnapshotMaterializer,
};
2026-03-12 07:44:20 +01:00
use cfgsync_artifacts::{ArtifactFile, ArtifactSet};
struct MyMaterializer;
impl RegistrationSnapshotMaterializer for MyMaterializer {
fn materialize_snapshot(
&self,
registrations: &RegistrationSnapshot,
2026-03-12 07:44:20 +01:00
) -> Result<MaterializationResult, DynCfgsyncError> {
if registrations.len() < 2 {
2026-03-12 07:44:20 +01:00
return Ok(MaterializationResult::NotReady);
}
2026-03-12 07:55:08 +01:00
let nodes = registrations.iter().map(|registration| {
(
2026-03-12 07:44:20 +01:00
registration.identifier.clone(),
ArtifactSet::new(vec![ArtifactFile::new(
"/config.yaml",
format!("id: {}\n", registration.identifier),
2026-03-12 07:44:20 +01:00
)]),
2026-03-12 07:55:08 +01:00
)
});
2026-03-12 07:44:20 +01:00
Ok(MaterializationResult::ready(
MaterializedArtifacts::from_nodes(nodes),
))
}
}
```
2026-03-12 07:55:08 +01:00
## Example: serving cfgsync
```rust
use cfgsync_runtime::serve_cfgsync;
# async fn run() -> anyhow::Result<()> {
serve_cfgsync(4400, MyMaterializer).await?;
# Ok(())
# }
```
A standalone version of this example lives in `cfgsync/runtime/examples/minimal_cfgsync.rs`.
2026-03-12 07:55:08 +01:00
## Example: fetching artifacts
```rust
use cfgsync_runtime::{ArtifactOutputMap, fetch_and_write_artifacts};
# async fn run(registration: cfgsync_core::NodeRegistration) -> anyhow::Result<()> {
let outputs = ArtifactOutputMap::new()
.route("/config.yaml", "/node-data/node-1/config.yaml")
.route("deployment-settings.yaml", "/node-data/shared/deployment-settings.yaml");
fetch_and_write_artifacts(&registration, "http://127.0.0.1:4400", &outputs).await?;
# Ok(())
# }
```
## What belongs in the adapter
2026-03-12 07:57:27 +01:00
The adapter should own the application-specific parts of bootstrap: the registration payload type, the readiness rule, the conversion from registration snapshots into artifacts, and any shared artifact generation your app needs. In practice that means things like waiting for `n` initial nodes, deriving peer lists from registrations, building node-local config files, or generating one shared deployment file for all nodes.
2026-03-12 07:55:08 +01:00
## What does not belong in cfgsync core
2026-03-12 07:57:27 +01:00
Do not push application-specific topology semantics, genesis or deployment generation, command/state-machine logic, or domain-specific ideas of what a node means into generic cfgsync. Those belong in the adapter or the consuming application.
2026-03-12 07:55:08 +01:00
## Recommended integration path
If you are integrating a new app, the shortest sensible path is to define a typed registration payload, implement `RegistrationSnapshotMaterializer`, return node-local and optional shared artifacts, serve them with `serve_cfgsync(...)`, and use `CfgsyncClient` or the runtime helpers on the node side. That gives you the main library value without forcing extra application logic into cfgsync itself.
## Compatibility
2026-03-12 07:55:08 +01:00
The primary supported surface is what is reexported from the crate roots.
2026-03-12 07:55:08 +01:00
Some older names and compatibility paths still exist internally, but they are not the intended public API.