From 0c1b7d77ac980e104c0bb2c4f58112b682d75279 Mon Sep 17 00:00:00 2001 From: Pavel Kirilin Date: Thu, 21 Nov 2024 16:53:52 +0100 Subject: [PATCH] Renamed leftovers from old project name. --- src/error.rs | 4 ++-- src/finalizers.rs | 10 +++++----- src/label_filter.rs | 6 +++--- src/lb.rs | 36 ++++++++++++++++++------------------ src/main.rs | 24 ++++++++++++------------ 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/src/error.rs b/src/error.rs index fb9d6b9..dae2e3f 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,9 +1,9 @@ use thiserror::Error; -pub type LBTrackerResult = Result; +pub type RobotLBResult = Result; #[derive(Debug, Error)] -pub enum LBTrackerError { +pub enum RobotLBError { #[error("Cannot parse node filter: {0}")] InvalidNodeFilter(String), #[error("Unsupported service type")] diff --git a/src/finalizers.rs b/src/finalizers.rs index 4ae6a06..f45d4fc 100644 --- a/src/finalizers.rs +++ b/src/finalizers.rs @@ -6,15 +6,15 @@ use kube::{ use crate::{ consts, - error::{LBTrackerError, LBTrackerResult}, + error::{RobotLBError, RobotLBResult}, }; /// Add finalizer to the service. /// This will prevent the service from being deleted. -pub async fn add(client: Client, svc: &Service) -> LBTrackerResult<()> { +pub async fn add(client: Client, svc: &Service) -> RobotLBResult<()> { let api = Api::::namespaced( client, - svc.namespace().ok_or(LBTrackerError::SkipService)?.as_str(), + svc.namespace().ok_or(RobotLBError::SkipService)?.as_str(), ); let patch = json!({ "metadata": { @@ -46,10 +46,10 @@ pub fn check(service: &Service) -> bool { /// This will allow the service to be deleted. /// /// if service does not have the finalizer, this function will do nothing. -pub async fn remove(client: Client, svc: &Service) -> LBTrackerResult<()> { +pub async fn remove(client: Client, svc: &Service) -> RobotLBResult<()> { let api = Api::::namespaced( client, - svc.namespace().ok_or(LBTrackerError::SkipService)?.as_str(), + svc.namespace().ok_or(RobotLBError::SkipService)?.as_str(), ); let finalizers = svc .finalizers() diff --git a/src/label_filter.rs b/src/label_filter.rs index c588a6e..56d1349 100644 --- a/src/label_filter.rs +++ b/src/label_filter.rs @@ -1,6 +1,6 @@ use std::{collections::BTreeMap, str::FromStr}; -use crate::error::LBTrackerError; +use crate::error::RobotLBError; /// Enum of all possible rules for label filtering. #[derive(Debug, Clone)] @@ -57,7 +57,7 @@ impl LabelFilter { /// The string should be in the following format: /// `key=value,key!=value,key,!key` impl FromStr for LabelFilter { - type Err = LBTrackerError; + type Err = RobotLBError; fn from_str(s: &str) -> Result { let mut rules = Vec::new(); @@ -83,7 +83,7 @@ impl FromStr for LabelFilter { } rules.push(Rule::Equal(key.to_string(), value.to_string())); } - _ => return Err(LBTrackerError::InvalidNodeFilter(rule.to_string())), + _ => return Err(RobotLBError::InvalidNodeFilter(rule.to_string())), } } Ok(Self { rules }) diff --git a/src/lb.rs b/src/lb.rs index 0ffde3b..bac19f1 100644 --- a/src/lb.rs +++ b/src/lb.rs @@ -22,7 +22,7 @@ use std::{collections::HashMap, str::FromStr}; use crate::{ consts, - error::{LBTrackerError, LBTrackerResult}, + error::{RobotLBError, RobotLBResult}, CurrentContext, }; @@ -67,7 +67,7 @@ impl LoadBalancer { /// from the service annotations and the context. /// If some of the required information is missing, the method will /// try to use the default values from the context. - pub fn try_from_svc(svc: &Service, context: &CurrentContext) -> LBTrackerResult { + pub fn try_from_svc(svc: &Service, context: &CurrentContext) -> RobotLBResult { let retries = svc .annotations() .get(consts::LB_RETRIES_ANN_NAME) @@ -172,7 +172,7 @@ impl LoadBalancer { /// Reconcile the load balancer to match the desired configuration. #[tracing::instrument(skip(self), fields(lb_name=self.name))] - pub async fn reconcile(&self) -> LBTrackerResult { + pub async fn reconcile(&self) -> RobotLBResult { let hcloud_balancer = self.get_or_create_hcloud_lb().await?; self.reconcile_algorithm(&hcloud_balancer).await?; self.reconcile_lb_type(&hcloud_balancer).await?; @@ -189,7 +189,7 @@ impl LoadBalancer { async fn reconcile_services( &self, hcloud_balancer: &hcloud::models::LoadBalancer, - ) -> LBTrackerResult<()> { + ) -> RobotLBResult<()> { for service in &hcloud_balancer.services { // Here we check that all the services are configured correctly. // If the service is not configured correctly, we update it. @@ -299,7 +299,7 @@ impl LoadBalancer { async fn reconcile_targets( &self, hcloud_balancer: &hcloud::models::LoadBalancer, - ) -> LBTrackerResult<()> { + ) -> RobotLBResult<()> { for target in &hcloud_balancer.targets { let Some(target_ip) = target.ip.clone() else { continue; @@ -351,7 +351,7 @@ impl LoadBalancer { async fn reconcile_algorithm( &self, hcloud_balancer: &hcloud::models::LoadBalancer, - ) -> LBTrackerResult<()> { + ) -> RobotLBResult<()> { if *hcloud_balancer.algorithm == self.algorithm.clone().into() { return Ok(()); } @@ -375,7 +375,7 @@ impl LoadBalancer { async fn reconcile_lb_type( &self, hcloud_balancer: &hcloud::models::LoadBalancer, - ) -> LBTrackerResult<()> { + ) -> RobotLBResult<()> { if hcloud_balancer.load_balancer_type.name == self.balancer_type { return Ok(()); } @@ -405,7 +405,7 @@ impl LoadBalancer { async fn reconcile_network( &self, hcloud_balancer: &hcloud::models::LoadBalancer, - ) -> LBTrackerResult<()> { + ) -> RobotLBResult<()> { // If the network name is not provided, and laod balancer is not attached to any network, // we can skip this step. if self.network_name.is_none() && hcloud_balancer.private_net.is_empty() { @@ -476,7 +476,7 @@ impl LoadBalancer { /// Cleanup the load balancer. /// This method will remove all the services and targets from the /// load balancer. - pub async fn cleanup(&self) -> LBTrackerResult<()> { + pub async fn cleanup(&self) -> RobotLBResult<()> { let Some(hcloud_balancer) = self.get_hcloud_lb().await? else { return Ok(()); }; @@ -529,7 +529,7 @@ impl LoadBalancer { /// /// The method might return an error if the load balancer is not found /// or if there are multiple load balancers with the same name. - async fn get_hcloud_lb(&self) -> LBTrackerResult> { + async fn get_hcloud_lb(&self) -> RobotLBResult> { let hcloud_balancers = hcloud::apis::load_balancers_api::list_load_balancers( &self.hcloud_config, ListLoadBalancersParams { @@ -543,7 +543,7 @@ impl LoadBalancer { "Found more than one balancer with name {}, skipping", self.name ); - return Err(LBTrackerError::SkipService); + return Err(RobotLBError::SkipService); } // Here we just return the first load balancer, // if it exists, otherwise we return None @@ -556,7 +556,7 @@ impl LoadBalancer { /// specified in the `LoadBalancer` struct. If the load balancer /// is not found, the method will create a new load balancer /// with the specified configuration in service's annotations. - async fn get_or_create_hcloud_lb(&self) -> LBTrackerResult { + async fn get_or_create_hcloud_lb(&self) -> RobotLBResult { let hcloud_lb = self.get_hcloud_lb().await?; if let Some(balancer) = hcloud_lb { return Ok(balancer); @@ -582,7 +582,7 @@ impl LoadBalancer { .await; if let Err(e) = response { tracing::error!("Failed to create load balancer: {:?}", e); - return Err(LBTrackerError::HCloudError(format!( + return Err(RobotLBError::HCloudError(format!( "Failed to create load balancer: {:?}", e ))); @@ -596,7 +596,7 @@ impl LoadBalancer { /// specified in the `LoadBalancer` struct. It returns `None` only /// in case the network name is not provided. If the network was not found, /// the error is returned. - async fn get_network(&self) -> LBTrackerResult> { + async fn get_network(&self) -> RobotLBResult> { let Some(network_name) = self.network_name.clone() else { return Ok(None); }; @@ -614,14 +614,14 @@ impl LoadBalancer { "Found more than one network with name {}, skipping", network_name ); - return Err(LBTrackerError::HCloudError(format!( + return Err(RobotLBError::HCloudError(format!( "Found more than one network with name {}", network_name, ))); } if response.networks.is_empty() { tracing::warn!("Network with name {} not found", network_name); - return Err(LBTrackerError::HCloudError(format!( + return Err(RobotLBError::HCloudError(format!( "Network with name {} not found", network_name, ))); @@ -632,12 +632,12 @@ impl LoadBalancer { } impl FromStr for LBAlgorithm { - type Err = LBTrackerError; + type Err = RobotLBError; fn from_str(s: &str) -> Result { match s { "round-robin" => Ok(Self::RoundRobin), "least-connections" => Ok(Self::LeastConnections), - _ => Err(LBTrackerError::UnknownLBAlgorithm), + _ => Err(RobotLBError::UnknownLBAlgorithm), } } } diff --git a/src/main.rs b/src/main.rs index f2d086c..023851e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ use clap::Parser; use config::OperatorConfig; -use error::{LBTrackerError, LBTrackerResult}; +use error::{RobotLBError, RobotLBResult}; use futures::StreamExt; use hcloud::apis::configuration::Configuration as HCloudConfig; use k8s_openapi::{ @@ -46,7 +46,7 @@ pub mod lb; static GLOBAL: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc; #[tokio::main] -async fn main() -> LBTrackerResult<()> { +async fn main() -> RobotLBResult<()> { dotenvy::dotenv().ok(); let operator_config = config::OperatorConfig::parse(); tracing_subscriber::fmt() @@ -80,7 +80,7 @@ async fn main() -> LBTrackerResult<()> { // During reconcilation process, // the controller has decided to skip the service. kube::runtime::controller::Error::ReconcilerFailed( - LBTrackerError::SkipService, + RobotLBError::SkipService, _, ) => {} _ => { @@ -122,7 +122,7 @@ impl CurrentContext { pub async fn reconcile_service( svc: Arc, context: Arc, -) -> LBTrackerResult { +) -> RobotLBResult { let svc_type = svc .spec .as_ref() @@ -131,7 +131,7 @@ pub async fn reconcile_service( .unwrap_or("ClusterIP"); if svc_type != "LoadBalancer" { tracing::debug!("Service type is not LoadBalancer. Skipping..."); - return Err(LBTrackerError::SkipService); + return Err(RobotLBError::SkipService); } tracing::info!("Starting service reconcilation"); @@ -161,7 +161,7 @@ pub async fn reconcile_service( async fn get_nodes_dynamically( svc: &Arc, context: &Arc, -) -> LBTrackerResult> { +) -> RobotLBResult> { let pod_api = kube::Api::::namespaced( context.client.clone(), svc.namespace() @@ -171,7 +171,7 @@ async fn get_nodes_dynamically( ); let Some(pod_selector) = svc.spec.as_ref().and_then(|spec| spec.selector.clone()) else { - return Err(LBTrackerError::ServiceWithoutSelector); + return Err(RobotLBError::ServiceWithoutSelector); }; let label_selector = pod_selector @@ -210,12 +210,12 @@ async fn get_nodes_dynamically( async fn get_nodes_by_selector( svc: &Arc, context: &Arc, -) -> LBTrackerResult> { +) -> RobotLBResult> { let node_selector = svc .annotations() .get(consts::LB_NODE_SELECTOR) .map(String::as_str) - .ok_or(LBTrackerError::ServiceWithoutSelector)?; + .ok_or(RobotLBError::ServiceWithoutSelector)?; let label_filter = LabelFilter::from_str(node_selector)?; let nodes_api = kube::Api::::all(context.client.clone()); let nodes = nodes_api @@ -234,7 +234,7 @@ pub async fn reconcile_load_balancer( mut lb: LoadBalancer, svc: Arc, context: Arc, -) -> LBTrackerResult { +) -> RobotLBResult { let mut node_ip_type = "InternalIP"; if lb.network_name.is_none() { node_ip_type = "ExternalIP"; @@ -335,9 +335,9 @@ pub async fn reconcile_load_balancer( /// Handle the error during reconcilation. #[allow(clippy::needless_pass_by_value)] -fn on_error(_: Arc, error: &LBTrackerError, _context: Arc) -> Action { +fn on_error(_: Arc, error: &RobotLBError, _context: Arc) -> Action { match error { - LBTrackerError::SkipService => Action::await_change(), + RobotLBError::SkipService => Action::await_change(), _ => Action::requeue(Duration::from_secs(30)), } }