Skip to content

Commit e31e6f8

Browse files
committed
simpler bash
1 parent 80af42e commit e31e6f8

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

pipelines/lib/util.groovy

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ def saveCache(
446446

447447
def labelPod(){
448448
if (env.NODE_NAME && (env.NODE_NAME =~ /(manager|snowflake)/)) {
449-
echo "Skipping pod label: ${env.NODE_NAME} is a static/snowflake node."
449+
echo "Skipping pod label: ${env.NODE_NAME} is a static manager/snowflake node."
450450
return
451451
}
452452

@@ -470,37 +470,35 @@ def labelPod(){
470470

471471
def jsonLabels = groovy.json.JsonOutput.toJson([metadata: [labels: labels]])
472472
echo "jsonLabels: ${jsonLabels}"
473+
writeFile file: '/tmp/patch.json', text: jsonLabels
473474

474-
bash """
475+
bash '''
475476
set -eu
476477
set +x
477478
478479
SA=/var/run/secrets/kubernetes.io/serviceaccount
479-
[ -r "\$SA/token" ] || { echo "not in k8s; skipping pod label"; exit 0; }
480-
NS=\$(cat \$SA/namespace)
481-
POD=\${HOSTNAME}
482-
TOKEN=\$(cat \$SA/token)
483-
CA=\$SA/ca.crt
484-
485-
cat <<EOF > /tmp/patch.json
486-
${jsonLabels}
487-
EOF
488-
489-
if curl -sS --fail \\
490-
--cacert "\$CA" \\
491-
-H "Authorization: Bearer \$TOKEN" \\
492-
-H "Content-Type: application/merge-patch+json" \\
493-
-X PATCH \\
494-
"https://kubernetes.default.svc/api/v1/namespaces/\$NS/pods/\$POD" \\
480+
[ -r "$SA/token" ] || { echo "not in k8s; skipping pod label"; exit 0; }
481+
NS=$(cat $SA/namespace)
482+
POD=${HOSTNAME}
483+
TOKEN=$(cat $SA/token)
484+
CA=$SA/ca.crt
485+
486+
487+
if curl -sS --fail \
488+
--cacert "$CA" \
489+
-H "Authorization: Bearer $TOKEN" \
490+
-H "Content-Type: application/merge-patch+json" \
491+
-X PATCH \
492+
"https://kubernetes.default.svc/api/v1/namespaces/\$NS/pods/\$POD" \
495493
--data-binary @/tmp/patch.json >/dev/null
496494
then
497-
echo "labeled for pod \$NS/\$POD"
495+
echo "labeled for pod $NS/$POD"
498496
else
499-
echo "pod label skipped for \$NS/\$POD"
497+
echo "pod label skipped for $NS/$POD"
500498
exit 0
501499
fi
502500
set -x
503-
"""
501+
'''
504502

505503
}
506504

0 commit comments

Comments
 (0)