Skip to content

Instantly share code, notes, and snippets.

@cosimo
Created September 14, 2021 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosimo/fd3ae5fd9326d6c8b4902cd953b2b0a0 to your computer and use it in GitHub Desktop.
Save cosimo/fd3ae5fd9326d6c8b4902cd953b2b0a0 to your computer and use it in GitHub Desktop.
Clean up older versions of charts from chartmuseum
#!/usr/bin/env python3
import os
import yaml
import requests
CHARTMUSEUM_URL = 'https://your.chartmuseum.url'
def get_all_chart_versions():
"""
Downloads the chartmuseum index and parses it into a list of (chart, version) tuples
"""
response = requests.get(f'{CHARTMUSEUM_URL}/index.yaml')
index_yaml = yaml.safe_load(response.content)
entries = index_yaml.get('entries', {}).values()
chart_versions = [
(chart.get('name'), chart.get('version'),)
for chart_versions in entries
for chart in chart_versions]
return chart_versions
def delete_chart_version(name, version):
"""
Deletes a version of a chart from chartmuseum
"""
# FIXME add http basic auth credentials to properly delete
response = requests.delete(f'{CHARTMUSEUM_URL}/api/charts/{name}/{version}')
return response.ok and b'{"deleted":true}' == response.content
def get_referenced_chart_versions():
"""
Obtain a list of all chart versions that are referred to by any environment
We don't want to delete from chartmuseum the versions of charts that are
active and referenced or we'd risk breaking something.
We do this by collecting all requirements.yaml files we want to check
into a local `./requirements` folder.
"""
yaml_req_files = []
with os.scandir('./requirements') as filenames:
for file in filenames:
if file.is_file():
yaml_req_files.append(file)
used_chart_versions = []
for req in yaml_req_files:
with open(req, 'r') as req_file:
req_file_yaml = yaml.safe_load(req_file)
deps = req_file_yaml.get('dependencies', [])
for dep in deps:
used_chart_versions.append((dep.get('name'), dep.get('version'),))
return used_chart_versions
if __name__ == "__main__":
all_published_charts = get_all_chart_versions()
referenced_charts = get_referenced_chart_versions()
candidates_for_deletion = list(set(all_published_charts) - set(referenced_charts))
for name, version in candidates_for_deletion:
print(name, "\t", version)
# delete_chart_version(name, version)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment