1
0
mirror of https://github.com/keycloak/keycloak.git synced 2026-10-23 19:29:23 +03:00
Files
keycloak/operator/scripts/post-process-helm-chart.sh
Steven Hawkins dce893ba38 fix: renaming the helm chart and adding a release note (#52146)
* fix: renaming the helm chart and adding a release note

closes: #52089

Signed-off-by: Steve Hawkins <shawkins@redhat.com>

* Refactor helm chart post-processing script

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Steven Hawkins <shawkins@redhat.com>

---------

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Signed-off-by: Steven Hawkins <shawkins@redhat.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
2026-09-03 15:58:18 +02:00

82 lines
3.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Post-processes the chart emitted by quarkus-helm so it covers things the
# extension cannot express today.
#
# CRDs: quarkus-operator-sdk-helm adds them to the chart's crds/ directory
# during the build, which Helm installs unconditionally. Move
# them into templates/ wrapped in a {{ if .Values.crds.enabled }} guard so
# users can manage CRDs out-of-band.
#
# RBAC subject namespaces: quarkus-helm populates empty ServiceAccount subject
# namespaces with {{ .Release.Namespace }}, but the ClusterRoleBinding
# hardcodes "keycloak" (the build-time namespace), which is wrong for a Helm
# release installed into an arbitrary namespace. Rewrite every ServiceAccount
# subject in ClusterRoleBindings to the release namespace.
set -euo pipefail
CHART_DIR="${1:?usage: post-process-helm-chart.sh <chart-dir>}"
CRDS_DIR="${CHART_DIR}/crds"
TEMPLATES_CRDS_DIR="${CHART_DIR}/templates/crds"
VALUES_FILE="${CHART_DIR}/values.yaml"
TEMPLATES_DIR="${CHART_DIR}/templates"
if [[ -d "${CRDS_DIR}" ]]; then
mkdir -p "${TEMPLATES_CRDS_DIR}"
shopt -s nullglob
for crd in "${CRDS_DIR}"/*.yml "${CRDS_DIR}"/*.yaml; do
dest="${TEMPLATES_CRDS_DIR}/$(basename "${crd}")"
{
echo '{{- if .Values.crds.enabled }}'
cat "${crd}"
echo '{{- end }}'
} > "${dest}"
rm "${crd}"
done
rmdir "${CRDS_DIR}" 2>/dev/null || true
echo "post-process-helm-chart: wrapped CRDs into ${TEMPLATES_CRDS_DIR}"
fi
if ! grep -q '^crds:' "${VALUES_FILE}" 2>/dev/null; then
printf '\ncrds:\n enabled: true\n' >> "${VALUES_FILE}"
fi
# Set every ServiceAccount subject's namespace to the release namespace. For
# each subject, replace an existing namespace line or inject one right after the
# "name:" line, preserving indentation.
for binding in "${TEMPLATES_DIR}/clusterrolebinding.yaml"; do
[[ -f "${binding}" ]] || continue
tmp="${binding}.tmp"
awk '
function flush_pending() {
if (pending) { print name_line; print indent "namespace: {{ .Release.Namespace }}"; pending=0 }
}
{
if (pending) {
if ($0 ~ /^[[:space:]]*namespace:[[:space:]]/) {
match($0, /^[[:space:]]*/); ind = substr($0, 1, RLENGTH)
print name_line; print ind "namespace: {{ .Release.Namespace }}"; pending=0; next
} else { flush_pending() }
}
if ($0 ~ /kind:[[:space:]]*ServiceAccount[[:space:]]*$/) { sa=1; print; next }
if (sa && $0 ~ /^[[:space:]]*name:[[:space:]]/) {
match($0, /^[[:space:]]*/); indent = substr($0, 1, RLENGTH)
name_line = $0; pending=1; sa=0; next
}
print
}
END { flush_pending() }
' "${binding}" > "${tmp}"
mv "${tmp}" "${binding}"
echo "post-process-helm-chart: set ServiceAccount subject namespaces in $(basename "${binding}")"
done
# Replace {{ $.Chart.Name }} with the fixed operator name across all templates
for tmpl in "${TEMPLATES_DIR}"/*.yaml "${TEMPLATES_DIR}"/*.yml; do
[[ -f "${tmpl}" ]] || continue
tmp="${tmpl}.tmp"
sed -e 's/{{ \$\.Chart\.Name }}/keycloak-operator/g' \
-e 's/{{ \.Chart\.Name }}/keycloak-operator/g' "${tmpl}" > "${tmp}"
mv "${tmp}" "${tmpl}"
done
echo "post-process-helm-chart: replaced Chart.Name references in templates"