Skip to content

Renamed leftovers from old project name. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use thiserror::Error;

pub type LBTrackerResult<T> = Result<T, LBTrackerError>;
pub type RobotLBResult<T> = Result<T, RobotLBError>;

#[derive(Debug, Error)]
pub enum LBTrackerError {
pub enum RobotLBError {
#[error("Cannot parse node filter: {0}")]
InvalidNodeFilter(String),
#[error("Unsupported service type")]
Expand Down
10 changes: 5 additions & 5 deletions src/finalizers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Service>::namespaced(
client,
svc.namespace().ok_or(LBTrackerError::SkipService)?.as_str(),
svc.namespace().ok_or(RobotLBError::SkipService)?.as_str(),
);
let patch = json!({
"metadata": {
Expand Down Expand Up @@ -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::<Service>::namespaced(
client,
svc.namespace().ok_or(LBTrackerError::SkipService)?.as_str(),
svc.namespace().ok_or(RobotLBError::SkipService)?.as_str(),
);
let finalizers = svc
.finalizers()
Expand Down
6 changes: 3 additions & 3 deletions src/label_filter.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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<Self, Self::Err> {
let mut rules = Vec::new();
Expand All @@ -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 })
Expand Down
36 changes: 18 additions & 18 deletions src/lb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use std::{collections::HashMap, str::FromStr};

use crate::{
consts,
error::{LBTrackerError, LBTrackerResult},
error::{RobotLBError, RobotLBResult},
CurrentContext,
};

Expand Down Expand Up @@ -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<Self> {
pub fn try_from_svc(svc: &Service, context: &CurrentContext) -> RobotLBResult<Self> {
let retries = svc
.annotations()
.get(consts::LB_RETRIES_ANN_NAME)
Expand Down Expand Up @@ -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<hcloud::models::LoadBalancer> {
pub async fn reconcile(&self) -> RobotLBResult<hcloud::models::LoadBalancer> {
let hcloud_balancer = self.get_or_create_hcloud_lb().await?;
self.reconcile_algorithm(&hcloud_balancer).await?;
self.reconcile_lb_type(&hcloud_balancer).await?;
Expand All @@ -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.
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(());
}
Expand All @@ -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(());
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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(());
};
Expand Down Expand Up @@ -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<Option<hcloud::models::LoadBalancer>> {
async fn get_hcloud_lb(&self) -> RobotLBResult<Option<hcloud::models::LoadBalancer>> {
let hcloud_balancers = hcloud::apis::load_balancers_api::list_load_balancers(
&self.hcloud_config,
ListLoadBalancersParams {
Expand All @@ -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
Expand All @@ -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<hcloud::models::LoadBalancer> {
async fn get_or_create_hcloud_lb(&self) -> RobotLBResult<hcloud::models::LoadBalancer> {
let hcloud_lb = self.get_hcloud_lb().await?;
if let Some(balancer) = hcloud_lb {
return Ok(balancer);
Expand All @@ -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
)));
Expand All @@ -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<Option<hcloud::models::Network>> {
async fn get_network(&self) -> RobotLBResult<Option<hcloud::models::Network>> {
let Some(network_name) = self.network_name.clone() else {
return Ok(None);
};
Expand All @@ -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,
)));
Expand All @@ -632,12 +632,12 @@ impl LoadBalancer {
}

impl FromStr for LBAlgorithm {
type Err = LBTrackerError;
type Err = RobotLBError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"round-robin" => Ok(Self::RoundRobin),
"least-connections" => Ok(Self::LeastConnections),
_ => Err(LBTrackerError::UnknownLBAlgorithm),
_ => Err(RobotLBError::UnknownLBAlgorithm),
}
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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,
_,
) => {}
_ => {
Expand Down Expand Up @@ -122,7 +122,7 @@ impl CurrentContext {
pub async fn reconcile_service(
svc: Arc<Service>,
context: Arc<CurrentContext>,
) -> LBTrackerResult<Action> {
) -> RobotLBResult<Action> {
let svc_type = svc
.spec
.as_ref()
Expand All @@ -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");
Expand Down Expand Up @@ -161,7 +161,7 @@ pub async fn reconcile_service(
async fn get_nodes_dynamically(
svc: &Arc<Service>,
context: &Arc<CurrentContext>,
) -> LBTrackerResult<Vec<Node>> {
) -> RobotLBResult<Vec<Node>> {
let pod_api = kube::Api::<Pod>::namespaced(
context.client.clone(),
svc.namespace()
Expand All @@ -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
Expand Down Expand Up @@ -210,12 +210,12 @@ async fn get_nodes_dynamically(
async fn get_nodes_by_selector(
svc: &Arc<Service>,
context: &Arc<CurrentContext>,
) -> LBTrackerResult<Vec<Node>> {
) -> RobotLBResult<Vec<Node>> {
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::<Node>::all(context.client.clone());
let nodes = nodes_api
Expand All @@ -234,7 +234,7 @@ pub async fn reconcile_load_balancer(
mut lb: LoadBalancer,
svc: Arc<Service>,
context: Arc<CurrentContext>,
) -> LBTrackerResult<Action> {
) -> RobotLBResult<Action> {
let mut node_ip_type = "InternalIP";
if lb.network_name.is_none() {
node_ip_type = "ExternalIP";
Expand Down Expand Up @@ -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<Service>, error: &LBTrackerError, _context: Arc<CurrentContext>) -> Action {
fn on_error(_: Arc<Service>, error: &RobotLBError, _context: Arc<CurrentContext>) -> Action {
match error {
LBTrackerError::SkipService => Action::await_change(),
RobotLBError::SkipService => Action::await_change(),
_ => Action::requeue(Duration::from_secs(30)),
}
}
Loading