1
1
#! /bin/bash
2
+ set -eu -o pipefail
2
3
3
- if [ " ${# } " == " 0" ]; then
4
+ helm_files=(${HELM_FILES[@]} )
5
+ if [ " ${# helm_files[@]} " == " 0" ]; then
4
6
echo " No Helm files specified, nothing to do"
5
7
exit
6
8
fi
7
- helm_files=( " ${@ } " )
8
- echo " Helm files to render: ${helm_files[*]} "
9
+ echo " ${# helm_files[@]} Helm file(s) to render: ${helm_files[*]} "
9
10
10
11
helm_template () {
11
- if [ -z " ${2} " ]; then
12
+ set -eu -o pipefail
13
+ if [ -z " ${1} " ]; then
12
14
echo " Error: Need file name to template" >&2
13
- return 1
15
+ return 2
14
16
fi
15
17
18
+ # 'head' or 'base' ref - used for logging output
19
+ ref=" ${1%%/* } "
20
+
16
21
# Set test = <something> to run against Helm teplates under test/
17
- if [ -z " ${test } " ]; then
18
- helm_file=" ${2 } "
22
+ if [ -z " ${TEST :- } " ]; then
23
+ helm_file=" ${1 } "
19
24
else
20
- helm_file=" test/${2 } "
25
+ helm_file=" test/${1 } "
21
26
fi
22
27
23
28
if [ ! -f " ${helm_file} " ]; then
24
- echo " Error : File \" ${helm_file} \" not found, skipping diff "
25
- echo " Error: File \" ${helm_file} \" not found, skipping diff " >&2
29
+ # echo "Warn : File \"${helm_file}\" not found, skipping"
30
+ echo " File \" ${helm_file} \" not found, skipping" >&2
26
31
return 1
27
32
fi
28
33
34
+ # Determine repo type - HelmRepository or OCIRepository
35
+ # https://fluxcd.io/flux/components/source/helmrepositories/
36
+ # https://fluxcd.io/flux/components/source/ocirepositories/
37
+ # https://fluxcd.io/flux/components/source/gitrepositories/
38
+ if [[ " HelmRepository" == " $( yq ' . | select(.kind == "HelmRelease").spec.chart.spec.sourceRef.kind' " ${helm_file} " ) " ]]; then
39
+ repo_type=helm
40
+ 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)
46
+ fi
47
+ elif [[ " OCIRepository" == " $( yq ' . | select(.kind == "HelmRelease").spec.chartRef.kind' " ${helm_file} " ) " ]]; then
48
+ repo_type=oci
49
+ repo_name=$( yq ' . | select(.kind == "HelmRelease").spec.chartRef.name' " ${helm_file} " )
50
+ chart=" ${repo_name} "
51
+ url=$( yq ' . | select(.kind == "OCIRepository").spec.url' " ${helm_file} " )
52
+ version=$( yq ' . | select(.kind == "OCIRepository").spec.ref.tag' " ${helm_file} " )
53
+ else
54
+ echo " Unable to determine repo type, skipping"
55
+ echo " Unable to determine repo type, skipping" >&2
56
+ return 2
57
+ fi
58
+
29
59
# Extracting chart properties
30
60
name=$( yq ' . | select(.kind == "HelmRelease").metadata.name' " ${helm_file} " )
31
61
namespace=$( yq ' . | select(.kind == "HelmRelease").metadata.namespace' " ${helm_file} " )
32
- version=$( yq ' . | select(.kind == "HelmRelease") | .spec.chart.spec.version' " ${helm_file} " )
33
- url=$( yq ' . | select(.kind == "HelmRepository") | .spec.url' " ${helm_file} " )
34
- chart=$( yq ' . | select(.kind == "HelmRelease") | .spec.chart.spec.chart' " ${helm_file} " )
35
62
values=$( yq ' . | select(.kind == "HelmRelease").spec.values' " ${helm_file} " )
36
- echo " Chart version ${1} : $version ($chart from $url )" >&2
37
63
38
- # Syntax for chart repos is different from OCI repos
64
+ # Let's see what information we got out about the chart...
65
+ echo " ${ref} repo type: ${repo_type} " >&2
66
+ echo " ${ref} repo name: ${repo_name} " >&2
67
+ echo " ${ref} repo/chart URL: ${url} " >&2
68
+ echo " ${ref} chart name: ${chart} " >&2
69
+ echo " ${ref} chart version: ${version} " >&2
70
+ echo " ${ref} release name: ${name} " >&2
71
+ echo " ${ref} release namespace: ${namespace} " >&2
72
+
73
+ # Syntax for chart repos is different from OCI repos (as HelmRepo kind)
39
74
if [[ " ${url} " = " oci://" * ]]; then
40
- chart_args=(" ${url} / ${chart} " ) # treat as array, to avoid adding single-quotes
75
+ chart_args=(" ${url} " ) # treat as array, to avoid adding single-quotes
41
76
else
42
77
chart_args=(" ${chart} " --repo " ${url} " )
43
78
fi
@@ -46,19 +81,16 @@ helm_template() {
46
81
template_out=$( helm template " ${name} " ${chart_args[@]} --version " ${version} " -n " ${namespace} " -f <( echo " ${values} " ) 2>&1 ) || {
47
82
echo " $template_out "
48
83
echo " $template_out " >&2
49
- return 1
84
+ return 2
50
85
}
51
86
52
87
# Cleanup template, removing comments, output
53
88
template_clean=$( yq -P ' sort_keys(..) comments=""' <( echo " ${template_out} " ) )
54
89
echo " $template_clean "
55
-
56
- # Debug info
57
- echo " Line count ${1} : values ($( echo " ${values} " | wc -l) ), template ($( echo " ${template_out} " | wc -l) ), template_clean ($( echo " ${template_clean} " | wc -l) )" >&2
58
90
}
59
91
60
92
EOF=$( dd if=/dev/urandom bs=15 count=1 status=none | base64)
61
- echo " markdown<<$EOF " >> " $GITHUB_OUTPUT "
93
+ echo " markdown<<$EOF " > " $GITHUB_OUTPUT "
62
94
echo " ## Flux Helm diffs" >> " $GITHUB_OUTPUT "
63
95
64
96
any_failed=0
@@ -70,26 +102,38 @@ for helm_file in "${helm_files[@]}"; do
70
102
71
103
# Template before
72
104
return_code=0
73
- before_out =$( helm_template before " base/${helm_file} " ) || return_code=1
74
- if [ $return_code -ne 0 ]; then
105
+ base_out =$( helm_template " base/${helm_file} " ) || return_code=$?
106
+ if [ $return_code -eq 2 ]; then # Ignore files skipped
75
107
{
76
108
echo ' ```'
77
- echo " ${before_out} "
109
+ echo " Error rendering base ref:"
110
+ echo " ${base_out} "
78
111
echo ' ```'
79
112
} >> " $GITHUB_OUTPUT "
80
113
any_failed=1
81
114
continue
82
115
fi
83
116
84
117
# Template after
85
- after_out=$( helm_template after " head/${helm_file} " ) || true
118
+ return_code=0
119
+ head_out=$( helm_template " head/${helm_file} " ) || return_code=$?
120
+ if [ $return_code -ne 0 ]; then
121
+ {
122
+ echo ' ```'
123
+ echo " Error rendering head ref:"
124
+ echo " ${head_out} "
125
+ echo ' ```'
126
+ } >> " $GITHUB_OUTPUT "
127
+ any_failed=1
128
+ continue
129
+ fi
86
130
87
131
# Template diff
88
- diff_out=$( diff --unified=5 <( echo " ${before_out } " ) <( echo " ${after_out } " ) ) || true
132
+ diff_out=$( diff --unified=5 <( echo " ${base_out } " ) <( echo " ${head_out } " ) ) || true
89
133
echo " Diff has $( echo " $diff_out " | wc -l) line(s)"
90
134
[ -z " ${diff_out} " ] && diff_out=" No changes"
91
135
{
92
- echo ' ```'
136
+ echo ' ```diff '
93
137
echo " ${diff_out} "
94
138
echo ' ```'
95
139
} >> " $GITHUB_OUTPUT "
99
143
echo " $EOF "
100
144
echo " any_failed=$any_failed "
101
145
} >> " $GITHUB_OUTPUT "
146
+
147
+ echo -e " \nAll done"
0 commit comments