From e23c97430f0b529cd0ec73061fe439704a746fa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorsten=20Ro=C3=9Fner?= Date: Wed, 29 Jan 2025 12:08:41 +0100 Subject: [PATCH] chore(helmfile): Dev tooling: Improve `charts-local.py` script to allow referencing local copies of pulled Helm charts --- dev/README.md | 32 +++++++++++++++++++---------- dev/charts-local.py | 49 +++++++++++++++++++++++---------------------- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/dev/README.md b/dev/README.md index 91a4371e..2bc55f0d 100644 --- a/dev/README.md +++ b/dev/README.md @@ -7,30 +7,40 @@ SPDX-License-Identifier: Apache-2.0 * [charts-local.py](#charts-localpy) * [Commandline parameter](#commandline-parameter) - * [`--branch`](#--branch) + * [`--match `](#--match-your_string) * [`--revert`](#--revert) + * [`--branch` (deprecated)](#--branch-deprecated) # charts-local.py -This script helps you on cloning the platform development Helm charts and referencing them directly in the openDesk -Helmfile deployment for comfortable local test and development. The charts will be cloned into a directory -parallel created next to the `opendesk` repo containing this documentation and the `charts-local.py` script. -The name of the chart directory is derived from the branch name you are working with in this `opendesk` repo. +This script helps you with cloning/pulling Helm charts and referencing them directly in the openDesk +Helmfile deployment for comfortable local test and development. The charts will be cloned/pulled into a directory +created next to the `opendesk` repo containing this documentation and the `charts-local.py` script. -The script will create `.bak` copies of the helmfiles that have been touched. +The name of the directory containing the charts is based on the (currently) selected branch of the openDesk +repo prefixed with `charts-`. + +The script will create `.bak` copies of the helmfiles that have been touched that can easily be reverted to +using the `--revert` option. Run the script with `-h` to get information about the script's parameter on commandline. ## Commandline parameter -### `--branch` +### `--match ` + +Will only fetch repos or pull images for charts which name matches ``. + +### `--revert` + +Reverts the changes in the helmfiles pointing to the local Helm charts by copying the backup files created by the +scripts itself back to their original location. + +### `--branch` (deprecated) Optional parameter: Defines a branch for the `opendesk` repo to work with. The script will create the branch if it does not exist yet. Otherwise it will switch to defined branch. If parameter is omitted the current branch of the `opendesk` repo will be used. -### `--revert` - -Reverts the changes in the helmfiles pointing to the local Helm charts by copying the backup files created by the -scripts itself back to their original location. +As this parameter was used rarely, we might remove the support in a later version. diff --git a/dev/charts-local.py b/dev/charts-local.py index e0375429..947399f5 100755 --- a/dev/charts-local.py +++ b/dev/charts-local.py @@ -18,7 +18,6 @@ p.add('--branch', env_var='CHART_DEV_BRANCH', help='The branch you want to work p.add('--git_hostname', env_var='GIT_HOSTNAME', default='git@gitlab.opencode.de', help='Set the hostname for the chart git checkouts.') p.add('--revert', default=False, action='store_true', help='Set this parameter if you want to revert the referencing of the local helm chart checkout paths in the helmfiles.') p.add('--match', default='', help="Clone/pull only charts that contain the given string in their name.") -p.add('--pull', default=False, action='store_true', help='Will also pull and unpack Helm charts that are not developed by product development.') p.add('--loglevel', env_var='LOGLEVEL', default='DEBUG', help='Set the loglevel: DEBUG, INFO, WARNING, ERROR, CRITICAL-') options = p.parse_args() @@ -78,13 +77,10 @@ def create_path_if_not_exists(path): Path(path).mkdir(parents=True, exist_ok=True) def clone_charts_locally(branch, charts): - charts_clone_path = script_path+'/../../chart-repo/'+branch.replace('/', '_') - charts_pull_path = script_path+'/../../chart-pull/'+branch.replace('/', '_') + charts_path = script_path+'/../../charts-'+branch.replace('/', '_') charts_dict = {} doublette_dict = {} - create_path_if_not_exists(charts_clone_path) - if options.pull: - create_path_if_not_exists(charts_pull_path) + create_path_if_not_exists(charts_path) for chart in charts['charts']: tag = charts['charts'][chart]['version'] @@ -92,41 +88,41 @@ def clone_charts_locally(branch, charts): registry = charts['charts'][chart]['registry'] name = charts['charts'][chart]['name'] logging.debug(f"Working on {chart} / tag {tag} / repo {repository}") + chart_local_path = charts_path+'/'+name if not options.match in name: logging.info(f"Chart name {name} does not match {options.match} - skipping...") + continue elif registry == '': logging.info("Empty registry definition - skipping...") + continue + if os.path.isdir(chart_local_path): + logging.debug(f"Found pre-existing {chart_local_path} skipping clone/pull, but will still reference chart in Helmfile...") + charts_dict[chart] = chart_local_path + continue elif 'opendesk/components/platform-development/charts' in repository: logging.info("Cloning the charts repo") git_url = options.git_hostname+':'+repository - chart_repo_path = charts_clone_path+'/'+charts['charts'][chart]['name'] if git_url in doublette_dict: logging.debug(f"{chart} located at {git_url} is already checked out to {doublette_dict[git_url]}") charts_dict[chart] = doublette_dict[git_url] else: - if os.path.isdir(chart_repo_path): - logging.debug(f"Already exists {chart_repo_path} leaving it unmodified") - else: - logging.debug(f"Cloning into {chart_repo_path}") - Repo.clone_from(git_url, chart_repo_path) - chart_repo = Repo(path=chart_repo_path) - chart_repo.git.checkout('v'+charts['charts'][chart]['version']) - doublette_dict[git_url] = chart_repo_path - charts_dict[chart] = chart_repo_path - elif options.pull: + logging.debug(f"Cloning into {chart_local_path}") + Repo.clone_from(git_url, chart_local_path) + chart_repo = Repo(path=chart_local_path) + chart_repo.git.checkout('v'+charts['charts'][chart]['version']) + doublette_dict[git_url] = chart_local_path + charts_dict[chart] = chart_local_path + else: logging.info("Pulling the chart") - helm_command = f"helm pull oci://{registry}/{repository}/{name} --version {tag} --untar --destination {charts_pull_path}" + helm_command = f"helm pull oci://{registry}/{repository}/{name} --version {tag} --untar --destination {charts_path}" logging.debug(f"CLI command: {helm_command}") try: - output = subprocess.check_output(helm_command, shell = True) + subprocess.check_output(helm_command, shell = True) except subprocess.CalledProcessError: sys.exit(f"! CLI command '{helm_command}' failed") - else: - logging.debug("Not a product development chart and `--pull` option not enabled - skipping...") - + charts_dict[chart] = chart_local_path return charts_dict - def grep_yaml(file): with open(file, 'r') as file: content = '' @@ -156,7 +152,12 @@ def process_the_helmfiles(charts_dict, charts): for chart_ident in charts_dict: if '.Values.charts.'+chart_ident+'.name' in line: logging.debug(f"found match with {chart_ident} in {line.strip()}") - line = chart_def_prefix+charts_dict[chart_ident]+'/charts/'+charts['charts'][chart_ident]['name']+'" # replaced by local-dev script'+"\n" + line = charts_dict[chart_ident] + if os.path.isdir(line+'/charts/'+chart_ident): + line += '/charts/'+charts['charts'][chart_ident]['name'] + elif not os.path.isdir(line): + sys.exit(f"! Did not find directory to reference in Helmfile: '{line}'") + line = chart_def_prefix+line+'" # replaced by local-dev script'+"\n" child_helmfile_updated = True break output.append(line)