feat(gateway): get node id from kubernetes

This commit is contained in:
alyssa 2025-04-20 19:25:11 +00:00
parent bfa0071f90
commit 44c5a2d106
2 changed files with 12 additions and 0 deletions

View file

@ -1,3 +1,4 @@
use anyhow::anyhow;
use futures::StreamExt;
use libpk::{_config::ClusterSettings, runtime_config::RuntimeConfig};
use metrics::counter;
@ -48,6 +49,12 @@ pub fn create_shards(redis: fred::clients::RedisPool) -> anyhow::Result<Vec<Shar
let (start_shard, end_shard): (u32, u32) = if cluster_settings.total_shards < 16 {
warn!("we have less than 16 shards, assuming single gateway process");
if cluster_settings.node_id != 0 {
return Err(anyhow!(
"expecting to be node 0 in single-process mode, but we are node {}",
cluster_settings.node_id
));
}
(0, (cluster_settings.total_shards - 1).into())
} else {
(

View file

@ -138,10 +138,15 @@ impl PKConfig {
lazy_static! {
#[derive(Debug)]
pub static ref CONFIG: Arc<PKConfig> = {
// hacks
if let Ok(var) = std::env::var("NOMAD_ALLOC_INDEX")
&& std::env::var("pluralkit__discord__cluster__total_nodes").is_ok() {
std::env::set_var("pluralkit__discord__cluster__node_id", var);
}
if let Ok(var) = std::env::var("STATEFULSET_NAME_FOR_INDEX")
&& std::env::var("pluralkit__discord__cluster__total_nodes").is_ok() {
std::env::set_var("pluralkit__discord__cluster__node_id", var.split("-").last().unwrap());
}
Arc::new(Config::builder()
.add_source(config::Environment::with_prefix("pluralkit").separator("__"))