Patches can be defined globally
ci/woodpecker/push/build Pipeline was successful Details

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

View File

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

View File

@ -41,6 +41,7 @@ pub(crate) struct CustomCommandPatch {
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub(crate) struct Patch {
name: Option<String>,
regexp: Option<RegexpPatch>,
git: Option<GitPatch>,
custom_command: Option<CustomCommandPatch>,
@ -48,8 +49,14 @@ pub(crate) struct Patch {
}
impl Patch {
pub(crate) fn apply(&self, chart_local_path: String) -> Result<(), Box<dyn std::error::Error>> {
let patch_action = patch_action_from_definition(self.clone())?;
pub(crate) fn apply(&self, chart_local_path: String, global_patches: Option<Vec<Patch>>) -> Result<(), Box<dyn std::error::Error>> {
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)
}
pub(crate) fn get_path(&self) -> String {
@ -63,7 +70,45 @@ impl Patch {
git.path = path;
} else if let Some(ref mut yq) = self.yq {
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 {
path: {
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(),
None => git.path.clone(),
None => git.path.clone(),
}
},
}));

View File

@ -160,7 +160,7 @@ fn main() {
}
if let Some(patches) = chart.patches.clone() {
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);
exit(1);
}