Patches can be defined globally
This commit is contained in:
		
							
								
								
									
										31
									
								
								helmule.yaml
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								helmule.yaml
									
									
									
									
									
								
							| @@ -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: | ||||
|   | ||||
| @@ -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>>, | ||||
|   | ||||
| @@ -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 { | ||||
| @@ -65,6 +72,44 @@ impl Patch { | ||||
|             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() | ||||
|                 ))) | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
|  | ||||
| trait PatchInterface { | ||||
| @@ -225,7 +270,7 @@ 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(), | ||||
|                 } | ||||
|   | ||||
| @@ -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); | ||||
|                             } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user