Skip to content

Commit a3e4027

Browse files
committed
chore(charts): automate CRD and RBAC manifest post-processing
Add scripts/post-generate-manifests.sh to post-process controller-gen output. It renames CRD files from the default policies.kubewarden.io_RESOURCE.yaml format to RESOURCE.yaml directly into charts/kubewarden-crds/templates/. Also it injects Helm template labels and annotations into each resource in controller-rbac-roles.yaml to match the conventions used by the rest of the chart. Update the Makefile manifests target to output CRDs directly into charts/kubewarden-crds/templates/ and invoke the post-processing script automatically. Signed-off-by: José Guilherme Vanz <jguilhermevanz@suse.com>
1 parent d9912cc commit a3e4027

8 files changed

Lines changed: 153 additions & 9 deletions

File tree

Makefile

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ generate-controller: manifests ## Generate code containing DeepCopy, DeepCopyIn
145145
$(GO_BUILD_ENV) $(CONTROLLER_GEN) object paths="./api/policies/v1"
146146

147147
.PHONY: manifests
148-
manifests: ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. We use yq to modify the generated files to match our naming and labels conventions.
149-
$(GO_BUILD_ENV) $(CONTROLLER_GEN) rbac:roleName=controller-role,fileName=controller-rbac-roles.yaml,roleName=kubewarden-controller-manager crd webhook \
148+
manifests: ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
149+
$(GO_BUILD_ENV) $(CONTROLLER_GEN) rbac:roleName=kubewarden-controller-manager,fileName=controller-rbac-roles.yaml crd webhook \
150150
paths="./api/policies/v1" paths="./internal/controller" paths="./cmd/controller" \
151-
output:crd:artifacts:config=config/crd/bases \
152-
output:rbac:artifacts:config=charts/kubewarden-controller/templates \
151+
output:crd:artifacts:config=charts/kubewarden-crds/templates \
152+
output:rbac:artifacts:config=charts/kubewarden-controller/templates
153+
./scripts/post-generate-manifests.sh
153154

154155
.PHONY: generate-chart
155156
generate-chart: ## Generate Helm chart values schema.

charts/kubewarden-controller/templates/NOTES.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@ You can now start defining admission policies by using the cluster-wide
44

55
For more information check out https://docs.kubewarden.io.
66

7+
{{- if (lookup "rbac.authorization.k8s.io/v1" "ClusterRole" "" "kubewarden-controller-manager-cluster-role") }}
8+
⚠️ **Upgrade notice: RBAC resource consolidation**
9+
10+
Starting with this release, all controller RBAC permissions have been
11+
consolidated into a single ClusterRole and a single Role, simplifying the
12+
permission model. The following resources from previous versions are no longer
13+
used and should be manually deleted after a successful upgrade:
14+
15+
kubectl delete clusterrole kubewarden-controller-manager-cluster-role
16+
kubectl delete clusterrole kubewarden-controller-proxy-role
17+
kubectl delete clusterrolebinding kubewarden-controller-manager-cluster-role
18+
kubectl delete clusterrolebinding kubewarden-controller-proxy-rolebinding
19+
kubectl delete rolebinding kubewarden-controller-leader-election-rolebinding -n {{ .Release.Namespace }}
20+
kubectl delete rolebinding kubewarden-controller-manager-namespaced-rolebinding -n {{ .Release.Namespace }}
21+
kubectl delete role kubewarden-controller-leader-election-role -n {{ .Release.Namespace }}
22+
kubectl delete role kubewarden-controller-manager-namespaced-role -n {{ .Release.Namespace }}
23+
{{- end }}
24+
725

826
🚀 **Kubewarden is applying to become a CNCF incubation project!** 🚀
927

