Skip to content

Commit 792c614

Browse files
committed
werft/deploy/post-process: Use intention-revealing variable name
Signed-off-by: ArthurSens <[email protected]>
1 parent baee93a commit 792c614

File tree

1 file changed

+45
-44
lines changed

1 file changed

+45
-44
lines changed

.werft/jobs/build/installer/post-process.sh

Lines changed: 45 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,45 +20,46 @@ else
2020
echo "Running with the following params: ${REG_DAEMON_PORT} ${WS_DAEMON_PORT} ${NODE_POOL_INDEX} ${DEV_BRANCH}"
2121
fi
2222

23+
echo "Use node pool index $NODE_POOL_INDEX"
24+
2325
# Optional params
2426
# default yes, we add a license
2527
LICENSE=$(cat /tmp/license)
2628
# default, no, we do not add feature flags, file is empty
2729
DEFAULT_FEATURE_FLAGS=$(cat /tmp/defaultFeatureFlags)
2830

29-
i=0
31+
3032

3133
# count YAML like lines in the k8s manifest file
3234
MATCHES="$(grep -c -- --- k8s.yaml)"
3335
# get the read number of K8s manifest docs
3436
# K8s object names and kinds are duplicated in a config map to faciliate deletion
3537
# subtract one (the config map) and then divide by 2 to get the actual # of docs we'll loop through
3638
DOCS="$((((MATCHES - 1) / 2) + 1))"
39+
documentIndex=0
3740

38-
echo "Use node pool index $NODE_POOL_INDEX"
39-
40-
while [ "$i" -le "$DOCS" ]; do
41+
while [ "$documentIndex" -le "$DOCS" ]; do
4142
# override daemon sets which bind to an actual node port via the hostPort setting on the pod
4243
unset PORT SIZE NAME UNSET
43-
PORT=$(yq r k8s.yaml -d "$i" spec.template.spec.containers.[0].ports.[0].hostPort)
44+
PORT=$(yq r k8s.yaml -d "$documentIndex" spec.template.spec.containers.[0].ports.[0].hostPort)
4445
SIZE="${#PORT}"
45-
NAME=$(yq r k8s.yaml -d "$i" metadata.name)
46-
KIND=$(yq r k8s.yaml -d "$i" kind)
46+
NAME=$(yq r k8s.yaml -d "$documentIndex" metadata.name)
47+
KIND=$(yq r k8s.yaml -d "$documentIndex" kind)
4748
if [[ "$SIZE" -ne "0" ]] && [[ "$NAME" == "registry-facade" ]] && [[ "$KIND" == "DaemonSet" ]] ; then
4849
echo "setting $NAME to $REG_DAEMON_PORT"
49-
yq w -i k8s.yaml -d "$i" spec.template.spec.containers.[0].ports.[0].hostPort "$REG_DAEMON_PORT"
50+
yq w -i k8s.yaml -d "$documentIndex" spec.template.spec.containers.[0].ports.[0].hostPort "$REG_DAEMON_PORT"
5051
fi
5152
if [[ "$SIZE" -ne "0" ]] && [[ "$NAME" == "ws-daemon" ]] && [[ "$KIND" == "DaemonSet" ]] ; then
5253
echo "setting $NAME to $WS_DAEMON_PORT"
53-
yq w -i k8s.yaml -d "$i" spec.template.spec.containers.[0].ports.[0].hostPort "$WS_DAEMON_PORT"
54-
yq w -i k8s.yaml -d "$i" spec.template.spec.containers.[0].ports.[0].containerPort "$WS_DAEMON_PORT"
54+
yq w -i k8s.yaml -d "$documentIndex" spec.template.spec.containers.[0].ports.[0].hostPort "$WS_DAEMON_PORT"
55+
yq w -i k8s.yaml -d "$documentIndex" spec.template.spec.containers.[0].ports.[0].containerPort "$WS_DAEMON_PORT"
5556
fi
5657

5758
# override details for registry-facade service
5859
if [[ "registry-facade" == "$NAME" ]] && [[ "$KIND" == "Service" ]]; then
5960
WORK="overrides for $NAME $KIND"
6061
echo "$WORK"
61-
yq w -i k8s.yaml -d "$i" spec.ports[0].port "$REG_DAEMON_PORT"
62+
yq w -i k8s.yaml -d "$documentIndex" spec.ports[0].port "$REG_DAEMON_PORT"
6263
fi
6364

