Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configure ceramic to use postgres #151

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 133 additions & 3 deletions operator/src/network/ceramic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use k8s_openapi::{
core::v1::{
ConfigMapVolumeSource, Container, ContainerPort, EmptyDirVolumeSource, EnvVar,
EnvVarSource, HTTPGetAction, PersistentVolumeClaim, PersistentVolumeClaimSpec,
PersistentVolumeClaimVolumeSource, PodSpec, PodTemplateSpec, Probe,
ResourceRequirements, SecretKeySelector, ServicePort, ServiceSpec, Volume, VolumeMount,
PersistentVolumeClaimVolumeSource, PodSecurityContext, PodSpec, PodTemplateSpec, Probe,
ResourceRequirements, SecretKeySelector, SecurityContext, ServicePort, ServiceSpec,
Volume, VolumeMount,
},
},
apimachinery::pkg::{
Expand Down Expand Up @@ -89,7 +90,7 @@ r#"{
"local-directory": "${CERAMIC_STATE_STORE_PATH}"
},
"indexing": {
"db": "sqlite://${CERAMIC_SQLITE_PATH}",
"db": "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@localhost/${POSTGRES_DB}",
"allow-queries-before-historical-sync": true,
"disable-composedb": false,
"enable-historical-sync": false
Expand Down Expand Up @@ -137,6 +138,7 @@ pub struct CeramicConfig {
pub image_pull_policy: String,
pub ipfs: IpfsConfig,
pub resource_limits: ResourceLimitsConfig,
pub postgres_resource_limits: ResourceLimitsConfig,
pub env: Option<BTreeMap<String, String>>,
}

Expand Down Expand Up @@ -256,6 +258,11 @@ impl Default for CeramicConfig {
memory: Some(Quantity("1Gi".to_owned())),
storage: Quantity("1Gi".to_owned()),
},
postgres_resource_limits: ResourceLimitsConfig {
cpu: Some(Quantity("250m".to_owned())),
memory: Some(Quantity("1Gi".to_owned())),
storage: Quantity("1Gi".to_owned()),
},
env: None,
}
}
Expand Down Expand Up @@ -290,6 +297,10 @@ impl From<CeramicSpec> for CeramicConfig {
value.resource_limits,
default.resource_limits,
),
postgres_resource_limits: ResourceLimitsConfig::from_spec(
value.postgres_resource_limits,
default.postgres_resource_limits,
),
env: value.env,
}
}
Expand Down Expand Up @@ -337,6 +348,35 @@ pub fn stateful_set_spec(ns: &str, bundle: &CeramicBundle<'_>) -> StatefulSetSpe
value: Some("2".to_owned()),
..Default::default()
},
EnvVar {
name: "POSTGRES_DB".to_owned(),
value: Some("ceramic".to_owned()),
..Default::default()
},
EnvVar {
name: "POSTGRES_USER".to_owned(),
value_from: Some(EnvVarSource {
secret_key_ref: Some(SecretKeySelector {
key: "username".to_owned(),
name: Some("postgres-auth".to_owned()),
..Default::default()
}),
..Default::default()
}),
..Default::default()
},
EnvVar {
name: "POSTGRES_PASSWORD".to_owned(),
value_from: Some(EnvVarSource {
secret_key_ref: Some(SecretKeySelector {
key: "password".to_owned(),
name: Some("postgres-auth".to_owned()),
..Default::default()
}),
..Default::default()
}),
..Default::default()
},
];

// js-ceramic does not allow specifying the pubsub topic for any network types except "inmemory" or "local"
Expand Down Expand Up @@ -414,6 +454,14 @@ pub fn stateful_set_spec(ns: &str, bundle: &CeramicBundle<'_>) -> StatefulSetSpe
}),
..Default::default()
},
Volume {
name: "postgres-data".to_owned(),
persistent_volume_claim: Some(PersistentVolumeClaimVolumeSource {
claim_name: "postgres-data".to_owned(),
..Default::default()
}),
..Default::default()
},
];

volumes.append(&mut bundle.config.ipfs.volumes(&bundle.info));
Expand Down Expand Up @@ -519,6 +567,66 @@ pub fn stateful_set_spec(ns: &str, bundle: &CeramicBundle<'_>) -> StatefulSetSpe
]),
..Default::default()
},
Container {
image: Some("postgres:15-alpine".to_owned()),
image_pull_policy: Some("IfNotPresent".to_owned()),
name: "postgres".to_owned(),
env: Some(vec![
EnvVar {
name: "POSTGRES_DB".to_owned(),
value: Some("ceramic".to_owned()),
..Default::default()
},
EnvVar {
name: "POSTGRES_PASSWORD".to_owned(),
value_from: Some(EnvVarSource {
secret_key_ref: Some(SecretKeySelector {
key: "password".to_owned(),
name: Some("postgres-auth".to_owned()),
..Default::default()
}),
..Default::default()
}),
..Default::default()
},
EnvVar {
name: "POSTGRES_USER".to_owned(),
value_from: Some(EnvVarSource {
secret_key_ref: Some(SecretKeySelector {
key: "username".to_owned(),
name: Some("postgres-auth".to_owned()),
..Default::default()
}),
..Default::default()
}),
..Default::default()
},
]),
ports: Some(vec![ContainerPort {
container_port: 5432,
name: Some("postgres".to_owned()),
..Default::default()
}]),
resources: Some(ResourceRequirements {
limits: Some(bundle.config.postgres_resource_limits.clone().into()),
requests: Some(
bundle.config.postgres_resource_limits.clone().into(),
),
..Default::default()
}),
volume_mounts: Some(vec![VolumeMount {
mount_path: "/var/lib/postgresql".to_owned(),
name: "postgres-data".to_owned(),
sub_path: Some("ceramic_data".to_owned()),
..Default::default()
}]),
security_context: Some(SecurityContext {
run_as_group: Some(70),
run_as_user: Some(70),
..Default::default()
}),
..Default::default()
},
bundle
.config
.ipfs
Expand Down Expand Up @@ -554,6 +662,10 @@ pub fn stateful_set_spec(ns: &str, bundle: &CeramicBundle<'_>) -> StatefulSetSpe
..Default::default()
}]),
volumes: Some(volumes),
security_context: Some(PodSecurityContext {
fs_group: Some(70),
..Default::default()
}),
..Default::default()
}),
}),
Expand Down Expand Up @@ -602,6 +714,24 @@ pub fn stateful_set_spec(ns: &str, bundle: &CeramicBundle<'_>) -> StatefulSetSpe
}),
..Default::default()
},
PersistentVolumeClaim {
metadata: ObjectMeta {
name: Some("postgres-data".to_owned()),
..Default::default()
},
spec: Some(PersistentVolumeClaimSpec {
access_modes: Some(vec!["ReadWriteOnce".to_owned()]),
resources: Some(ResourceRequirements {
requests: Some(BTreeMap::from_iter(vec![(
"storage".to_owned(),
Quantity("10Gi".to_owned()),
)])),
..Default::default()
}),
..Default::default()
}),
..Default::default()
},
]),
..Default::default()
}
Expand Down
Loading
Loading