Skip to content

Commit 38ddf5c

Browse files
committed
fix(script): Combine helm repo add with update
This had a side-effect: When the `repo.url` is updated (eg: https://grafana.github.io/helm-charts -> https://grafana-community.github.io/helm-charts), but the user has a named repo with the old url (because the `repo.name` wasn't updated), this caused chart downgrades. Now we do an add and force update (once), along with a repo URL check and bail out with this message: ``` ⚠️ Error processing ./stacks/observability/grafana.yaml: You have a local repo named grafana pointing to https://grafana.github.io/helm-charts, but the stack/demo wants https://grafana-community.github.io/helm-charts Either delete or update your local repo URL, or update the stack/demo repo name so it doesn't conflict ```
1 parent 5d4c984 commit 38ddf5c

1 file changed

Lines changed: 31 additions & 11 deletions

File tree

.scripts/update_helm_charts.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
import subprocess
55
import yaml
66
from typing import Tuple, Set
7+
from textwrap import dedent
78

89

910
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
11+
subprocess.run(
12+
["helm", "repo", "add", repo_name, repo_url, "--force-update"], check=True
13+
)
1714

1815

19-
def run_helm_repo_update(repo_name: str) -> None:
20-
subprocess.run(["helm", "repo", "update", repo_name], check=True)
16+
def local_repo_list():
17+
result = subprocess.run(
18+
["helm", "repo", "list", "-o", "yaml"],
19+
capture_output=True,
20+
text=True,
21+
check=True,
22+
)
23+
repo_list = yaml.safe_load(result.stdout)
24+
return repo_list
2125

2226

2327
def get_chart_and_app_version(repo_name: str, chart_name: str) -> Tuple[str, str]:
@@ -34,6 +38,7 @@ def get_chart_and_app_version(repo_name: str, chart_name: str) -> Tuple[str, str
3438

3539
def process_yaml_files(top_dir: str) -> None:
3640
seen_repos: Set[str] = set()
41+
local_repos = local_repo_list()
3742

3843
for root, _, files in os.walk(top_dir):
3944
for file in files:
@@ -73,12 +78,24 @@ def process_yaml_files(top_dir: str) -> None:
7378
if not repo_name or not repo_url:
7479
continue
7580

81+
local_repo = next(
82+
(repo for repo in local_repos if repo["name"] == repo_name), None
83+
)
84+
if local_repo is not None:
85+
local_repo_url = local_repo["url"]
86+
if local_repo_url != repo_url:
87+
raise Exception(
88+
dedent(f"""
89+
You have a local repo named {repo_name} pointing to {local_repo_url}, but the stack/demo wants {repo_url}
90+
Either delete or update your local repo URL, or update the stack/demo repo name so it doesn't conflict
91+
""")
92+
)
93+
7694
if repo_name not in seen_repos:
95+
# TODO: If the repo exists locally, but with a different URL, let the user know
7796
run_helm_repo_add(repo_name, repo_url)
7897
seen_repos.add(repo_name)
7998

80-
run_helm_repo_update(repo_name)
81-
8299
# print(f"📦 Updating {filepath} -> {repo_name}/{chart_name}")
83100
new_chart_version, new_app_version = get_chart_and_app_version(
84101
repo_name, chart_name
@@ -98,6 +115,9 @@ def process_yaml_files(top_dir: str) -> None:
98115
print(f"✅ Updated {filepath}")
99116

100117
except Exception as e:
118+
# If debugging, you can get the exception line number like this:
119+
# _, _, tb = os.sys.exc_info()
120+
# lineno = tb.tb_lineno
101121
print(f"⚠️ Error processing {filepath}: {e}")
102122

103123

0 commit comments

Comments
 (0)