Skip to content

Commit

Permalink
Implemented resource name (#137)
Browse files Browse the repository at this point in the history
* feat: implemented resource name

* feat: improved cluster domain and resource name

* chore: updated test manifest

* chore: updated bootstrap files

* fix: fixed daemon config

* chore: updated rpc kafka consumer name

* fix: adjusted expect namespace watcher
  • Loading branch information
paulobressan authored Sep 25, 2024
1 parent da7816a commit f0565da
Show file tree
Hide file tree
Showing 26 changed files with 289 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .github/iac/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ module "fabric_rpc" {
namespace = local.namespace
image = var.rpc_image
broker_urls = local.broker_urls
consumer_name = "rpc-ahid02"
consumer_name = "rpc-ahid03"
kafka_username = local.kafka_rpc_username
kafka_password = local.kafka_rpc_password
kafka_topic = local.kafka_topic
Expand Down

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion bootstrap/daemon/daemon.toml.tftpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
topic = "${topic}"

cluster_id = "${cluster_id}"
prometheus_url = "${prometheus_url}"
delay_sec = ${prometheus_delay_sec}

[prometheus]
url = "${prometheus_url}"
query_step = "${prometheus_query_step}"

[kafka]
"bootstrap.servers" = "${broker_urls}"
"group.id"= "${consumer_name}"
Expand Down
6 changes: 6 additions & 0 deletions bootstrap/daemon/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ variable "prometheus_delay_sec" {
default = 60
}

variable "prometheus_query_step" {
type = string
description = "Usage Query Step"
default = "10m"
}

variable "tolerations" {
type = list(object({
effect = string
Expand Down
11 changes: 9 additions & 2 deletions src/bin/daemon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ async fn main() -> Result<()> {
Ok(())
}

#[derive(Debug, Deserialize, Clone)]
struct Prometheus {
url: String,
query_step: String,
}

#[derive(Debug, Deserialize, Clone)]
struct Config {
cluster_id: String,
prometheus_url: String,
prometheus: Prometheus,
#[serde(deserialize_with = "deserialize_duration")]
#[serde(rename(deserialize = "delay_sec"))]
delay: Duration,
Expand Down Expand Up @@ -70,7 +76,8 @@ impl From<Config> for UsageConfig {
fn from(value: Config) -> Self {
Self {
cluster_id: value.cluster_id,
prometheus_url: value.prometheus_url,
prometheus_url: value.prometheus.url,
prometheus_query_step: value.prometheus.query_step,
delay: value.delay,
kafka: value.kafka,
topic: value.topic,
Expand Down
22 changes: 15 additions & 7 deletions src/domain/event/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ pub struct ResourceCreated {
pub id: String,
pub project_id: String,
pub project_namespace: String,
pub name: String,
pub kind: String,
pub spec: String,
pub status: String,
Expand All @@ -112,6 +113,7 @@ pub struct ResourceUpdated {
pub id: String,
pub project_id: String,
pub project_namespace: String,
pub name: String,
pub kind: String,
pub spec_patch: String,
pub updated_at: DateTime<Utc>,
Expand All @@ -121,17 +123,19 @@ into_event!(ResourceUpdated);
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResourceDeleted {
pub id: String,
pub kind: String,
pub status: String,
pub project_id: String,
pub project_namespace: String,
pub name: String,
pub kind: String,
pub status: String,
pub deleted_at: DateTime<Utc>,
}
into_event!(ResourceDeleted);

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UsageUnitCreated {
pub resource_id: String,
pub project_namespace: String,
pub resource_name: String,
pub tier: String,
pub units: i64,
pub interval: u64,
Expand Down Expand Up @@ -219,6 +223,7 @@ mod tests {
project::{ProjectStatus, ProjectUserRole},
resource::ResourceStatus,
tests::{PHC, SECRET},
utils::get_random_salt,
};

use super::*;
Expand Down Expand Up @@ -280,7 +285,8 @@ mod tests {
Self {
id: Uuid::new_v4().to_string(),
project_id: Uuid::new_v4().to_string(),
project_namespace: "prj-test".into(),
project_namespace: "test".into(),
name: format!("cardanonode-{}", get_random_salt()),
kind: "CardanoNodePort".into(),
spec: "{\"version\":\"stable\",\"network\":\"mainnet\",\"throughputTier\":\"1\"}"
.into(),
Expand All @@ -294,10 +300,11 @@ mod tests {
fn default() -> Self {
Self {
id: Uuid::new_v4().to_string(),
project_id: Uuid::new_v4().to_string(),
project_namespace: "test".into(),
name: format!("cardanonode-{}", get_random_salt()),
kind: "CardanoNodePort".into(),
status: ResourceStatus::Deleted.to_string(),
project_id: Uuid::new_v4().to_string(),
project_namespace: "prj-test".into(),
deleted_at: Utc::now(),
}
}
Expand All @@ -308,7 +315,8 @@ mod tests {
id: Uuid::new_v4().to_string(),
cluster_id: Uuid::new_v4().to_string(),
usages: vec![UsageUnitCreated {
resource_id: Uuid::new_v4().to_string(),
project_namespace: "test".into(),
resource_name: format!("cardanonode-{}", get_random_salt()),
units: 120,
tier: "0".into(),
interval: 10,
Expand Down
5 changes: 3 additions & 2 deletions src/domain/project/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use tracing::info;

use crate::domain::{
event::{ProjectCreated, ProjectDeleted},
utils::cluster_namespace,
Result,
};

Expand All @@ -22,7 +23,7 @@ pub async fn apply_manifest(
) -> Result<()> {
let namespace = Namespace {
metadata: ObjectMeta {
name: Some(evt.namespace),
name: Some(cluster_namespace(&evt.namespace)),
..Default::default()
},
..Default::default()
Expand All @@ -41,7 +42,7 @@ pub async fn delete_manifest(
) -> Result<()> {
let namespace = Namespace {
metadata: ObjectMeta {
name: Some(evt.namespace),
name: Some(cluster_namespace(&evt.namespace)),
..Default::default()
},
..Default::default()
Expand Down
2 changes: 1 addition & 1 deletion src/domain/project/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ pub struct CreateCmd {
impl CreateCmd {
pub fn new(credential: Credential, name: String) -> Self {
let id = Uuid::new_v4().to_string();
let namespace = format!("prj-{}", utils::get_random_name());
let namespace = utils::get_random_name();

Self {
credential,
Expand Down
3 changes: 3 additions & 0 deletions src/domain/resource/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ use chrono::{DateTime, Utc};
pub trait ResourceDrivenCache: Send + Sync {
async fn find(&self, project_id: &str, page: &u32, page_size: &u32) -> Result<Vec<Resource>>;
async fn find_by_id(&self, id: &str) -> Result<Option<Resource>>;
async fn find_by_name(&self, project_id: &str, name: &str) -> Result<Option<Resource>>;
async fn find_by_name_for_usage(&self, namespace: &str, name: &str)
-> Result<Option<Resource>>;
async fn create(&self, resource: &Resource) -> Result<()>;
async fn update(&self, resource: &ResourceUpdate) -> Result<()>;
async fn delete(&self, id: &str, deleted_at: &DateTime<Utc>) -> Result<()>;
Expand Down
19 changes: 10 additions & 9 deletions src/domain/resource/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use tracing::info;

use crate::domain::{
event::{ResourceCreated, ResourceDeleted, ResourceUpdated},
utils::cluster_namespace,
Result,
};

Expand All @@ -24,10 +25,10 @@ pub async fn apply_manifest(
) -> Result<()> {
let api = build_api_resource(&evt.kind);

let mut obj = DynamicObject::new(&evt.id, &api);
let mut obj = DynamicObject::new(&evt.name, &api);
obj.metadata = ObjectMeta {
name: Some(evt.id),
namespace: Some(evt.project_namespace),
name: Some(evt.name),
namespace: Some(cluster_namespace(&evt.project_namespace)),
..Default::default()
};

Expand All @@ -47,10 +48,10 @@ pub async fn patch_manifest(
evt: ResourceUpdated,
) -> Result<()> {
let api = build_api_resource(&evt.kind);
let mut obj = DynamicObject::new(&evt.id, &api);
let mut obj = DynamicObject::new(&evt.name, &api);
obj.metadata = ObjectMeta {
name: Some(evt.id),
namespace: Some(evt.project_namespace),
name: Some(evt.name),
namespace: Some(cluster_namespace(&evt.project_namespace)),
..Default::default()
};

Expand All @@ -71,10 +72,10 @@ pub async fn delete_manifest(
) -> Result<()> {
let api = build_api_resource(&evt.kind);

let mut obj = DynamicObject::new(&evt.id, &api);
let mut obj = DynamicObject::new(&evt.name, &api);
obj.metadata = ObjectMeta {
name: Some(evt.id),
namespace: Some(evt.project_namespace),
name: Some(evt.name),
namespace: Some(cluster_namespace(&evt.project_namespace)),
..Default::default()
};

Expand Down
Loading

0 comments on commit f0565da

Please sign in to comment.