From 1ecee01b174a8578d5792b27bc173c6c8bca1794 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Fri, 9 May 2025 16:39:12 +0200 Subject: [PATCH] Some fixes and updates Signed-off-by: Nikolai Rodionov --- example/helmule.yaml | 9 ++++++++- src/git.rs | 2 +- src/helm/chart.rs | 4 ++++ src/main.rs | 41 ++++++++++++++++++++++++++++++++--------- src/mirror/mod.rs | 22 +++++++++++++++++++++- 5 files changed, 66 insertions(+), 12 deletions(-) diff --git a/example/helmule.yaml b/example/helmule.yaml index 2c2cb1c..01f5295 100644 --- a/example/helmule.yaml +++ b/example/helmule.yaml @@ -4,10 +4,17 @@ repositories: url: https://grafana.github.io/helm-charts charts: - - name: k8s-monitoring + - name: mimir-distributed repository: grafana + patches: + - name: vendor-deps mirrors: - custom-command +patches: + - name: vendor-deps + custom_command: + commands: + - yq -i '.dependencies[].repository = "file://charts"' Chart.yaml mirrors: - name: custom-commands custom_command: diff --git a/src/git.rs b/src/git.rs index 598f9d2..49220a3 100644 --- a/src/git.rs +++ b/src/git.rs @@ -90,7 +90,7 @@ impl Git { let _ = self.exec(cmd, git_opts.workdir.clone())?; } let cmd = format!( - "{} diff --staged --quiet || {} -C {} commit -m \"{}\"", + "{} diff --cached --quiet || {} -C {} commit -m \"{}\"", git_opts.bin, git_opts.bin, self.repo_path, opts.message ); match self.exec(cmd, git_opts.workdir.clone()) { diff --git a/src/helm/chart.rs b/src/helm/chart.rs index 24b258b..376d4dd 100644 --- a/src/helm/chart.rs +++ b/src/helm/chart.rs @@ -20,6 +20,10 @@ pub(crate) fn latest() -> String { "latest".to_string() } +pub(crate) fn def_false() -> bool { + false +} + impl Chart { pub fn find_repo( &self, diff --git a/src/main.rs b/src/main.rs index 6757c63..163e733 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use clap::Parser; use cli::{check_prerequisites, get_full_path_dir}; -use config::{read_config, Config, ConfigImpl}; +use config::{read_config, ChartExtended, Config, ConfigImpl}; +use helm::chart::Chart; use output::{message_empty, message_error}; use std::{error::Error, process::exit}; @@ -24,6 +25,12 @@ struct Args { /// Name of the working dir #[arg(short, long)] workdir: Option, + /// Configure the chart section from cli + /// If only one chart is needed + #[arg(long)] + chart: Option, + #[arg(long)] + version_override: Option, /// Path to the configuration file #[arg(short, long)] config: String, @@ -53,12 +60,26 @@ fn exec(args: Args) -> Result<(), Box> { let workdir_path = workdir::setup_workdir(args.workdir)?; let mut config: Config = read_config(args.config.clone())?; let config_full_path = get_full_path_dir(args.config.clone())?; + if let Some(chart_arg) = args.chart { + config.charts = config.charts.into_iter().filter(|chart| chart.chart.name == chart_arg).collect::>(); + if let Some(version_arg) = args.version_override { + config.charts.iter_mut() + .for_each(|chart| chart.chart.version = version_arg.clone()); + }; + }; config.apply_includes(config_full_path.clone())?; config .charts .into_iter() .try_for_each(|mut chart| -> Result<(), Box> { chart.populate_variables(config.variables.clone()); + let mut chart_mirrors = config.mirrors.clone(); + chart_mirrors.retain(|mirror| mirror.check_chart(&chart) == false); + + if chart_mirrors.is_empty() { + output::message_info(&format!("mirrors are up-to-date for {}", chart.chart.name.clone())); + return Ok(()); + } // First step is to pull the chart to the working dir let current_repo = chart.chart.find_repo(config.repositories.clone())?; let chart_path = current_repo.pull_chart(chart.chart.clone(), workdir_path.clone())?; @@ -77,16 +98,18 @@ fn exec(args: Args) -> Result<(), Box> { patch.apply(chart_path.clone(), config.patches.clone()) })? } - config - .mirrors + chart_mirrors .iter() .try_for_each(|mirror| -> Result<(), Box> { - mirror_from_mirror_obj(mirror.clone())?.push( - workdir_path.clone(), - chart_path.clone(), - chart.clone(), - args.dry_run, - ) + if chart.chart.mirrors.contains(&mirror.name) { + mirror_from_mirror_obj(mirror.clone())?.push( + workdir_path.clone(), + chart_path.clone(), + chart.clone(), + args.dry_run, + )? + }; + Ok(()) })?; message_empty(&format!("{}", chart.chart.name)); Ok(()) diff --git a/src/mirror/mod.rs b/src/mirror/mod.rs index a0284db..89911fc 100644 --- a/src/mirror/mod.rs +++ b/src/mirror/mod.rs @@ -1,6 +1,6 @@ use serde::{Deserialize, Serialize}; -use crate::config::ChartExtended; +use crate::{cli::{self, cli_exec, cli_exec_from_dir}, config::ChartExtended, output::message_info, template::render}; pub(crate) mod custom_command; pub(crate) mod git; @@ -20,6 +20,26 @@ pub(crate) struct Mirror { pub(crate) name: String, pub(crate) git: Option, pub(crate) custom_command: Option, + pub(crate) check: Option, +} + +impl Mirror { + pub(crate) fn check_chart(&self, chart_local: &ChartExtended) -> bool { + match &self.check { + Some(check_cmd) => { + let cmd = match render(check_cmd.to_string(), chart_local) { + Ok(res) => res, + Err(_) => return false, + }; + message_info(&format!("checking the mirror {}", self.name.clone())); + match cli_exec(cmd.clone()) { + Ok(_) => return true, + Err(_) => return false, + } + }, + None => false, + } + } } #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]