From adb2a19f31d7ddda7df90667508b5e900bbddac1 Mon Sep 17 00:00:00 2001 From: Samuel Gomez Date: Tue, 17 Dec 2024 20:47:44 +0000 Subject: [PATCH] fix: removing all references of ServiceNameScope --- integrationos-api/src/domain/config.rs | 10 +++- integrationos-api/src/helper/k8s_driver.rs | 57 ++++--------------- integrationos-api/src/logic/connection.rs | 14 +---- integrationos-api/src/logic/event_callback.rs | 7 +-- .../tests/http/connection.rs | 4 +- 5 files changed, 26 insertions(+), 66 deletions(-) diff --git a/integrationos-api/src/domain/config.rs b/integrationos-api/src/domain/config.rs index abfb0bd1..72a5f744 100644 --- a/integrationos-api/src/domain/config.rs +++ b/integrationos-api/src/domain/config.rs @@ -86,6 +86,8 @@ pub struct ConnectionsConfig { default = "integrationos-database" )] pub database_connection_docker_image: String, + #[envconfig(from = "NAMESPACE", default = "development")] + pub namespace: String, #[envconfig(from = "DATABASE_CONNECTION_PROBE_TIMEOUT_SECS", default = "10")] pub database_connection_probe_timeout_secs: u64, #[envconfig(from = "K8S_MODE", default = "logger")] @@ -152,7 +154,13 @@ impl Display for ConnectionsConfig { writeln!(f, "{}", self.db_config)?; writeln!(f, "{}", self.cache_config)?; writeln!(f, "RATE_LIMIT_ENABLED: {}", self.rate_limit_enabled)?; - writeln!(f, "ENVIRONMENT: {}", self.environment) + writeln!(f, "ENVIRONMENT: {}", self.environment)?; + writeln!( + f, + "DATABASE_CONNECTION_DOCKER_IMAGE: {}", + self.database_connection_docker_image + )?; + writeln!(f, "NAMESPACE: {}", self.namespace) } } diff --git a/integrationos-api/src/helper/k8s_driver.rs b/integrationos-api/src/helper/k8s_driver.rs index 7da08055..9aec7237 100644 --- a/integrationos-api/src/helper/k8s_driver.rs +++ b/integrationos-api/src/helper/k8s_driver.rs @@ -19,43 +19,6 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use std::fmt::Debug; use std::{collections::BTreeMap, fmt::Display}; -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] -#[serde(rename_all = "kebab-case")] -pub enum NamespaceScope { - Development, - Production, -} - -impl TryFrom<&str> for NamespaceScope { - type Error = IntegrationOSError; - - fn try_from(value: &str) -> Result { - match value { - "development-db-conns" => Ok(NamespaceScope::Development), - "production-db-conns" => Ok(NamespaceScope::Production), - _ => Err(InternalError::invalid_argument( - &format!("Invalid namespace scope: {}", value), - None, - )), - } - } -} - -impl AsRef for NamespaceScope { - fn as_ref(&self) -> &str { - match self { - NamespaceScope::Development => "development-db-conns", - NamespaceScope::Production => "production-db-conns", - } - } -} - -impl Display for NamespaceScope { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.as_ref()) - } -} - #[async_trait] pub trait K8sDriver: Send + Sync { async fn create_service( @@ -68,7 +31,7 @@ pub trait K8sDriver: Send + Sync { ) -> Result; async fn delete_all( &self, - namespace: NamespaceScope, + namespace: String, name: ServiceName, ) -> Result; async fn coordinator( @@ -110,7 +73,7 @@ impl K8sDriver for K8sDriverImpl { async fn delete_all( &self, - namespace: NamespaceScope, + namespace: String, name: ServiceName, ) -> Result { delete_all_impl(self.client.clone(), namespace, name).await @@ -161,13 +124,13 @@ impl K8sDriver for K8sDriverLogger { /// - `namespace` - Namespace the existing deployment resides in async fn delete_all( &self, - namespace: NamespaceScope, + namespace: String, name: ServiceName, ) -> Result { tracing::info!( "Deleting k8s resource {} in namespace {}", name.as_ref(), - namespace.as_ref() + namespace, ); Ok(()) } @@ -204,7 +167,7 @@ pub struct ServiceSpecParams { /// Annotations to apply to the service. Has to match with the deployment metadata pub name: ServiceName, /// Namespace the service should reside in - pub namespace: NamespaceScope, + pub namespace: String, } async fn create_service_impl( @@ -215,7 +178,7 @@ async fn create_service_impl( metadata: ObjectMeta { name: Some(params.name.as_ref().to_string()), labels: Some(params.labels.clone()), - namespace: Some(params.namespace.as_ref().to_owned()), + namespace: Some(params.namespace.clone()), ..Default::default() }, spec: Some(ServiceSpec { @@ -227,7 +190,7 @@ async fn create_service_impl( ..Default::default() }; - let service_api: Api = Api::namespaced(client, params.namespace.as_ref()); + let service_api: Api = Api::namespaced(client, ¶ms.namespace); service_api .create(&PostParams::default(), &service) .await @@ -241,7 +204,7 @@ pub struct DeploymentSpecParams { /// Labels to apply to the deployment pub labels: BTreeMap, /// Namespace the deployment should reside in - pub namespace: NamespaceScope, + pub namespace: String, /// Image to use for the deployment pub image: String, /// Environment variables to apply @@ -260,7 +223,7 @@ async fn create_deployment_impl( let deployment: Deployment = Deployment { metadata: ObjectMeta { name: Some(params.name.as_ref().to_string()), - namespace: Some(params.namespace.as_ref().to_owned()), + namespace: Some(params.namespace.clone()), labels: Some(params.labels.clone()), ..ObjectMeta::default() }, @@ -317,7 +280,7 @@ where pub async fn delete_all_impl( client: Client, - namespace: NamespaceScope, + namespace: String, name: ServiceName, ) -> Result { delete_resource_impl::(client.clone(), name.as_ref(), namespace.as_ref()).await?; diff --git a/integrationos-api/src/logic/connection.rs b/integrationos-api/src/logic/connection.rs index e03253de..c8cc58da 100644 --- a/integrationos-api/src/logic/connection.rs +++ b/integrationos-api/src/logic/connection.rs @@ -1,6 +1,6 @@ use super::{delete, event_access::DEFAULT_NAMESPACE, read, PublicExt, RequestExt}; use crate::{ - helper::{DeploymentSpecParams, NamespaceScope, ServiceName, ServiceSpecParams}, + helper::{DeploymentSpecParams, ServiceName, ServiceSpecParams}, logic::event_access::{ generate_event_access, get_client_throughput, CreateEventAccessPayloadWithOwnership, }, @@ -22,7 +22,6 @@ use integrationos_domain::{ database::{DatabasePodConfig, PostgresConfig}, database_secret::DatabaseConnectionSecret, domain::connection::SanitizedConnection, - environment::Environment, event_access::EventAccess, id::{prefix::IdPrefix, Id}, record_metadata::RecordMetadata, @@ -380,11 +379,7 @@ async fn generate_k8s_specs_and_secret( Ok(match connection_config.to_connection_type() { integrationos_domain::ConnectionType::DatabaseSql {} => { let service_name = ServiceName::from_id(*connection_id)?; - - let namespace = match state.config.environment { - Environment::Test | Environment::Development => NamespaceScope::Development, - Environment::Live | Environment::Production => NamespaceScope::Production, - }; + let namespace = state.config.namespace.clone(); let mut labels: BTreeMap = BTreeMap::new(); labels.insert(APP_LABEL.to_owned(), service_name.as_ref().to_string()); @@ -675,11 +670,8 @@ pub async fn delete_connection( .await?; if let ConnectionType::DatabaseSql { .. } = connection.args.r#type { - let namespace = match state.config.environment { - Environment::Test | Environment::Development => NamespaceScope::Development, - Environment::Live | Environment::Production => NamespaceScope::Production, - }; let service_name = ServiceName::from_id(connection.args.id)?; + let namespace = state.config.namespace.clone(); state.k8s_client.delete_all(namespace, service_name).await?; }; diff --git a/integrationos-api/src/logic/event_callback.rs b/integrationos-api/src/logic/event_callback.rs index e5e41bd6..919ab39c 100644 --- a/integrationos-api/src/logic/event_callback.rs +++ b/integrationos-api/src/logic/event_callback.rs @@ -1,7 +1,4 @@ -use crate::{ - helper::{NamespaceScope, ServiceName}, - server::AppState, -}; +use crate::{helper::ServiceName, server::AppState}; use axum::{ extract::{Path, State}, routing::post, @@ -56,8 +53,8 @@ async fn database_connection_lost_callback( // This means that there's a pod resource that is not running // and we need to delete these resources if let Ok(secret) = secret.decode::() { - let namespace: NamespaceScope = secret.namespace.as_str().try_into()?; let service_name = ServiceName::from_id(connection_id)?; + let namespace = secret.namespace; tracing::info!( "Deleting all resources for connection {id} in namespace {}", diff --git a/integrationos-database/tests/http/connection.rs b/integrationos-database/tests/http/connection.rs index 8f780b67..2c0ede6c 100644 --- a/integrationos-database/tests/http/connection.rs +++ b/integrationos-database/tests/http/connection.rs @@ -21,7 +21,7 @@ async fn test_execute_probe() -> Result { let port = postgres.get_host_port_ipv4(5432); let database_secret = DatabaseConnectionSecret { - namespace: "development-db-conns".to_string(), + namespace: "development".to_string(), service_name: "service_name".to_string(), connection_id, postgres_config: PostgresConfig { @@ -88,7 +88,7 @@ async fn test_execute_raw() -> Result { let port = postgres.get_host_port_ipv4(5432); let database_secret = DatabaseConnectionSecret { - namespace: "development-db-conns".to_string(), + namespace: "development".to_string(), service_name: "service_name".to_string(), connection_id, postgres_config: PostgresConfig {