From 080aaba91959d8097eff3175500ea9a3b3588fe6 Mon Sep 17 00:00:00 2001 From: mvlabat Date: Fri, 7 Jan 2022 00:53:19 +0200 Subject: [PATCH] Implement discovering mr_persistence in mr_server --- Cargo.lock | 93 +++------------------------ bins/matchmaker/Cargo.toml | 1 - libs/server_lib/Cargo.toml | 2 + libs/server_lib/src/kube_discovery.rs | 28 ++++++++ libs/server_lib/src/lib.rs | 6 +- 5 files changed, 44 insertions(+), 86 deletions(-) create mode 100644 libs/server_lib/src/kube_discovery.rs diff --git a/Cargo.lock b/Cargo.lock index 75253152..334aa2c1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1962,16 +1962,6 @@ dependencies = [ "syn", ] -[[package]] -name = "dashmap" -version = "4.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e77a43b28d0668df09411cb0bc9a8c2adc40f9a048afe863e05fd43251e8e39c" -dependencies = [ - "cfg-if 1.0.0", - "num_cpus", -] - [[package]] name = "debugid" version = "0.7.2" @@ -3363,17 +3353,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "json-patch" -version = "0.2.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f995a3c8f2bc3dd52a18a583e90f9ec109c047fa1603a853e46bcda14d2e279d" -dependencies = [ - "serde", - "serde_json", - "treediff", -] - [[package]] name = "jsonpath_lib" version = "0.3.0" @@ -3468,7 +3447,7 @@ dependencies = [ "kube-core", "openssl", "pem", - "pin-project 1.0.8", + "pin-project", "serde", "serde_json", "serde_yaml", @@ -3489,7 +3468,6 @@ checksum = "04ccd59635e9b21353da8d4a394bb5d3473b5965ed44496c8f857281b0625ffe" dependencies = [ "form_urlencoded", "http", - "json-patch", "k8s-openapi", "once_cell", "serde", @@ -3510,28 +3488,6 @@ dependencies = [ "syn", ] -[[package]] -name = "kube-runtime" -version = "0.60.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eec378b03890f9f2bfa9448a51aa0f6a4299f6bb2ed0d180330e628c7a395918" -dependencies = [ - "dashmap", - "derivative", - "futures", - "json-patch", - "k8s-openapi", - "kube", - "pin-project 1.0.8", - "serde", - "serde_json", - "smallvec", - "snafu", - "tokio", - "tokio-util", - "tracing", -] - [[package]] name = "language-tags" version = "0.3.2" @@ -3951,7 +3907,6 @@ dependencies = [ "k8s-openapi", "kube", "kube-derive", - "kube-runtime", "log", "mr_messages_lib", "mr_utils_lib", @@ -4018,6 +3973,8 @@ dependencies = [ "chrono", "env_logger 0.8.4", "jwt-compact", + "k8s-openapi", + "kube", "mr_messages_lib", "mr_shared_lib", "mr_utils_lib", @@ -4830,33 +4787,13 @@ dependencies = [ "indexmap", ] -[[package]] -name = "pin-project" -version = "0.4.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9615c18d31137579e9ff063499264ddc1278e7b1982757ebc111028c4d1dc909" -dependencies = [ - "pin-project-internal 0.4.29", -] - [[package]] name = "pin-project" version = "1.0.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "576bc800220cc65dac09e99e97b08b358cfab6e17078de8dc5fee223bd2d0c08" dependencies = [ - "pin-project-internal 1.0.8", -] - -[[package]] -name = "pin-project-internal" -version = "0.4.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "044964427019eed9d49d9d5bbce6047ef18f37100ea400912a9fa4a3523ab12a" -dependencies = [ - "proc-macro2", - "quote", - "syn", + "pin-project-internal", ] [[package]] @@ -5966,8 +5903,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eab12d3c261b2308b0d80c26fffb58d17eba81a4be97890101f416b478c79ca7" dependencies = [ "doc-comment", - "futures-core", - "pin-project 0.4.29", "snafu-derive", ] @@ -6549,7 +6484,7 @@ checksum = "511de3f85caf1c98983545490c3d09685fa8eb634e57eec22bb4db271f46cbd8" dependencies = [ "futures-util", "log", - "pin-project 1.0.8", + "pin-project", "tokio", "tungstenite", ] @@ -6565,7 +6500,6 @@ dependencies = [ "futures-sink", "log", "pin-project-lite", - "slab", "tokio", ] @@ -6596,7 +6530,7 @@ dependencies = [ "hyper", "hyper-timeout", "percent-encoding", - "pin-project 1.0.8", + "pin-project", "prost", "prost-derive", "tokio", @@ -6618,7 +6552,7 @@ dependencies = [ "futures-core", "futures-util", "indexmap", - "pin-project 1.0.8", + "pin-project", "pin-project-lite", "rand 0.8.4", "slab", @@ -6642,7 +6576,7 @@ dependencies = [ "futures-util", "http", "http-body", - "pin-project 1.0.8", + "pin-project", "tower-layer", "tower-service", "tracing", @@ -6699,7 +6633,7 @@ version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2" dependencies = [ - "pin-project 1.0.8", + "pin-project", "tracing", ] @@ -6757,15 +6691,6 @@ dependencies = [ "wasm-bindgen", ] -[[package]] -name = "treediff" -version = "3.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "761e8d5ad7ce14bb82b7e61ccc0ca961005a275a060b9644a2431aa11553c2ff" -dependencies = [ - "serde_json", -] - [[package]] name = "try-lock" version = "0.2.3" diff --git a/bins/matchmaker/Cargo.toml b/bins/matchmaker/Cargo.toml index e894bf3f..268e0eb7 100644 --- a/bins/matchmaker/Cargo.toml +++ b/bins/matchmaker/Cargo.toml @@ -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" diff --git a/libs/server_lib/Cargo.toml b/libs/server_lib/Cargo.toml index 21e40cd8..d96c8b4d 100644 --- a/libs/server_lib/Cargo.toml +++ b/libs/server_lib/Cargo.toml @@ -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" diff --git a/libs/server_lib/src/kube_discovery.rs b/libs/server_lib/src/kube_discovery.rs new file mode 100644 index 00000000..d206ba50 --- /dev/null +++ b/libs/server_lib/src/kube_discovery.rs @@ -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 { + let client = Client::try_default().await.ok()?; + log::info!("Kubernetes environment detected, trying to fetch mr-persistence pods..."); + + let pods: Api = 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) +} diff --git a/libs/server_lib/src/lib.rs b/libs/server_lib/src/lib.rs index 2c2ab206..9a38a1c3 100644 --- a/libs/server_lib/src/lib.rs +++ b/libs/server_lib/src/lib.rs @@ -32,6 +32,7 @@ use mr_shared_lib::{ registry::IncrementId, simulations_per_second, MuddleSharedPlugin, }; +use reqwest::Url; use std::{ lazy::SyncLazy, time::{Duration, Instant}, @@ -39,6 +40,7 @@ use std::{ use tokio::sync::mpsc::UnboundedReceiver; mod game_events; +mod kube_discovery; mod net; mod persistence; mod player_updates; @@ -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 = 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")