6465
# override labels for pod scheduling on nodes
@@ -72,7 +73,7 @@ while [ "$i" -le "$DOCS" ]; do
7273
yq w -i /tmp/"$NAME"pool.yaml spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].key "$LABEL"
7374
yq w -i /tmp/"$NAME"pool.yaml spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].operator Exists
7475
# append it
75-
yq m --arrays=overwrite -i k8s.yaml -d "$i" /tmp/"$NAME"pool.yaml
76+
yq m --arrays=overwrite -i k8s.yaml -d "$documentIndex" /tmp/"$NAME"pool.yaml
7677
elif [[ "$KIND" == "DaemonSet" ]] || [[ "$KIND" == "Deployment" ]] || [[ "$KIND" == "StatefulSet" ]] || [[ "$KIND" == "Job" ]]; then
7778
LABEL="gitpod.io/workload_meta"
7879
echo "setting $LABEL for $NAME"
@@ -81,15 +82,15 @@ while [ "$i" -le "$DOCS" ]; do
8182
yq w -i /tmp/"$NAME"pool.yaml spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].key "$LABEL"
8283
yq w -i /tmp/"$NAME"pool.yaml spec.template.spec.affinity.nodeAffinity.requiredDuringSchedulingIgnoredDuringExecution.nodeSelectorTerms[0].matchExpressions[0].operator Exists
8384
# append it
84-
yq m --arrays=overwrite -i k8s.yaml -d "$i" /tmp/"$NAME"pool.yaml
85+
yq m --arrays=overwrite -i k8s.yaml -d "$documentIndex" /tmp/"$NAME"pool.yaml
8586
fi
8687

8788
# overrides for server-config
8889
if [[ "server-config" == "$NAME" ]] && [[ "$KIND" == "ConfigMap" ]]; then
8990
WORK="overrides for $NAME $KIND"
9091
echo "$WORK"
9192
touch /tmp/"$NAME"overrides.yaml
92-
yq r k8s.yaml -d "$i" data | yq prefix - data > /tmp/"$NAME"overrides.yaml
93+
yq r k8s.yaml -d "$documentIndex" data | yq prefix - data > /tmp/"$NAME"overrides.yaml
9394

9495
THEIA_BUCKET_NAME=$(yq r ./.werft/jobs/build/helm/values.dev.yaml components.server.theiaPluginsBucketNameOverride)
9596
THEIA_BUCKET_NAME_EXPR="s/\"theiaPluginsBucketNameOverride\": \"\"/\"theiaPluginsBucketNameOverride\": \"$THEIA_BUCKET_NAME\"/"
@@ -129,36 +130,36 @@ while [ "$i" -le "$DOCS" ]; do
129130
fi
130131

131132
# Merge the changes
132-
yq m -x -i k8s.yaml -d "$i" /tmp/"$NAME"overrides.yaml
133+
yq m -x -i k8s.yaml -d "$documentIndex" /tmp/"$NAME"overrides.yaml
133134
fi
134135

135136
# overrides for ws-manager-bridge configmap
136137
if [[ "ws-manager-bridge-config" == "$NAME" ]] && [[ "$KIND" == "ConfigMap" ]]; then
137138
WORK="overrides for $NAME $KIND"
138139
echo "$WORK"
139140
touch /tmp/"$NAME"overrides.yaml
140-
yq r k8s.yaml -d "$i" data | yq prefix - data > /tmp/"$NAME"overrides.yaml
141+
yq r k8s.yaml -d "$documentIndex" data | yq prefix - data > /tmp/"$NAME"overrides.yaml
141142

142143
# simliar to server, except the ConfigMap hierarchy, key, and value are different
143144
SHORT_NAME=$(yq r ./.werft/jobs/build/helm/values.dev.yaml installation.shortname)
144145
INSTALL_SHORT_NAME_EXPR="s/\"installation\": \"\"/\"installation\": \"$SHORT_NAME\"/"
145146
sed -i "$INSTALL_SHORT_NAME_EXPR" /tmp/"$NAME"overrides.yaml
146-
yq m -x -i k8s.yaml -d "$i" /tmp/"$NAME"overrides.yaml
147+
yq m -x -i k8s.yaml -d "$documentIndex" /tmp/"$NAME"overrides.yaml
147148
fi
148149

