feat: move examples out

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2026-01-07 10:33:18 +01:00
parent 6bd23f3b49
commit 01f7bc9c83
6 changed files with 54 additions and 8 deletions

View File

@@ -14,17 +14,15 @@ anyhow.workspace = true
async-trait = "0.1.89"
hex = "0.4.3"
jiff = { version = "0.2.17", features = ["serde"] }
rand = "0.9.2"
serde = { version = "1.0.228", features = ["derive"] }
rand.workspace = true
serde.workspace = true
serde_json = "1.0.148"
sha2 = "0.10.9"
tokio.workspace = true
tokio-util = "0.7.18"
tracing.workspace = true
uuid = { version = "1.19.0", features = ["serde", "v4", "v7"] }
uuid.workspace = true
[dev-dependencies]
nocontrol-tui = { path = "../nocontrol-tui", version = "0.1.0" }
insta = "1.46.0"
tracing-subscriber = { version = "0.3.22", features = ["env-filter"] }
tracing-test = { version = "0.2.5", features = ["no-env-filter"] }

View File

@@ -1,120 +0,0 @@
use std::time::Duration;
use nocontrol::{
Operator, OperatorState, Specification,
manifests::{Action, Manifest, ManifestMetadata, ManifestState},
};
use serde::{Deserialize, Serialize};
use tracing_subscriber::EnvFilter;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Setup logging to file
let output_file = std::fs::File::create("target/nocontrol.log")?;
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.with_writer(output_file)
.with_file(false)
.with_line_number(false)
.with_target(false)
.without_time()
.init();
let operator = OperatorState::new(MyOperator {});
let control_plane = nocontrol::ControlPlane::new(operator);
// Add initial manifest
control_plane
.add_manifest(Manifest {
name: "initial-deployment".into(),
metadata: ManifestMetadata {},
spec: Specifications::Deployment(DeploymentControllerManifest {
name: "initial-app".into(),
}),
})
.await?;
// Spawn random manifest updater
tokio::spawn({
let control_plane = control_plane.clone();
async move {
loop {
let rand = {
use rand::Rng;
let mut rng = rand::rng();
rng.random_range(3..8)
};
tokio::time::sleep(Duration::from_secs(rand)).await;
let random = uuid::Uuid::now_v7();
let _ = control_plane
.add_manifest(Manifest {
name: "initial-deployment".into(),
metadata: ManifestMetadata {},
spec: Specifications::Deployment(DeploymentControllerManifest {
name: format!("app-{}", &random.to_string()),
}),
})
.await;
}
}
});
// Spawn control plane
tokio::spawn({
let control_plane = control_plane.clone();
async move {
let _ = control_plane.execute().await;
}
});
// Run TUI
nocontrol_tui::run(control_plane).await?;
Ok(())
}
#[derive(Clone)]
pub struct MyOperator {}
impl Operator for MyOperator {
type Specifications = Specifications;
async fn reconcile(
&self,
desired_manifest: &mut ManifestState<Specifications>,
) -> anyhow::Result<Action> {
match &desired_manifest.manifest.spec {
Specifications::Deployment(spec) => {
tracing::info!(
"reconciliation was called for name = {}, value = {}",
desired_manifest.manifest.name,
spec.name
)
}
}
Ok(Action::Requeue(std::time::Duration::from_secs(10)))
}
}
#[derive(Clone, Serialize, Deserialize)]
pub enum Specifications {
Deployment(DeploymentControllerManifest),
}
impl Specification for Specifications {
fn kind(&self) -> &'static str {
match self {
Specifications::Deployment(_) => "deployment",
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeploymentControllerManifest {
name: String,
}