Add pre-commit to update release manifest #211

Merged
nbelouin merged 1 commits from nbelouin/Factory:pre-commit-manifest into main 2025-07-29 11:27:57 +02:00
4 changed files with 148 additions and 99 deletions

View File

@@ -17,7 +17,7 @@ jobs:
object-format: 'sha256' object-format: 'sha256'
- name: Setup dependencies - name: Setup dependencies
run: | run: |
zypper in -y python3-PyYAML zypper in -y python3-ruamel.yaml
- name: Check release manifest - name: Check release manifest
run: | run: |
python3 .obs/manifest-check.py python3 .obs/manifest-check.py --check

51
.obs/manifest-check.py Normal file → Executable file
View File

@@ -1,11 +1,15 @@
#!/usr/bin/python3 #!/usr/bin/python3
import yaml import ruamel.yaml
import pathlib
import argparse
import sys import sys
yaml = ruamel.yaml.YAML()
def get_chart_version(chart_name: str) -> str: def get_chart_version(chart_name: str) -> str:
with open(f"./{chart_name}-chart/Chart.yaml") as f: with open(f"./{chart_name}-chart/Chart.yaml") as f:
chart = yaml.safe_load(f) chart = yaml.load(f)
steven.hardy marked this conversation as resolved Outdated
In future I wonder if we could also add validation for k3s/RKE2, e.g parsing https://github.com/k3s-io/k3s/tree/v1.32.4%2Bk3s1/manifests and https://github.com/rancher/rke2/blob/v1.32.4%2Brke2r1/charts/chart_versions.yaml ?

I'd rather see that as a part of the renovate-like process we want, but can be envisioned. I just would not do this as a pre-commit hook as we don't want "random" PRs to contain upgrades to non-related components.
Here we just seek to keep the charts version in sync between the charts definition and release manifest.

I'd rather see that as a part of the renovate-like process we want, but can be envisioned. I just would not do this as a pre-commit hook as we don't want "random" PRs to contain upgrades to non-related components. Here we just seek to keep the charts version in sync between the charts definition and release manifest.

Sounds good, we can consider other approaches in future, just wanted to mention it :)

Sounds good, we can consider other approaches in future, just wanted to mention it :)
return chart["version"] return chart["version"]
def get_charts(chart): def get_charts(chart):
@@ -21,22 +25,57 @@ def get_charts(chart):
def get_charts_list(): def get_charts_list():
with open("./release-manifest-image/release_manifest.yaml") as f: with open("./release-manifest-image/release_manifest.yaml") as f:
manifest = yaml.safe_load(f) manifest = yaml.load(f)
charts = {} charts = {}
for chart in manifest["spec"]["components"]["workloads"]["helm"]: for chart in manifest["spec"]["components"]["workloads"]["helm"]:
charts.update(get_charts(chart)) charts.update(get_charts(chart))
return charts return charts
def main(): def check_charts(fix: bool) -> bool:
print("Checking charts versions in release manifest")
success = True success = True
charts = get_charts_list() charts = get_charts_list()
to_fix = {}
for chart in charts: for chart in charts:
expected_version = get_chart_version(chart) expected_version = get_chart_version(chart)
if expected_version != charts[chart]: if expected_version != charts[chart]:
success = False success = False
to_fix[f'%%CHART_REPO%%/%%CHART_PREFIX%%{chart}'] = expected_version
print(f"{chart}: Expected: {expected_version}, Got: {charts[chart]}") print(f"{chart}: Expected: {expected_version}, Got: {charts[chart]}")
if not success: if fix and not success:
fix_charts(to_fix)
return True
return success
def fix_charts(to_fix):
manifest_path = pathlib.Path("./release-manifest-image/release_manifest.yaml")
manifest = yaml.load(manifest_path)
yaml.indent(mapping=2, sequence=4, offset=2)
yaml.width = 4096
for chart_index, chart in enumerate(manifest["spec"]["components"]["workloads"]["helm"]):
changed = False
if chart["chart"] in to_fix.keys():
changed = True
chart["version"] = to_fix[chart["chart"]]
for subchart_index, subchart in enumerate(chart.get("addonCharts", [])):
if subchart["chart"] in to_fix.keys():
changed = True
subchart["version"] = to_fix[subchart["chart"]]
chart["addonCharts"][subchart_index] = subchart
for subchart_index, subchart in enumerate(chart.get("dependencyCharts", [])):
if subchart["chart"] in to_fix.keys():
changed = True
subchart["version"] = to_fix[subchart["chart"]]
chart["dependencyCharts"][subchart_index] = subchart
if changed:
manifest["spec"]["components"]["workloads"]["helm"][chart_index] = chart
yaml.dump(manifest, manifest_path)
def main():
print("Checking charts versions in release manifest")
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--check', action='store_true')
args = parser.parse_args()
if not check_charts(not args.check):
sys.exit(1) sys.exit(1)
else: else:
print("All local charts in release manifest are using the right version") print("All local charts in release manifest are using the right version")

10
.pre-commit-config.yaml Normal file
View File

@@ -0,0 +1,10 @@
repos:
- repo: local
hooks:
- id: check-manifest
name: "Check release-manifest"
entry: .obs/manifest-check.py
language: python
additional_dependencies: ['ruamel.yaml']
pass_filenames: false
always_run: true

View File

