Skip to content

Commit 0350f3e

Browse files
committed
chore: Add basic script to upgrade most helm-charts
1 parent ce1cd56 commit 0350f3e

3 files changed

Lines changed: 110 additions & 1 deletion

File tree

.scripts/update_helm_charts.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import subprocess
5+
import yaml
6+
from typing import Tuple, Set
7+
8+
9+
def run_helm_repo_add(repo_name: str, repo_url: str) -> None:
10+
try:
11+
subprocess.run(["helm", "repo", "add", repo_name, repo_url], check=True)
12+
except subprocess.CalledProcessError as e:
13+
if "already exists" in e.stderr.decode() if e.stderr else str(e):
14+
pass # Ignore "already exists" error
15+
else:
16+
raise
17+
18+
19+
def run_helm_repo_update() -> None:
20+
subprocess.run(["helm", "repo", "update"], check=True)
21+
22+
23+
def get_chart_and_app_version(repo_name: str, chart_name: str) -> Tuple[str, str]:
24+
full_chart = f"{repo_name}/{chart_name}"
25+
result = subprocess.run(
26+
["helm", "show", "chart", full_chart],
27+
capture_output=True,
28+
text=True,
29+
check=True
30+
)
31+
chart_yaml = yaml.safe_load(result.stdout)
32+
return chart_yaml["version"], chart_yaml["appVersion"]
33+
34+
35+
def process_yaml_files(top_dir: str) -> None:
36+
seen_repos: Set[str] = set()
37+
38+
for root, _, files in os.walk(top_dir):
39+
for file in files:
40+
if not file.endswith(".yaml"):
41+
continue
42+
43+
filepath = os.path.join(root, file)
44+
45+
try:
46+
with open(filepath, "r") as f:
47+
raw_lines = f.readlines()
48+
49+
try:
50+
first_doc = yaml.safe_load("".join(raw_lines))
51+
except yaml.YAMLError as e:
52+
if "expected a single document" in str(e):
53+
continue # silently skip multi-doc YAMLs
54+
print(f"⚠️ Skipping invalid YAML: {filepath}")
55+
continue
56+
57+
if not isinstance(first_doc, dict):
58+
continue
59+
60+
if not (
61+
"releaseName" in first_doc
62+
and "version" in first_doc
63+
and "repo" in first_doc
64+
and "name" in first_doc
65+
):
66+
continue
67+
68+
repo_info = first_doc["repo"]
69+
chart_name = first_doc["name"]
70+
repo_name = repo_info.get("name")
71+
repo_url = repo_info.get("url")
72+
73+
if not repo_name or not repo_url:
74+
continue
75+
76+
if repo_name not in seen_repos:
77+
run_helm_repo_add(repo_name, repo_url)
78+
seen_repos.add(repo_name)
79+
80+
# print(f"📦 Updating {filepath} -> {repo_name}/{chart_name}")
81+
new_chart_version, new_app_version = get_chart_and_app_version(repo_name, chart_name)
82+
83+
updated_lines = []
84+
for line in raw_lines:
85+
if line.strip().startswith("version:"):
86+
new_line = f'version: {new_chart_version} # {new_app_version}\n'
87+
updated_lines.append(new_line)
88+
else:
89+
updated_lines.append(line)
90+
91+
with open(filepath, "w") as f:
92+
f.writelines(updated_lines)
93+
94+
print(f"✅ Updated {filepath}")
95+
96+
except Exception as e:
97+
print(f"⚠️ Error processing {filepath}: {e}")
98+
99+
if __name__ == "__main__":
100+
print('⚠️⚠️⚠️ This script is best-effort! Always check the result using "git diff"! ⚠️⚠️⚠️')
101+
print('Notably, it skips invalid YAMLs, which can be the case because we sometimes use templating syntax, even for helm-chart definitions')
102+
print('Please judge on the skipped files if they contain a helm-chart and should be manually bumped')
103+
print('The script can be improved to handle such files in the future')
104+
print()
105+
106+
process_yaml_files(".")

shell.nix

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
pkgs.mkShell {
33
packages = with pkgs; [
44
gettext # envsubst
5+
(pkgs.python3.withPackages (python-pkgs: [
6+
python-pkgs.pyyaml
7+
]))
58
];
69
}

stacks/observability/opentelemetry-operator.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ name: opentelemetry-operator
55
repo:
66
name: opentelemetry-operator
77
url: https://open-telemetry.github.io/opentelemetry-helm-charts
8-
version: 0.91.0 # 0.127.0
8+
version: 0.91.1 # 0.127.0
99
options:
1010
admissionWebhooks:
1111
certManager:

0 commit comments

Comments
 (0)