charts/kubewarden-controller/templates/controller-rbac-extras.yaml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1+
# This ClusterRole grants read access to the /metrics endpoint.
2+
# It is not generated by controller-gen because nonResourceURLs cannot be
3+
# expressed via kubebuilder RBAC markers in a way that keeps it in a separate
4+
# ClusterRole. It is maintained manually to preserve a dedicated role for
5+
# metrics scraping (e.g., by Prometheus).
6+
apiVersion: rbac.authorization.k8s.io/v1
7+
kind: ClusterRole
8+
metadata:
9+
name: kubewarden-controller-manager-metrics-reader
10+
labels:
11+
{{- include "kubewarden-controller.labels" . | nindent 4 }}
12+
annotations:
13+
{{- include "kubewarden-controller.annotations" . | nindent 4 }}
14+
rules:
15+
- nonResourceURLs:
16+
- /metrics
17+
verbs:
18+
- get
19+
---
20+
apiVersion: rbac.authorization.k8s.io/v1
21+
kind: ClusterRoleBinding
22+
metadata:
23+
name: kubewarden-controller-metrics-reader
24+
labels:
25+
{{- include "kubewarden-controller.labels" . | nindent 4 }}
26+
annotations:
27+
{{- include "kubewarden-controller.annotations" . | nindent 4 }}
28+
roleRef:
29+
apiGroup: rbac.authorization.k8s.io
30+
kind: ClusterRole
31+
name: kubewarden-controller-manager-metrics-reader
32+
subjects:
33+
- kind: ServiceAccount
34+
name: {{ include "kubewarden-controller.serviceAccountName" . }}
35+
namespace: {{ .Release.Namespace }}
36+
---
137
apiVersion: rbac.authorization.k8s.io/v1
238
kind: RoleBinding
339
metadata:
@@ -19,7 +55,7 @@ subjects:
1955
apiVersion: rbac.authorization.k8s.io/v1
2056
kind: ClusterRoleBinding
2157
metadata:
22-
name: kubewarden-controller-manager-cluster-role
58+
name: kubewarden-controller-manager
2359
labels:
2460
{{- include "kubewarden-controller.labels" . | nindent 4 }}
2561
annotations:

charts/kubewarden-controller/templates/controller-rbac-roles.yaml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ apiVersion: rbac.authorization.k8s.io/v1
33
kind: ClusterRole
44
metadata:
55
name: kubewarden-controller-manager
6+
labels:
7+
{{- include "kubewarden-controller.labels" . | nindent 4 }}
8+
annotations:
9+
{{- include "kubewarden-controller.annotations" . | nindent 4 }}
610
rules:
711
- apiGroups:
812
- admissionregistration.k8s.io
@@ -70,12 +74,15 @@ apiVersion: rbac.authorization.k8s.io/v1
7074
kind: Role
7175
metadata:
7276
name: kubewarden-controller-manager
73-
namespace: kubewarden
77+
namespace: {{ .Release.Namespace }}
78+
labels:
79+
{{- include "kubewarden-controller.labels" . | nindent 4 }}
80+
annotations:
81+
{{- include "kubewarden-controller.annotations" . | nindent 4 }}
7482
rules:
7583
- apiGroups:
7684
- ""
7785
resources:
78-
- configmap
7986
- configmaps
8087
- secrets
8188
- services
@@ -90,7 +97,7 @@ rules:
9097
- apiGroups:
9198
- ""
9299
resources:
93-
- event
100+
- events
94101
verbs:
95102
- create
96103
- patch

charts/kubewarden-crds/templates/admissionpolicies.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
apiVersion: apiextensions.k8s.io/v1
23
kind: CustomResourceDefinition
34
metadata:

charts/kubewarden-crds/templates/admissionpolicygroups.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
apiVersion: apiextensions.k8s.io/v1
23
kind: CustomResourceDefinition
34
metadata:

charts/kubewarden-crds/templates/policyservers.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
---
12
apiVersion: apiextensions.k8s.io/v1
23
kind: CustomResourceDefinition
34
metadata:
@@ -1641,7 +1642,7 @@ spec:
16411642
Name of SigstoreTrustConfig configmap in the kubewarden namespace (same
16421643
namespace as the controller deployment), containing Sigstore trust
16431644
configuration (ClientTrustConfig JSON). The configuration must be under a
1644-
key named sigstore-trust-config in the Configmap. This is used to configure
1645+
key named sigstore-trust-config in the ConfigMap. This is used to configure
16451646
a custom Sigstore instance instead of the default public Sigstore infrastructure.
16461647
WARNING: This feature requires strict access control. Users with write access
16471648
to this ConfigMap can influence policy signature verification.

scripts/post-generate-manifests.sh

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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

Comments
 (0)