From 1710279e299294d340fdaeee6d92489eee5ab9c4 Mon Sep 17 00:00:00 2001 From: Ankit R Gadiya Date: Fri, 7 Feb 2025 12:02:18 +0530 Subject: [PATCH] feat(apply): add support for `--delete-existing` flag --- riocli/apply/__init__.py | 25 +++++++++++++++++++++++++ riocli/apply/parse.py | 7 ++++--- riocli/chart/apply.py | 30 +++++++++++++++++++++++------- riocli/chart/chart.py | 10 ++++++---- 4 files changed, 58 insertions(+), 14 deletions(-) diff --git a/riocli/apply/__init__.py b/riocli/apply/__init__.py index c8af371b..7eced09c 100644 --- a/riocli/apply/__init__.py +++ b/riocli/apply/__init__.py @@ -67,6 +67,14 @@ help="Number of parallel workers while running apply " "command. defaults to 6.", type=int, ) +@click.option( + "--recreate", + "--delete-existing", + "delete_existing", + is_flag=True, + default=False, + help="Overwrite existing resources", +) @click.option( "-f", "--force", @@ -98,6 +106,7 @@ def apply( files: Iterable[str], retry_count: int = 50, retry_interval: int = 6, + delete_existing: bool = False, dryrun: bool = False, workers: int = 6, silent: bool = False, @@ -150,6 +159,10 @@ def apply( $ rio apply -v values1.yaml -v values2.yaml templates/** + Re-create existing resources from the manifests. + + $ rio apply -v values.yaml --delete-existing templates/ + """ glob_files, abs_values, abs_secrets = process_files_values_secrets( files, values, secrets @@ -177,6 +190,18 @@ def apply( if not silent and not dryrun: click.confirm("\nDo you want to proceed?", default=True, abort=True) + if delete_existing: + deleter = Applier(glob_files, abs_values, abs_secrets) + deleter.parse_dependencies(print_resources=False) + + print_centered_text("Deleting Resources") + deleter.delete( + dryrun=dryrun, + workers=workers, + retry_count=retry_count, + retry_interval=retry_interval, + ) + print_centered_text("Applying Manifests") applier.apply( dryrun=dryrun, diff --git a/riocli/apply/parse.py b/riocli/apply/parse.py index 787f7719..9ab98792 100644 --- a/riocli/apply/parse.py +++ b/riocli/apply/parse.py @@ -224,15 +224,16 @@ def _get_async_delete_order(self): while stack: yield stack.pop() - def parse_dependencies(self): + def parse_dependencies(self, print_resources: bool = True): for _, data in self.files.items(): for model in data: key = self._get_object_key(model) self._parse_dependency(key, model) self._add_graph_node(key) - print_centered_text("Parsed Resources") - print_resolved_objects(self.resolved_objects) + if print_resources: + print_centered_text("Parsed Resources") + print_resolved_objects(self.resolved_objects) def show_dependency_graph(self): """Lauches mermaid.live dependency graph""" diff --git a/riocli/chart/apply.py b/riocli/chart/apply.py index d570cf0b..ed66ab2d 100644 --- a/riocli/chart/apply.py +++ b/riocli/chart/apply.py @@ -12,6 +12,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +from typing import Iterable + import click from click_help_colors import HelpColorsCommand @@ -61,6 +63,14 @@ "expects sops to be authorized for decoding files on " "this computer", ) +@click.option( + "--recreate", + "--delete-existing", + "delete_existing", + is_flag=True, + default=False, + help="Overwrite existing resources", +) @click.option( "--workers", "-w", @@ -83,12 +93,13 @@ @click.argument("chart", type=str) def apply_chart( chart: str, - values: str, - secrets: str, - dryrun: bool, - workers: int = 6, + values: Iterable[str], + secrets: Iterable[str], retry_count: int = 50, retry_interval: int = 6, + delete_existing: bool = False, + dryrun: bool = False, + workers: int = 6, silent: bool = False, ) -> None: """Install a chart from the rapyuta-charts repository. @@ -118,6 +129,10 @@ def apply_chart( Apply a chart with values and secrets files without confirmation $ rio chart apply ioconfig-syncer -v values.yaml -s secrets.yaml -f + + Re-create existing chart resources. + + $ rio chart apply -v values.yaml --delete-existing """ versions = find_chart(chart) if len(versions) > 1: @@ -126,14 +141,15 @@ def apply_chart( fg=Colors.RED, ) - chart = Chart(**versions[0]) - chart.apply_chart( + c = Chart(**versions[0]) + c.apply_chart( values, secrets, dryrun=dryrun, + delete_existing=delete_existing, workers=workers, silent=silent, retry_count=retry_count, retry_interval=retry_interval, ) - chart.cleanup() + c.cleanup() diff --git a/riocli/chart/chart.py b/riocli/chart/chart.py index 3874c16a..f45ef3cb 100644 --- a/riocli/chart/chart.py +++ b/riocli/chart/chart.py @@ -33,7 +33,8 @@ def apply_chart( self, values: str = None, secrets: str = None, - dryrun: bool = None, + delete_existing: bool = False, + dryrun: bool = False, workers: int = 6, retry_count: int = 50, retry_interval: int = 6, @@ -47,13 +48,14 @@ def apply_chart( apply.callback( values=values, - files=[templates_dir], secrets=secrets, + files=[templates_dir], + retry_count=retry_count, + retry_interval=retry_interval, + delete_existing=delete_existing, dryrun=dryrun, workers=workers, silent=silent, - retry_count=retry_count, - retry_interval=retry_interval, ) def delete_chart(