|
| 1 | +#!/bin/bash |
| 2 | +# Post-processing script for controller-gen output. |
| 3 | +# |
| 4 | +# This script: |
| 5 | +# 1. Renames CRD files generated by controller-gen from the default |
| 6 | +# "policies.kubewarden.io_RESOURCE.yaml" format to "RESOURCE.yaml", |
| 7 | +# as expected by the charts/kubewarden-crds Helm chart. |
| 8 | +# 2. Injects Helm template labels and annotations into each resource in |
| 9 | +# controller-rbac-roles.yaml to match the rest of the chart templates. |
| 10 | +# |
| 11 | +# Usage: called automatically from "make manifests" |
| 12 | + |
| 13 | +set -euo pipefail |
| 14 | + |
| 15 | +REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 16 | +CRD_CHART_DIR="${REPO_ROOT}/charts/kubewarden-crds/templates" |
| 17 | +RBAC_FILE="${REPO_ROOT}/charts/kubewarden-controller/templates/controller-rbac-roles.yaml" |
| 18 | + |
| 19 | +# ── 1. Rename CRD files ────────────────────────────────────────────────────── |
| 20 | +echo "Renaming CRD files in ${CRD_CHART_DIR}..." |
| 21 | +for src in "${CRD_CHART_DIR}"/policies.kubewarden.io_*.yaml; do |
| 22 | + [[ -e "${src}" ]] || continue |
| 23 | + filename="$(basename "${src}")" |
| 24 | + dest="${CRD_CHART_DIR}/${filename#policies.kubewarden.io_}" |
| 25 | + mv "${src}" "${dest}" |
| 26 | + echo " ${filename} -> $(basename "${dest}")" |
| 27 | +done |
| 28 | + |
| 29 | +# ── 2. Inject Helm labels/annotations into controller-rbac-roles.yaml ──────── |
| 30 | +# Guard: skip injection if labels are already present (idempotency) |
| 31 | +if grep -q 'kubewarden-controller.labels' "${RBAC_FILE}"; then |
| 32 | + echo "Labels already present in ${RBAC_FILE}, skipping injection." |
| 33 | +else |
| 34 | +echo "Injecting Helm labels and annotations into ${RBAC_FILE}..." |
| 35 | + |
| 36 | +# Use awk to insert labels/annotations after each " name: ..." line that |
| 37 | +# appears inside a metadata block. We detect a metadata block by looking for |
| 38 | +# the line "^metadata:" and then find the " name:" line within it. |
| 39 | +# If a " namespace:" line follows name, it is emitted before labels. |
| 40 | +awk ' |
| 41 | +/^metadata:$/ { in_metadata = 1 } |
| 42 | +!in_metadata { print; next } |
| 43 | +/^ name:/ { |
| 44 | + name_line = $0 |
| 45 | + in_name = 1 |
| 46 | + next |
| 47 | +} |
| 48 | +in_name && /^ namespace:/ { |
| 49 | + print name_line |
| 50 | + print |
| 51 | + print " labels:" |
| 52 | + print " {{- include \"kubewarden-controller.labels\" . | nindent 4 }}" |
| 53 | + print " annotations:" |
| 54 | + print " {{- include \"kubewarden-controller.annotations\" . | nindent 4 }}" |
| 55 | + in_metadata = 0 |
| 56 | + in_name = 0 |
| 57 | + next |
| 58 | +} |
| 59 | +in_name { |
| 60 | + print name_line |
| 61 | + print " labels:" |
| 62 | + print " {{- include \"kubewarden-controller.labels\" . | nindent 4 }}" |
| 63 | + print " annotations:" |
| 64 | + print " {{- include \"kubewarden-controller.annotations\" . | nindent 4 }}" |
| 65 | + in_metadata = 0 |
| 66 | + in_name = 0 |
| 67 | + print |
| 68 | + next |
| 69 | +} |
| 70 | +{ print } |
| 71 | +' "${RBAC_FILE}" > "${RBAC_FILE}.tmp" |
| 72 | + |
| 73 | +mv "${RBAC_FILE}.tmp" "${RBAC_FILE}" |
| 74 | + |
| 75 | +# ── 3. Replace hardcoded namespace with Helm template expression ───────────── |
| 76 | +echo "Replacing hardcoded namespace in ${RBAC_FILE}..." |
| 77 | +sed -i 's/ namespace: kubewarden/ namespace: {{ .Release.Namespace }}/' "${RBAC_FILE}" |
| 78 | +echo " Done." |
| 79 | +fi |
0 commit comments