Patches can be defined globally

This commit is contained in:
Nikolai Rodionov 2024-01-14 18:38:12 +01:00
parent 492e15a0af
commit c1bc743717
Signed by: allanger
GPG Key ID: 0AA46A90E25592AD
4 changed files with 65 additions and 24 deletions

View File

@ -1,7 +1,16 @@
patches:
- name: yamlfmt
custom_command:
commands:
- |-
cat <<EOT >> .yamlfmt
formatter:
pad_line_comments: 2
EOT
- yamlfmt values.yaml --conf ./yamlfmt.yaml
- rm -f yamlfmt.yaml
# mirror charts # mirror charts
include:
- kind: Charts
path: ./examples/use/charts/vaultwardens.yaml
repositories: repositories:
- name: bitnami-oci - name: bitnami-oci
helm: helm:
@ -21,11 +30,6 @@ repositories:
helm: helm:
url: https://fluxcd-community.github.io/helm-charts url: https://fluxcd-community.github.io/helm-charts
charts: charts:
- name: postgresql
repository: bitnami-oci
version: 13.2.29
mirrors:
- badhouseplants-git
- name: flux2 - name: flux2
repository: flux-community repository: flux-community
extensions: extensions:
@ -48,16 +52,7 @@ charts:
- name: Remove installCRDs value from the default values - name: Remove installCRDs value from the default values
regexp: regexp:
path: ./examples/patches/flux-regexp path: ./examples/patches/flux-regexp
- name: yaml-fmt - name: yamlfmt
custom_command:
commands:
- |-
cat <<EOT >> .yamlfmt
formatter:
pad_line_comments: 2
EOT
- yamlfmt values.yaml --conf ./yamlfmt.yaml
- rm -f yamlfmt.yaml
mirrors: mirrors:
- custom-command - custom-command
mirrors: mirrors:

View File

@ -8,6 +8,7 @@ pub(crate) mod include;
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub(crate) struct Config { pub(crate) struct Config {
pub(crate) patches: Option<Vec<patch::Patch>>,
pub(crate) include: Option<Vec<include::Include>>, pub(crate) include: Option<Vec<include::Include>>,
pub(crate) variables: Option<HashMap<String, String>>, pub(crate) variables: Option<HashMap<String, String>>,
pub(crate) repositories: Option<Vec<Repository>>, pub(crate) repositories: Option<Vec<Repository>>,

View File

@ -41,6 +41,7 @@ pub(crate) struct CustomCommandPatch {
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub(crate) struct Patch { pub(crate) struct Patch {
name: Option<String>,
regexp: Option<RegexpPatch>, regexp: Option<RegexpPatch>,
git: Option<GitPatch>, git: Option<GitPatch>,
custom_command: Option<CustomCommandPatch>, custom_command: Option<CustomCommandPatch>,
@ -48,8 +49,14 @@ pub(crate) struct Patch {
} }
impl Patch { impl Patch {
pub(crate) fn apply(&self, chart_local_path: String) -> Result<(), Box<dyn std::error::Error>> { pub(crate) fn apply(&self, chart_local_path: String, global_patches: Option<Vec<Patch>>) -> Result<(), Box<dyn std::error::Error>> {
let patch_action = patch_action_from_definition(self.clone())?; let patch_action: Box<dyn PatchInterface>;
if self.is_ref(){
let patch_ref = self.get_ref(global_patches)?;
patch_action = Box::from(patch_action_from_definition(patch_ref)?);
} else {
patch_action = Box::from(patch_action_from_definition(self.clone())?);
}
patch_action.apply(chart_local_path) patch_action.apply(chart_local_path)
} }
pub(crate) fn get_path(&self) -> String { pub(crate) fn get_path(&self) -> String {
@ -63,7 +70,45 @@ impl Patch {
git.path = path; git.path = path;
} else if let Some(ref mut yq) = self.yq { } else if let Some(ref mut yq) = self.yq {
yq.file = path yq.file = path
} }
}
fn is_ref(&self) -> bool {
self.regexp.is_none()
&& self.git.is_none()
&& self.custom_command.is_none()
&& self.yq.is_none()
&& self.name.is_some()
}
pub(crate) fn get_ref(
&self,
global_patches: Option<Vec<Patch>>,
) -> Result<Patch, Box<dyn std::error::Error>> {
match global_patches {
Some(patches) => {
let patch = patches
.iter()
.find(|&patch| patch.clone().name.unwrap() == self.clone().name.unwrap());
match patch {
Some(patch) => {
return Ok(patch.clone());
}
None => {
return Err(Box::from(format!(
"global patch is not found: {}",
self.clone().name.unwrap()
)))
}
}
}
None => {
return Err(Box::from(format!(
"patch {} is recognized as a reference, but global patches are not defined",
self.clone().name.unwrap()
)))
}
}
} }
} }
@ -225,9 +270,9 @@ fn patch_action_from_definition(
return Ok(Box::new(GitPatch { return Ok(Box::new(GitPatch {
path: { path: {
let path = PathBuf::from(git.path.clone()); let path = PathBuf::from(git.path.clone());
match fs::canonicalize(path).ok(){ match fs::canonicalize(path).ok() {
Some(can_path) => can_path.into_os_string().into_string().ok().unwrap(), Some(can_path) => can_path.into_os_string().into_string().ok().unwrap(),
None => git.path.clone(), None => git.path.clone(),
} }
}, },
})); }));

View File

@ -160,7 +160,7 @@ fn main() {
} }
if let Some(patches) = chart.patches.clone() { if let Some(patches) = chart.patches.clone() {
for patch in patches { for patch in patches {
if let Err(err) = patch.apply(res.clone().path) { if let Err(err) = patch.apply(res.clone().path, config.patches.clone()) {
error!("{}", err); error!("{}", err);
exit(1); exit(1);
} }