149150
# override details for Minio
150151
if [[ "minio" == "$NAME" ]] && [[ "$KIND" == "Deployment" ]]; then
151152
WORK="overrides for $NAME $KIND"
152153
echo "$WORK"
153-
yq -d "$i" w -i k8s.yaml spec.template.spec.serviceAccountName ws-daemon
154+
yq -d "$documentIndex" w -i k8s.yaml spec.template.spec.serviceAccountName ws-daemon
154155
fi
155156

156157
# overrides for ws-manager
157158
if [[ "ws-manager" == "$NAME" ]] && [[ "$KIND" == "ConfigMap" ]]; then
158159
WORK="overrides for $NAME $KIND"
159160
echo "$WORK"
160161
touch /tmp/"$NAME"overrides.yaml
161-
yq r k8s.yaml -d "$i" data | yq prefix - data > /tmp/"$NAME"overrides.yaml
162+
yq r k8s.yaml -d "$documentIndex" data | yq prefix - data > /tmp/"$NAME"overrides.yaml
162163

163164
SHORT_NAME=$(yq r ./.werft/jobs/build/helm/values.dev.yaml installation.shortname)
164165
STAGING_HOST_NAME=$(yq r ./.werft/jobs/build/helm/values.dev.yaml hostname)
@@ -192,15 +193,15 @@ while [ "$i" -le "$DOCS" ]; do
192193
yq w -i /tmp/"$NAME"-cm-overrides.yaml "data.[config.json]" -- "$(< /tmp/"$NAME"-cm-overrides.json)"
193194
yq m -x -i /tmp/"$NAME"overrides.yaml /tmp/"$NAME"-cm-overrides.yaml
194195

195-
yq m -x -i k8s.yaml -d "$i" /tmp/"$NAME"overrides.yaml
196+
yq m -x -i k8s.yaml -d "$documentIndex" /tmp/"$NAME"overrides.yaml
196197
fi
197198

198199
# overrides for ws-proxy
199200
if [[ "ws-proxy" == "$NAME" ]] && [[ "$KIND" == "ConfigMap" ]]; then
200201
WORK="overrides for $NAME $KIND"
201202
echo "$WORK"
202203
touch /tmp/"$NAME"overrides.yaml
203-
yq r k8s.yaml -d "$i" data | yq prefix - data > /tmp/"$NAME"overrides.yaml
204+
yq r k8s.yaml -d "$documentIndex" data | yq prefix - data > /tmp/"$NAME"overrides.yaml
204205

205206
# simliar to server, except the ConfigMap hierarchy, key, and value are different
206207
SHORT_NAME=$(yq r ./.werft/jobs/build/helm/values.dev.yaml installation.shortname)
@@ -215,7 +216,7 @@ while [ "$i" -le "$DOCS" ]; do
215216
# In this, we only do a find replace on a given line if we find workspaceHostSuffixRegex on the line
216217
sed -i -e "/workspaceHostSuffixRegex/s/$CURRENT_WS_SUFFIX_REGEX/$DEV_BRANCH\\\\\\\\.staging\\\\\\\\.gitpod-dev\\\\\\\\.com/g" /tmp/"$NAME"overrides.yaml
217218

218-
yq m -x -i k8s.yaml -d "$i" /tmp/"$NAME"overrides.yaml
219+
yq m -x -i k8s.yaml -d "$documentIndex" /tmp/"$NAME"overrides.yaml
219220
fi
220221

221222
if [[ "openvsx-proxy" == "$NAME" ]] && [[ "$KIND" == "StatefulSet" ]]; then
@@ -224,23 +225,23 @@ while [ "$i" -le "$DOCS" ]; do
224225
# We're being hit by this while trying to install Gitpod on GKE and k3s running different versions
225226
# where 'availableReplicas' is unkown in GKE while being required on k3s.
226227
# This workaround should be deleted when https://github.com/gitpod-io/gitpod/issues/8529 gets fixed.
227-
yq d -i k8s.yaml -d "$i" 'status'
228+
yq d -i k8s.yaml -d "$documentIndex" 'status'
228229
fi
229230

