Skip to content

Commit

Permalink
fix(shulker-operator): assert valid cluster ref before reconciling (j…
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremylvln authored Oct 26, 2023
1 parent 3e99751 commit c207cce
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 3 deletions.
18 changes: 18 additions & 0 deletions packages/shulker-operator/src/reconcilers/cluster_ref.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use kube::{Api, Client};
use shulker_crds::v1alpha1::minecraft_cluster::{MinecraftCluster, MinecraftClusterRef};

use super::{ReconcilerError, Result};

pub async fn resolve_cluster_ref(
client: &Client,
namespace: &str,
cluster_ref: &MinecraftClusterRef,
) -> Result<MinecraftCluster> {
let clusters_api: Api<MinecraftCluster> = Api::namespaced(client.clone(), namespace);
let cluster = clusters_api
.get(&cluster_ref.name)
.await
.map_err(|e| ReconcilerError::InvalidClusterRef(cluster_ref.name.clone(), e))?;

Ok(cluster)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ use shulker_crds::{

use self::{config_map::ConfigMapBuilder, gameserver::GameServerBuilder};

use super::{builder::reconcile_builder, status::patch_status, ReconcilerError, Result};
use super::{
builder::reconcile_builder, cluster_ref::resolve_cluster_ref, status::patch_status,
ReconcilerError, Result,
};

pub mod config_map;
pub mod gameserver;
Expand All @@ -39,6 +42,13 @@ impl MinecraftServerReconciler {
api: Api<MinecraftServer>,
minecraft_server: Arc<MinecraftServer>,
) -> Result<Action> {
resolve_cluster_ref(
&self.client,
&minecraft_server.namespace().unwrap(),
&minecraft_server.spec.cluster_ref,
)
.await?;

reconcile_builder(&self.config_map_builder, minecraft_server.as_ref()).await?;
let gameserver =
reconcile_builder(&self.gameserver_builder, minecraft_server.as_ref()).await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use self::{
config_map::ConfigMapBuilder, fleet::FleetBuilder, fleet_autoscaler::FleetAutoscalerBuilder,
};

use super::{builder::reconcile_builder, status::patch_status, ReconcilerError, Result};
use super::{
builder::reconcile_builder, cluster_ref::resolve_cluster_ref, status::patch_status,
ReconcilerError, Result,
};

mod config_map;
mod fleet;
Expand All @@ -43,6 +46,13 @@ impl MinecraftServerFleetReconciler {
api: Api<MinecraftServerFleet>,
minecraft_server_fleet: Arc<MinecraftServerFleet>,
) -> Result<Action> {
resolve_cluster_ref(
&self.client,
&minecraft_server_fleet.namespace().unwrap(),
&minecraft_server_fleet.spec.cluster_ref,
)
.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(
Expand Down
4 changes: 4 additions & 0 deletions packages/shulker-operator/src/reconcilers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use thiserror::Error;

pub mod builder;
mod cluster_ref;
pub mod minecraft_cluster;
pub mod minecraft_server;
pub mod minecraft_server_fleet;
Expand All @@ -18,6 +19,9 @@ pub enum ReconcilerError {
#[error("failed to delete stale resource: {0}")]
FailedToDeleteStale(#[source] kube::Error),

#[error("failed to resolve cluster ref: {1}")]
InvalidClusterRef(String, #[source] kube::Error),

#[error("builder {0} failed to build resource: {1}")]
BuilderError(&'static str, #[source] anyhow::Error),
}
Expand Down
12 changes: 11 additions & 1 deletion packages/shulker-operator/src/reconcilers/proxy_fleet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ use self::{
service::ServiceBuilder,
};

use super::{builder::reconcile_builder, status::patch_status, ReconcilerError, Result};
use super::{
builder::reconcile_builder, cluster_ref::resolve_cluster_ref, status::patch_status,
ReconcilerError, Result,
};

mod config_map;
mod fleet;
Expand All @@ -46,6 +49,13 @@ impl ProxyFleetReconciler {
api: Api<ProxyFleet>,
proxy_fleet: Arc<ProxyFleet>,
) -> Result<Action> {
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?;
Expand Down

0 comments on commit c207cce

Please sign in to comment.