-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdateChart
executable file
·174 lines (153 loc) · 4.73 KB
/
updateChart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/bin/bash
set -e
set -o nounset
set -o pipefail
dependencies_names=()
chart=
new_version=
keep_version="0"
repository=
local_charts_dir=
# returns the index of the dependency in the chart
# $1 - dependency name
# if the dependency is not found, returns -1
function get_dependency_index() {
local target_dependency=$1 # Specify the dependency you are searching for
local index=0
while true; do
local current_dependency=$(yq e ".dependencies[$index].name" $chart) # Get name of current dependency
if [ "$current_dependency" = "$target_dependency" ]; then
echo $index
return 0
fi
if [ "$current_dependency" = "null" ]; then # If current dependency name is null, we've reached end of array
echo "-1"
return 1
fi
index=$((index + 1)) # Increment the index
done
}
# Execute the function
# get_dependency_index "<path_to_your_yaml_file>" "<target_dependency>"
# checks if the dependency helm chart is managed locally
# $1 - index of dependency in the chart
# $2 - directory where the helm charts are stored
# returns true if the dependency is managed locally
# returns false if the dependency is not managed locally
local_dependency() {
local dependency_index=${1}
local dir=${2}
dependency_repository=$(yq eval '.dependencies['${dependency_index}'].repository' ${chart})
if [[ ${repository} == ${dependency_repository} ]]; then
dependency_name=$(yq eval '.dependencies['${dependency_index}'].name' ${chart})
if [[ -d ${dir}/${dependency_name} ]]; then
echo "true"
else
echo "false"
fi
else
echo "false"
fi
}
get_dependencies_names() {
dependencies_names=($(yq eval '.dependencies[].name' ${chart}))
}
show_help() {
cat <<EOF
Usage: $(basename "$0") <options>
-h, --help Display this help
-c, --chart Chart.yaml to update
-v, --new-version New version to set for the chart and its dependencies
-k, --keep-version Keep the old version of the chart. Only sets dependencies versions.
-l, --local-charts-dir Location where local managed dependency charts are located
-r, --repository Repository of the chart
EOF
}
parse_command_line() {
# Check if the script was called with the right number of arguments
if [ $# -eq 0 ]; then
show_help
exit 1
fi
while :; do
case "${1:-}" in
-h | --help)
show_help
exit
;;
-c | --chart)
if [[ -n "${2:-}" ]]; then
chart="$2"
shift
else
echo "ERROR: '--chart' cannot be empty." >&2
show_help
exit 1
fi
;;
-v | --new-version)
if [[ -n "${2:-}" ]]; then
new_version="$2"
shift
else
echo "ERROR: '--new-version' cannot be empty." >&2
show_help
exit 1
fi
;;
-r | --repository)
if [[ -n "${2:-}" ]]; then
repository="$2"
shift
else
echo "ERROR: '--repository' cannot be empty." >&2
show_help
exit 1
fi
;;
-l | --local-charts-dir)
if [[ -n "${2:-}" ]]; then
local_charts_dir="$2"
shift
else
echo "ERROR: '--local-charts-dir' cannot be empty." >&2
show_help
exit 1
fi
;;
-k | --keep-version)
keep_version="1"
;;
*)
break
;;
esac
shift
done
if [ "${keep_version}" -eq "0" ] && [ -z "${new_version}" ]; then
echo "ERROR: '--new-version' cannot be empty." >&2
show_help
exit 1
fi
}
main() {
parse_command_line "$@"
get_dependencies_names
for dependency in "${dependencies_names[@]}"; do
index=$(get_dependency_index ${dependency})
loc_dep=$(local_dependency ${index} ${local_charts_dir})
if [[ ${loc_dep} == "true" ]]; then
echo "Dependency '${dependency}' updated to version ${new_version}"
yq eval '.dependencies['${index}'].version = "'${new_version}'"' -i ${chart}
else
echo "Dependency '${dependency}' managed externally. Skipping."
fi
done
if [ "${keep_version}" -eq "0" ]; then
echo "Updating chart version to ${new_version}..."
yq e ".version = \"${new_version}\"" -i ${chart}
yq e ".appVersion = \"${new_version}\"" -i ${chart}
fi
exit 0
}
main "$@"