From 6918c64730e25442b2ac45c76de3877b2b461eaf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Levilain?= Date: Fri, 27 Oct 2023 12:16:20 +0200 Subject: [PATCH] refactor(shulker-operator): builder accept an optional context, pass Redis env to proxies --- ...ckages.google-agones-sdk-bindings-java.iml | 2 +- ...s.google-agones-sdk-bindings-java.main.iml | 2 +- ...s.google-agones-sdk-bindings-java.test.iml | 2 +- ...ackages.shulker-proxy-agent.bungeecord.iml | 2 +- ...er.packages.shulker-proxy-agent.common.iml | 2 +- .../shulker.packages.shulker-proxy-agent.iml | 2 +- ...lker.packages.shulker-proxy-agent.main.iml | 2 +- ...lker.packages.shulker-proxy-agent.test.iml | 2 +- ....packages.shulker-proxy-agent.velocity.iml | 2 +- .../shulker.packages.shulker-proxy-api.iml | 2 +- ...hulker.packages.shulker-proxy-api.main.iml | 2 +- ...hulker.packages.shulker-proxy-api.test.iml | 2 +- ...ker.packages.shulker-sdk-bindings-java.iml | 2 +- ...ackages.shulker-sdk-bindings-java.main.iml | 2 +- ...ackages.shulker-sdk-bindings-java.test.iml | 2 +- ...r.packages.shulker-server-agent.common.iml | 2 +- .../shulker.packages.shulker-server-agent.iml | 2 +- ...ker.packages.shulker-server-agent.main.iml | 2 +- ...er.packages.shulker-server-agent.paper.iml | 2 +- ...ker.packages.shulker-server-agent.test.iml | 2 +- .../shulker.packages.shulker-server-api.iml | 2 +- ...ulker.packages.shulker-server-api.main.iml | 2 +- ...ulker.packages.shulker-server-api.test.iml | 2 +- .idea/modules/packages/shulker.packages.iml | 2 +- .../packages/shulker.packages.main.iml | 2 +- .../packages/shulker.packages.test.iml | 2 +- .idea/modules/shulker.iml | 2 +- .idea/modules/shulker.main.iml | 2 +- .idea/modules/shulker.test.iml | 2 +- .../src/reconcilers/builder.rs | 14 ++- .../minecraft_cluster/forwarding_secret.rs | 21 +++- .../minecraft_server_role.rs | 24 ++++- .../minecraft_server_role_binding.rs | 19 +++- .../minecraft_server_service_account.rs | 9 +- .../src/reconcilers/minecraft_cluster/mod.rs | 20 ++-- .../minecraft_cluster/proxy_role.rs | 24 ++++- .../minecraft_cluster/proxy_role_binding.rs | 19 +++- .../proxy_service_account.rs | 9 +- .../minecraft_cluster/redis_service.rs | 9 +- .../minecraft_cluster/redis_stateful_set.rs | 9 +- .../minecraft_server/config_map.rs | 9 +- .../minecraft_server/gameserver.rs | 9 +- .../src/reconcilers/minecraft_server/mod.rs | 4 +- .../minecraft_server_fleet/config_map.rs | 6 +- .../minecraft_server_fleet/fleet.rs | 12 ++- .../fleet_autoscaler.rs | 6 +- .../reconcilers/minecraft_server_fleet/mod.rs | 11 ++- .../shulker-operator/src/reconcilers/mod.rs | 1 + .../src/reconcilers/proxy_fleet/config_map.rs | 26 +++-- .../src/reconcilers/proxy_fleet/fleet.rs | 99 +++++++++++++++++-- .../proxy_fleet/fleet_autoscaler.rs | 9 +- .../src/reconcilers/proxy_fleet/mod.rs | 19 ++-- .../src/reconcilers/proxy_fleet/service.rs | 9 +- ...y_fleet__fleet__tests__build_snapshot.snap | 4 + .../src/reconcilers/redis_ref.rs | 37 +++++++ .../src/resources/resourceref_resolver.rs | 1 - 56 files changed, 380 insertions(+), 117 deletions(-) create mode 100644 packages/shulker-operator/src/reconcilers/redis_ref.rs diff --git a/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.iml b/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.iml index 5881a06a..bcba3c0c 100644 --- a/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.iml +++ b/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.main.iml b/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.main.iml index 3df4c780..e02e12bd 100644 --- a/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.main.iml +++ b/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.test.iml b/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.test.iml index c4c3a25b..3d57d996 100644 --- a/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.test.iml +++ b/.idea/modules/packages/google-agones-sdk/bindings/java/shulker.packages.google-agones-sdk-bindings-java.test.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.bungeecord.iml b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.bungeecord.iml index 1b1b7a18..17c99862 100644 --- a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.bungeecord.iml +++ b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.bungeecord.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.common.iml b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.common.iml index 8e8f4870..de646be6 100644 --- a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.common.iml +++ b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.common.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.iml b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.iml index e2e45887..c945212f 100644 --- a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.iml +++ b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.main.iml b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.main.iml index 6d132254..aa621815 100644 --- a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.main.iml +++ b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.test.iml b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.test.iml index 9a69e6aa..eaf1b81c 100644 --- a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.test.iml +++ b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.test.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.velocity.iml b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.velocity.iml index aaeb27ce..0dd29b61 100644 --- a/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.velocity.iml +++ b/.idea/modules/packages/shulker-proxy-agent/shulker.packages.shulker-proxy-agent.velocity.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.iml b/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.iml index 4f250d53..dc341c56 100644 --- a/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.iml +++ b/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.main.iml b/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.main.iml index d707437d..69a1a5b3 100644 --- a/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.main.iml +++ b/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.test.iml b/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.test.iml index 885e02e9..e6c98412 100644 --- a/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.test.iml +++ b/.idea/modules/packages/shulker-proxy-api/shulker.packages.shulker-proxy-api.test.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.iml b/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.iml index f4322160..a9ba75c6 100644 --- a/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.iml +++ b/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.main.iml b/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.main.iml index 64507745..68375a29 100644 --- a/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.main.iml +++ b/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.test.iml b/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.test.iml index a230270f..40331a09 100644 --- a/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.test.iml +++ b/.idea/modules/packages/shulker-sdk/bindings/java/shulker.packages.shulker-sdk-bindings-java.test.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.common.iml b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.common.iml index 2742e45a..dd07f0b0 100644 --- a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.common.iml +++ b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.common.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.iml b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.iml index 0eafef32..22142d9a 100644 --- a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.iml +++ b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.main.iml b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.main.iml index 9bc87ef8..ae7c08c0 100644 --- a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.main.iml +++ b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.paper.iml b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.paper.iml index 3cd61c0f..028d91ec 100644 --- a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.paper.iml +++ b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.paper.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.test.iml b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.test.iml index 604aa898..d8335c92 100644 --- a/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.test.iml +++ b/.idea/modules/packages/shulker-server-agent/shulker.packages.shulker-server-agent.test.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.iml b/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.iml index 49a9a7f7..dbebaa91 100644 --- a/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.iml +++ b/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.main.iml b/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.main.iml index 690d8420..ea4ba241 100644 --- a/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.main.iml +++ b/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.test.iml b/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.test.iml index 8978e2e6..2a4afd10 100644 --- a/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.test.iml +++ b/.idea/modules/packages/shulker-server-api/shulker.packages.shulker-server-api.test.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker.packages.iml b/.idea/modules/packages/shulker.packages.iml index 3078cfef..5979f7de 100644 --- a/.idea/modules/packages/shulker.packages.iml +++ b/.idea/modules/packages/shulker.packages.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker.packages.main.iml b/.idea/modules/packages/shulker.packages.main.iml index ef5fae91..4d21f4eb 100644 --- a/.idea/modules/packages/shulker.packages.main.iml +++ b/.idea/modules/packages/shulker.packages.main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/packages/shulker.packages.test.iml b/.idea/modules/packages/shulker.packages.test.iml index fc89cf14..5cf223db 100644 --- a/.idea/modules/packages/shulker.packages.test.iml +++ b/.idea/modules/packages/shulker.packages.test.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/shulker.iml b/.idea/modules/shulker.iml index ab72b847..79ac7189 100644 --- a/.idea/modules/shulker.iml +++ b/.idea/modules/shulker.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/shulker.main.iml b/.idea/modules/shulker.main.iml index cc7886a3..3a6e57fa 100644 --- a/.idea/modules/shulker.main.iml +++ b/.idea/modules/shulker.main.iml @@ -1,5 +1,5 @@ - + diff --git a/.idea/modules/shulker.test.iml b/.idea/modules/shulker.test.iml index 40c6400c..eabeca5a 100644 --- a/.idea/modules/shulker.test.iml +++ b/.idea/modules/shulker.test.iml @@ -1,5 +1,5 @@ - + diff --git a/packages/shulker-operator/src/reconcilers/builder.rs b/packages/shulker-operator/src/reconcilers/builder.rs index 23c38490..7045e32f 100644 --- a/packages/shulker-operator/src/reconcilers/builder.rs +++ b/packages/shulker-operator/src/reconcilers/builder.rs @@ -8,13 +8,14 @@ use kube::{ use serde::{de::DeserializeOwned, Serialize}; #[async_trait::async_trait] -pub trait ResourceBuilder { +pub trait ResourceBuilder<'a> { type OwnerType: kube::Resource + Clone + Serialize + DeserializeOwned + Debug; type ResourceType: kube::Resource + Clone + Serialize + DeserializeOwned + Debug; + type Context: 'a + Sized + Clone + Debug; fn name(owner: &Self::OwnerType) -> String; fn api(&self, owner: &Self::OwnerType) -> Api; @@ -35,7 +36,10 @@ pub trait ResourceBuilder { owner: &Self::OwnerType, name: &str, existing_resource: Option<&Self::ResourceType>, - ) -> Result; + context: Option, + ) -> Result + where + 'a: 'async_trait; } async fn get_existing< @@ -66,12 +70,14 @@ fn set_controller_reference< } pub async fn reconcile_builder< + 'a, O: kube::Resource + Clone + Serialize + DeserializeOwned + Debug, R: kube::Resource + Clone + Serialize + DeserializeOwned + Debug, - RB: ResourceBuilder, + RB: ResourceBuilder<'a, OwnerType = O, ResourceType = R>, >( builder: &RB, owner: &O, + context: Option, ) -> super::Result> { let api = builder.api(owner); let name = RB::name(owner); @@ -139,7 +145,7 @@ pub async fn reconcile_builder< } let mut new_resource = builder - .build(owner, &name, existing_resource.as_ref()) + .build(owner, &name, existing_resource.as_ref(), context) .await .map_err(|e| super::ReconcilerError::BuilderError(std::any::type_name::(), e))?; diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/forwarding_secret.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/forwarding_secret.rs index 08b66073..a78b9050 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/forwarding_secret.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/forwarding_secret.rs @@ -20,9 +20,10 @@ pub struct ForwardingSecretBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for ForwardingSecretBuilder { +impl<'a> ResourceBuilder<'a> for ForwardingSecretBuilder { type OwnerType = MinecraftCluster; type ResourceType = Secret; + type Context = (); fn name(cluster: &Self::OwnerType) -> String { format!("{}-forwarding-secret", cluster.name_any()) @@ -37,6 +38,7 @@ impl ResourceBuilder for ForwardingSecretBuilder { cluster: &Self::OwnerType, name: &str, existing_secret: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let secret = Secret { metadata: ObjectMeta { @@ -132,7 +134,10 @@ mod tests { let name = super::ForwardingSecretBuilder::name(&TEST_CLUSTER); // W - let secret = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let secret = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(secret, { @@ -148,7 +153,10 @@ mod tests { let name = super::ForwardingSecretBuilder::name(&TEST_CLUSTER); // W - let secret = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let secret = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert!(secret @@ -164,11 +172,14 @@ mod tests { let client = create_client_mock(); let builder = super::ForwardingSecretBuilder::new(client); let name = super::ForwardingSecretBuilder::name(&TEST_CLUSTER); - let existing_secret = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let existing_secret = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // W let secret = builder - .build(&TEST_CLUSTER, &name, Some(&existing_secret)) + .build(&TEST_CLUSTER, &name, Some(&existing_secret), None) .await .unwrap(); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_role.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_role.rs index 942fef51..2d09d1a9 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_role.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_role.rs @@ -14,9 +14,10 @@ pub struct MinecraftServerRoleBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for MinecraftServerRoleBuilder { +impl<'a> ResourceBuilder<'a> for MinecraftServerRoleBuilder { type OwnerType = MinecraftCluster; type ResourceType = Role; + type Context = (); fn name(cluster: &Self::OwnerType) -> String { format!("shulker:{}:server", cluster.name_any()) @@ -31,6 +32,7 @@ impl ResourceBuilder for MinecraftServerRoleBuilder { cluster: &Self::OwnerType, name: &str, _existing_role: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let role = Role { metadata: ObjectMeta { @@ -97,7 +99,10 @@ mod tests { let name = super::MinecraftServerRoleBuilder::name(&TEST_CLUSTER); // W - let role = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(role); @@ -111,7 +116,10 @@ mod tests { let name = super::MinecraftServerRoleBuilder::name(&TEST_CLUSTER); // W - let role = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert!(role.rules.as_ref().unwrap().iter().any(|rule| { @@ -129,7 +137,10 @@ mod tests { let name = super::MinecraftServerRoleBuilder::name(&TEST_CLUSTER); // W - let role = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert!(role.rules.as_ref().unwrap().iter().any(|rule| { @@ -152,7 +163,10 @@ mod tests { let name = super::MinecraftServerRoleBuilder::name(&TEST_CLUSTER); // W - let role = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert!(role.rules.as_ref().unwrap().iter().any(|rule| { diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_role_binding.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_role_binding.rs index 4a94ec59..5eb6fffd 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_role_binding.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_role_binding.rs @@ -17,9 +17,10 @@ pub struct MinecraftServerRoleBindingBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for MinecraftServerRoleBindingBuilder { +impl<'a> ResourceBuilder<'a> for MinecraftServerRoleBindingBuilder { type OwnerType = MinecraftCluster; type ResourceType = RoleBinding; + type Context = (); fn name(cluster: &Self::OwnerType) -> String { format!("shulker:{}:server", cluster.name_any()) @@ -38,6 +39,7 @@ impl ResourceBuilder for MinecraftServerRoleBindingBuilder { cluster: &Self::OwnerType, name: &str, _existing_role_binding: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let role_binding = RoleBinding { metadata: ObjectMeta { @@ -141,7 +143,10 @@ mod tests { let name = super::MinecraftServerRoleBindingBuilder::name(&TEST_CLUSTER); // W - let role_binding = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role_binding = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(role_binding); @@ -155,7 +160,10 @@ mod tests { let name = super::MinecraftServerRoleBindingBuilder::name(&TEST_CLUSTER); // W - let role_binding = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role_binding = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert_eq!( @@ -176,7 +184,10 @@ mod tests { let name = super::MinecraftServerRoleBindingBuilder::name(&TEST_CLUSTER); // W - let role_binding = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role_binding = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert_eq!( diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_service_account.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_service_account.rs index f7bab3b3..4d66e986 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_service_account.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/minecraft_server_service_account.rs @@ -13,9 +13,10 @@ pub struct MinecraftServerServiceAccountBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for MinecraftServerServiceAccountBuilder { +impl<'a> ResourceBuilder<'a> for MinecraftServerServiceAccountBuilder { type OwnerType = MinecraftCluster; type ResourceType = ServiceAccount; + type Context = (); fn name(cluster: &Self::OwnerType) -> String { format!("shulker-{}-server", cluster.name_any()) @@ -30,6 +31,7 @@ impl ResourceBuilder for MinecraftServerServiceAccountBuilder { cluster: &Self::OwnerType, name: &str, _existing_service_account: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let service_account = ServiceAccount { metadata: ObjectMeta { @@ -79,7 +81,10 @@ mod tests { let name = super::MinecraftServerServiceAccountBuilder::name(&TEST_CLUSTER); // W - let service_account = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let service_account = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(service_account); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/mod.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/mod.rs index 555dbd22..7e6eb058 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/mod.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/mod.rs @@ -38,11 +38,11 @@ mod minecraft_server_service_account; mod proxy_role; mod proxy_role_binding; mod proxy_service_account; -mod redis_service; +pub mod redis_service; mod redis_stateful_set; #[cfg(test)] -mod fixtures; +pub mod fixtures; static FINALIZER: &str = "minecraftclusters.shulkermc.io"; @@ -67,23 +67,25 @@ impl MinecraftClusterReconciler { _api: Api, cluster: Arc, ) -> Result { - reconcile_builder(&self.forwarding_secret_builder, cluster.as_ref()).await?; - reconcile_builder(&self.proxy_service_account_builder, cluster.as_ref()).await?; - reconcile_builder(&self.proxy_role_builder, cluster.as_ref()).await?; - reconcile_builder(&self.proxy_role_binding_builder, cluster.as_ref()).await?; + reconcile_builder(&self.forwarding_secret_builder, cluster.as_ref(), None).await?; + reconcile_builder(&self.proxy_service_account_builder, cluster.as_ref(), None).await?; + reconcile_builder(&self.proxy_role_builder, cluster.as_ref(), None).await?; + reconcile_builder(&self.proxy_role_binding_builder, cluster.as_ref(), None).await?; reconcile_builder( &self.minecraft_server_service_account_builder, cluster.as_ref(), + None, ) .await?; - reconcile_builder(&self.minecraft_server_role_builder, cluster.as_ref()).await?; + reconcile_builder(&self.minecraft_server_role_builder, cluster.as_ref(), None).await?; reconcile_builder( &self.minecraft_server_role_binding_builder, cluster.as_ref(), + None, ) .await?; - reconcile_builder(&self.redis_service_builder, cluster.as_ref()).await?; - reconcile_builder(&self.redis_stateful_set_builder, cluster.as_ref()).await?; + reconcile_builder(&self.redis_service_builder, cluster.as_ref(), None).await?; + reconcile_builder(&self.redis_stateful_set_builder, cluster.as_ref(), None).await?; Ok(Action::requeue(Duration::from_secs(5 * 60))) } diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_role.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_role.rs index 789f51ba..5c673f2f 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_role.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_role.rs @@ -14,9 +14,10 @@ pub struct ProxyRoleBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for ProxyRoleBuilder { +impl<'a> ResourceBuilder<'a> for ProxyRoleBuilder { type OwnerType = MinecraftCluster; type ResourceType = Role; + type Context = (); fn name(cluster: &Self::OwnerType) -> String { format!("shulker:{}:proxy", cluster.name_any()) @@ -31,6 +32,7 @@ impl ResourceBuilder for ProxyRoleBuilder { cluster: &Self::OwnerType, name: &str, _existing_role: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let role = Role { metadata: ObjectMeta { @@ -97,7 +99,10 @@ mod tests { let name = super::ProxyRoleBuilder::name(&TEST_CLUSTER); // W - let role = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(role); @@ -111,7 +116,10 @@ mod tests { let name = super::ProxyRoleBuilder::name(&TEST_CLUSTER); // W - let role = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert!(role.rules.as_ref().unwrap().iter().any(|rule| { @@ -129,7 +137,10 @@ mod tests { let name = super::ProxyRoleBuilder::name(&TEST_CLUSTER); // W - let role = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert!(role.rules.as_ref().unwrap().iter().any(|rule| { @@ -152,7 +163,10 @@ mod tests { let name = super::ProxyRoleBuilder::name(&TEST_CLUSTER); // W - let role = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert!(role.rules.as_ref().unwrap().iter().any(|rule| { diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_role_binding.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_role_binding.rs index 094b88f1..ee71905c 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_role_binding.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_role_binding.rs @@ -17,9 +17,10 @@ pub struct ProxyRoleBindingBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for ProxyRoleBindingBuilder { +impl<'a> ResourceBuilder<'a> for ProxyRoleBindingBuilder { type OwnerType = MinecraftCluster; type ResourceType = RoleBinding; + type Context = (); fn name(cluster: &Self::OwnerType) -> String { format!("shulker:{}:proxy", cluster.name_any()) @@ -38,6 +39,7 @@ impl ResourceBuilder for ProxyRoleBindingBuilder { cluster: &Self::OwnerType, name: &str, _existing_role_binding: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let role_binding = RoleBinding { metadata: ObjectMeta { @@ -137,7 +139,10 @@ mod tests { let name = super::ProxyRoleBindingBuilder::name(&TEST_CLUSTER); // W - let role_binding = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role_binding = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(role_binding); @@ -151,7 +156,10 @@ mod tests { let name = super::ProxyRoleBindingBuilder::name(&TEST_CLUSTER); // W - let role_binding = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role_binding = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert_eq!( @@ -172,7 +180,10 @@ mod tests { let name = super::ProxyRoleBindingBuilder::name(&TEST_CLUSTER); // W - let role_binding = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role_binding = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T assert_eq!( diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_service_account.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_service_account.rs index aa7cdef0..501be630 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_service_account.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/proxy_service_account.rs @@ -13,9 +13,10 @@ pub struct ProxyServiceAccountBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for ProxyServiceAccountBuilder { +impl<'a> ResourceBuilder<'a> for ProxyServiceAccountBuilder { type OwnerType = MinecraftCluster; type ResourceType = ServiceAccount; + type Context = (); fn name(cluster: &Self::OwnerType) -> String { format!("shulker-{}-proxy", cluster.name_any()) @@ -30,6 +31,7 @@ impl ResourceBuilder for ProxyServiceAccountBuilder { cluster: &Self::OwnerType, name: &str, _existing_service_account: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let service_account = ServiceAccount { metadata: ObjectMeta { @@ -79,7 +81,10 @@ mod tests { let name = super::ProxyServiceAccountBuilder::name(&TEST_CLUSTER); // W - let service_account = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let service_account = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(service_account); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/redis_service.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/redis_service.rs index de191411..d7c4aa19 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/redis_service.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/redis_service.rs @@ -18,9 +18,10 @@ pub struct RedisServiceBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for RedisServiceBuilder { +impl<'a> ResourceBuilder<'a> for RedisServiceBuilder { type OwnerType = MinecraftCluster; type ResourceType = Service; + type Context = (); fn name(cluster: &Self::OwnerType) -> String { format!("{}-redis-managed", cluster.name_any()) @@ -41,6 +42,7 @@ impl ResourceBuilder for RedisServiceBuilder { cluster: &Self::OwnerType, name: &str, _existing_service: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let service = Service { metadata: ObjectMeta { @@ -159,7 +161,10 @@ mod tests { let name = super::RedisServiceBuilder::name(&TEST_CLUSTER); // W - let service = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let service = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(service); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_cluster/redis_stateful_set.rs b/packages/shulker-operator/src/reconcilers/minecraft_cluster/redis_stateful_set.rs index 18a2e93e..9f5308e3 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_cluster/redis_stateful_set.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_cluster/redis_stateful_set.rs @@ -48,9 +48,10 @@ pub struct RedisStatefulSetBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for RedisStatefulSetBuilder { +impl<'a> ResourceBuilder<'a> for RedisStatefulSetBuilder { type OwnerType = MinecraftCluster; type ResourceType = StatefulSet; + type Context = (); fn name(cluster: &Self::OwnerType) -> String { format!("{}-redis-managed", cluster.name_any()) @@ -71,6 +72,7 @@ impl ResourceBuilder for RedisStatefulSetBuilder { cluster: &Self::OwnerType, name: &str, _existing_stateful_set: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let stateful_set = StatefulSet { metadata: ObjectMeta { @@ -252,7 +254,10 @@ mod tests { let name = super::RedisStatefulSetBuilder::name(&TEST_CLUSTER); // W - let role = builder.build(&TEST_CLUSTER, &name, None).await.unwrap(); + let role = builder + .build(&TEST_CLUSTER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(role); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_server/config_map.rs b/packages/shulker-operator/src/reconcilers/minecraft_server/config_map.rs index 6a7698b0..e054661c 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_server/config_map.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_server/config_map.rs @@ -17,9 +17,10 @@ pub struct ConfigMapBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for ConfigMapBuilder { +impl<'a> ResourceBuilder<'a> for ConfigMapBuilder { type OwnerType = MinecraftServer; type ResourceType = ConfigMap; + type Context = (); fn name(minecraft_server: &Self::OwnerType) -> String { format!("{}-config", minecraft_server.name_any()) @@ -45,6 +46,7 @@ impl ResourceBuilder for ConfigMapBuilder { minecraft_server: &Self::OwnerType, name: &str, _existing_config_map: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let config_map = ConfigMap { metadata: ObjectMeta { @@ -120,7 +122,10 @@ mod tests { let name = super::ConfigMapBuilder::name(&TEST_SERVER); // W - let config_map = builder.build(&TEST_SERVER, &name, None).await.unwrap(); + let config_map = builder + .build(&TEST_SERVER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(config_map); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_server/gameserver.rs b/packages/shulker-operator/src/reconcilers/minecraft_server/gameserver.rs index 699cba5b..6ae15759 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_server/gameserver.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_server/gameserver.rs @@ -63,9 +63,10 @@ pub struct GameServerBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for GameServerBuilder { +impl<'a> ResourceBuilder<'a> for GameServerBuilder { type OwnerType = MinecraftServer; type ResourceType = GameServer; + type Context = (); fn name(minecraft_server: &Self::OwnerType) -> String { minecraft_server.name_any() @@ -83,6 +84,7 @@ impl ResourceBuilder for GameServerBuilder { minecraft_server: &Self::OwnerType, name: &str, _existing_game_server: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let game_server = GameServer { metadata: ObjectMeta { @@ -489,7 +491,10 @@ mod tests { let name = super::GameServerBuilder::name(&TEST_SERVER); // W - let game_server = builder.build(&TEST_SERVER, &name, None).await.unwrap(); + let game_server = builder + .build(&TEST_SERVER, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(game_server); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_server/mod.rs b/packages/shulker-operator/src/reconcilers/minecraft_server/mod.rs index 4b9be521..52a20601 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_server/mod.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_server/mod.rs @@ -49,9 +49,9 @@ impl MinecraftServerReconciler { ) .await?; - reconcile_builder(&self.config_map_builder, minecraft_server.as_ref()).await?; + reconcile_builder(&self.config_map_builder, minecraft_server.as_ref(), None).await?; let gameserver = - reconcile_builder(&self.gameserver_builder, minecraft_server.as_ref()).await?; + reconcile_builder(&self.gameserver_builder, minecraft_server.as_ref(), None).await?; if let Some(gameserver) = &gameserver { if let Some(gameserver_status) = &gameserver.status { diff --git a/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/config_map.rs b/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/config_map.rs index 0593293b..eab52d97 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/config_map.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/config_map.rs @@ -14,9 +14,10 @@ pub struct ConfigMapBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for ConfigMapBuilder { +impl<'a> ResourceBuilder<'a> for ConfigMapBuilder { type OwnerType = MinecraftServerFleet; type ResourceType = ConfigMap; + type Context = (); fn name(minecraft_server_fleet: &Self::OwnerType) -> String { format!("{}-config", minecraft_server_fleet.name_any()) @@ -34,6 +35,7 @@ impl ResourceBuilder for ConfigMapBuilder { minecraft_server_fleet: &Self::OwnerType, name: &str, _existing_config_map: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let config_map = ConfigMap { metadata: ObjectMeta { @@ -87,7 +89,7 @@ mod tests { // W let config_map = builder - .build(&TEST_SERVER_FLEET, &name, None) + .build(&TEST_SERVER_FLEET, &name, None, None) .await .unwrap(); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/fleet.rs b/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/fleet.rs index 58116678..b3359ab2 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/fleet.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/fleet.rs @@ -25,9 +25,10 @@ pub struct FleetBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for FleetBuilder { +impl<'a> ResourceBuilder<'a> for FleetBuilder { type OwnerType = MinecraftServerFleet; type ResourceType = Fleet; + type Context = (); fn name(minecraft_server_fleet: &Self::OwnerType) -> String { minecraft_server_fleet.name_any() @@ -45,6 +46,7 @@ impl ResourceBuilder for FleetBuilder { minecraft_server_fleet: &Self::OwnerType, name: &str, _existing_fleet: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let fake_mincraft_server = MinecraftServer { metadata: ObjectMeta { @@ -181,7 +183,7 @@ mod tests { // W let fleet = builder - .build(&TEST_SERVER_FLEET, &name, None) + .build(&TEST_SERVER_FLEET, &name, None, None) .await .unwrap(); @@ -198,7 +200,7 @@ mod tests { // W let fleet = builder - .build(&TEST_SERVER_FLEET, &name, None) + .build(&TEST_SERVER_FLEET, &name, None, None) .await .unwrap(); @@ -239,7 +241,7 @@ mod tests { // W let fleet = builder - .build(&TEST_SERVER_FLEET, &name, None) + .build(&TEST_SERVER_FLEET, &name, None, None) .await .unwrap(); @@ -280,7 +282,7 @@ mod tests { // W let fleet = builder - .build(&TEST_SERVER_FLEET, &name, None) + .build(&TEST_SERVER_FLEET, &name, None, None) .await .unwrap(); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/fleet_autoscaler.rs b/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/fleet_autoscaler.rs index 5431b30f..19a7e983 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/fleet_autoscaler.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/fleet_autoscaler.rs @@ -16,9 +16,10 @@ pub struct FleetAutoscalerBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for FleetAutoscalerBuilder { +impl<'a> ResourceBuilder<'a> for FleetAutoscalerBuilder { type OwnerType = MinecraftServerFleet; type ResourceType = FleetAutoscaler; + type Context = (); fn name(minecraft_server_fleet: &Self::OwnerType) -> String { minecraft_server_fleet.name_any() @@ -43,6 +44,7 @@ impl ResourceBuilder for FleetAutoscalerBuilder { minecraft_server_fleet: &Self::OwnerType, name: &str, _existing_fleet_autoscaler: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let fleet_autoscaler = FleetAutoscaler { metadata: ObjectMeta { @@ -133,7 +135,7 @@ mod tests { // W let fleet_autoscaler = builder - .build(&TEST_SERVER_FLEET, &name, None) + .build(&TEST_SERVER_FLEET, &name, None, None) .await .unwrap(); diff --git a/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/mod.rs b/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/mod.rs index 31b0e5ab..a7797d68 100644 --- a/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/mod.rs +++ b/packages/shulker-operator/src/reconcilers/minecraft_server_fleet/mod.rs @@ -53,11 +53,18 @@ impl MinecraftServerFleetReconciler { ) .await?; - reconcile_builder(&self.config_map_builder, minecraft_server_fleet.as_ref()).await?; - let fleet = reconcile_builder(&self.fleet_builder, minecraft_server_fleet.as_ref()).await?; + reconcile_builder( + &self.config_map_builder, + minecraft_server_fleet.as_ref(), + None, + ) + .await?; + let fleet = + reconcile_builder(&self.fleet_builder, minecraft_server_fleet.as_ref(), None).await?; reconcile_builder( &self.fleet_autoscaler_builder, minecraft_server_fleet.as_ref(), + None, ) .await?; diff --git a/packages/shulker-operator/src/reconcilers/mod.rs b/packages/shulker-operator/src/reconcilers/mod.rs index 9f4f9433..0cf7ce38 100644 --- a/packages/shulker-operator/src/reconcilers/mod.rs +++ b/packages/shulker-operator/src/reconcilers/mod.rs @@ -6,6 +6,7 @@ pub mod minecraft_cluster; pub mod minecraft_server; pub mod minecraft_server_fleet; pub mod proxy_fleet; +mod redis_ref; pub mod status; #[derive(Error, Debug)] diff --git a/packages/shulker-operator/src/reconcilers/proxy_fleet/config_map.rs b/packages/shulker-operator/src/reconcilers/proxy_fleet/config_map.rs index 00718bec..291e122e 100644 --- a/packages/shulker-operator/src/reconcilers/proxy_fleet/config_map.rs +++ b/packages/shulker-operator/src/reconcilers/proxy_fleet/config_map.rs @@ -17,9 +17,10 @@ pub struct ConfigMapBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for ConfigMapBuilder { +impl<'a> ResourceBuilder<'a> for ConfigMapBuilder { type OwnerType = ProxyFleet; type ResourceType = ConfigMap; + type Context = (); fn name(proxy_fleet: &Self::OwnerType) -> String { format!("{}-config", proxy_fleet.name_any()) @@ -37,6 +38,7 @@ impl ResourceBuilder for ConfigMapBuilder { proxy_fleet: &Self::OwnerType, name: &str, _existing_config_map: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let mut data = BTreeMap::from([ ( @@ -121,7 +123,10 @@ mod tests { let name = super::ConfigMapBuilder::name(&TEST_PROXY_FLEET); // W - let config_map = builder.build(&TEST_PROXY_FLEET, &name, None).await.unwrap(); + let config_map = builder + .build(&TEST_PROXY_FLEET, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(config_map); @@ -135,7 +140,10 @@ mod tests { let name = super::ConfigMapBuilder::name(&TEST_PROXY_FLEET); // W - let config_map = builder.build(&TEST_PROXY_FLEET, &name, None).await.unwrap(); + let config_map = builder + .build(&TEST_PROXY_FLEET, &name, None, None) + .await + .unwrap(); // T assert!(config_map.data.as_ref().unwrap().contains_key("init-fs.sh")); @@ -149,7 +157,10 @@ mod tests { let name = super::ConfigMapBuilder::name(&TEST_PROXY_FLEET); // W - let config_map = builder.build(&TEST_PROXY_FLEET, &name, None).await.unwrap(); + let config_map = builder + .build(&TEST_PROXY_FLEET, &name, None, None) + .await + .unwrap(); // T assert!(config_map @@ -167,7 +178,10 @@ mod tests { let name = super::ConfigMapBuilder::name(&TEST_PROXY_FLEET); // W - let config_map = builder.build(&TEST_PROXY_FLEET, &name, None).await.unwrap(); + let config_map = builder + .build(&TEST_PROXY_FLEET, &name, None, None) + .await + .unwrap(); // T assert!(config_map @@ -192,7 +206,7 @@ mod tests { fleet.spec.template.spec.version.channel = ProxyFleetTemplateVersion::BungeeCord; // W - let config_map = builder.build(&fleet, &name, None).await.unwrap(); + let config_map = builder.build(&fleet, &name, None, None).await.unwrap(); // T assert!(config_map diff --git a/packages/shulker-operator/src/reconcilers/proxy_fleet/fleet.rs b/packages/shulker-operator/src/reconcilers/proxy_fleet/fleet.rs index e46c99fe..4bce4dc9 100644 --- a/packages/shulker-operator/src/reconcilers/proxy_fleet/fleet.rs +++ b/packages/shulker-operator/src/reconcilers/proxy_fleet/fleet.rs @@ -15,6 +15,7 @@ use k8s_openapi::api::core::v1::ObjectFieldSelector; use k8s_openapi::api::core::v1::PodSpec; use k8s_openapi::api::core::v1::PodTemplateSpec; use k8s_openapi::api::core::v1::Probe; +use k8s_openapi::api::core::v1::SecretKeySelector; use k8s_openapi::api::core::v1::SecretVolumeSource; use k8s_openapi::api::core::v1::SecurityContext; use k8s_openapi::api::core::v1::Volume; @@ -24,9 +25,11 @@ use k8s_openapi::apimachinery::pkg::util::intstr::IntOrString; use kube::Api; use kube::Client; use kube::ResourceExt; +use shulker_crds::v1alpha1::minecraft_cluster::MinecraftCluster; use shulker_crds::v1alpha1::proxy_fleet::ProxyFleetTemplateVersion; use crate::reconcilers::builder::ResourceBuilder; +use crate::reconcilers::redis_ref::RedisRef; use crate::resources::resourceref_resolver::ResourceRefResolver; use google_agones_crds::v1::fleet::Fleet; use google_agones_crds::v1::fleet::FleetSpec; @@ -69,10 +72,16 @@ pub struct FleetBuilder { resourceref_resolver: ResourceRefResolver, } +#[derive(Clone, Debug)] +pub struct FleetBuilderContext<'a> { + pub cluster: &'a MinecraftCluster, +} + #[async_trait::async_trait] -impl ResourceBuilder for FleetBuilder { +impl<'a> ResourceBuilder<'a> for FleetBuilder { type OwnerType = ProxyFleet; type ResourceType = Fleet; + type Context = FleetBuilderContext<'a>; fn name(proxy_fleet: &Self::OwnerType) -> String { proxy_fleet.name_any() @@ -90,8 +99,11 @@ impl ResourceBuilder for FleetBuilder { proxy_fleet: &Self::OwnerType, name: &str, _existing_fleet: Option<&Self::ResourceType>, + context: Option>, ) -> Result { - let game_server_spec = self.get_game_server_spec(proxy_fleet).await?; + let game_server_spec = self + .get_game_server_spec(context.unwrap().cluster, proxy_fleet) + .await?; let replicas = match &proxy_fleet.spec.autoscaling { Some(_) => 0, None => proxy_fleet.spec.replicas as i32, @@ -140,9 +152,10 @@ impl FleetBuilder { async fn get_game_server_spec( &self, + cluster: &MinecraftCluster, proxy_fleet: &ProxyFleet, ) -> Result { - let pod_template_spec = self.get_pod_template_spec(proxy_fleet).await?; + let pod_template_spec = self.get_pod_template_spec(cluster, proxy_fleet).await?; let game_server_spec = GameServerSpec { ports: Some(vec![]), eviction: Some(GameServerEvictionSpec { @@ -162,6 +175,7 @@ impl FleetBuilder { async fn get_pod_template_spec( &self, + cluster: &MinecraftCluster, proxy_fleet: &ProxyFleet, ) -> Result { let mut pod_spec = PodSpec { @@ -197,7 +211,7 @@ impl FleetBuilder { container_port: 25577, ..ContainerPort::default() }]), - env: Some(self.get_env(&proxy_fleet.spec.template.spec)), + env: Some(self.get_env(cluster, &proxy_fleet.spec.template.spec)?), readiness_probe: Some(Probe { exec: Some(ExecAction { command: Some(vec![ @@ -398,7 +412,13 @@ impl FleetBuilder { Ok(env) } - fn get_env(&self, spec: &ProxyFleetTemplateSpec) -> Vec { + fn get_env( + &self, + cluster: &MinecraftCluster, + spec: &ProxyFleetTemplateSpec, + ) -> Result, anyhow::Error> { + let redis_ref = RedisRef::from_cluster(cluster)?; + let mut env: Vec = vec![ EnvVar { name: "SHULKER_PROXY_NAME".to_string(), @@ -427,6 +447,16 @@ impl FleetBuilder { value: Some(spec.config.ttl_seconds.to_string()), ..EnvVar::default() }, + EnvVar { + name: "SHULKER_PROXY_REDIS_HOST".to_string(), + value: Some(redis_ref.host), + ..EnvVar::default() + }, + EnvVar { + name: "SHULKER_PROXY_REDIS_PORT".to_string(), + value: Some(redis_ref.port.to_string()), + ..EnvVar::default() + }, EnvVar { name: "TYPE".to_string(), value: Some(Self::get_type_from_version_channel(&spec.version.channel)), @@ -444,13 +474,43 @@ impl FleetBuilder { }, ]; + if let Some(redis_ref_credentials_secret_name) = redis_ref.credentials_secret_name.as_ref() + { + env.append(&mut vec![ + EnvVar { + name: "SHULKER_PROXY_REDIS_USERNAME".to_string(), + value_from: Some(EnvVarSource { + secret_key_ref: Some(SecretKeySelector { + name: Some(redis_ref_credentials_secret_name.clone()), + key: "username".to_string(), + ..SecretKeySelector::default() + }), + ..EnvVarSource::default() + }), + ..EnvVar::default() + }, + EnvVar { + name: "SHULKER_PROXY_REDIS_PASSWORD".to_string(), + value_from: Some(EnvVarSource { + secret_key_ref: Some(SecretKeySelector { + name: Some(redis_ref_credentials_secret_name.clone()), + key: "password".to_string(), + ..SecretKeySelector::default() + }), + ..EnvVarSource::default() + }), + ..EnvVar::default() + }, + ]) + } + if let Some(pod_overrides) = &spec.pod_overrides { if let Some(env_overrides) = &pod_overrides.env { env.extend(env_overrides.clone()); } } - env + Ok(env) } fn get_type_from_version_channel(channel: &ProxyFleetTemplateVersion) -> String { @@ -476,6 +536,7 @@ mod tests { use crate::reconcilers::{ builder::ResourceBuilder, + minecraft_cluster::fixtures::TEST_CLUSTER, proxy_fleet::fixtures::{create_client_mock, TEST_PROXY_FLEET}, }; @@ -494,9 +555,15 @@ mod tests { let client = create_client_mock(); let builder = super::FleetBuilder::new(client); let name = super::FleetBuilder::name(&TEST_PROXY_FLEET); + let context = super::FleetBuilderContext { + cluster: &TEST_CLUSTER, + }; // W - let fleet = builder.build(&TEST_PROXY_FLEET, &name, None).await.unwrap(); + let fleet = builder + .build(&TEST_PROXY_FLEET, &name, None, Some(context)) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(fleet); @@ -508,9 +575,15 @@ mod tests { let client = create_client_mock(); let builder = super::FleetBuilder::new(client); let name = super::FleetBuilder::name(&TEST_PROXY_FLEET); + let context = super::FleetBuilderContext { + cluster: &TEST_CLUSTER, + }; // W - let fleet = builder.build(&TEST_PROXY_FLEET, &name, None).await.unwrap(); + let fleet = builder + .build(&TEST_PROXY_FLEET, &name, None, Some(context)) + .await + .unwrap(); // T let additional_labels = TEST_PROXY_FLEET @@ -546,9 +619,15 @@ mod tests { let client = create_client_mock(); let builder = super::FleetBuilder::new(client); let name = super::FleetBuilder::name(&TEST_PROXY_FLEET); + let context = super::FleetBuilderContext { + cluster: &TEST_CLUSTER, + }; // W - let fleet = builder.build(&TEST_PROXY_FLEET, &name, None).await.unwrap(); + let fleet = builder + .build(&TEST_PROXY_FLEET, &name, None, Some(context)) + .await + .unwrap(); // T let additional_annotations = TEST_PROXY_FLEET @@ -636,7 +715,7 @@ mod tests { let spec = TEST_PROXY_FLEET.spec.clone(); // W - let env = builder.get_env(&spec.template.spec); + let env = builder.get_env(&TEST_CLUSTER, &spec.template.spec).unwrap(); // T spec.template diff --git a/packages/shulker-operator/src/reconcilers/proxy_fleet/fleet_autoscaler.rs b/packages/shulker-operator/src/reconcilers/proxy_fleet/fleet_autoscaler.rs index 499fdcb5..d0fbd41d 100644 --- a/packages/shulker-operator/src/reconcilers/proxy_fleet/fleet_autoscaler.rs +++ b/packages/shulker-operator/src/reconcilers/proxy_fleet/fleet_autoscaler.rs @@ -16,9 +16,10 @@ pub struct FleetAutoscalerBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for FleetAutoscalerBuilder { +impl<'a> ResourceBuilder<'a> for FleetAutoscalerBuilder { type OwnerType = ProxyFleet; type ResourceType = FleetAutoscaler; + type Context = (); fn name(proxy_fleet: &Self::OwnerType) -> String { proxy_fleet.name_any() @@ -43,6 +44,7 @@ impl ResourceBuilder for FleetAutoscalerBuilder { proxy_fleet: &Self::OwnerType, name: &str, _existing_fleet_autoscaler: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let fleet_autoscaler = FleetAutoscaler { metadata: ObjectMeta { @@ -132,7 +134,10 @@ mod tests { let name = super::FleetAutoscalerBuilder::name(&TEST_PROXY_FLEET); // W - let fleet_autoscaler = builder.build(&TEST_PROXY_FLEET, &name, None).await.unwrap(); + let fleet_autoscaler = builder + .build(&TEST_PROXY_FLEET, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(fleet_autoscaler); diff --git a/packages/shulker-operator/src/reconcilers/proxy_fleet/mod.rs b/packages/shulker-operator/src/reconcilers/proxy_fleet/mod.rs index 702ead3f..d617c0ce 100644 --- a/packages/shulker-operator/src/reconcilers/proxy_fleet/mod.rs +++ b/packages/shulker-operator/src/reconcilers/proxy_fleet/mod.rs @@ -16,7 +16,9 @@ use shulker_crds::{ }; use self::{ - config_map::ConfigMapBuilder, fleet::FleetBuilder, fleet_autoscaler::FleetAutoscalerBuilder, + config_map::ConfigMapBuilder, + fleet::{FleetBuilder, FleetBuilderContext}, + fleet_autoscaler::FleetAutoscalerBuilder, service::ServiceBuilder, }; @@ -49,17 +51,22 @@ impl ProxyFleetReconciler { api: Api, proxy_fleet: Arc, ) -> Result { - resolve_cluster_ref( + let cluster = resolve_cluster_ref( &self.client, &proxy_fleet.namespace().unwrap(), &proxy_fleet.spec.cluster_ref, ) .await?; - reconcile_builder(&self.config_map_builder, proxy_fleet.as_ref()).await?; - reconcile_builder(&self.service_builder, proxy_fleet.as_ref()).await?; - let fleet = reconcile_builder(&self.fleet_builder, proxy_fleet.as_ref()).await?; - reconcile_builder(&self.fleet_autoscaler_builder, proxy_fleet.as_ref()).await?; + reconcile_builder(&self.config_map_builder, proxy_fleet.as_ref(), None).await?; + reconcile_builder(&self.service_builder, proxy_fleet.as_ref(), None).await?; + let fleet = reconcile_builder( + &self.fleet_builder, + proxy_fleet.as_ref(), + Some(FleetBuilderContext { cluster: &cluster }), + ) + .await?; + reconcile_builder(&self.fleet_autoscaler_builder, proxy_fleet.as_ref(), None).await?; if let Some(fleet) = &fleet { if let Some(fleet_status) = &fleet.status { diff --git a/packages/shulker-operator/src/reconcilers/proxy_fleet/service.rs b/packages/shulker-operator/src/reconcilers/proxy_fleet/service.rs index 77f463bb..3f92127a 100644 --- a/packages/shulker-operator/src/reconcilers/proxy_fleet/service.rs +++ b/packages/shulker-operator/src/reconcilers/proxy_fleet/service.rs @@ -17,9 +17,10 @@ pub struct ServiceBuilder { } #[async_trait::async_trait] -impl ResourceBuilder for ServiceBuilder { +impl<'a> ResourceBuilder<'a> for ServiceBuilder { type OwnerType = ProxyFleet; type ResourceType = Service; + type Context = (); fn name(proxy_fleet: &Self::OwnerType) -> String { proxy_fleet.name_any() @@ -41,6 +42,7 @@ impl ResourceBuilder for ServiceBuilder { proxy_fleet: &Self::OwnerType, name: &str, _existing_service: Option<&Self::ResourceType>, + _context: Option, ) -> Result { let service_config = proxy_fleet.spec.service.as_ref().unwrap(); let service = Service { @@ -139,7 +141,10 @@ mod tests { let name = super::ServiceBuilder::name(&TEST_PROXY_FLEET); // W - let service = builder.build(&TEST_PROXY_FLEET, &name, None).await.unwrap(); + let service = builder + .build(&TEST_PROXY_FLEET, &name, None, None) + .await + .unwrap(); // T insta::assert_yaml_snapshot!(service); diff --git a/packages/shulker-operator/src/reconcilers/proxy_fleet/snapshots/shulker_operator__reconcilers__proxy_fleet__fleet__tests__build_snapshot.snap b/packages/shulker-operator/src/reconcilers/proxy_fleet/snapshots/shulker_operator__reconcilers__proxy_fleet__fleet__tests__build_snapshot.snap index ec642535..0b5d4640 100644 --- a/packages/shulker-operator/src/reconcilers/proxy_fleet/snapshots/shulker_operator__reconcilers__proxy_fleet__fleet__tests__build_snapshot.snap +++ b/packages/shulker-operator/src/reconcilers/proxy_fleet/snapshots/shulker_operator__reconcilers__proxy_fleet__fleet__tests__build_snapshot.snap @@ -69,6 +69,10 @@ spec: fieldPath: metadata.namespace - name: SHULKER_PROXY_TTL_SECONDS value: "3600" + - name: SHULKER_PROXY_REDIS_HOST + value: my-cluster-redis-managed + - name: SHULKER_PROXY_REDIS_PORT + value: "6379" - name: TYPE value: VELOCITY - name: VELOCITY_BUILD_ID diff --git a/packages/shulker-operator/src/reconcilers/redis_ref.rs b/packages/shulker-operator/src/reconcilers/redis_ref.rs new file mode 100644 index 00000000..60e486e4 --- /dev/null +++ b/packages/shulker-operator/src/reconcilers/redis_ref.rs @@ -0,0 +1,37 @@ +use shulker_crds::v1alpha1::minecraft_cluster::{ + MinecraftCluster, MinecraftClusterRedisDeploymentType, +}; + +use super::{builder::ResourceBuilder, minecraft_cluster::redis_service::RedisServiceBuilder}; + +pub struct RedisRef { + pub host: String, + pub port: u16, + pub credentials_secret_name: Option, +} + +impl RedisRef { + pub fn from_cluster(cluster: &MinecraftCluster) -> Result { + let is_managed = cluster.spec.redis.as_ref().map_or(true, |r| { + r.type_ == MinecraftClusterRedisDeploymentType::ManagedSingleNode + }); + + match is_managed { + true => Ok(RedisRef { + host: RedisServiceBuilder::name(cluster), + port: 6379, + credentials_secret_name: None, + }), + false => match cluster.spec.redis.as_ref().unwrap().provided.as_ref() { + Some(provided_spec) => Ok(RedisRef { + host: provided_spec.host.clone(), + port: provided_spec.port, + credentials_secret_name: provided_spec.credentials_secret_name.clone(), + }), + None => Err(anyhow::anyhow!( + "Redis is not managed and no provided spec was found" + )), + }, + } + } +} diff --git a/packages/shulker-operator/src/resources/resourceref_resolver.rs b/packages/shulker-operator/src/resources/resourceref_resolver.rs index 42c222ef..fda86594 100644 --- a/packages/shulker-operator/src/resources/resourceref_resolver.rs +++ b/packages/shulker-operator/src/resources/resourceref_resolver.rs @@ -90,7 +90,6 @@ mod tests { impl ApiServerVerifier { pub fn run(self, scenario: Scenario) -> tokio::task::JoinHandle<()> { tokio::spawn(async move { - // moving self => one scenario per test match scenario { Scenario::SecretFound(secret) => self.handle_secret_found(secret).await, Scenario::SecretNotFound => self.handle_secret_not_found().await,