From 3b0a46d5e1bcc3eaa750b385584221b490b1c119 Mon Sep 17 00:00:00 2001 From: Nathaniel Cook Date: Fri, 1 Dec 2023 11:38:54 -0700 Subject: [PATCH] refactor: allow ceramic spec to be optional --- operator/src/network/ceramic.rs | 14 +++++--- operator/src/network/controller.rs | 55 ++++++++++++++++-------------- operator/src/network/spec.rs | 2 +- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/operator/src/network/ceramic.rs b/operator/src/network/ceramic.rs index 40815dcb..02b11b05 100644 --- a/operator/src/network/ceramic.rs +++ b/operator/src/network/ceramic.rs @@ -358,12 +358,16 @@ impl Default for CeramicConfig { pub struct CeramicConfigs(pub Vec); -impl From> for CeramicConfigs { - fn from(value: Vec) -> Self { - if value.is_empty() { - Self(vec![CeramicConfig::default()]) +impl From>> for CeramicConfigs { + fn from(value: Option>) -> Self { + if let Some(value) = value { + if value.is_empty() { + Self(vec![CeramicConfig::default()]) + } else { + Self(value.into_iter().map(CeramicConfig::from).collect()) + } } else { - Self(value.into_iter().map(CeramicConfig::from).collect()) + Self(vec![CeramicConfig::default()]) } } } diff --git a/operator/src/network/controller.rs b/operator/src/network/controller.rs index ed4e1cc2..e44988cd 100644 --- a/operator/src/network/controller.rs +++ b/operator/src/network/controller.rs @@ -182,7 +182,9 @@ async fn reconcile( } else { NetworkStatus::default() }; - if spec.ceramic.len() > MAX_CERAMICS { + + let ceramic_configs: CeramicConfigs = spec.ceramic.clone().into(); + if ceramic_configs.0.len() > MAX_CERAMICS { return Err(Error::App { source: anyhow!("too many ceramics configured, maximum {MAX_CERAMICS}"), }); @@ -216,7 +218,6 @@ async fn reconcile( let datadog: DataDogConfig = (&spec.datadog).into(); // Only create CAS resources if the Ceramic network was "local" - let ceramic_configs: CeramicConfigs = spec.ceramic.clone().into(); if net_config.network_type == CERAMIC_LOCAL_NETWORK_TYPE { apply_cas(cx.clone(), &ns, network.clone(), spec.cas.clone(), &datadog).await?; } @@ -1882,10 +1883,10 @@ mod tests { // Setup network spec and status let network = Network::test() .with_spec(NetworkSpec { - ceramic: vec![CeramicSpec { + ceramic: Some(vec![CeramicSpec { ipfs: Some(IpfsSpec::Go(GoIpfsSpec::default())), ..Default::default() - }], + }]), ..Default::default() }) .with_status(NetworkStatus { @@ -2007,7 +2008,7 @@ mod tests { // Setup network spec and status let network = Network::test() .with_spec(NetworkSpec { - ceramic: vec![CeramicSpec { + ceramic: Some(vec![CeramicSpec { ipfs: Some(IpfsSpec::Go(GoIpfsSpec { image: Some("ipfs/ipfs:go".to_owned()), resource_limits: Some(ResourceLimitsSpec { @@ -2018,7 +2019,7 @@ mod tests { ..Default::default() })), ..Default::default() - }], + }]), ..Default::default() }) .with_status(NetworkStatus { @@ -2161,7 +2162,7 @@ mod tests { // Setup network spec and status let network = Network::test() .with_spec(NetworkSpec { - ceramic: vec![CeramicSpec { + ceramic: Some(vec![CeramicSpec { ipfs: Some(IpfsSpec::Go(GoIpfsSpec { commands: Some(vec![ "ipfs config Pubsub.SeenMessagesTTL 10m".to_owned(), @@ -2170,7 +2171,7 @@ mod tests { ..Default::default() })), ..Default::default() - }], + }]), ..Default::default() }) .with_status(NetworkStatus { @@ -2297,7 +2298,7 @@ mod tests { // Setup network spec and status let network = Network::test() .with_spec(NetworkSpec { - ceramic: vec![CeramicSpec { + ceramic: Some(vec![CeramicSpec { ipfs: Some(IpfsSpec::Rust(RustIpfsSpec { image: Some("ipfs/ipfs:rust".to_owned()), resource_limits: Some(ResourceLimitsSpec { @@ -2314,7 +2315,7 @@ mod tests { ..Default::default() })), ..Default::default() - }], + }]), ..Default::default() }) .with_status(NetworkStatus { @@ -2664,14 +2665,14 @@ mod tests { // Setup network spec and status let network = Network::test() .with_spec(NetworkSpec { - ceramic: vec![CeramicSpec { + ceramic: Some(vec![CeramicSpec { resource_limits: Some(ResourceLimitsSpec { cpu: Some(Quantity("4".to_owned())), memory: Some(Quantity("4Gi".to_owned())), storage: Some(Quantity("4Gi".to_owned())), }), ..Default::default() - }], + }]), ..Default::default() }) .with_status(NetworkStatus { @@ -2953,11 +2954,11 @@ mod tests { async fn ceramic_image() { // Setup network spec and status let network = Network::test().with_spec(NetworkSpec { - ceramic: vec![CeramicSpec { + ceramic: Some(vec![CeramicSpec { image: Some("ceramic:foo".to_owned()), image_pull_policy: Some("IfNotPresent".to_owned()), ..Default::default() - }], + }]), ..Default::default() }); let mock_rpc_client = default_ipfs_rpc_mock(); @@ -3112,7 +3113,7 @@ mod tests { async fn multiple_default_ceramics() { // Setup network spec and status let network = Network::test().with_spec(NetworkSpec { - ceramic: vec![CeramicSpec::default(), CeramicSpec::default()], + ceramic: Some(vec![CeramicSpec::default(), CeramicSpec::default()]), ..Default::default() }); let mock_rpc_client = default_ipfs_rpc_mock(); @@ -3139,7 +3140,7 @@ mod tests { async fn multiple_ceramics() { // Setup network spec and status let network = Network::test().with_spec(NetworkSpec { - ceramic: vec![ + ceramic: Some(vec![ CeramicSpec::default(), CeramicSpec { ipfs: Some(IpfsSpec::Go(GoIpfsSpec { @@ -3147,7 +3148,7 @@ mod tests { })), ..Default::default() }, - ], + ]), ..Default::default() }); let mock_rpc_client = default_ipfs_rpc_mock(); @@ -3181,13 +3182,15 @@ mod tests { assert_eq!(weights.iter().sum::(), replicas); let network = Network::test().with_spec(NetworkSpec { replicas, - ceramic: weights - .iter() - .map(|w| CeramicSpec { - weight: Some(*w), - ..Default::default() - }) - .collect(), + ceramic: Some( + weights + .iter() + .map(|w| CeramicSpec { + weight: Some(*w), + ..Default::default() + }) + .collect(), + ), ..Default::default() }); @@ -3297,10 +3300,10 @@ mod tests { let mut env = HashMap::default(); env.insert("SOME_ENV_VAR".to_string(), "SOME_ENV_VALUE".to_string()); let network = Network::test().with_spec(NetworkSpec { - ceramic: vec![CeramicSpec { + ceramic: Some(vec![CeramicSpec { env: Some(env), ..Default::default() - }], + }]), ..Default::default() }); let mock_rpc_client = default_ipfs_rpc_mock(); diff --git a/operator/src/network/spec.rs b/operator/src/network/spec.rs index eff07940..ab74dc1b 100644 --- a/operator/src/network/spec.rs +++ b/operator/src/network/spec.rs @@ -28,7 +28,7 @@ pub struct NetworkSpec { /// Total replicas will be split across each ceramic spec according to relative weights. /// It is possible that if the weight is small enough compared to others that a single spec /// will be assigned zero replicas. - pub ceramic: Vec, + pub ceramic: Option>, /// Name of secret containing the private key used for signing anchor requests and generating /// the Admin DID. pub private_key_secret: Option,