Skip to main content

helm_rs/command/render/
lint.rs

1use std::path::PathBuf;
2
3use crate::backend::Command;
4use crate::command::builder_collections;
5use crate::values::Values;
6
7/// `helm lint` (`pkg/cmd/lint.go`, `pkg/action/lint.go`): checks charts and returns the findings.
8///
9/// A chart with errors (or, with `strict`, warnings) makes Helm exit non-zero; that is a result,
10/// not an error: read [`LintReport::passed`]. An error is returned only when Helm could not lint
11/// at all (for example an unreadable values file).
12#[derive(Debug, Clone, PartialEq, bon::Builder)]
13#[builder(start_fn(name = start, vis = ""), on(String, into), on(bool, default))]
14#[allow(clippy::struct_excessive_bools, reason = "one field per Helm flag")]
15pub struct Lint {
16    /// The `PATH` arguments: chart directories or `.tgz` archives; at least one.
17    #[builder(field)]
18    pub paths: Vec<PathBuf>,
19    /// Flags `-f`, `--set`, `--set-string`, `--set-file`, `--set-json` and `--set-literal`.
20    #[builder(default)]
21    pub values: Values,
22    /// Flag `--kube-version`: the Kubernetes version for capabilities and deprecation checks.
23    pub kube_version: Option<String>,
24    /// Flag `--quiet`: print only warnings and errors (charts without them are left out of the
25    /// report).
26    pub quiet: bool,
27    /// Flag `--skip-schema-validation`: do not validate the values against the chart's schema.
28    pub skip_schema_validation: bool,
29    /// Flag `--strict`: a warning fails the chart too.
30    pub strict: bool,
31    /// Flag `--with-subcharts`: lint the charts below `charts/` as well.
32    pub with_subcharts: bool,
33}
34
35builder_collections! {
36    LintBuilder, lint_builder;
37    vec paths / path: PathBuf, "`PATH` arguments", "the chart directories or `.tgz` archives";
38}
39
40impl Lint {
41    /// Starts building a lint of the chart at `path`, the first of the required `PATH...`.
42    pub fn builder(path: impl Into<PathBuf>) -> LintBuilder {
43        Self::start().path(path)
44    }
45
46    /// Lints the chart at `path` with Helm's defaults.
47    #[must_use]
48    pub fn new(path: impl Into<PathBuf>) -> Self {
49        Self::builder(path).build()
50    }
51}
52
53/// How serious a lint finding is (`pkg/chart/v2/lint/support/message.go`).
54#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
55pub enum Severity {
56    /// `UNKNOWN`: never fails a chart.
57    Unknown,
58    /// `INFO`: for example a missing `values.yaml`.
59    Info,
60    /// `WARNING`: against convention but likely to work; fails the chart with `--strict`.
61    Warning,
62    /// `ERROR`: the chart is not likely to work; fails the chart.
63    Error,
64}
65
66/// One finding: Helm prints it as `[SEVERITY] path: message`.
67#[derive(Debug, Clone, PartialEq, Eq)]
68pub struct LintMessage {
69    /// The severity.
70    pub severity: Severity,
71    /// The file or directory the finding is about, relative to the chart (`Chart.yaml`,
72    /// `templates/`); empty when Helm names none.
73    pub path: String,
74    /// What is wrong; may span several lines.
75    pub message: String,
76}
77
78/// The findings for one linted chart.
79#[derive(Debug, Clone, PartialEq, Eq)]
80pub struct LintedChart {
81    /// The chart path, as Helm printed it (`==> Linting <path>`).
82    pub path: String,
83    /// The findings, in the order Helm printed them.
84    pub messages: Vec<LintMessage>,
85    /// Failures that were not lint rules (`Error unable to check Chart.yaml ...`); Helm prints
86    /// them only when there are no findings.
87    pub errors: Vec<String>,
88    /// Whether Helm counts the chart as failed: an error finding or failure, or a warning with
89    /// `strict`.
90    pub failed: bool,
91}
92
93/// The totals Helm reports (`N chart(s) linted, M chart(s) failed`).
94#[derive(Debug, Clone, Copy, PartialEq, Eq)]
95pub struct LintSummary {
96    /// How many charts were linted, subcharts included.
97    pub linted: usize,
98    /// How many of them failed.
99    pub failed: usize,
100}
101
102/// What `helm lint` printed.
103#[derive(Debug, Clone, PartialEq, Eq)]
104pub struct LintReport {
105    /// The charts Helm printed a section for; with `quiet`, only those with warnings or errors.
106    pub charts: Vec<LintedChart>,
107    /// The totals, absent when Helm prints none (`quiet` and nothing to report).
108    pub summary: Option<LintSummary>,
109}
110
111impl LintReport {
112    /// True when no chart failed.
113    #[must_use]
114    pub fn passed(&self) -> bool {
115        self.charts.iter().all(|chart| !chart.failed)
116    }
117}
118
119impl Command for Lint {
120    type Output = LintReport;
121}