| 435 | /// * `namespace` - list charts from a kubernetes namespace or use None to select all namespaces |
| 436 | #[track_caller] |
| 437 | pub fn list_release(&self, namespace: Option<&str>, envs: &[(&str, &str)]) -> Result<Vec<HelmChart>, HelmError> { |
| 438 | if tracing::enabled!(tracing::Level::DEBUG) { |
| 439 | let caller = Location::caller(); |
| 440 | debug!( |
| 441 | caller_file = caller.file(), |
| 442 | caller_line = caller.line(), |
| 443 | namespace = namespace.unwrap_or_default(), |
| 444 | "list_release called; use list_deployed_charts for deploy-path lookups" |
| 445 | ); |
| 446 | } |
| 447 | |
| 448 | let mut helm_args = vec!["list", "-a", "-o", "json"]; |
| 449 | match namespace { |
| 450 | Some(ns) => helm_args.append(&mut vec!["-n", ns]), |
| 451 | None => helm_args.push("-A"), |
| 452 | } |
| 453 | |
| 454 | let mut output_string: Vec<String> = Vec::with_capacity(20); |
| 455 | if let Err(cmd_error) = helm_exec_with_output( |
| 456 | &helm_args, |
| 457 | &self.get_all_envs(envs), |
| 458 | &mut |line| output_string.push(line), |
| 459 | &mut |line| error!("{}", line), |
| 460 | &CommandKiller::never(), |
| 461 | ) { |
| 462 | return Err(CmdError("none".to_string(), LIST, cmd_error.into())); |
| 463 | } |
| 464 | |
| 465 | let values = serde_json::from_str::<Vec<HelmListItem>>(&output_string.join("")); |
| 466 | let mut helms_charts: Vec<HelmChart> = Vec::new(); |
| 467 | |
| 468 | match values { |
| 469 | Ok(all_helms) => { |
| 470 | for helm in all_helms { |
| 471 | // chart version is stored in chart name (i.e loki-3.4.5) so we look for last dash position to parse name. |
| 472 | let mut last_dash_pos = helm.chart.rfind('-').expect("Can't parse helm chart") + 1; |
| 473 | // sometime chart version in name start with 'v' (i.e loki-v3.4.5). We squeeze it. |
| 474 | if helm.chart[last_dash_pos..].starts_with('v') { |
| 475 | last_dash_pos += 1 |
| 476 | } |
| 477 | |
| 478 | let chart_version_raw = helm.chart[last_dash_pos..].to_string(); |
| 479 | let chart_version = Version::from_str(chart_version_raw.as_str()).ok(); |
| 480 | |
| 481 | let mut app_version_raw = helm.app_version; |
| 482 | // sometime app version start with 'v'. We squeeze it. |
| 483 | if app_version_raw.starts_with('v') { |
| 484 | app_version_raw = app_version_raw[1..].to_string() |
| 485 | } |
| 486 | let app_version = Version::from_str(app_version_raw.as_str()).ok(); |
| 487 | |
| 488 | helms_charts.push(HelmChart::new(helm.name, helm.namespace, chart_version, app_version)) |
| 489 | } |
| 490 | |
| 491 | Ok(helms_charts) |
| 492 | } |
| 493 | Err(e) => Err(CmdError( |
| 494 | "none".to_string(), |