Skip to content

Commit

Permalink
Implement discovering mr_persistence in mr_server
Browse files Browse the repository at this point in the history
  • Loading branch information
vladbat00 committed Jan 6, 2022
1 parent 0e13881 commit 080aaba
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 86 deletions.
93 changes: 9 additions & 84 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bins/matchmaker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ futures = "0.3.17"
hyper = "0.14.15"
kube = "0.60.0"
kube-derive = "0.60.0"
kube-runtime = "0.60.0"
log = "0.4.14"
k8s-openapi = { version = "0.13.1", default-features = false, features = ["v1_20"] }
rymder = "0.2.2"
Expand Down
2 changes: 2 additions & 0 deletions libs/server_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ bevy_rapier2d = { version = "0.11" }
chrono = "0.4.19"
env_logger = "0.8.1"
jwt-compact = { version = "0.5.0", features = ["std", "clock", "with_rsa"], default-features = false }
kube = "0.60.0"
k8s-openapi = { version = "0.13.1", default-features = false, features = ["v1_20"] }
puffin = { version = "0.8.1", optional = true }
rand = "0.8.4"
reqwest = "0.11.8"
Expand Down
28 changes: 28 additions & 0 deletions libs/server_lib/src/kube_discovery.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use bevy::log;
use k8s_openapi::api::core::v1::Pod;
use kube::{api::ListParams, Api, Client};
use reqwest::Url;

pub async fn discover_persistence() -> Option<Url> {
let client = Client::try_default().await.ok()?;
log::info!("Kubernetes environment detected, trying to fetch mr-persistence pods...");

let pods: Api<Pod> = Api::namespaced(client, "default");
let lp = ListParams::default()
.labels("app=muddle-run,service=mr-persistence")
.timeout(0);
let pods_list = pods
.list(&lp)
.await
.map_err(|err| {
log::warn!("Failed to fetch kubernetes pods: {:?}", err);
err
})
.ok()?;

let pod_ip = pods_list.items.first()?.status.as_ref()?.pod_ip.as_ref()?;
let persistence_url = format!("http://{}:8083", pod_ip).parse().unwrap();
log::info!("Using \"{}\" as MUDDLE_PERSISTENCE_URL", persistence_url);

Some(persistence_url)
}
6 changes: 5 additions & 1 deletion libs/server_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ use mr_shared_lib::{
registry::IncrementId,
simulations_per_second, MuddleSharedPlugin,
};
use reqwest::Url;
use std::{
lazy::SyncLazy,
time::{Duration, Instant},
};
use tokio::sync::mpsc::UnboundedReceiver;

mod game_events;
mod kube_discovery;
mod net;
mod persistence;
mod player_updates;
Expand Down Expand Up @@ -84,7 +86,9 @@ impl Plugin for MuddleServerPlugin {
builder.add_startup_system(init_level.system());
builder.add_startup_system(startup.system());

if let Some(url) = try_parse_from_env!("MUDDLE_PERSISTENCE_URL") {
let persistence_url: Option<Url> = try_parse_from_env!("MUDDLE_PERSISTENCE_URL")
.or_else(|| TOKIO.block_on(kube_discovery::discover_persistence()));
if let Some(url) = persistence_url {
let config = PersistenceConfig {
url,
google_web_client_id: std::env::var("MUDDLE_GOOGLE_WEB_CLIENT_ID")
Expand Down

0 comments on commit 080aaba

Please sign in to comment.