@@ -77,13 +77,13 @@ spec:
version: 0.0.0 version: 0.0.0
type: HelmChart type: HelmChart
operatingSystem: operatingSystem:
version: "6.1" version: '6.1'
zypperID: "SL-Micro" zypperID: SL-Micro
cpeScheme: "cpe:/o:suse:sl-micro:6.1" cpeScheme: cpe:/o:suse:sl-micro:6.1
prettyName: "SUSE Linux Micro 6.1" prettyName: SUSE Linux Micro 6.1
supportedArchs: supportedArchs:
- "x86_64" - x86_64
- "aarch64" - aarch64
workloads: workloads:
helm: helm:
- prettyName: Rancher - prettyName: Rancher
@@ -106,20 +106,20 @@ spec:
repository: https://charts.rancher.io repository: https://charts.rancher.io
- prettyName: MetalLB - prettyName: MetalLB
releaseName: metallb releaseName: metallb
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%metallb" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%metallb'
version: "%%CHART_MAJOR%%.0.0+up0.14.9" version: '%%CHART_MAJOR%%.0.0+up0.14.9'
- prettyName: CDI - prettyName: CDI
releaseName: cdi releaseName: cdi
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%cdi" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%cdi'
version: "%%CHART_MAJOR%%.0.0+up0.5.0" version: '%%CHART_MAJOR%%.0.0+up0.5.0'
- prettyName: KubeVirt - prettyName: KubeVirt
releaseName: kubevirt releaseName: kubevirt
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%kubevirt" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%kubevirt'
version: "%%CHART_MAJOR%%.0.0+up0.5.0" version: '%%CHART_MAJOR%%.0.0+up0.5.0'
addonCharts: addonCharts:
- releaseName: kubevirt-dashboard-extension - releaseName: kubevirt-dashboard-extension
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%kubevirt-dashboard-extension" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%kubevirt-dashboard-extension'
version: "%%CHART_MAJOR%%.0.2+up1.3.2" version: '%%CHART_MAJOR%%.0.2+up1.3.2'
- prettyName: NeuVector - prettyName: NeuVector
releaseName: neuvector releaseName: neuvector
chart: neuvector chart: neuvector
@@ -137,8 +137,8 @@ spec:
version: 2.1.3 version: 2.1.3
- prettyName: EndpointCopierOperator - prettyName: EndpointCopierOperator
releaseName: endpoint-copier-operator releaseName: endpoint-copier-operator
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%endpoint-copier-operator" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%endpoint-copier-operator'
version: "%%CHART_MAJOR%%.0.1+up0.3.0" version: '%%CHART_MAJOR%%.0.1+up0.3.0'
- prettyName: Elemental - prettyName: Elemental
releaseName: elemental-operator releaseName: elemental-operator
chart: oci://registry.suse.com/rancher/elemental-operator-chart chart: oci://registry.suse.com/rancher/elemental-operator-chart
@@ -154,29 +154,29 @@ spec:
version: 3.0.0 version: 3.0.0
- prettyName: SRIOV - prettyName: SRIOV
releaseName: sriov-network-operator releaseName: sriov-network-operator
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%sriov-network-operator" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%sriov-network-operator'
version: "%%CHART_MAJOR%%.0.2+up1.5.0" version: '%%CHART_MAJOR%%.0.2+up1.5.0'
dependencyCharts: dependencyCharts:
- releaseName: sriov-crd - releaseName: sriov-crd
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%sriov-crd" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%sriov-crd'
version: "%%CHART_MAJOR%%.0.2+up1.5.0" version: '%%CHART_MAJOR%%.0.2+up1.5.0'
- prettyName: Akri - prettyName: Akri
releaseName: akri releaseName: akri
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%akri" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%akri'
version: "%%CHART_MAJOR%%.0.0+up0.12.20" version: '%%CHART_MAJOR%%.0.0+up0.12.20'
addonCharts: addonCharts:
- releaseName: akri-dashboard-extension - releaseName: akri-dashboard-extension
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%akri-dashboard-extension" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%akri-dashboard-extension'
version: "%%CHART_MAJOR%%.0.2+up1.3.1" version: '%%CHART_MAJOR%%.0.2+up1.3.1'
- prettyName: Metal3 - prettyName: Metal3
releaseName: metal3 releaseName: metal3
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%metal3" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%metal3'
version: "%%CHART_MAJOR%%.0.10+up0.12.0" version: '%%CHART_MAJOR%%.0.10+up0.12.0'
- prettyName: RancherTurtles - prettyName: RancherTurtles
releaseName: rancher-turtles releaseName: rancher-turtles
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%rancher-turtles" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%rancher-turtles'
version: "%%CHART_MAJOR%%.0.4+up0.20.0" version: '%%CHART_MAJOR%%.0.4+up0.20.0'
- prettyName: RancherTurtlesAirgapResources - prettyName: RancherTurtlesAirgapResources
releaseName: rancher-turtles-airgap-resources releaseName: rancher-turtles-airgap-resources
chart: "%%CHART_REPO%%/%%CHART_PREFIX%%rancher-turtles-airgap-resources" chart: '%%CHART_REPO%%/%%CHART_PREFIX%%rancher-turtles-airgap-resources'
version: "%%CHART_MAJOR%%.0.4+up0.20.0" version: '%%CHART_MAJOR%%.0.4+up0.20.0'