-
Notifications
You must be signed in to change notification settings - Fork 706
/
chart_sync_utils.sh
executable file
·410 lines (359 loc) · 19.2 KB
/
chart_sync_utils.sh
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/bin/bash
# Copyright 2018-2022 the Kubeapps contributors.
# SPDX-License-Identifier: Apache-2.0
set -o errexit
set -o nounset
set -o pipefail
PROJECT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")/.." >/dev/null && pwd)
source "${PROJECT_DIR}/script/lib/liblog.sh"
# Path of the Kubeapps chart in the charts repo.
# For instance, given "https://github.com/bitnami/charts/tree/main/bitnami/kubeapps" it should be "bitnami/kubeapps"
CHART_REPO_PATH="bitnami/kubeapps"
# Path of the Kubeapps chart in the Kubeapps repo.
# For instance, given "https://github.com/vmware-tanzu/kubeapps/tree/main/chart/kubeapps", it should be "chart/kubeapps"
KUBEAPPS_CHART_DIR="${PROJECT_DIR}/chart/kubeapps"
# Paths of the templates files, note they are also used elsewhere
PR_INTERNAL_TEMPLATE_FILE="${PROJECT_DIR}/script/tpl/PR_internal_chart_template.md"
PR_EXTERNAL_TEMPLATE_FILE="${PROJECT_DIR}/script/tpl/PR_external_chart_template.md"
# shellcheck disable=SC2034
RELEASE_NOTES_TEMPLATE_FILE="${PROJECT_DIR}/script/tpl/release_notes.md"
########################################################################################################################
# Returns the tag for the latest release of the given repo.
# Globals:
# None
# Arguments:
# $1 - TARGET_REPO: Path to the git repo from which to extract the tag.
# Returns:
# Name of the latest tag found in the given repo.
########################################################################################################################
latestReleaseTag() {
local TARGET_REPO=${1:?}
info "getting latest release from ${TARGET_REPO}"
git -C "${TARGET_REPO}/.git" fetch --tags
git -C "${TARGET_REPO}/.git" describe --tags "$(git rev-list --tags --max-count=1)"
}
########################################################################################################################
# Configs Git for a given repository with the given params.
# Globals:
# None
# Arguments:
# $1 - TARGET_REPO: Path to the git repo to configure.
# $2 - USERNAME: Username for git commits.
# $3 - EMAIL: Email for git commits.
# $4 - GPG_KEY: GPG key to sign off the commits.
# Returns:
# None
########################################################################################################################
configUser() {
local TARGET_REPO=${1:?}
local USERNAME=${2:?}
local EMAIL=${3:?}
local GPG_KEY=${4:?}
cd "${TARGET_REPO}"
git config user.name "${USERNAME}"
git config user.email "${EMAIL}"
git config user.signingkey "${GPG_KEY}"
git config --global commit.gpgSign true
git config --global tag.gpgSign true
git config pull.rebase false
cd -
}
########################################################################################################################
# For the given Kubeapps service and Helm chart's values file, it replaces the image for the service to make it point
# to the production version of the image (the one generated by Bitnami folks), and to the latest tag available for that
# image, instead of pointing to `latest` version as the local chart does.
# Note: this function is used when updating the Bitnami chart with the latest changes from the local Helm chart.
# Globals:
# None
# Arguments:
# $1 - SERVICE: Name of the Kubeapps service (eg. kubeapps-apis)
# $2 - FILE: Path to the Helm chart's values.yaml file in the local chart. It is the file in which the substitution
# is done.
# Returns:
# None
########################################################################################################################
replaceImage_latestToProduction() {
local SERVICE=${1:?}
local FILE=${2:?}
local repoName="bitnami-docker-kubeapps-${SERVICE}"
local currentImageEscaped="kubeapps\/${SERVICE}"
local targetImageEscaped="bitnami\/kubeapps-${SERVICE}"
# Prevent a wrong image name "bitnami/kubeapps-kubeapps-apis"
# with a manual rename to "bitnami/kubeapps-apis"
if [ "${targetImageEscaped}" == "bitnami\/kubeapps-kubeapps-apis" ]; then
targetImageEscaped="bitnami\/kubeapps-apis"
fi
if [ "${repoName}" == "bitnami-docker-kubeapps-kubeapps-apis" ]; then
repoName="bitnami-docker-kubeapps-apis"
fi
echo "Replacing ${SERVICE}"...
local curl_opts=()
if [[ $GITHUB_TOKEN != "" ]]; then
curl_opts=(-s -H "Authorization: token ${GITHUB_TOKEN}")
fi
# Get the latest tag from the bitnami repository
local tag
tag=$(curl "${curl_opts[@]}" "https://api.github.com/repos/bitnami/${repoName}/tags" | jq -r '.[0].name')
if [[ $tag == "" ]]; then
echo "ERROR: Unable to obtain latest tag for ${repoName}. Stopping..."
exit 1
fi
# Replace image and tag from the values.yaml
sed -i.bk -e '1h;2,$H;$!d;g' -re \
's/repository: '${currentImageEscaped}'\n tag: latest/repository: '${targetImageEscaped}'\n tag: '${tag}'/g' \
"${FILE}"
rm "${FILE}.bk"
}
########################################################################################################################
# For the given Kubeapps service and Helm chart's values file, it replaces the image for the service to make it point
# to the development version of the image (the one generated in our CI/CD pipeline) instead of the production one
# generated by Bitnami, and to the `latest` tag, instead of the specific latest tag available for the image.
# Note: this function is used when updating the local Helm chart with the latest changes from the Bitnami chart.
# Globals:
# None
# Arguments:
# $1 - SERVICE: Name of the Kubeapps service (eg. kubeapps-apis)
# $2 - FILE: Path to the Helm chart's values.yaml file in the local chart. It is the file in which the substitution
# is done.
# Returns:
# None
########################################################################################################################
replaceImage_productionToLatest() {
local SERVICE=${1:?}
local FILE=${2:?}
local repoName="bitnami-docker-kubeapps-${SERVICE}"
local currentImageEscaped="bitnami\/kubeapps-${SERVICE}"
local targetImageEscaped="kubeapps\/${SERVICE}"
# Prevent a wrong image name "bitnami/kubeapps-kubeapps-apis"
# with a manual rename to "bitnami/kubeapps-apis"
if [ "${currentImageEscaped}" == "bitnami\/kubeapps-kubeapps-apis" ]; then
currentImageEscaped="bitnami\/kubeapps-apis"
fi
echo "Replacing ${SERVICE}"...
# Replace image and tag from the values.yaml
sed -i.bk -e '1h;2,$H;$!d;g' -re \
's/repository: '${currentImageEscaped}'\n tag: \S*/repository: '${targetImageEscaped}'\n tag: latest/g' \
"${FILE}"
rm "${FILE}.bk"
}
########################################################################################################################
# Syncs the fork of the charts repo with its upstream, then updates the local copy with the information from the local
# chart stored in the kubeapps repo, and applying to it the new version extracted from the passed TARGET_TAG param.
# Globals:
# KUBEAPPS_CHART_DIR: Path of the Kubeapps chart in the Kubeapps repo.
# CHART_REPO_PATH: Path of the Kubeapps chart in the charts repo.
# Arguments:
# $1 - CHARTS_REPO_FORK_LOCAL_PATH: Path to the clone of the bitnami/charts repo in the local machine.
# $2 - TARGET_TAG: Tag from which to take the new version for the Helm chart.
# $3 - CHARTS_FORK_SSH_KEY_FILENAME: Filename of the SSH key to connect with the remote charts repo fork.
# $4 - CHARTS_REPO_UPSTREAM: Name of the upstream version of the bitnami/charts repo without the GitHub part (eg. bitnami/charts).
# $5 - CHARTS_REPO_UPSTREAM_BRANCH: Name of the main branch in the upstream of the charts repo.
# $6 - CHARTS_REPO_FORK_BRANCH: Name of the main branch in the origin remove of the fork of charts repo.
# Returns:
# 0 - Success
# 1 - Failure
########################################################################################################################
updateRepoWithLocalChanges() {
local CHARTS_REPO_FORK_LOCAL_PATH=${1:?}
local TARGET_TAG=${2:?}
local CHARTS_FORK_SSH_KEY_FILENAME=${3:?}
local CHARTS_REPO_UPSTREAM=${4:?}
local CHARTS_REPO_UPSTREAM_BRANCH=${5:?}
local CHARTS_REPO_FORK_BRANCH=${6:?}
local targetTagWithoutV=${TARGET_TAG#v}
local targetChartPath="${CHARTS_REPO_FORK_LOCAL_PATH}/${CHART_REPO_PATH}"
local chartYaml="${targetChartPath}/Chart.yaml"
if [ ! -f "${chartYaml}" ]; then
echo "Wrong repo path. You should provide the root of the repository" >/dev/stderr
return 1
fi
# Fetch latest upstream changes, and commit&push them to the forked charts repo
git -C "${CHARTS_REPO_FORK_LOCAL_PATH}" remote add upstream "https://github.com/${CHARTS_REPO_UPSTREAM}.git"
git -C "${CHARTS_REPO_FORK_LOCAL_PATH}" pull upstream "${CHARTS_REPO_UPSTREAM_BRANCH}"
# https://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use
GIT_SSH_COMMAND="ssh -i ~/.ssh/${CHARTS_FORK_SSH_KEY_FILENAME}" git -C "${CHARTS_REPO_FORK_LOCAL_PATH}" push origin "${CHARTS_REPO_FORK_BRANCH}"
rm -rf "${targetChartPath}"
cp -R "${KUBEAPPS_CHART_DIR}" "${targetChartPath}"
# Update Chart.yaml with new version
sed -i.bk 's/appVersion: DEVEL/appVersion: '"${targetTagWithoutV}"'/g' "${chartYaml}"
rm "${targetChartPath}/Chart.yaml.bk"
info "New version ${targetTagWithoutV} applied to file ${chartYaml}"
# Replace images for the latest available
# TODO: use the IMAGES_TO_PUSH var already set in the CI config
replaceImage_latestToProduction dashboard "${targetChartPath}/values.yaml"
replaceImage_latestToProduction apprepository-controller "${targetChartPath}/values.yaml"
replaceImage_latestToProduction asset-syncer "${targetChartPath}/values.yaml"
replaceImage_latestToProduction pinniped-proxy "${targetChartPath}/values.yaml"
replaceImage_latestToProduction kubeapps-apis "${targetChartPath}/values.yaml"
}
########################################################################################################################
# Syncs the fork of the charts repo with its upstream, then updates the local Helm chart stored in the kubeapps repo
# with the chart information taken from the bitnami charts repo, and applying to it the new version extracted from the
# passed TARGET_TAG param.
# Globals:
# KUBEAPPS_CHART_DIR: Path of the Kubeapps chart in the Kubeapps repo.
# CHART_REPO_PATH: Path of the Kubeapps chart in the charts repo.
# Arguments:
# $1 - CHARTS_REPO_FORK_LOCAL_PATH: Path to the clone of the bitnami/charts repo in the local machine.
# $2 - TARGET_TAG: Tag from which to take the new version for the Helm chart.
# $3 - CHARTS_FORK_SSH_KEY_FILENAME: Filename of the SSH key to connect with the remote charts repo fork.
# $4 - CHARTS_REPO_UPSTREAM: Name of the upstream version of the bitnami/charts repo without the GitHub part (eg. bitnami/charts).
# $5 - CHARTS_REPO_UPSTREAM_BRANCH: Name of the main branch in the upstream of the charts repo.
# $6 - CHARTS_REPO_FORK_BRANCH: Name of the main branch in the origin remove of the fork of charts repo.
# Returns:
# 0 - Success
# 1 - Failure
########################################################################################################################
updateRepoWithRemoteChanges() {
local CHARTS_REPO_FORK_LOCAL_PATH=${1:?}
local TARGET_TAG=${2:?}
local CHARTS_FORK_SSH_KEY_FILENAME=${3:?}
local CHARTS_REPO_UPSTREAM=${4:?}
local CHARTS_REPO_UPSTREAM_BRANCH=${5:?}
local CHARTS_REPO_FORK_BRANCH=${6:?}
local targetTagWithoutV=${TARGET_TAG#v}
local targetChartPath="${CHARTS_REPO_FORK_LOCAL_PATH}/${CHART_REPO_PATH}"
local remoteChartYaml="${targetChartPath}/Chart.yaml"
local localChartYaml="${KUBEAPPS_CHART_DIR}/Chart.yaml"
if [ ! -f "${remoteChartYaml}" ]; then
echo "Wrong repo path. You should provide the root of the repository" >/dev/stderr
return 1
fi
# Fetch latest upstream changes, and commit&push them to the forked charts repo
git -C "${CHARTS_REPO_FORK_LOCAL_PATH}" remote add upstream "https://github.com/${CHARTS_REPO_UPSTREAM}.git"
git -C "${CHARTS_REPO_FORK_LOCAL_PATH}" pull upstream "${CHARTS_REPO_UPSTREAM_BRANCH}"
# https://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use
GIT_SSH_COMMAND="ssh -i ~/.ssh/${CHARTS_FORK_SSH_KEY_FILENAME}" git -C "${CHARTS_REPO_FORK_LOCAL_PATH}" push origin "${CHARTS_REPO_FORK_BRANCH}"
rm -rf "${KUBEAPPS_CHART_DIR}"
cp -R "${targetChartPath}" "${KUBEAPPS_CHART_DIR}"
# Update Chart.yaml with new version
sed -i.bk "s/appVersion: ${targetTagWithoutV}/appVersion: DEVEL/g" "${localChartYaml}"
rm "${KUBEAPPS_CHART_DIR}/Chart.yaml.bk"
info "New version ${targetTagWithoutV} applied to file ${localChartYaml}"
# Replace images for the latest available
# TODO: use the IMAGES_TO_PUSH var already set in the CI config
replaceImage_productionToLatest dashboard "${KUBEAPPS_CHART_DIR}/values.yaml"
replaceImage_productionToLatest apprepository-controller "${KUBEAPPS_CHART_DIR}/values.yaml"
replaceImage_productionToLatest asset-syncer "${KUBEAPPS_CHART_DIR}/values.yaml"
replaceImage_productionToLatest pinniped-proxy "${KUBEAPPS_CHART_DIR}/values.yaml"
replaceImage_productionToLatest kubeapps-apis "${KUBEAPPS_CHART_DIR}/values.yaml"
}
########################################################################################################################
# Generates the README file for the given Helm chart, using the given readme generator repo.
# Globals:
# None
# Arguments:
# $1 - README_GENERATOR_REPO: ID of the readme-generator GitHub repository (eg. bitnami-labs/readme-generator-for-helm)
# $2 - CHART_PATH: Local path to the Helm chart for which the README is generated.
# Returns:
# None
########################################################################################################################
generateReadme() {
local README_GENERATOR_REPO=${1:?}
local CHART_PATH=${2:?}
info "Generating Helm charts' README"
TMP_DIR=$(mktemp -u)/readme
local chartReadmePath="${CHART_PATH}/README.md"
local chartValuesPath="${CHART_PATH}/values.yaml"
git clone "https://github.com/${README_GENERATOR_REPO}" "${TMP_DIR}" --depth 1 --no-single-branch
cd "${TMP_DIR}"
npm install --production
node bin/index.js -r "${chartReadmePath}" -v "${chartValuesPath}"
}
########################################################################################################################
# Files a PR to update the Helm chart in the bitnami/charts repository to a new version.
# Globals:
# KUBEAPPS_CHART_DIR: Path of the Kubeapps chart in the Kubeapps repo.
# CHART_REPO_PATH: Path of the Kubeapps chart in the charts repo.
# Arguments:
# $1 - LOCAL_CHARTS_REPO_PATH: Path to the clone of the bitnami/charts repo in the local machine.
# $2 - TARGET_BRANCH: Name of the branch to create for the PR.
# $3 - CHART_VERSION: New version for the chart.
# $4 - CHARTS_REPO_UPSTREAM: Name of the upstream version of the bitnami/charts repo without the GitHub part (eg. bitnami/charts).
# $5 - CHARTS_REPO_UPSTREAM_BRANCH: Name of the main branch in the upstream of the charts repo.
# $6 - CHARTS_FORK_SSH_KEY_FILENAME: Name of the file with the SSH private key to connect with the upstream of the charts fork.
# Returns:
# 0 - Success
# 1 - Failure
########################################################################################################################
commitAndSendExternalPR() {
local LOCAL_CHARTS_REPO_PATH=${1:?}
local TARGET_BRANCH=${2:?}
local CHART_VERSION=${3:?}
local CHARTS_REPO_UPSTREAM=${4:?}
local CHARTS_REPO_UPSTREAM_BRANCH=${5:?}
local CHARTS_FORK_SSH_KEY_FILENAME=${6:?}
local targetChartPath="${LOCAL_CHARTS_REPO_PATH}/${CHART_REPO_PATH}"
local chartYaml="${targetChartPath}/Chart.yaml"
if [ ! -f "${chartYaml}" ]; then
echo "Wrong repo path. You should provide the root of the repository" >/dev/stderr
return 1
fi
cd "${LOCAL_CHARTS_REPO_PATH}"
if [[ ! $(git diff-index HEAD) ]]; then
echo "Not found any change to commit" >/dev/stderr
cd -
return 1
fi
PR_TITLE="[bitnami/kubeapps] Bump chart version to ${CHART_VERSION}"
sed -i.bk -e "s/<USER>/$(git config user.name)/g" "${PR_EXTERNAL_TEMPLATE_FILE}"
sed -i.bk -e "s/<EMAIL>/$(git config user.email)/g" "${PR_EXTERNAL_TEMPLATE_FILE}"
git checkout -b "${TARGET_BRANCH}"
git add --all .
git commit --signoff -m "kubeapps: bump chart version to ${CHART_VERSION}"
# NOTE: This expects to have a loaded SSH key
if [[ $(GIT_SSH_COMMAND="ssh -i ~/.ssh/${CHARTS_FORK_SSH_KEY_FILENAME}" git ls-remote origin "${TARGET_BRANCH}" | wc -l) -eq 0 ]]; then
GIT_SSH_COMMAND="ssh -i ~/.ssh/${CHARTS_FORK_SSH_KEY_FILENAME}" git push -u origin "${TARGET_BRANCH}"
gh pr create -d -B "${CHARTS_REPO_UPSTREAM_BRANCH}" -R "${CHARTS_REPO_UPSTREAM}" -F "${PR_EXTERNAL_TEMPLATE_FILE}" --title "${PR_TITLE}"
else
echo "The remote branch '${TARGET_BRANCH}' already exists, please check if there is already an open PR at the repository '${CHARTS_REPO_UPSTREAM}'"
return 1
fi
cd -
}
########################################################################################################################
# Updates the local Helm chart to a new version and files a PR against the upstream Kubeapps repo.
# Globals:
# KUBEAPPS_CHART_DIR: Path of the Kubeapps chart in the Kubeapps repo.
# Arguments:
# $1 - LOCAL_REPO_PATH: Path to the clone of the Kubeapps repo in the local machine.
# $2 - TARGET_BRANCH: Name of the branch to create for the PR.
# $3 - CHART_VERSION: New version for the chart.
# $4 - UPSTREAM_REPO: Name of the upstream version of the kubeapps repo without the GitHub part (eg. vmware-tanzu/kubeapps).
# $5 - UPSTREAM_MAIN_BRANCH: Name of the main branch in the upstream repo.
# Returns:
# 0 - Success
# 1 - Failure
########################################################################################################################
commitAndSendInternalPR() {
local LOCAL_REPO_PATH=${1:?}
local TARGET_BRANCH=${2:?}
local CHART_VERSION=${3:?}
local UPSTREAM_REPO=${4:?}
local UPSTREAM_MAIN_BRANCH=${5:?}
local targetChartPath="${KUBEAPPS_CHART_DIR}/Chart.yaml"
local localChartYaml="${KUBEAPPS_CHART_DIR}/Chart.yaml"
if [ ! -f "${localChartYaml}" ]; then
echo "Wrong repo path. You should provide the root of the repository" >/dev/stderr
return 1
fi
cd "${LOCAL_REPO_PATH}"
if [[ ! $(git diff-index HEAD) ]]; then
echo "Not found any change to commit" >/dev/stderr
cd -
return 1
fi
PR_TITLE="Sync chart with bitnami/kubeapps chart (version ${CHART_VERSION})"
git checkout -b "${TARGET_BRANCH}"
git add --all .
git commit --signoff -m "bump chart version to ${CHART_VERSION}"
# NOTE: This expects to have a loaded SSH key
if [[ $(git ls-remote origin "${TARGET_BRANCH}" | wc -l) -eq 0 ]]; then
git push -u origin "${TARGET_BRANCH}"
gh pr create -d -B "${UPSTREAM_MAIN_BRANCH}" -R "${UPSTREAM_REPO}" -F "${PR_INTERNAL_TEMPLATE_FILE}" --title "${PR_TITLE}"
else
echo "The remote branch '${TARGET_BRANCH}' already exists, please check if there is already an open PR at the repository '${UPSTREAM_REPO}'"
return 1
fi
cd -
}