Skip to content

Commit 28adb85

Browse files
authored
Add support for GitRepository (#4)
1 parent 0ec358a commit 28adb85

File tree

2 files changed

+48
-19
lines changed

2 files changed

+48
-19
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
debug.*
1+
debug.*
2+
tmp/*

flux-helm-diff.sh

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,28 +38,52 @@ helm_template() {
3838
if [[ "HelmRepository" == "$(yq '. | select(.kind == "HelmRelease").spec.chart.spec.sourceRef.kind' "${helm_file}")" ]]; then
3939
repo_type=helm
4040
repo_name=$(yq '. | select(.kind == "HelmRelease").spec.chart.spec.sourceRef.name' "${helm_file}")
41-
chart=$(yq '. | select(.kind == "HelmRelease").spec.chart.spec.chart' "${helm_file}")
42-
url=$(yq '. | select(.kind == "HelmRepository").spec.url' "${helm_file}")
43-
version=$(yq '. | select(.kind == "HelmRelease").spec.chart.spec.version' "${helm_file}")
44-
if [[ "${url}" = "oci://"* ]]; then
45-
url="${url}/${chart}" # Syntax for chart repos is different from OCI repos (as HelmRepo kind)
41+
repo_url=$(yq '. | select(.kind == "HelmRepository").spec.url' "${helm_file}")
42+
chart_name=$(yq '. | select(.kind == "HelmRelease").spec.chart.spec.chart' "${helm_file}")
43+
chart_version=$(yq '. | select(.kind == "HelmRelease").spec.chart.spec.version' "${helm_file}")
44+
if [[ "${repo_url}" = "oci://"* ]]; then
45+
url="${repo_url}/${chart_name}" # Syntax for chart repos is different from OCI repos (as HelmRepo kind)
46+
else
47+
url="${repo_url}"
4648
fi
49+
4750
elif [[ "OCIRepository" == "$(yq '. | select(.kind == "HelmRelease").spec.chartRef.kind' "${helm_file}")" ]]; then
4851
repo_type=oci
4952
repo_name=$(yq '. | select(.kind == "HelmRelease").spec.chartRef.name' "${helm_file}")
50-
chart="${repo_name}"
53+
chart_name="${repo_name}"
54+
chart_version=$(yq '. | select(.kind == "OCIRepository").spec.ref.tag' "${helm_file}")
5155
url=$(yq '. | select(.kind == "OCIRepository").spec.url' "${helm_file}")
52-
version=$(yq '. | select(.kind == "OCIRepository").spec.ref.tag' "${helm_file}")
56+
57+
elif [[ "GitRepository" == "$(yq '. | select(.kind == "HelmRelease").spec.chart.spec.sourceRef.kind' "${helm_file}")" ]]; then
58+
repo_type=git
59+
repo_name=$(yq '. | select(.kind == "HelmRelease").spec.chart.spec.sourceRef.name' "${helm_file}")
60+
repo_url=$(yq '. | select(.kind == "GitRepository").spec.url' "${helm_file}")
61+
repo_tag=$(yq '. | select(.kind == "GitRepository").spec.ref.tag' "${helm_file}")
62+
chart_name="${repo_name}"
63+
chart_path=$(yq '. | select(.kind == "HelmRelease").spec.chart.spec.chart' "${helm_file}")
64+
65+
# Download and extract release artifact - may only work with GitHub?
66+
release_id="${repo_name}_${repo_tag}"
67+
mkdir -p "./tmp/${release_id}"
68+
curl --no-progress-meter -Lo "${release_id}.tar.gz" "${repo_url}/archive/refs/tags/${repo_tag}.tar.gz"
69+
tar -xzf "${release_id}.tar.gz" --directory "./tmp/${release_id}"
70+
rm "./${release_id}.tar.gz" || true
71+
72+
# Find the chart directory
73+
find_chart_path=$(echo "${chart_path}" | sed -e 's|^./|/|' -e 's|/$||')
74+
url=$(find "./tmp/${release_id}" -type d -path "*${find_chart_path}" | head -n 1)
75+
5376
else
5477
echo "Unable to determine repo type, skipping"
5578
echo "Unable to determine repo type, skipping" >&2
5679
return 2
5780
fi
5881

5982
# Extracting chart properties
60-
name=$(yq '. | select(.kind == "HelmRelease").metadata.name' "${helm_file}")
61-
namespace=$(yq '. | select(.kind == "HelmRelease").metadata.namespace' "${helm_file}")
62-
values=$(yq '. | select(.kind == "HelmRelease").spec.values' "${helm_file}")
83+
release_name=$(yq '. | select(.kind == "HelmRelease").metadata.name' "${helm_file}")
84+
release_namespace=$(yq '. | select(.kind == "HelmRelease").metadata.namespace' "${helm_file}")
85+
chart_values=$(yq '. | select(.kind == "HelmRelease").spec.values' "${helm_file}")
86+
chart_version="${chart_version:-N/A}" # not relevant for local charts, e.g. downloaded via GitRepository
6387

6488
# Use Capabilities.APIVersions
6589
mapfile -t api_versions < <(yq '. | foot_comment' "${helm_file}" | yq '.helm-api-versions[]')
@@ -68,21 +92,24 @@ helm_template() {
6892
echo "${ref} repo type: ${repo_type}" >&2
6993
echo "${ref} repo name: ${repo_name}" >&2
7094
echo "${ref} repo/chart URL: ${url}" >&2
71-
echo "${ref} chart name: ${chart}" >&2
72-
echo "${ref} chart version: ${version}" >&2
73-
echo "${ref} release name: ${name}" >&2
74-
echo "${ref} release namespace: ${namespace}" >&2
95+
echo "${ref} chart name: ${chart_name}" >&2
96+
echo "${ref} chart version: ${chart_version}" >&2
97+
echo "${ref} release name: ${release_name}" >&2
98+
echo "${ref} release namespace: ${release_namespace}" >&2
7599
echo "${ref} API versions: $(IFS=,; echo "${api_versions[*]}")" >&2
76100

77101
# Syntax for chart repos is different from OCI repos (as HelmRepo kind)
78-
if [[ "${url}" = "oci://"* ]]; then
79-
chart_args=("${url}") # treat as array, to avoid adding single-quotes
102+
if [[ "${url}" = "https://"* ]]; then
103+
chart_args=("${chart_name}" --repo "${url}" --version "${chart_version}") # treat as array, to avoid adding single-quotes
104+
elif [[ "${url}" = "oci://"* ]]; then
105+
chart_args=("${url}" --version "${chart_version}") # treat as array, to avoid adding single-quotes
80106
else
81-
chart_args=("${chart}" --repo "${url}")
107+
# Assume local path (i.e. GitRepository)
108+
chart_args=("${url}") # treat as array, to avoid adding single-quotes
82109
fi
83110

84111
# Render template
85-
template_out=$(helm template "${name}" ${chart_args[@]} --version "${version}" -n "${namespace}" -f <(echo "${values}") --api-versions "$(IFS=,; echo "${api_versions[*]}")" 2>&1) || {
112+
template_out=$(helm template "${release_name}" ${chart_args[@]} -n "${release_namespace}" -f <(echo "${chart_values}") --api-versions "$(IFS=,; echo "${api_versions[*]}")" 2>&1) || {
86113
echo "$template_out"
87114
echo "$template_out" >&2
88115
return 2
@@ -148,4 +175,5 @@ done
148175
echo "any_failed=$any_failed"
149176
} >> "$GITHUB_OUTPUT"
150177

178+
if [ -d "./tmp" ]; then rm -rf "./tmp"; fi
151179
echo -e "\nAll done"

0 commit comments

Comments
 (0)