230231
if [[ ! -v WITH_VM ]] && [[ "ws-proxy" == "$NAME" ]] && [[ "$KIND" == "Service" ]]; then
231232
WORK="overrides for $NAME $KIND"
232233
echo "$WORK"
233-
yq w -i k8s.yaml -d "$i" "spec.ports[+].name" http-lb
234-
yq w -i k8s.yaml -d "$i" "spec.ports.(name==http-lb).port" 80
235-
yq w -i k8s.yaml -d "$i" "spec.ports.(name==http-lb).protocol" TCP
236-
yq w -i k8s.yaml -d "$i" "spec.ports.(name==http-lb).targetPort" 8080
237-
238-
yq w -i k8s.yaml -d "$i" "spec.ports[+].name" https-lb
239-
yq w -i k8s.yaml -d "$i" "spec.ports.(name==https-lb).port" 443
240-
yq w -i k8s.yaml -d "$i" "spec.ports.(name==https-lb).protocol" TCP
241-
yq w -i k8s.yaml -d "$i" "spec.ports.(name==https-lb).targetPort" 9090
242-
yq w -i k8s.yaml -d "$i" "metadata.annotations[cloud.google.com/neg]" '{"exposed_ports": {"22":{},"80":{},"443":{}}}'
243-
yq w -i k8s.yaml -d "$i" spec.type LoadBalancer
234+
yq w -i k8s.yaml -d "$documentIndex" "spec.ports[+].name" http-lb
235+
yq w -i k8s.yaml -d "$documentIndex" "spec.ports.(name==http-lb).port" 80
236+
yq w -i k8s.yaml -d "$documentIndex" "spec.ports.(name==http-lb).protocol" TCP
237+
yq w -i k8s.yaml -d "$documentIndex" "spec.ports.(name==http-lb).targetPort" 8080
238+
239+
yq w -i k8s.yaml -d "$documentIndex" "spec.ports[+].name" https-lb
240+
yq w -i k8s.yaml -d "$documentIndex" "spec.ports.(name==https-lb).port" 443
241+
yq w -i k8s.yaml -d "$documentIndex" "spec.ports.(name==https-lb).protocol" TCP
242+
yq w -i k8s.yaml -d "$documentIndex" "spec.ports.(name==https-lb).targetPort" 9090
243+
yq w -i k8s.yaml -d "$documentIndex" "metadata.annotations[cloud.google.com/neg]" '{"exposed_ports": {"22":{},"80":{},"443":{}}}'
244+
yq w -i k8s.yaml -d "$documentIndex" spec.type LoadBalancer
244245
fi
245246

246247
# update workspace-templates configmap to set affinity for workspace, ghosts, image builders, etc.
@@ -251,7 +252,7 @@ while [ "$i" -le "$DOCS" ]; do
251252
touch /tmp/"$NAME"overrides.yaml
252253

253254
# get the data to modify
254-
yq r k8s.yaml -d "$i" 'data.[default.yaml]' > /tmp/"$NAME"overrides.yaml
255+
yq r k8s.yaml -d "$documentIndex" 'data.[default.yaml]' > /tmp/"$NAME"overrides.yaml
255256

256257
# add the proper affinity
257258
LABEL="gitpod.io/workspace_$NODE_POOL_INDEX"
@@ -261,29 +262,29 @@ while [ "$i" -le "$DOCS" ]; do
261262
yq w -i /tmp/"$NAME"overrides.yaml "spec.containers.(name==workspace).env[+].name" GITPOD_PREVENT_METADATA_ACCESS
262263
yq w -i /tmp/"$NAME"overrides.yaml "spec.containers.(name==workspace).env.(name==GITPOD_PREVENT_METADATA_ACCESS).value" "true"
263264

264-
yq w -i k8s.yaml -d "$i" "data.[default.yaml]" -- "$(< /tmp/"$NAME"overrides.yaml)"
265+
yq w -i k8s.yaml -d "$documentIndex" "data.[default.yaml]" -- "$(< /tmp/"$NAME"overrides.yaml)"
265266
fi
266267

267268
# NetworkPolicy for ws-daemon
268269
if [[ "ws-daemon" == "$NAME" ]] && [[ "$KIND" == "NetworkPolicy" ]]; then
269270
WORK="overrides for $NAME $KIND"
270271
echo "$WORK"
271-
yq w -i k8s.yaml -d "$i" spec.ingress[0].ports[0].port "$WS_DAEMON_PORT"
272+
yq w -i k8s.yaml -d "$documentIndex" spec.ingress[0].ports[0].port "$WS_DAEMON_PORT"
272273
fi
273274

