From 8e472e4c65dbbb6e4b3851fb60422e25f5e04413 Mon Sep 17 00:00:00 2001 From: Jean-Gabriel Gill-Couture Date: Sun, 8 Jun 2025 21:23:29 -0400 Subject: [PATCH 1/3] feat: Add Default implementation for Harmony Id along with documentation. This Id implementation is optimized for ease of use. Ids are prefixed with the unix epoch and suffixed with 7 alphanumeric characters. But Ids can also contain any String the user wants to pass it --- Cargo.lock | 8 ++++++++ harmony/Cargo.toml | 2 ++ harmony/src/domain/data/id.rs | 31 ++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index ded4c85..a2d8c40 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1431,6 +1431,7 @@ dependencies = [ "harmony_macros", "harmony_types", "helm-wrapper-rs", + "hex", "http 1.3.1", "inquire", "k3d-rs", @@ -1442,6 +1443,7 @@ dependencies = [ "non-blank-string-rs", "opnsense-config", "opnsense-config-xml", + "rand 0.9.1", "reqwest 0.11.27", "russh", "rust-ipmi", @@ -1566,6 +1568,12 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" +[[package]] +name = "hex" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" + [[package]] name = "hex-literal" version = "0.4.1" diff --git a/harmony/Cargo.toml b/harmony/Cargo.toml index fcf69cf..b98ec49 100644 --- a/harmony/Cargo.toml +++ b/harmony/Cargo.toml @@ -6,6 +6,8 @@ readme.workspace = true license.workspace = true [dependencies] +rand = "0.9" +hex = "0.4" libredfish = "0.1.1" reqwest = { version = "0.11", features = ["blocking", "json"] } russh = "0.45.0" diff --git a/harmony/src/domain/data/id.rs b/harmony/src/domain/data/id.rs index b9f9e7a..1f64054 100644 --- a/harmony/src/domain/data/id.rs +++ b/harmony/src/domain/data/id.rs @@ -1,5 +1,23 @@ +use std::time::SystemTime; +use std::time::UNIX_EPOCH; +use rand::distr::Alphanumeric; +use rand::distr::SampleString; + use serde::{Deserialize, Serialize}; +/// A unique identifier designed for ease of use. +/// +/// You can pass it any String to use and Id, or you can use the default format with `Id::default()` +/// +/// The default format looks like this +/// +/// `462d4c_g2COgai` +/// +/// The first part is the unix timesamp in hexadecimal which makes Id easily sorted by creation time. +/// Second part is a serie of 7 random characters. +/// +/// **It is not meant to be very secure or unique**, it is suitable to generate up to 10 000 items per +/// second with a reasonable collision rate of 0,000014 % as calculated by this calculator : https://kevingal.com/apps/collision.html #[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Id { value: String, @@ -19,6 +37,17 @@ impl std::fmt::Display for Id { impl Default for Id { fn default() -> Self { - todo!() + let start = SystemTime::now(); + let since_the_epoch = start + .duration_since(UNIX_EPOCH) + .expect("Time went backwards"); + let timestamp = since_the_epoch.as_secs(); + + let hex_timestamp = format!("{:x}", timestamp & 0xffffff); + + let random_part: String = Alphanumeric.sample_string(&mut rand::rng(), 7); + + let value = format!("{}_{}", hex_timestamp, random_part); + Self { value } } } From 14fc4345c1f15959f5cff280634d10d4537cbfcb Mon Sep 17 00:00:00 2001 From: Jean-Gabriel Gill-Couture Date: Sun, 8 Jun 2025 23:24:41 -0400 Subject: [PATCH 2/3] feat: Initialize k8s tenant properly --- harmony/src/domain/data/id.rs | 4 ++-- harmony/src/domain/topology/k8s_anywhere.rs | 20 ++++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/harmony/src/domain/data/id.rs b/harmony/src/domain/data/id.rs index 1f64054..2950324 100644 --- a/harmony/src/domain/data/id.rs +++ b/harmony/src/domain/data/id.rs @@ -1,7 +1,7 @@ -use std::time::SystemTime; -use std::time::UNIX_EPOCH; use rand::distr::Alphanumeric; use rand::distr::SampleString; +use std::time::SystemTime; +use std::time::UNIX_EPOCH; use serde::{Deserialize, Serialize}; diff --git a/harmony/src/domain/topology/k8s_anywhere.rs b/harmony/src/domain/topology/k8s_anywhere.rs index 369f030..bd2f261 100644 --- a/harmony/src/domain/topology/k8s_anywhere.rs +++ b/harmony/src/domain/topology/k8s_anywhere.rs @@ -170,6 +170,22 @@ impl K8sAnywhereTopology { Ok(Some(state)) } + async fn ensure_k8s_tenant_manager(&self) -> Result<(), String> { + if let Some(_) = self.tenant_manager.get() { + return Ok(()); + } + + self.tenant_manager + .get_or_try_init(async || -> Result { + let k8s_client = self.k8s_client().await?; + Ok(K8sTenantManager::new(k8s_client)) + }) + .await + .unwrap(); + + Ok(()) + } + fn get_k8s_tenant_manager(&self) -> Result<&K8sTenantManager, ExecutorError> { match self.tenant_manager.get() { Some(t) => Ok(t), @@ -217,6 +233,10 @@ impl Topology for K8sAnywhereTopology { "No K8s client could be found or installed".to_string(), ))?; + self.ensure_k8s_tenant_manager() + .await + .map_err(|e| InterpretError::new(e))?; + match self.is_helm_available() { Ok(()) => Ok(Outcome::success(format!( "{} + helm available", From 24e466fadd600a6774561f5a73cbeeab6ad9476e Mon Sep 17 00:00:00 2001 From: Jean-Gabriel Gill-Couture Date: Sun, 8 Jun 2025 23:51:11 -0400 Subject: [PATCH 3/3] fix: formatting --- harmony/src/domain/data/id.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/harmony/src/domain/data/id.rs b/harmony/src/domain/data/id.rs index 1f64054..2950324 100644 --- a/harmony/src/domain/data/id.rs +++ b/harmony/src/domain/data/id.rs @@ -1,7 +1,7 @@ -use std::time::SystemTime; -use std::time::UNIX_EPOCH; use rand::distr::Alphanumeric; use rand::distr::SampleString; +use std::time::SystemTime; +use std::time::UNIX_EPOCH; use serde::{Deserialize, Serialize};