Skip to content

Commit 552a8a9

Browse files
committed
automate Helm chart CRD sync from controller-gen output
Signed-off-by: kahirokunn <okinakahiro@gmail.com>
1 parent cf30b44 commit 552a8a9

7 files changed

Lines changed: 7425 additions & 4905 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,5 @@ Session.vim
8080
.history
8181
# End of https://www.gitignore.io/api/go,vim,emacs,visualstudiocode
8282

83-
charts/
83+
/charts/
8484
.tmp/

config/charts/knative-operator/templates/crds/knativeeventings.yaml

Lines changed: 3442 additions & 0 deletions
Large diffs are not rendered by default.

config/charts/knative-operator/templates/crds/knativeservings.yaml

Lines changed: 3888 additions & 0 deletions
Large diffs are not rendered by default.

config/charts/knative-operator/templates/operator.yaml

Lines changed: 0 additions & 4903 deletions
Large diffs are not rendered by default.

hack/sync-helm-crds.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2025 The Knative Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Syncs CRD definitions from config/crd/bases/ to the Helm chart templates.
18+
# This script transforms controller-gen output into Helm-ready CRD templates
19+
# by adding Helm template variables for labels and conversion webhook configuration.
20+
#
21+
# Usage: hack/sync-helm-crds.sh
22+
23+
set -o errexit
24+
set -o nounset
25+
set -o pipefail
26+
27+
REPO_ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
28+
29+
HELM_CRD_DIR="${REPO_ROOT_DIR}/config/charts/knative-operator/templates/crds"
30+
CRD_BASES_DIR="${REPO_ROOT_DIR}/config/crd/bases"
31+
32+
# Create the output directory
33+
mkdir -p "${HELM_CRD_DIR}"
34+
35+
# Install yq if not already available
36+
if ! command -v yq &> /dev/null; then
37+
echo "Installing mikefarah/yq..."
38+
GOFLAGS=-mod=mod go install github.com/mikefarah/yq/v4@v4.52.5
39+
export PATH="$(go env GOPATH)/bin:$PATH"
40+
fi
41+
42+
echo "Syncing CRDs from ${CRD_BASES_DIR} to ${HELM_CRD_DIR}"
43+
44+
for crd_file in "${CRD_BASES_DIR}"/*.yaml; do
45+
filename=$(basename "${crd_file}")
46+
# Remove "operator.knative.dev_" prefix for cleaner filenames
47+
short_name="${filename#operator.knative.dev_}"
48+
target="${HELM_CRD_DIR}/${short_name}"
49+
50+
echo " Processing ${filename} -> crds/${short_name}"
51+
52+
# Use yq to add Helm labels and conversion webhook to the CRD.
53+
# The source CRD from controller-gen is pure YAML, so yq can safely process it.
54+
# Helm template variables like "{{ .Chart.Version }}" are valid YAML strings.
55+
# The -c flag preserves compact sequence indentation (e.g., "- name:" stays at the same level).
56+
if ! yq eval -c '
57+
.metadata.labels."app.kubernetes.io/version" = "{{ .Chart.Version }}" |
58+
.metadata.labels."app.kubernetes.io/name" = "knative-operator" |
59+
.spec.conversion = {
60+
"strategy": "Webhook",
61+
"webhook": {
62+
"conversionReviewVersions": ["v1beta1"],
63+
"clientConfig": {
64+
"service": {
65+
"name": "operator-webhook",
66+
"namespace": "{{ .Release.Namespace }}",
67+
"path": "/resource-conversion"
68+
}
69+
}
70+
}
71+
}
72+
' "${crd_file}" > "${target}"; then
73+
echo "ERROR: yq failed to process ${filename}"
74+
rm -f "${target}"
75+
exit 1
76+
fi
77+
78+
# Prepend auto-generation header
79+
tmp="${target}.tmp"
80+
echo "# Code generated by hack/sync-helm-crds.sh; DO NOT EDIT." > "${tmp}"
81+
cat "${target}" >> "${tmp}"
82+
mv "${tmp}" "${target}"
83+
done
84+
85+
echo "Done. CRDs synced to ${HELM_CRD_DIR}"

hack/update-codegen.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ GOFLAGS=-mod=mod controller-gen \
6666
paths="${REPO_ROOT_DIR}/pkg/apis/..." \
6767
output:crd:dir="${REPO_ROOT_DIR}/config/crd/bases"
6868

69+
group "Sync CRDs to Helm chart"
70+
71+
"${REPO_ROOT_DIR}/hack/sync-helm-crds.sh"
72+
6973
group "Update deps post-codegen"
7074

7175
# Make sure our dependencies are up-to-date

hack/verify-codegen.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ trap "cleanup" EXIT SIGINT
3131
cleanup
3232

3333
# Save working tree state
34-
mkdir -p "${TMP_DIFFROOT}/pkg"
34+
mkdir -p "${TMP_DIFFROOT}/pkg" "${TMP_DIFFROOT}/config"
3535
cp -aR "${REPO_ROOT_DIR}/go.sum" "${REPO_ROOT_DIR}/pkg" "${REPO_ROOT_DIR}/vendor" "${TMP_DIFFROOT}"
36+
cp -aR "${REPO_ROOT_DIR}/config/charts" "${TMP_DIFFROOT}/config/"
3637

3738
# TODO(mattmoor): We should be able to rm -rf pkg/client/ and vendor/
3839

@@ -41,8 +42,11 @@ echo "Diffing ${REPO_ROOT_DIR} against freshly generated codegen"
4142
ret=0
4243
diff -Nupr --no-dereference "${REPO_ROOT_DIR}/pkg" "${TMP_DIFFROOT}/pkg" || ret=1
4344
diff -Nupr --no-dereference "${REPO_ROOT_DIR}/vendor" "${TMP_DIFFROOT}/vendor" || ret=1
45+
diff -Nupr --no-dereference "${REPO_ROOT_DIR}/config/charts" "${TMP_DIFFROOT}/config/charts" || ret=1
4446

4547
# Restore working tree state
48+
rm -fr "${REPO_ROOT_DIR}/config/charts"
49+
cp -aR "${TMP_DIFFROOT}/config/charts" "${REPO_ROOT_DIR}/config/"
4650
rm -fr "${TMP_DIFFROOT}/config"
4751
rm -fr "${REPO_ROOT_DIR}/go.sum" "${REPO_ROOT_DIR}/pkg" "${REPO_ROOT_DIR}/vendor"
4852
cp -aR "${TMP_DIFFROOT}"/* "${REPO_ROOT_DIR}"

0 commit comments

Comments
 (0)