forked from NationTech/harmony
Make k8stenantmanager a oncecell
This commit is contained in:
parent
6490e5e82a
commit
7c809bf18a
@ -1,4 +1,4 @@
|
|||||||
use std::{process::Command, sync::Arc};
|
use std::{io::Error, process::Command, sync::Arc};
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use inquire::Confirm;
|
use inquire::Confirm;
|
||||||
@ -15,7 +15,11 @@ use crate::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
k8s::K8sClient, tenant::{k8s::K8sTenantManager, ResourceLimits, TenantConfig, TenantManager, TenantNetworkPolicy}, HelmCommand, K8sclient, Topology
|
HelmCommand, K8sclient, Topology,
|
||||||
|
k8s::K8sClient,
|
||||||
|
tenant::{
|
||||||
|
ResourceLimits, TenantConfig, TenantManager, TenantNetworkPolicy, k8s::K8sTenantManager,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
struct K8sState {
|
struct K8sState {
|
||||||
@ -32,10 +36,9 @@ enum K8sSource {
|
|||||||
|
|
||||||
pub struct K8sAnywhereTopology {
|
pub struct K8sAnywhereTopology {
|
||||||
k8s_state: OnceCell<Option<K8sState>>,
|
k8s_state: OnceCell<Option<K8sState>>,
|
||||||
tenant_manager: K8sTenantManager,
|
tenant_manager: OnceCell<K8sTenantManager>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl K8sclient for K8sAnywhereTopology {
|
impl K8sclient for K8sAnywhereTopology {
|
||||||
async fn k8s_client(&self) -> Result<Arc<K8sClient>, String> {
|
async fn k8s_client(&self) -> Result<Arc<K8sClient>, String> {
|
||||||
@ -57,6 +60,7 @@ impl K8sAnywhereTopology {
|
|||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
k8s_state: OnceCell::new(),
|
k8s_state: OnceCell::new(),
|
||||||
|
tenant_manager: OnceCell::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -165,6 +169,15 @@ impl K8sAnywhereTopology {
|
|||||||
|
|
||||||
Ok(Some(state))
|
Ok(Some(state))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_k8s_tenant_manager(&self) -> Result<&K8sTenantManager, ExecutorError> {
|
||||||
|
match self.tenant_manager.get() {
|
||||||
|
Some(t) => Ok(t),
|
||||||
|
None => Err(ExecutorError::UnexpectedError(
|
||||||
|
"K8sTenantManager not available".to_string(),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct K8sAnywhereConfig {
|
struct K8sAnywhereConfig {
|
||||||
@ -219,7 +232,9 @@ impl HelmCommand for K8sAnywhereTopology {}
|
|||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl TenantManager for K8sAnywhereTopology {
|
impl TenantManager for K8sAnywhereTopology {
|
||||||
async fn provision_tenant(&self, config: &TenantConfig) -> Result<(), ExecutorError> {
|
async fn provision_tenant(&self, config: &TenantConfig) -> Result<(), ExecutorError> {
|
||||||
self.tenant_manager.provision_tenant(config).await
|
self.get_k8s_tenant_manager()?
|
||||||
|
.provision_tenant(config)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_tenant_resource_limits(
|
async fn update_tenant_resource_limits(
|
||||||
@ -227,7 +242,9 @@ impl TenantManager for K8sAnywhereTopology {
|
|||||||
tenant_name: &str,
|
tenant_name: &str,
|
||||||
new_limits: &ResourceLimits,
|
new_limits: &ResourceLimits,
|
||||||
) -> Result<(), ExecutorError> {
|
) -> Result<(), ExecutorError> {
|
||||||
self.tenant_manager.update_tenant_resource_limits(tenant_name, new_limits).await
|
self.get_k8s_tenant_manager()?
|
||||||
|
.update_tenant_resource_limits(tenant_name, new_limits)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn update_tenant_network_policy(
|
async fn update_tenant_network_policy(
|
||||||
@ -235,10 +252,14 @@ impl TenantManager for K8sAnywhereTopology {
|
|||||||
tenant_name: &str,
|
tenant_name: &str,
|
||||||
new_policy: &TenantNetworkPolicy,
|
new_policy: &TenantNetworkPolicy,
|
||||||
) -> Result<(), ExecutorError> {
|
) -> Result<(), ExecutorError> {
|
||||||
self.tenant_manager.update_tenant_network_policy(tenant_name, new_policy).await
|
self.get_k8s_tenant_manager()?
|
||||||
|
.update_tenant_network_policy(tenant_name, new_policy)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn deprovision_tenant(&self, tenant_name: &str) -> Result<(), ExecutorError> {
|
async fn deprovision_tenant(&self, tenant_name: &str) -> Result<(), ExecutorError> {
|
||||||
self.tenant_manager.deprovision_tenant(tenant_name).await
|
self.get_k8s_tenant_manager()?
|
||||||
|
.deprovision_tenant(tenant_name)
|
||||||
|
.await
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,11 +2,13 @@ use std::sync::Arc;
|
|||||||
|
|
||||||
use crate::{executors::ExecutorError, topology::k8s::K8sClient};
|
use crate::{executors::ExecutorError, topology::k8s::K8sClient};
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
use derive_new::new;
|
||||||
use k8s_openapi::api::core::v1::Namespace;
|
use k8s_openapi::api::core::v1::Namespace;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
use super::{ResourceLimits, TenantConfig, TenantManager, TenantNetworkPolicy};
|
use super::{ResourceLimits, TenantConfig, TenantManager, TenantNetworkPolicy};
|
||||||
|
|
||||||
|
#[derive(new)]
|
||||||
pub struct K8sTenantManager {
|
pub struct K8sTenantManager {
|
||||||
k8s_client: Arc<K8sClient>,
|
k8s_client: Arc<K8sClient>,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,5 +1,5 @@
|
|||||||
mod manager;
|
|
||||||
pub mod k8s;
|
pub mod k8s;
|
||||||
|
mod manager;
|
||||||
pub use manager::*;
|
pub use manager::*;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user