this repo has no description
1#!/usr/bin/env bash
2
3# shellcheck source=/dev/null
4source "$(dirname "${0}")/lib/functions.sh"
5
6set -o errexit
7set -o nounset
8set -o pipefail
9shopt -s lastpipe
10
11show_help() {
12cat << EOF
13Usage: $(basename "$0") <options>
14 -h, --help Display help
15 --source-file Original helm release
16 --target-file New helm release
17 --remove-common-labels Remove common labels from manifests
18EOF
19}
20
21main() {
22 local source_file=
23 local target_file=
24 local remove_common_labels=
25 parse_command_line "$@"
26 check "helm"
27 check "yq"
28 entry
29}
30
31parse_command_line() {
32 while :; do
33 case "${1:-}" in
34 -h|--help)
35 show_help
36 exit
37 ;;
38 --source-file)
39 if [[ -n "${2:-}" ]]; then
40 source_file="$2"
41 shift
42 else
43 echo "ERROR: '--source-file' cannot be empty." >&2
44 show_help
45 exit 1
46 fi
47 ;;
48 --target-file)
49 if [[ -n "${2:-}" ]]; then
50 target_file="$2"
51 shift
52 else
53 echo "ERROR: '--target-file' cannot be empty." >&2
54 show_help
55 exit 1
56 fi
57 ;;
58 --remove-common-labels)
59 remove_common_labels=true
60 ;;
61 *)
62 break
63 ;;
64 esac
65 shift
66 done
67
68 if [[ -z "${source_file}" ]]; then
69 echo "ERROR: '--source-file' is required." >&2
70 show_help
71 exit 1
72 fi
73
74 if [[ $(yq eval .kind "${source_file}" 2>/dev/null) != "HelmRelease" ]]; then
75 echo "ERROR: '--source-file' is not a HelmRelease"
76 show_help
77 exit 1
78 fi
79
80 if [[ -z "${target_file}" ]]; then
81 echo "ERROR: '--target-file' is required." >&2
82 show_help
83 exit 1
84 fi
85
86 if [[ $(yq eval .kind "${target_file}" 2>/dev/null) != "HelmRelease" ]]; then
87 echo "ERROR: '--target-file' is not a HelmRelease"
88 show_help
89 exit 1
90 fi
91
92 if [[ -z "$remove_common_labels" ]]; then
93 remove_common_labels=false
94 fi
95}
96
97_resources() {
98 local chart_name=${1}
99 local chart_version=${2}
100 local chart_registry_url=${3}
101 local chart_values=${4}
102 local resources=
103
104 helm repo add main "${chart_registry_url}" > /dev/null 2>&1
105 pushd "$(mktemp -d)" > /dev/null 2>&1
106 helm pull "main/${chart_name}" --untar --version "${chart_version}"
107 resources=$(echo "${chart_values}" | helm template "${chart_name}" "${chart_name}" --version "${chart_version}" -f -)
108 if [[ "${remove_common_labels}" == "true" ]]; then
109 labels='.metadata.labels."helm.sh/chart"'
110 labels+=',.metadata.labels.chart'
111 labels+=',.metadata.labels."app.kubernetes.io/version"'
112 labels+=',.spec.template.metadata.labels."helm.sh/chart"'
113 labels+=',.spec.template.metadata.labels.chart'
114 labels+=',.spec.template.metadata.labels."app.kubernetes.io/version"'
115 echo "${resources}" | yq eval "del($labels)" -
116 else
117 echo "${resources}"
118 fi
119 popd > /dev/null 2>&1
120 helm repo remove main > /dev/null 2>&1
121}
122
123entry() {
124 local comments=
125
126 source_chart_name=$(chart_name "${source_file}")
127 source_chart_version=$(chart_version "${source_file}")
128 source_chart_registry_url=$(chart_registry_url "${source_file}")
129 source_chart_values=$(chart_values "${source_file}")
130 source_resources=$(_resources "${source_chart_name}" "${source_chart_version}" "${source_chart_registry_url}" "${source_chart_values}")
131 echo "${source_resources}" > /tmp/source_resources
132
133 target_chart_version=$(chart_version "${target_file}")
134 target_chart_name=$(chart_name "${target_file}")
135 target_chart_registry_url=$(chart_registry_url "${target_file}")
136 target_chart_values=$(chart_values "${target_file}")
137 target_resources=$(_resources "${target_chart_name}" "${target_chart_version}" "${target_chart_registry_url}" "${target_chart_values}")
138 echo "${target_resources}" > /tmp/target_resources
139
140 # Diff the files and always return true
141 diff -u /tmp/source_resources /tmp/target_resources > /tmp/diff || true
142 # Remove the filenames
143 sed -i -e '1,2d' /tmp/diff
144
145 # Store the comment in an array
146 comments=()
147
148 # shellcheck disable=SC2016
149 comments+=( "$(printf 'Path: `%s`' "${target_file}")" )
150 if [[ "${source_chart_name}" != "${target_chart_name}" ]]; then
151 # shellcheck disable=SC2016
152 comments+=( "$(printf 'Chart: `%s` -> `%s`' "${source_chart_name}" "${target_chart_name}")" )
153 fi
154 if [[ "${source_chart_version}" != "${target_chart_version}" ]]; then
155 # shellcheck disable=SC2016
156 comments+=( "$(printf 'Version: `%s` -> `%s`' "${source_chart_version}" "${target_chart_version}")" )
157 fi
158 if [[ "${source_chart_registry_url}" != "${target_chart_registry_url}" ]]; then
159 # shellcheck disable=SC2016
160 comments+=( "$(printf 'Registry URL: `%s` -> `%s`' "${source_chart_registry_url}" "${target_chart_registry_url}")" )
161 fi
162 comments+=( "$(printf '\n\n')" )
163 if [[ -f /tmp/diff && -s /tmp/diff ]]; then
164 # shellcheck disable=SC2016
165 comments+=( "$(printf '```diff\n%s\n```' "$(cat /tmp/diff)")" )
166 else
167 # shellcheck disable=SC2016
168 comments+=( "$(printf '```\nNo changes in detected in resources\n```')" )
169 fi
170
171 # Join the array with a new line and print it
172 printf "%s\n" "${comments[@]}"
173}
174
175main "$@"