Skip to content

Commit

Permalink
feat: add env overrides to rust ipfs spec
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Nov 2, 2023
1 parent 83cb089 commit 84f6d9a
Show file tree
Hide file tree
Showing 15 changed files with 248 additions and 199 deletions.
108 changes: 63 additions & 45 deletions operator/src/network/ceramic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};

use k8s_openapi::{
api::{
Expand Down Expand Up @@ -268,6 +268,7 @@ pub struct RustIpfsConfig {
image_pull_policy: String,
resource_limits: ResourceLimitsConfig,
rust_log: String,
env: Option<HashMap<String, String>>,
}

impl Default for RustIpfsConfig {
Expand All @@ -281,6 +282,7 @@ impl Default for RustIpfsConfig {
storage: Quantity("1Gi".to_owned()),
},
rust_log: "info,ceramic_one=debug,tracing_actix_web=debug".to_owned(),
env: None,
}
}
}
Expand All @@ -295,6 +297,7 @@ impl From<RustIpfsSpec> for RustIpfsConfig {
default.resource_limits,
),
rust_log: value.rust_log.unwrap_or(default.rust_log),
env: value.env,
}
}
}
Expand Down Expand Up @@ -391,51 +394,66 @@ impl From<IpfsSpec> for IpfsConfig {

impl RustIpfsConfig {
fn container(&self) -> Container {
Container {
env: Some(vec![
EnvVar {
name: "RUST_LOG".to_owned(),
value: Some(self.rust_log.to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_BIND_ADDRESS".to_owned(),
value: Some(format!("0.0.0.0:{CERAMIC_SERVICE_IPFS_PORT}")),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_METRICS".to_owned(),
value: Some("true".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_METRICS_BIND_ADDRESS".to_owned(),
value: Some("0.0.0.0:9090".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_SWARM_ADDRESSES".to_owned(),
value: Some("/ip4/0.0.0.0/tcp/4001".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_STORE_DIR".to_owned(),
value: Some("/data/ipfs".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_NETWORK".to_owned(),
value: Some("local".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_LOCAL_NETWORK_ID".to_owned(),
// We can use a hard coded value since nodes from other networks should not be
// able to connect.
value: Some("0".to_owned()),
let mut env = vec![
EnvVar {
name: "RUST_LOG".to_owned(),
value: Some(self.rust_log.to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_BIND_ADDRESS".to_owned(),
value: Some(format!("0.0.0.0:{CERAMIC_SERVICE_IPFS_PORT}")),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_METRICS".to_owned(),
value: Some("true".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_METRICS_BIND_ADDRESS".to_owned(),
value: Some("0.0.0.0:9090".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_SWARM_ADDRESSES".to_owned(),
value: Some("/ip4/0.0.0.0/tcp/4001".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_STORE_DIR".to_owned(),
value: Some("/data/ipfs".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_NETWORK".to_owned(),
value: Some("local".to_owned()),
..Default::default()
},
EnvVar {
name: "CERAMIC_ONE_LOCAL_NETWORK_ID".to_owned(),
// We can use a hard coded value since nodes from other networks should not be
// able to connect.
value: Some("0".to_owned()),
..Default::default()
},
];
if let Some(extra_env) = &self.env {
extra_env.iter().for_each(|(key, value)| {
if let Some((pos, _)) = env.iter().enumerate().find(|(_, var)| &var.name == key) {
env.swap_remove(pos);
}
env.push(EnvVar {
name: key.to_string(),
value: Some(value.to_string()),
..Default::default()
},
]),
})
});
}
// Sort env vars so we can have stable tests
env.sort_unstable_by(|a, b| a.name.cmp(&b.name));
Container {
env: Some(env),
image: Some(self.image.to_owned()),
image_pull_policy: Some(self.image_pull_policy.to_owned()),
name: IPFS_CONTAINER_NAME.to_owned(),
Expand Down
95 changes: 61 additions & 34 deletions operator/src/network/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,8 +765,8 @@ async fn reset_bootstrap_job(
// Stub tests relying on stub.rs and its apiserver stubs
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::{collections::BTreeMap, time::Duration};
use std::{collections::HashMap, sync::Arc};

use super::{reconcile, Network};

Expand Down Expand Up @@ -1780,14 +1780,14 @@ mod tests {
{
- "env": [
- {
- "name": "RUST_LOG",
- "value": "info,ceramic_one=debug,tracing_actix_web=debug"
- },
- {
- "name": "CERAMIC_ONE_BIND_ADDRESS",
- "value": "0.0.0.0:5001"
- },
- {
- "name": "CERAMIC_ONE_LOCAL_NETWORK_ID",
- "value": "0"
- },
- {
- "name": "CERAMIC_ONE_METRICS",
- "value": "true"
- },
Expand All @@ -1796,20 +1796,20 @@ mod tests {
- "value": "0.0.0.0:9090"
- },
- {
- "name": "CERAMIC_ONE_SWARM_ADDRESSES",
- "value": "/ip4/0.0.0.0/tcp/4001"
- "name": "CERAMIC_ONE_NETWORK",
- "value": "local"
- },
- {
- "name": "CERAMIC_ONE_STORE_DIR",
- "value": "/data/ipfs"
- },
- {
- "name": "CERAMIC_ONE_NETWORK",
- "value": "local"
- "name": "CERAMIC_ONE_SWARM_ADDRESSES",
- "value": "/ip4/0.0.0.0/tcp/4001"
- },
- {
- "name": "CERAMIC_ONE_LOCAL_NETWORK_ID",
- "value": "0"
- "name": "RUST_LOG",
- "value": "info,ceramic_one=debug,tracing_actix_web=debug"
- }
- ],
- "image": "public.ecr.aws/r5b3e0r5/3box/ceramic-one:latest",
Expand Down Expand Up @@ -1905,14 +1905,14 @@ mod tests {
{
- "env": [
- {
- "name": "RUST_LOG",
- "value": "info,ceramic_one=debug,tracing_actix_web=debug"
- },
- {
- "name": "CERAMIC_ONE_BIND_ADDRESS",
- "value": "0.0.0.0:5001"
- },
- {
- "name": "CERAMIC_ONE_LOCAL_NETWORK_ID",
- "value": "0"
- },
- {
- "name": "CERAMIC_ONE_METRICS",
- "value": "true"
- },
Expand All @@ -1921,20 +1921,20 @@ mod tests {
- "value": "0.0.0.0:9090"
- },
- {
- "name": "CERAMIC_ONE_SWARM_ADDRESSES",
- "value": "/ip4/0.0.0.0/tcp/4001"
- "name": "CERAMIC_ONE_NETWORK",
- "value": "local"
- },
- {
- "name": "CERAMIC_ONE_STORE_DIR",
- "value": "/data/ipfs"
- },
- {
- "name": "CERAMIC_ONE_NETWORK",
- "value": "local"
- "name": "CERAMIC_ONE_SWARM_ADDRESSES",
- "value": "/ip4/0.0.0.0/tcp/4001"
- },
- {
- "name": "CERAMIC_ONE_LOCAL_NETWORK_ID",
- "value": "0"
- "name": "RUST_LOG",
- "value": "info,ceramic_one=debug,tracing_actix_web=debug"
- }
- ],
- "image": "public.ecr.aws/r5b3e0r5/3box/ceramic-one:latest",
Expand Down Expand Up @@ -2049,14 +2049,14 @@ mod tests {
{
- "env": [
- {
- "name": "RUST_LOG",
- "value": "info,ceramic_one=debug,tracing_actix_web=debug"
- },
- {
- "name": "CERAMIC_ONE_BIND_ADDRESS",
- "value": "0.0.0.0:5001"
- },
- {
- "name": "CERAMIC_ONE_LOCAL_NETWORK_ID",
- "value": "0"
- },
- {
- "name": "CERAMIC_ONE_METRICS",
- "value": "true"
- },
Expand All @@ -2065,20 +2065,20 @@ mod tests {
- "value": "0.0.0.0:9090"
- },
- {
- "name": "CERAMIC_ONE_SWARM_ADDRESSES",
- "value": "/ip4/0.0.0.0/tcp/4001"
- "name": "CERAMIC_ONE_NETWORK",
- "value": "local"
- },
- {
- "name": "CERAMIC_ONE_STORE_DIR",
- "value": "/data/ipfs"
- },
- {
- "name": "CERAMIC_ONE_NETWORK",
- "value": "local"
- "name": "CERAMIC_ONE_SWARM_ADDRESSES",
- "value": "/ip4/0.0.0.0/tcp/4001"
- },
- {
- "name": "CERAMIC_ONE_LOCAL_NETWORK_ID",
- "value": "0"
- "name": "RUST_LOG",
- "value": "info,ceramic_one=debug,tracing_actix_web=debug"
- }
- ],
- "image": "public.ecr.aws/r5b3e0r5/3box/ceramic-one:latest",
Expand Down Expand Up @@ -2141,6 +2141,12 @@ mod tests {
memory: Some(Quantity("4Gi".to_owned())),
storage: Some(Quantity("4Gi".to_owned())),
}),
env: Some(HashMap::from_iter([
("ENV_KEY_A".to_string(), "ENV_VALUE_A".to_string()),
("ENV_KEY_B".to_string(), "ENV_VALUE_B".to_string()),
// Override one existing var
("CERAMIC_ONE_METRICS".to_string(), "false".to_string()),
])),
..Default::default()
})),
..Default::default()
Expand Down Expand Up @@ -2170,16 +2176,37 @@ mod tests {
stub.ceramics[0].stateful_set.patch(expect![[r#"
--- original
+++ modified
@@ -171,7 +171,7 @@
"value": "0"
@@ -148,7 +148,7 @@
},
{
"name": "CERAMIC_ONE_METRICS",
- "value": "true"
+ "value": "false"
},
{
"name": "CERAMIC_ONE_METRICS_BIND_ADDRESS",
@@ -167,11 +167,19 @@
"value": "/ip4/0.0.0.0/tcp/4001"
},
{
+ "name": "ENV_KEY_A",
+ "value": "ENV_VALUE_A"
+ },
+ {
+ "name": "ENV_KEY_B",
+ "value": "ENV_VALUE_B"
+ },
+ {
"name": "RUST_LOG",
"value": "info,ceramic_one=debug,tracing_actix_web=debug"
}
],
- "image": "public.ecr.aws/r5b3e0r5/3box/ceramic-one:latest",
+ "image": "ipfs/ipfs:rust",
"imagePullPolicy": "Always",
"name": "ipfs",
"ports": [
@@ -193,14 +193,14 @@
@@ -193,14 +201,14 @@
],
"resources": {
"limits": {
Expand Down
4 changes: 4 additions & 0 deletions operator/src/network/spec.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Place all spec types into a single module so they can be used as a lightweight dependency
use std::collections::HashMap;

use k8s_openapi::apimachinery::pkg::api::resource::Quantity;
use keramik_common::peer_info::Peer;
Expand Down Expand Up @@ -119,6 +120,9 @@ pub struct RustIpfsSpec {
pub resource_limits: Option<ResourceLimitsSpec>,
/// Value of the RUST_LOG env var.
pub rust_log: Option<String>,
/// Extra env values to pass to the image.
/// CAUTION: Any env vars specified in this set will override any predefined values.
pub env: Option<HashMap<String, String>>,
}

/// Describes how the Go IPFS node for a peer should behave.
Expand Down
Loading

0 comments on commit 84f6d9a

Please sign in to comment.