Skip to content

Commit

Permalink
Refactoring to modules and upgrading dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
ausias-armesto committed Feb 1, 2024
1 parent 5e277e6 commit bc92c25
Show file tree
Hide file tree
Showing 25 changed files with 361 additions and 306 deletions.
425 changes: 244 additions & 181 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "hoprd_operator"
version = "0.2.5"
version = "0.2.6"
authors = ["HOPR Association <[email protected]>"]
edition = "2021"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

use futures::StreamExt;
use kube::{
api::Api,
Expand All @@ -13,10 +14,10 @@ use tokio::time::Duration;
use tracing::error;

use crate::{
cluster::{ClusterHoprd, ClusterHoprdPhaseEnum},
cluster::cluster_hoprd::{ClusterHoprd, ClusterHoprdPhaseEnum},
constants::{self},
context_data::ContextData,
hoprd::Hoprd,
hoprd::hoprd_resource::Hoprd,
model::Error,
};

Expand Down Expand Up @@ -122,4 +123,4 @@ pub async fn run(client: Client, context_data: Arc<ContextData>) {
}
})
.await;
}
}
5 changes: 2 additions & 3 deletions src/cluster.rs → src/cluster/cluster_hoprd.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::constants::SupportedReleaseEnum;
use crate::events::ClusterHoprdEventEnum;
use crate::hoprd::HoprdSpec;
use crate::hoprd_deployment_spec::HoprdDeploymentSpec;
use crate::hoprd::{hoprd_deployment_spec::HoprdDeploymentSpec, hoprd_resource::{HoprdSpec, Hoprd}};
use crate::model::Error;
use crate::{utils, resource_generics};
use crate::{constants, context_data::ContextData, hoprd::Hoprd};
use crate::{constants, context_data::ContextData};
use chrono::Utc;
use k8s_openapi::apimachinery::pkg::apis::meta::v1::OwnerReference;
use kube::api::{ DeleteParams, ListParams, PostParams};
Expand Down
2 changes: 2 additions & 0 deletions src/cluster/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cluster_hoprd;
pub mod cluster_controller;
9 changes: 2 additions & 7 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,11 @@ pub const HOPRD_INIT: &str = "HOPRD_INIT";
pub const HOPRD_HEALTH_CHECK: &str = "HOPRD_HEALTH_CHECK";
pub const HOPRD_HEALTH_CHECK_HOST: &str = "HOPRD_HEALTH_CHECK_HOST";

#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Hash, Copy, JsonSchema)]
#[derive(Serialize, Deserialize, Debug, PartialEq, Default, Clone, Hash, Copy, JsonSchema)]
pub enum SupportedReleaseEnum {
#[serde(rename= "providence")]
#[default]
Providence,
#[serde(rename= "saint-louis")]
SaintLouis
}

impl Default for SupportedReleaseEnum {
fn default() -> Self {
SupportedReleaseEnum::Providence
}
}
4 changes: 2 additions & 2 deletions src/context_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use kube::{
Client, Resource, Api, api::ListParams, ResourceExt};

use crate::{
constants, hoprd::Hoprd, identity_hoprd::IdentityHoprd,
identity_pool::IdentityPool, operator_config::OperatorConfig, events::ResourceEvent,
constants, hoprd::hoprd_resource::Hoprd, identity_hoprd::identity_hoprd_resource::IdentityHoprd,
identity_pool::identity_pool_resource::IdentityPool, operator_config::OperatorConfig, events::ResourceEvent,
};