274275
# NetworkPolicy for workspace-default
275276
if [[ "workspace-default" == "$NAME" ]] && [[ "$KIND" == "NetworkPolicy" ]]; then
276277
WORK="overrides for $NAME $KIND"
277278
echo "$WORK"
278-
yq w -i k8s.yaml -d "$i" spec.egress[0].to[0].ipBlock.except[0] 169.254.169.254/30
279+
yq w -i k8s.yaml -d "$documentIndex" spec.egress[0].to[0].ipBlock.except[0] 169.254.169.254/30
279280
fi
280281

281282
# host ws-daemon on $WS_DAEMON_PORT
282283
if [[ "ws-daemon" == "$NAME" ]] && [[ "$KIND" == "ConfigMap" ]]; then
283284
WORK="overrides for $NAME $KIND"
284285
echo "$WORK"
285286
# Get a copy of the config we're working with
286-
yq r k8s.yaml -d "$i" > /tmp/"$NAME"-"$KIND"-overrides.yaml
287+
yq r k8s.yaml -d "$documentIndex" > /tmp/"$NAME"-"$KIND"-overrides.yaml
287288
# Parse and update the JSON, and write it to a file
288289
yq r /tmp/"$NAME"-"$KIND"-overrides.yaml 'data.[config.json]' \
289290
| jq ".service.address = $WS_DAEMON_PORT" > /tmp/"$NAME"-"$KIND"-overrides.json
@@ -297,14 +298,14 @@ while [ "$i" -le "$DOCS" ]; do
297298
# merge the updated data object with existing config
298299
yq m -x -i /tmp/"$NAME"-"$KIND"-overrides.yaml /tmp/"$NAME"-"$KIND"-data-overrides.yaml
299300
# merge the updated config map with k8s.yaml
300-
yq m -x -i k8s.yaml -d "$i" /tmp/"$NAME"-"$KIND"-overrides.yaml
301+
yq m -x -i k8s.yaml -d "$documentIndex" /tmp/"$NAME"-"$KIND"-overrides.yaml
301302
fi
302303

303304
# suspend telemetry cron job
304305
if [[ "gitpod-telemetry" == "$NAME" ]] && [[ "$KIND" == "CronJob" ]]; then
305306
WORK="suspend $NAME $KIND"
306307
echo "$WORK"
307-
yq w -i k8s.yaml -d "$i" spec.suspend "true"
308+
yq w -i k8s.yaml -d "$documentIndex" spec.suspend "true"
308309
fi
309310

310311
# Uncomment to change or remove resources from the configmap which can be used to uninstall Gitpod
@@ -315,7 +316,7 @@ while [ "$i" -le "$DOCS" ]; do
315316
# WORK="overrides for $NAME $KIND"
316317
# echo "$WORK"
317318
# # Get a copy of the config we're working with
318-
# yq r k8s.yaml -d "$i" > /tmp/"$NAME"-"$KIND".yaml
319+
# yq r k8s.yaml -d "$documentIndex" > /tmp/"$NAME"-"$KIND".yaml
319320
# # Parse the YAML string from the config map
320321
# yq r /tmp/"$NAME"-"$KIND".yaml 'data.[app.yaml]' > /tmp/"$NAME"-"$KIND"-original.yaml
321322
# # Loop through the config YAML docs
@@ -355,13 +356,13 @@ while [ "$i" -le "$DOCS" ]; do
355356
# # merge overrides into base
356357
# yq w -i /tmp/"$NAME"-"$KIND".yaml "data.[app.yaml]" -- "$(< /tmp/"$NAME"-"$KIND"-overrides.yaml)"
357358
# # merge base into k8s.yaml
358-
# yq m -x -i -d "$i" k8s.yaml /tmp/"$NAME"-"$KIND".yaml
359+
# yq m -x -i -d "$documentIndex" k8s.yaml /tmp/"$NAME"-"$KIND".yaml
359360
# fi
360361

361362
# TODO: integrate with chargebees
362363
# won't fix now, use Helm
363364

364-
i=$((i + 1))
365+
documentIndex=$((documentIndex + 1))
365366
done
366367

367368
exit

0 commit comments

Comments
 (0)