Skip to content

Commit

Permalink
fix: removing all references of ServiceNameScope
Browse files Browse the repository at this point in the history
  • Loading branch information
sagojez committed Dec 17, 2024
1 parent 07736de commit adb2a19
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 66 deletions.
10 changes: 9 additions & 1 deletion integrationos-api/src/domain/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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)
}
}

Expand Down
57 changes: 10 additions & 47 deletions integrationos-api/src/helper/k8s_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Self::Error> {
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<str> 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(
Expand All @@ -68,7 +31,7 @@ pub trait K8sDriver: Send + Sync {
) -> Result<Deployment, IntegrationOSError>;
async fn delete_all(
&self,
namespace: NamespaceScope,
namespace: String,
name: ServiceName,
) -> Result<Unit, IntegrationOSError>;
async fn coordinator(
Expand Down Expand Up @@ -110,7 +73,7 @@ impl K8sDriver for K8sDriverImpl {

async fn delete_all(
&self,
namespace: NamespaceScope,
namespace: String,
name: ServiceName,
) -> Result<Unit, IntegrationOSError> {
delete_all_impl(self.client.clone(), namespace, name).await
Expand Down Expand Up @@ -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<Unit, IntegrationOSError> {
tracing::info!(
"Deleting k8s resource {} in namespace {}",
name.as_ref(),
namespace.as_ref()
namespace,
);
Ok(())
}
Expand Down Expand Up @@ -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(
Expand All @@ -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 {
Expand All @@ -227,7 +190,7 @@ async fn create_service_impl(
..Default::default()
};

let service_api: Api<Service> = Api::namespaced(client, params.namespace.as_ref());
let service_api: Api<Service> = Api::namespaced(client, &params.namespace);
service_api
.create(&PostParams::default(), &service)
.await
Expand All @@ -241,7 +204,7 @@ pub struct DeploymentSpecParams {
/// Labels to apply to the deployment
pub labels: BTreeMap<String, String>,
/// 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
Expand All @@ -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()
},
Expand Down Expand Up @@ -317,7 +280,7 @@ where

pub async fn delete_all_impl(
client: Client,
namespace: NamespaceScope,
namespace: String,
name: ServiceName,
) -> Result<Unit, IntegrationOSError> {
delete_resource_impl::<Service>(client.clone(), name.as_ref(), namespace.as_ref()).await?;
Expand Down
14 changes: 3 additions & 11 deletions integrationos-api/src/logic/connection.rs
Original file line number Diff line number Diff line change
@@ -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,
},
Expand All @@ -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,
Expand Down Expand Up @@ -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<String, String> = BTreeMap::new();
labels.insert(APP_LABEL.to_owned(), service_name.as_ref().to_string());
Expand Down Expand Up @@ -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?;
};

Expand Down
7 changes: 2 additions & 5 deletions integrationos-api/src/logic/event_callback.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
helper::{NamespaceScope, ServiceName},
server::AppState,
};
use crate::{helper::ServiceName, server::AppState};
use axum::{
extract::{Path, State},
routing::post,
Expand Down Expand Up @@ -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::<DatabaseConnectionSecret>() {
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 {}",
Expand Down
4 changes: 2 additions & 2 deletions integrationos-database/tests/http/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn test_execute_probe() -> Result<Unit, IntegrationOSError> {
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 {
Expand Down Expand Up @@ -88,7 +88,7 @@ async fn test_execute_raw() -> Result<Unit, IntegrationOSError> {
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 {
Expand Down

0 comments on commit adb2a19

Please sign in to comment.