forked from NationTech/harmony
- Adds a `deploy_database` function to the `LAMPInterpret` struct to deploy a MariaDB database using Helm. - Integrates `HelmCommand` trait requirement to the `LAMPInterpret` struct. - Introduces `HelmChartScore` to manage MariaDB deployment. - Adds namespace configuration for helm deployments. - Updates trait bounds for `LAMPInterpret` to include `HelmCommand`. - Implements `get_namespace` function to retrieve the namespace.
158 lines
5.0 KiB
Rust
158 lines
5.0 KiB
Rust
use crate::data::{Id, Version};
|
|
use crate::interpret::{Interpret, InterpretError, InterpretName, InterpretStatus, Outcome};
|
|
use crate::inventory::Inventory;
|
|
use crate::score::Score;
|
|
use crate::topology::{HelmCommand, Topology};
|
|
use async_trait::async_trait;
|
|
use helm_wrapper_rs;
|
|
use helm_wrapper_rs::blocking::{DefaultHelmExecutor, HelmExecutor};
|
|
use log::info;
|
|
pub use non_blank_string_rs::NonBlankString;
|
|
use serde::Serialize;
|
|
use std::collections::HashMap;
|
|
use std::path::Path;
|
|
use std::str::FromStr;
|
|
use temp_file::TempFile;
|
|
|
|
#[derive(Debug, Clone, Serialize)]
|
|
pub struct HelmChartScore {
|
|
pub namespace: Option<NonBlankString>,
|
|
pub release_name: NonBlankString,
|
|
pub chart_name: NonBlankString,
|
|
pub chart_version: Option<NonBlankString>,
|
|
pub values_overrides: Option<HashMap<NonBlankString, String>>,
|
|
pub values_yaml: Option<String>,
|
|
pub create_namespace: bool,
|
|
|
|
/// Wether to run `helm upgrade --install` under the hood or only install when not present
|
|
pub install_only: bool,
|
|
}
|
|
|
|
impl<T: Topology + HelmCommand> Score<T> for HelmChartScore {
|
|
fn create_interpret(&self) -> Box<dyn Interpret<T>> {
|
|
Box::new(HelmChartInterpret {
|
|
score: self.clone(),
|
|
})
|
|
}
|
|
|
|
fn name(&self) -> String {
|
|
format!("{} {} HelmChartScore", self.release_name, self.chart_name)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
pub struct HelmChartInterpret {
|
|
pub score: HelmChartScore,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl<T: Topology + HelmCommand> Interpret<T> for HelmChartInterpret {
|
|
async fn execute(
|
|
&self,
|
|
_inventory: &Inventory,
|
|
_topology: &T,
|
|
) -> Result<Outcome, InterpretError> {
|
|
let ns = self
|
|
.score
|
|
.namespace
|
|
.as_ref()
|
|
.unwrap_or_else(|| todo!("Get namespace from active kubernetes cluster"));
|
|
|
|
let tf: TempFile;
|
|
let yaml_path: Option<&Path> = match self.score.values_yaml.as_ref() {
|
|
Some(yaml_str) => {
|
|
tf = temp_file::with_contents(yaml_str.as_bytes());
|
|
Some(tf.path())
|
|
}
|
|
None => None,
|
|
};
|
|
|
|
let helm_executor = DefaultHelmExecutor::new();
|
|
|
|
let mut helm_options = Vec::new();
|
|
if self.score.create_namespace {
|
|
helm_options.push(NonBlankString::from_str("--create-namespace").unwrap());
|
|
}
|
|
|
|
if self.score.install_only {
|
|
let chart_list = match helm_executor.list(Some(ns)) {
|
|
Ok(charts) => charts,
|
|
Err(e) => {
|
|
return Err(InterpretError::new(format!(
|
|
"Failed to list scores in namespace {:?} because of error : {}",
|
|
self.score.namespace, e
|
|
)));
|
|
}
|
|
};
|
|
|
|
if chart_list
|
|
.iter()
|
|
.any(|item| item.name == self.score.release_name.to_string())
|
|
{
|
|
info!(
|
|
"Release '{}' already exists in namespace '{}'. Skipping installation as install_only is true.",
|
|
self.score.release_name, ns
|
|
);
|
|
|
|
return Ok(Outcome::new(
|
|
InterpretStatus::SUCCESS,
|
|
format!(
|
|
"Helm Chart '{}' already installed to namespace {ns} and install_only=true",
|
|
self.score.release_name
|
|
),
|
|
));
|
|
} else {
|
|
info!(
|
|
"Release '{}' not found in namespace '{}'. Proceeding with installation.",
|
|
self.score.release_name, ns
|
|
);
|
|
}
|
|
}
|
|
|
|
let res = helm_executor.install_or_upgrade(
|
|
&ns,
|
|
&self.score.release_name,
|
|
&self.score.chart_name,
|
|
self.score.chart_version.as_ref(),
|
|
self.score.values_overrides.as_ref(),
|
|
yaml_path,
|
|
Some(&helm_options),
|
|
);
|
|
|
|
let status = match res {
|
|
Ok(status) => status,
|
|
Err(err) => return Err(InterpretError::new(err.to_string())),
|
|
};
|
|
|
|
match status {
|
|
helm_wrapper_rs::HelmDeployStatus::Deployed => Ok(Outcome::new(
|
|
InterpretStatus::SUCCESS,
|
|
"Helm Chart deployed".to_string(),
|
|
)),
|
|
helm_wrapper_rs::HelmDeployStatus::PendingInstall => Ok(Outcome::new(
|
|
InterpretStatus::RUNNING,
|
|
"Helm Chart Pending install".to_string(),
|
|
)),
|
|
helm_wrapper_rs::HelmDeployStatus::PendingUpgrade => Ok(Outcome::new(
|
|
InterpretStatus::RUNNING,
|
|
"Helm Chart pending upgrade".to_string(),
|
|
)),
|
|
helm_wrapper_rs::HelmDeployStatus::Failed => Err(InterpretError::new(
|
|
"Failed to install helm chart".to_string(),
|
|
)),
|
|
}
|
|
}
|
|
fn get_name(&self) -> InterpretName {
|
|
todo!()
|
|
}
|
|
fn get_version(&self) -> Version {
|
|
todo!()
|
|
}
|
|
fn get_status(&self) -> InterpretStatus {
|
|
todo!()
|
|
}
|
|
fn get_children(&self) -> Vec<Id> {
|
|
todo!()
|
|
}
|
|
}
|