feat: add very basic in memory coordination
Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
91
crates/nocontrol/tests/mod.rs
Normal file
91
crates/nocontrol/tests/mod.rs
Normal file
@@ -0,0 +1,91 @@
|
||||
use async_trait::async_trait;
|
||||
use nocontrol::{
|
||||
manifests::{Manifest, ManifestMetadata, ManifestState},
|
||||
Operator,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tracing_test::traced_test;
|
||||
|
||||
#[tokio::test]
|
||||
#[traced_test]
|
||||
async fn test_can_run_reconciler() -> anyhow::Result<()> {
|
||||
let operator = MyOperator {};
|
||||
|
||||
let mut control_plane = nocontrol::ControlPlane::new(operator);
|
||||
control_plane.with_deadline(std::time::Duration::from_secs(3));
|
||||
|
||||
tokio::spawn({
|
||||
let control_plane = control_plane.clone();
|
||||
async move {
|
||||
control_plane
|
||||
.add_manifest(Manifest {
|
||||
name: "some-manifest".into(),
|
||||
metadata: ManifestMetadata {},
|
||||
spec: Specifications::Deployment(DeploymentControllerManifest {
|
||||
name: "some-name".into(),
|
||||
}),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
tokio::time::sleep(std::time::Duration::from_secs(1)).await;
|
||||
|
||||
control_plane
|
||||
.add_manifest(Manifest {
|
||||
name: "some-manifest".into(),
|
||||
metadata: ManifestMetadata {},
|
||||
spec: Specifications::Deployment(DeploymentControllerManifest {
|
||||
name: "some-changed-name".into(),
|
||||
}),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
});
|
||||
|
||||
control_plane.execute().await?;
|
||||
|
||||
Err(anyhow::anyhow!("fail"))
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct MyOperator {}
|
||||
|
||||
#[async_trait]
|
||||
impl Operator for MyOperator {
|
||||
type Specifications = Specifications;
|
||||
|
||||
async fn reconcile(
|
||||
&self,
|
||||
desired_manifest: &mut ManifestState<Specifications>,
|
||||
) -> anyhow::Result<()> {
|
||||
let now = jiff::Timestamp::now();
|
||||
|
||||
desired_manifest.status.status = nocontrol::manifests::ManifestStatusState::Started;
|
||||
desired_manifest.updated = now;
|
||||
|
||||
match &desired_manifest.manifest.spec {
|
||||
Specifications::Deployment(spec) => {
|
||||
tracing::info!(
|
||||
"reconciliation was called for name = {}, value = {}",
|
||||
desired_manifest.manifest.name,
|
||||
spec.name
|
||||
)
|
||||
}
|
||||
}
|
||||
desired_manifest.status.status = nocontrol::manifests::ManifestStatusState::Running;
|
||||
desired_manifest.updated = now;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Serialize)]
|
||||
pub enum Specifications {
|
||||
Deployment(DeploymentControllerManifest),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct DeploymentControllerManifest {
|
||||
name: String,
|
||||
}
|
||||
Reference in New Issue
Block a user