#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion src/controller_hoprd.rs → src/hoprd/hoprd_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use tracing::error;
use crate::{
constants::{self},
context_data::ContextData,
hoprd::{Hoprd, HoprdPhaseEnum},
hoprd::hoprd_resource::{Hoprd, HoprdPhaseEnum},
model::Error,
servicemonitor::ServiceMonitor,
};
Expand Down
32 changes: 20 additions & 12 deletions src/hoprd_deployment.rs → src/hoprd/hoprd_deployment.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::context_data::ContextData;
use crate::hoprd_deployment_spec::HoprdDeploymentSpec;
use crate::identity_hoprd::IdentityHoprd;
use crate::identity_pool::IdentityPool;
use crate::hoprd::hoprd_deployment_spec::HoprdDeploymentSpec;
use crate::identity_hoprd::identity_hoprd_resource::IdentityHoprd;
use crate::identity_pool::identity_pool_resource::IdentityPool;
use crate::model::Error;
use crate::operator_config::IngressConfig;
use base64::{Engine as _, engine::general_purpose};
use crate::{
constants,
hoprd::{Hoprd, HoprdSpec},
hoprd::hoprd_resource::{Hoprd, HoprdSpec},
utils,
};
use k8s_openapi::api::apps::v1::{Deployment, DeploymentSpec, DeploymentStrategy};
Expand Down Expand Up @@ -253,6 +253,22 @@ fn build_env_vars(identity_pool: &IdentityPool, identity_hoprd: &IdentityHoprd,
value: Some("true".to_owned()),
..EnvVar::default()
});
env_vars.push(EnvVar {
name: constants::HOPRD_API.to_owned(),
value: Some("true".to_owned()),
..EnvVar::default()
});
env_vars.push(EnvVar {
name: constants::HOPRD_INIT.to_owned(),
value: Some("true".to_owned()),
..EnvVar::default()
});
} else {
env_vars.push(EnvVar {
name: constants::HOPRD_API.to_owned(),
value: Some("1".to_owned()),
..EnvVar::default()
});
}
env_vars
}
Expand Down Expand Up @@ -327,18 +343,10 @@ fn build_default_env_var(hoprd_host: &String) -> Vec<EnvVar> {
name: constants::HOPRD_HOST.to_owned(),
value: Some(hoprd_host.to_owned()),
..EnvVar::default()
}, EnvVar {
name: constants::HOPRD_API.to_owned(),
value: Some("true".to_owned()),
..EnvVar::default()
}, EnvVar {
name: constants::HOPRD_API_HOST.to_owned(),
value: Some("0.0.0.0".to_owned()),
..EnvVar::default()
}, EnvVar {
name: constants::HOPRD_INIT.to_owned(),
value: Some("true".to_owned()),
..EnvVar::default()
}, EnvVar {
name: constants::HOPRD_HEALTH_CHECK.to_owned(),
value: Some("true".to_owned()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl HoprdDeploymentSpec {
match supported_release {
SupportedReleaseEnum::Providence => None,
SupportedReleaseEnum::SaintLouis => {
let default_liveness_probe = HoprdDeploymentSpec::build_probe("/api/v3/healthyz/".to_owned(),Some(5), Some(1), Some(3));
let default_liveness_probe = HoprdDeploymentSpec::build_probe("/healthyz".to_owned(),Some(5), Some(1), Some(3));
if let Some(hoprd_deployment_spec) = hoprd_deployment_spec_option {
if let Some (liveness_probe_string) = hoprd_deployment_spec.liveness_probe {
Some(serde_yaml::from_str(&liveness_probe_string).unwrap())
Expand All @@ -127,7 +127,7 @@ impl HoprdDeploymentSpec {
match supported_release {
SupportedReleaseEnum::Providence => None,
SupportedReleaseEnum::SaintLouis => {
let default_startup_probe = HoprdDeploymentSpec::build_probe("/api/v3/startedz/".to_owned(),Some(30), Some(1), Some(960));
let default_startup_probe = HoprdDeploymentSpec::build_probe("/startedz".to_owned(),Some(15), Some(1), Some(8));
if let Some(hoprd_deployment_spec) = hoprd_deployment_spec_option {
if let Some (startup_probe_string) = hoprd_deployment_spec.startup_probe {
Some(serde_yaml::from_str(&startup_probe_string).unwrap())
Expand All @@ -145,7 +145,7 @@ impl HoprdDeploymentSpec {
match supported_release {
SupportedReleaseEnum::Providence => None,
SupportedReleaseEnum::SaintLouis => {
let default_readiness_probe = HoprdDeploymentSpec::build_probe("/api/v3/readyz/".to_owned(),Some(30), Some(1), Some(20));
let default_readiness_probe = HoprdDeploymentSpec::build_probe("/readyz".to_owned(),Some(10), Some(1), Some(6));
if let Some(hoprd_deployment_spec) = hoprd_deployment_spec_option {
if let Some (readiness_probe_string) = hoprd_deployment_spec.readiness_probe {
Some(serde_yaml::from_str(&readiness_probe_string).unwrap())
Expand Down
8 changes: 2 additions & 6 deletions src/hoprd_ingress.rs → src/hoprd/hoprd_ingress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,10 @@ pub async fn delete_ingress(client: Client, name: &str, namespace: &str) -> Resu
if let Some(ingress) = api.get_opt(name).await? {
let uid = ingress.metadata.uid.unwrap();
api.delete(name, &DeleteParams::default()).await?;
await_condition(api, name, conditions::is_deleted(&uid))
.await
.unwrap();
await_condition(api, name, conditions::is_deleted(&uid)).await.unwrap();
Ok(info!("Ingress {name} successfully deleted"))
} else {
Ok(info!(
"Ingress {name} in namespace {namespace} about to delete not found"
))
Ok(info!("Ingress {name} in namespace {namespace} about to delete not found"))
}
}

Expand Down
43 changes: 27 additions & 16 deletions src/hoprd.rs → src/hoprd/hoprd_resource.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::cluster::{ClusterHoprd, ClusterHoprdPhaseEnum};
use crate::cluster::cluster_hoprd::{ClusterHoprd, ClusterHoprdPhaseEnum};
use crate::constants::SupportedReleaseEnum;
use crate::events::{ClusterHoprdEventEnum, HoprdEventEnum, IdentityHoprdEventEnum, IdentityPoolEventEnum};
use crate::resource_generics;
use crate::hoprd_deployment_spec::HoprdDeploymentSpec;
use crate::identity_hoprd::{IdentityHoprd, IdentityHoprdPhaseEnum};
use crate::identity_pool::{IdentityPool, IdentityPoolPhaseEnum};
use crate::identity_hoprd::identity_hoprd_resource::{IdentityHoprd, IdentityHoprdPhaseEnum};
use crate::identity_pool::identity_pool_resource::{IdentityPool, IdentityPoolPhaseEnum};
use crate::model::Error;
use crate::{
constants, context_data::ContextData, hoprd_deployment, hoprd_ingress,
hoprd_service
hoprd::{hoprd_deployment_spec::HoprdDeploymentSpec, hoprd_deployment, hoprd_service, hoprd_ingress},
constants, context_data::ContextData,
};
use chrono::Utc;
use futures::{StreamExt, TryStreamExt};
Expand Down Expand Up @@ -238,12 +237,19 @@ impl Hoprd {
hoprd_deployment::delete_depoyment(client.clone(), &hoprd_name, &hoprd_namespace).await.unwrap();
if let Some(identity) = self.get_identity(client.clone()).await? {
identity.unlock(context_data.clone()).await?;
} else {
warn!("HoprdNode {hoprd_name} in namespace {hoprd_namespace} does not have a locked identity")
}
// Once all the resources are successfully removed, remove the finalizer to make it possible
// for Kubernetes to delete the `Hoprd` resource.
context_data.send_event(self, HoprdEventEnum::Deleted, None).await;
self.notify_cluster(context_data.clone()).await.unwrap();
resource_generics::delete_finalizer(client.clone(), self).await;
if let Some(cluster) = self.belongs_to_cluster(context_data.clone()).await? {
resource_generics::delete_finalizer(client.clone(), self).await;
context_data.send_event(&cluster, ClusterHoprdEventEnum::NodeDeleted, None).await;
cluster.update_status(context_data.clone(), ClusterHoprdPhaseEnum::NodeDeleted).await.unwrap();
} else {
resource_generics::delete_finalizer(client.clone(), self).await;
}
info!("Hoprd node {hoprd_name} in namespace {hoprd_namespace} has been successfully deleted");
Ok(Action::await_change()) // Makes no sense to delete after a successful delete, as the resource is gone
}
Expand Down Expand Up @@ -347,23 +353,22 @@ impl Hoprd {
}
}

async fn notify_cluster(&self, context_data: Arc<ContextData>) -> Result<(), Error> {
async fn belongs_to_cluster(&self, context_data: Arc<ContextData>) -> Result<Option<ClusterHoprd>, Error> {
if let Some(owner_reference) = self.owner_references().to_owned().first() {
let hoprd_namespace = self.metadata.namespace.as_ref().unwrap().to_owned();
let api: Api<ClusterHoprd> = Api::namespaced(context_data.client.clone(), &hoprd_namespace.to_owned());
if let Some(cluster) = api.get_opt(&owner_reference.name).await? {
let current_phase = cluster.to_owned().status.unwrap().phase;
if current_phase.ne(&ClusterHoprdPhaseEnum::Deleting) && current_phase.ne(&ClusterHoprdPhaseEnum::NotScaled) && current_phase.ne(&ClusterHoprdPhaseEnum::Scaling) {
debug!("Current Phase {} of {}", current_phase, self.name_any().to_owned());
context_data.send_event(&cluster, ClusterHoprdEventEnum::NodeDeleted, None).await;
cluster.update_status(context_data.clone(), ClusterHoprdPhaseEnum::NodeDeleted).await.unwrap();
info!("Notifying ClusterHoprd {} that hoprd node {} is being deleted", &owner_reference.name, self.name_any().to_owned())
info!("Notifying ClusterHoprd {} that hoprd node {} is being deleted", &owner_reference.name, self.name_any().to_owned());
return Ok(Some(cluster));
}
} else {
debug!("ClusterHoprd {} not found", &owner_reference.name);
}
};
Ok(())
}
Ok(None)
}

pub fn get_checksum(&self) -> String {
Expand All @@ -378,10 +383,16 @@ impl Hoprd {
let api: Api<IdentityHoprd> = Api::namespaced(client.clone(), &self.namespace().unwrap());
match status.to_owned().identity_name {
Some(identity_name) => Ok(api.get_opt(identity_name.as_str()).await.unwrap()),
None => Ok(None),
None => {
warn!("HoprdNode status for {} is incomplete as it does not contain a identity_name", &self.name_any());
Ok(None)
}
}
},
None => {
warn!("HoprdNode {} has empty status", &self.name_any());
Ok(None)
}
None => Ok(None)
}
}
// Wait for the Hoprd deployment to be created
Expand Down
8 changes: 2 additions & 6 deletions src/hoprd_service.rs → src/hoprd/hoprd_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,9 @@ pub async fn delete_service(client: Client, name: &str, namespace: &str) -> Resu
if let Some(service) = api.get_opt(name).await? {
let uid = service.metadata.uid.unwrap();
api.delete(name, &DeleteParams::default()).await?;
await_condition(api, name, conditions::is_deleted(&uid))
.await
.unwrap();
await_condition(api, name, conditions::is_deleted(&uid)).await.unwrap();
Ok(info!("Service {name} successfully deleted"))
} else {
Ok(info!(
"Service {name} in namespace {namespace} about to delete not found"
))
Ok(info!("Service {name} in namespace {namespace} about to delete not found"))
}
}
6 changes: 6 additions & 0 deletions src/hoprd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod hoprd_resource;
pub mod hoprd_deployment;
pub mod hoprd_deployment_spec;
pub mod hoprd_controller;
pub mod hoprd_service;
pub mod hoprd_ingress;
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tracing::error;
use crate::{
constants::{self},
context_data::ContextData,
identity_hoprd::IdentityHoprd,
identity_hoprd::identity_hoprd_resource::IdentityHoprd,
model::Error,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use kube::{Api, Resource, ResourceExt};
use std::collections::BTreeMap;
use std::sync::Arc;

use crate::identity_hoprd::IdentityHoprd;
use crate::identity_hoprd::identity_hoprd_resource::IdentityHoprd;
use crate::utils;

/// Creates the Persitence Volume Claim
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::events::{ IdentityHoprdEventEnum, IdentityPoolEventEnum};
use crate::{identity_hoprd_persistence, resource_generics};
use crate::identity_pool::{IdentityPool, IdentityPoolPhaseEnum, IdentityPoolStatus};
use crate::{identity_hoprd::identity_hoprd_persistence, resource_generics};
use crate::identity_pool::identity_pool_resource::{IdentityPool, IdentityPoolPhaseEnum, IdentityPoolStatus};
use crate::model::Error;
use crate::{constants, context_data::ContextData};
use chrono::Utc;
Expand Down
3 changes: 3 additions & 0 deletions src/identity_hoprd/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod identity_hoprd_resource;
pub mod identity_hoprd_controller;
mod identity_hoprd_persistence;
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use tracing::error;
use crate::{
constants::{self},
context_data::ContextData,
identity_pool::IdentityPool,
identity_pool::identity_pool_resource::{IdentityPool,IdentityPoolPhaseEnum},
model::Error,
servicemonitor::ServiceMonitor, identity_hoprd::IdentityHoprd,
servicemonitor::ServiceMonitor, identity_hoprd::identity_hoprd_resource::IdentityHoprd,
};

/// Action to be taken upon an `IdentityPool` resource during reconciliation
Expand Down Expand Up @@ -46,7 +46,7 @@ fn determine_action(identity_pool: &IdentityPool) -> IdentityPoolAction {
} else if identity_pool.meta().finalizers.as_ref().map_or(true, |finalizers| finalizers.is_empty())
{
IdentityPoolAction::Create
} else if identity_pool.status.as_ref().unwrap().phase.eq(&crate::identity_pool::IdentityPoolPhaseEnum::OutOfSync)
} else if identity_pool.status.as_ref().unwrap().phase.eq(&IdentityPoolPhaseEnum::OutOfSync)
{
IdentityPoolAction::Sync
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use std::sync::Arc;
use tracing::info;

use crate::context_data::ContextData;
use crate::hoprd_deployment_spec::HoprdDeploymentSpec;
use crate::identity_pool::IdentityPool;
use crate::hoprd::hoprd_deployment_spec::HoprdDeploymentSpec;
use crate::identity_pool::identity_pool_resource::IdentityPool;
use crate::model::Error;
use crate::{utils, constants};

Expand Down Expand Up @@ -170,13 +170,9 @@ pub async fn delete_cron_job(client: Client, name: &str,namespace: &str) -> Resu
if let Some(create_cron_job) = api.get_opt(name).await? {
let uid = create_cron_job.metadata.uid.unwrap();
api.delete(name, &DeleteParams::default()).await?;
await_condition(api, name, conditions::is_deleted(&uid))
.await
.unwrap();
await_condition(api, name, conditions::is_deleted(&uid)).await.unwrap();
Ok(info!("CronJob {name} successfully deleted"))
} else {
Ok(info!(
"CronJob {name} in namespace {namespace} about to delete not found"
))
Ok(info!("CronJob {name} in namespace {namespace} about to delete not found"))
}
}
Loading

0 comments on commit bc92c25

Please sign in to comment.