mod digest;
mod fetch;
mod ignore;
mod loader;
mod metadata;
mod yaml;
use std::path::{Path, PathBuf};
use jiff::Timestamp;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use crate::go_json;
pub use digest::{archive_digest, hash_dependencies, hash_dependencies_v2};
pub use ignore::{HELM_IGNORE, IgnoreError, Rules};
pub use loader::MAX_DECOMPRESSED_SIZE;
pub use metadata::{Dependency, Lock, Maintainer, Metadata, ValidationError, is_valid_semver};
pub use yaml::YamlError;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("{path}: {source}")]
Io {
path: PathBuf,
source: std::io::Error,
},
#[error("unable to detect chart at {path}: {source}")]
NoChart {
path: PathBuf,
source: std::io::Error,
},
#[error("cannot load {file}: {detail}")]
Load {
file: String,
detail: String,
},
#[error(transparent)]
Validation(#[from] ValidationError),
#[error("unsupported chart version")]
UnsupportedApiVersion,
#[error("Chart.yaml file is missing")]
MissingChartYaml,
#[error("{0}")]
NotAnArchive(String),
#[error("no files in chart archive")]
EmptyArchive,
#[error("{0}")]
Archive(String),
#[error("decompressed chart is larger than the maximum size {limit}")]
TooLarge {
limit: i64,
},
#[error("symlink cycle in chart directory at {}", .path.display())]
SymlinkCycle {
path: PathBuf,
},
#[error("invalid .helmignore: {0}")]
Ignore(#[from] IgnoreError),
#[error("error unpacking subchart {name} in {parent}: {source}")]
Subchart {
name: String,
parent: String,
source: Box<Error>,
},
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct File {
#[serde(default)]
pub name: String,
#[serde(default, with = "go_json::bytes")]
pub data: Vec<u8>,
#[serde(
default,
rename = "modtime",
with = "go_json::omit_zero",
skip_serializing_if = "Option::is_none"
)]
pub mod_time: Option<Timestamp>,
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct Chart {
#[serde(default, deserialize_with = "go_json::null_default")]
pub metadata: Metadata,
#[serde(default)]
pub lock: Option<Lock>,
#[serde(default, deserialize_with = "go_json::null_default")]
pub templates: Vec<File>,
#[serde(default)]
pub values: Value,
#[serde(default, rename = "schema", with = "go_json::option_bytes")]
pub schema_raw: Option<Vec<u8>>,
#[serde(default, rename = "schemamodtime", with = "go_json::always")]
pub schema_mod_time: Option<Timestamp>,
#[serde(default, deserialize_with = "go_json::null_default")]
pub files: Vec<File>,
#[serde(
default,
rename = "modtime",
with = "go_json::omit_zero",
skip_serializing_if = "Option::is_none"
)]
pub mod_time: Option<Timestamp>,
#[serde(skip)]
pub values_raw: Option<Vec<u8>>,
#[serde(skip)]
pub dependencies: Vec<Chart>,
}
impl Chart {
pub fn load(path: impl AsRef<Path>) -> Result<Self, Error> {
loader::load(path.as_ref(), MAX_DECOMPRESSED_SIZE)
}
pub fn load_archive(bytes: &[u8]) -> Result<Self, Error> {
loader::load_archive_bytes(bytes, MAX_DECOMPRESSED_SIZE)
}
#[must_use]
pub fn name(&self) -> &str {
&self.metadata.name
}
#[must_use]
pub fn default_values(&self) -> &Value {
&self.values
}
pub fn schema(&self) -> Result<Option<Value>, serde_json::Error> {
self.schema_raw
.as_deref()
.map(serde_json::from_slice)
.transpose()
}
pub fn crds(&self) -> impl Iterator<Item = &File> {
self.files.iter().filter(|f| {
let lower = f.name.to_ascii_lowercase();
f.name.starts_with("crds/")
&& [".yaml", ".yml", ".json"]
.iter()
.any(|e| lower.ends_with(e))
})
}
#[must_use]
pub fn lock_is_stale(&self) -> Option<bool> {
let lock = self.lock.as_ref()?;
let current = hash_dependencies(&self.metadata.dependencies, &lock.dependencies);
let v1 = self.metadata.api_version == "v1"
&& hash_dependencies_v2(&self.metadata.dependencies) == lock.digest;
Some(current != lock.digest && !v1)
}
}
pub fn read_values_file(path: impl AsRef<Path>) -> Result<Value, Error> {
let path = path.as_ref();
let data = std::fs::read(path).map_err(|source| Error::Io {
path: path.to_owned(),
source,
})?;
read_values(&data)
}
pub fn read_values(data: &[u8]) -> Result<Value, Error> {
parse_values(data).map_err(|e| Error::Load {
file: "values".into(),
detail: e.to_string(),
})
}
fn parse_values(data: &[u8]) -> Result<Value, YamlError> {
let mut values = Map::new();
for document in yaml::parse_documents(data)? {
merge_maps(&mut values, yaml::into_map(document)?);
}
Ok(Value::Object(values))
}
fn merge_maps(a: &mut Map<String, Value>, b: Map<String, Value>) {
for (key, value) in b {
match (a.get_mut(&key), value) {
(Some(Value::Object(existing)), Value::Object(incoming)) => {
merge_maps(existing, incoming);
}
(_, value) => {
a.insert(key, value);
}
}
}
}
#[cfg(test)]
mod tests;