chore(helmfile): Dev tooling: Improve charts-local.py script to allow referencing local copies of pulled Helm charts

This commit is contained in:
Thorsten Roßner
2025-01-29 12:08:41 +01:00
parent 61d94a8de6
commit e23c97430f
2 changed files with 46 additions and 35 deletions

View File

@@ -7,30 +7,40 @@ SPDX-License-Identifier: Apache-2.0
* [charts-local.py](#charts-localpy) * [charts-local.py](#charts-localpy)
* [Commandline parameter](#commandline-parameter) * [Commandline parameter](#commandline-parameter)
* [`--branch`](#--branch) * [`--match <your_string>`](#--match-your_string)
* [`--revert`](#--revert) * [`--revert`](#--revert)
* [`--branch` (deprecated)](#--branch-deprecated)
# charts-local.py # charts-local.py
This script helps you on cloning the platform development Helm charts and referencing them directly in the openDesk 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 into a directory Helmfile deployment for comfortable local test and development. The charts will be cloned/pulled into a directory
parallel created next to the `opendesk` repo containing this documentation and the `charts-local.py` script. 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.
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. Run the script with `-h` to get information about the script's parameter on commandline.
## Commandline parameter ## Commandline parameter
### `--branch` ### `--match <your_string>`
Will only fetch repos or pull images for charts which name matches `<your_string>`.
### `--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 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. 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. If parameter is omitted the current branch of the `opendesk` repo will be used.
### `--revert` As this parameter was used rarely, we might remove the support in a later version.
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.

View File

@@ -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('--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('--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('--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-') p.add('--loglevel', env_var='LOGLEVEL', default='DEBUG', help='Set the loglevel: DEBUG, INFO, WARNING, ERROR, CRITICAL-')
options = p.parse_args() options = p.parse_args()
@@ -78,13 +77,10 @@ def create_path_if_not_exists(path):
Path(path).mkdir(parents=True, exist_ok=True) Path(path).mkdir(parents=True, exist_ok=True)
def clone_charts_locally(branch, charts): def clone_charts_locally(branch, charts):
charts_clone_path = script_path+'/../../chart-repo/'+branch.replace('/', '_') charts_path = script_path+'/../../charts-'+branch.replace('/', '_')
charts_pull_path = script_path+'/../../chart-pull/'+branch.replace('/', '_')
charts_dict = {} charts_dict = {}
doublette_dict = {} doublette_dict = {}
create_path_if_not_exists(charts_clone_path) create_path_if_not_exists(charts_path)
if options.pull:
create_path_if_not_exists(charts_pull_path)
for chart in charts['charts']: for chart in charts['charts']:
tag = charts['charts'][chart]['version'] tag = charts['charts'][chart]['version']
@@ -92,41 +88,41 @@ def clone_charts_locally(branch, charts):
registry = charts['charts'][chart]['registry'] registry = charts['charts'][chart]['registry']
name = charts['charts'][chart]['name'] name = charts['charts'][chart]['name']
logging.debug(f"Working on {chart} / tag {tag} / repo {repository}") logging.debug(f"Working on {chart} / tag {tag} / repo {repository}")
chart_local_path = charts_path+'/'+name
if not options.match in name: if not options.match in name:
logging.info(f"Chart name {name} does not match {options.match} - skipping...") logging.info(f"Chart name {name} does not match {options.match} - skipping...")
continue
elif registry == '': elif registry == '':
logging.info("Empty registry definition - skipping...") 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: elif 'opendesk/components/platform-development/charts' in repository:
logging.info("Cloning the charts repo") logging.info("Cloning the charts repo")
git_url = options.git_hostname+':'+repository git_url = options.git_hostname+':'+repository
chart_repo_path = charts_clone_path+'/'+charts['charts'][chart]['name']
if git_url in doublette_dict: if git_url in doublette_dict:
logging.debug(f"{chart} located at {git_url} is already checked out to {doublette_dict[git_url]}") logging.debug(f"{chart} located at {git_url} is already checked out to {doublette_dict[git_url]}")
charts_dict[chart] = doublette_dict[git_url] charts_dict[chart] = doublette_dict[git_url]
else: else:
if os.path.isdir(chart_repo_path): logging.debug(f"Cloning into {chart_local_path}")
logging.debug(f"Already exists {chart_repo_path} leaving it unmodified") Repo.clone_from(git_url, chart_local_path)
else: chart_repo = Repo(path=chart_local_path)
logging.debug(f"Cloning into {chart_repo_path}") chart_repo.git.checkout('v'+charts['charts'][chart]['version'])
Repo.clone_from(git_url, chart_repo_path) doublette_dict[git_url] = chart_local_path
chart_repo = Repo(path=chart_repo_path) charts_dict[chart] = chart_local_path
chart_repo.git.checkout('v'+charts['charts'][chart]['version']) else:
doublette_dict[git_url] = chart_repo_path
charts_dict[chart] = chart_repo_path
elif options.pull:
logging.info("Pulling the chart") 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}") logging.debug(f"CLI command: {helm_command}")
try: try:
output = subprocess.check_output(helm_command, shell = True) subprocess.check_output(helm_command, shell = True)
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
sys.exit(f"! CLI command '{helm_command}' failed") sys.exit(f"! CLI command '{helm_command}' failed")
else: charts_dict[chart] = chart_local_path
logging.debug("Not a product development chart and `--pull` option not enabled - skipping...")
return charts_dict return charts_dict
def grep_yaml(file): def grep_yaml(file):
with open(file, 'r') as file: with open(file, 'r') as file:
content = '' content = ''
@@ -156,7 +152,12 @@ def process_the_helmfiles(charts_dict, charts):
for chart_ident in charts_dict: for chart_ident in charts_dict:
if '.Values.charts.'+chart_ident+'.name' in line: if '.Values.charts.'+chart_ident+'.name' in line:
logging.debug(f"found match with {chart_ident} in {line.strip()}") 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 child_helmfile_updated = True
break break
output.append(line) output.append(line)