Skip to content

Commit

Permalink
refactor: allow ceramic spec to be optional
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Dec 1, 2023
1 parent 4c1c510 commit 3b0a46d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
14 changes: 9 additions & 5 deletions operator/src/network/ceramic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,12 +358,16 @@ impl Default for CeramicConfig {

pub struct CeramicConfigs(pub Vec<CeramicConfig>);

impl From<Vec<CeramicSpec>> for CeramicConfigs {
fn from(value: Vec<CeramicSpec>) -> Self {
if value.is_empty() {
Self(vec![CeramicConfig::default()])
impl From<Option<Vec<CeramicSpec>>> for CeramicConfigs {
fn from(value: Option<Vec<CeramicSpec>>) -> 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()])
}
}
}
Expand Down
55 changes: 29 additions & 26 deletions operator/src/network/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"),
});
Expand Down Expand Up @@ -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?;
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -2018,7 +2019,7 @@ mod tests {
..Default::default()
})),
..Default::default()
}],
}]),
..Default::default()
})
.with_status(NetworkStatus {
Expand Down Expand Up @@ -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(),
Expand All @@ -2170,7 +2171,7 @@ mod tests {
..Default::default()
})),
..Default::default()
}],
}]),
..Default::default()
})
.with_status(NetworkStatus {
Expand Down Expand Up @@ -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 {
Expand All @@ -2314,7 +2315,7 @@ mod tests {
..Default::default()
})),
..Default::default()
}],
}]),
..Default::default()
})
.with_status(NetworkStatus {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -3139,15 +3140,15 @@ 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 {
..Default::default()
})),
..Default::default()
},
],
]),
..Default::default()
});
let mock_rpc_client = default_ipfs_rpc_mock();
Expand Down Expand Up @@ -3181,13 +3182,15 @@ mod tests {
assert_eq!(weights.iter().sum::<i32>(), 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()
});
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion operator/src/network/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CeramicSpec>,
pub ceramic: Option<Vec<CeramicSpec>>,
/// Name of secret containing the private key used for signing anchor requests and generating
/// the Admin DID.
pub private_key_secret: Option<String>,
Expand Down

0 comments on commit 3b0a46d

Please sign in to comment.