diff --git a/operator/Containerfile b/operator/Containerfile index 9673a0b..08e09c3 100644 --- a/operator/Containerfile +++ b/operator/Containerfile @@ -5,7 +5,8 @@ RUN cp $(which rc) . FROM rust AS builder WORKDIR /src COPY . . -RUN cargo build --release +RUN rustup toolchain install nightly +RUN cargo +nightly build --release -Z sparse-registry FROM alpine COPY --from=rc /output/rc /usr/bin/rc diff --git a/operator/src/config.rs b/operator/src/config.rs new file mode 100644 index 0000000..8804f45 --- /dev/null +++ b/operator/src/config.rs @@ -0,0 +1,36 @@ +use serde::{Deserialize, Serialize}; +use std::fs::File; + +#[derive(Debug, Deserialize, Serialize, Clone)] +#[serde(rename_all = "camelCase")] +pub struct OperatorConfig { + pub set_owner_reference: bool, +} + +pub(crate) fn read_config_from_file(path: String) -> Result { + let file = File::open(path)?; + let config: OperatorConfig = serde_json::from_reader(file)?; + Ok(config) +} + +#[cfg(test)] +mod tests { + use std::io::Write; + use tempfile::NamedTempFile; + + use crate::config::read_config_from_file; + + #[test] + fn test_read_config() { + let config_json = r#"{ +"setOwnerReference": true +} +"#; + let mut file = NamedTempFile::new().expect("Can't create a file"); + let path = file.path().to_path_buf(); + writeln!(file, "{}", config_json).expect("Can't write a config file"); + let config = read_config_from_file(path.to_str().expect("Can't get the path").to_string()) + .expect("Can't read the config file"); + assert!(config.set_owner_reference); + } +} diff --git a/operator/src/controller.rs b/operator/src/controller.rs index 5993874..9b0bbd7 100644 --- a/operator/src/controller.rs +++ b/operator/src/controller.rs @@ -2,6 +2,7 @@ mod conditions; mod controllers; mod rc; mod cli; +mod config; use crate::controllers::{rustfs_instance}; @@ -10,6 +11,7 @@ use clap::Parser; use kube::Client; use tracing_subscriber::EnvFilter; +use self::config::read_config_from_file; use self::controllers::{rustfs_bucket, rustfs_user}; /// Simple program to greet a person @@ -29,6 +31,9 @@ struct Args { /// If enabled, DB Operator will run full reconciliation only /// when changes are detected is_change_check_nabled: bool, + #[arg(long, default_value = "/src/config/config.json")] + /// A path to a config file + config: String, } #[get("/health")] @@ -45,8 +50,9 @@ async fn main() -> anyhow::Result<()> { let client = Client::try_default() .await .expect("failed to create kube Client"); + let config = read_config_from_file("test".to_string())?; let rustfs_instance_ctrl = rustfs_instance::run(client.clone()); - let rustfs_bucket_ctrl = rustfs_bucket::run(client.clone()); + let rustfs_bucket_ctrl = rustfs_bucket::run(client.clone(), config.clone()); let rustfs_user_ctrl = rustfs_user::run(client.clone()); // Start web server let server = HttpServer::new(move || { diff --git a/operator/src/controllers/rustfs_bucket.rs b/operator/src/controllers/rustfs_bucket.rs index d000f12..7b32367 100644 --- a/operator/src/controllers/rustfs_bucket.rs +++ b/operator/src/controllers/rustfs_bucket.rs @@ -1,4 +1,5 @@ use crate::conditions::{is_condition_true, set_condition}; +use crate::config::OperatorConfig; use crate::rc::{create_bucket, list_buckets}; use api::api::v1beta1_rustfs_instance::RustFSInstance; use futures::StreamExt; @@ -383,14 +384,14 @@ pub(crate) fn error_policy(_: Arc, err: &RustFSBucketError, _: Arc } #[instrument(skip(client), fields(trace_id))] -pub async fn run(client: Client) { +pub async fn run(client: Client, config: OperatorConfig) { let buckets = Api::::all(client.clone()); if let Err(err) = buckets.list(&ListParams::default().limit(1)).await { error!("{}", err); std::process::exit(1); } let recorder = Recorder::new(client.clone(), "bucket-controller".into()); - let context = Context { client, recorder }; + let context = Context { client, recorder, config }; Controller::new(buckets, Config::default().any_semantic()) .shutdown_on_signal() .run(reconcile, error_policy, Arc::new(context)) @@ -405,6 +406,7 @@ pub(crate) struct Context { pub client: Client, /// Event recorder pub recorder: Recorder, + pub(crate) config: OperatorConfig, } #[derive(Error, Debug)]