Skip to content

Commit 308a9d0

Browse files
Restore operator CRD conversion webhooks (#2294)
Signed-off-by: kahirokunn <okinakahiro@gmail.com> Co-authored-by: kahirokunn <okinakahiro@gmail.com>
1 parent 2f25197 commit 308a9d0

8 files changed

Lines changed: 152 additions & 37 deletions

File tree

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ spec:
3131
plural: knativeeventings
3232
singular: knativeeventing
3333
scope: Namespaced
34+
conversion:
35+
strategy: Webhook
36+
webhook:
37+
conversionReviewVersions: ["v1beta1"]
38+
clientConfig:
39+
service:
40+
name: operator-webhook
41+
namespace: '{{ .Release.Namespace }}'
42+
path: /resource-conversion
3443
versions:
3544
- additionalPrinterColumns:
3645
- jsonPath: .spec.clusterProfileRef.name
@@ -3596,13 +3605,3 @@ spec:
35963605
storage: true
35973606
subresources:
35983607
status: {}
3599-
conversion:
3600-
strategy: Webhook
3601-
webhook:
3602-
conversionReviewVersions:
3603-
- v1beta1
3604-
clientConfig:
3605-
service:
3606-
name: operator-webhook
3607-
namespace: '{{ .Release.Namespace }}'
3608-
path: /resource-conversion

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ spec:
3131
plural: knativeservings
3232
singular: knativeserving
3333
scope: Namespaced
34+
conversion:
35+
strategy: Webhook
36+
webhook:
37+
conversionReviewVersions: ["v1beta1"]
38+
clientConfig:
39+
service:
40+
name: operator-webhook
41+
namespace: '{{ .Release.Namespace }}'
42+
path: /resource-conversion
3443
versions:
3544
- additionalPrinterColumns:
3645
- jsonPath: .spec.clusterProfileRef.name
@@ -3867,13 +3876,3 @@ spec:
38673876
storage: true
38683877
subresources:
38693878
status: {}
3870-
conversion:
3871-
strategy: Webhook
3872-
webhook:
3873-
conversionReviewVersions:
3874-
- v1beta1
3875-
clientConfig:
3876-
service:
3877-
name: operator-webhook
3878-
namespace: '{{ .Release.Namespace }}'
3879-
path: /resource-conversion

config/crd/bases/operator.knative.dev_knativeeventings.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ spec:
2727
plural: knativeeventings
2828
singular: knativeeventing
2929
scope: Namespaced
30+
conversion:
31+
strategy: Webhook
32+
webhook:
33+
conversionReviewVersions: ["v1beta1"]
34+
clientConfig:
35+
service:
36+
name: operator-webhook
37+
namespace: knative-operator
38+
path: /resource-conversion
3039
versions:
3140
- additionalPrinterColumns:
3241
- jsonPath: .spec.clusterProfileRef.name

config/crd/bases/operator.knative.dev_knativeservings.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ spec:
2727
plural: knativeservings
2828
singular: knativeserving
2929
scope: Namespaced
30+
conversion:
31+
strategy: Webhook
32+
webhook:
33+
conversionReviewVersions: ["v1beta1"]
34+
clientConfig:
35+
service:
36+
name: operator-webhook
37+
namespace: knative-operator
38+
path: /resource-conversion
3039
versions:
3140
- additionalPrinterColumns:
3241
- jsonPath: .spec.clusterProfileRef.name

hack/sync-helm-crds.sh

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
# limitations under the License.
1616

1717
# 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.
18+
# This script transforms generated CRD bases into Helm-ready CRD templates by
19+
# adding Helm template variables for labels and the conversion webhook namespace.
2020
#
2121
# Usage: hack/sync-helm-crds.sh
2222

@@ -45,23 +45,16 @@ for crd_file in "${CRD_BASES_DIR}"/*.yaml; do
4545

4646
echo " Processing ${filename} -> crds/${short_name}"
4747

48+
if [[ "$(yq eval '.spec.conversion.webhook.clientConfig.service.namespace // ""' "${crd_file}")" != "knative-operator" ]]; then
49+
echo "ERROR: ${filename} is missing the operator conversion webhook. Run ./hack/update-crd-conversion.sh first." >&2
50+
exit 1
51+
fi
52+
4853
# -c keeps compact sequence indentation so "- name:" stays flush with its parent key.
4954
if ! yq eval -c '
5055
.metadata.labels."app.kubernetes.io/version" = "{{ .Chart.Version }}" |
5156
.metadata.labels."app.kubernetes.io/name" = "knative-operator" |
52-
.spec.conversion = {
53-
"strategy": "Webhook",
54-
"webhook": {
55-
"conversionReviewVersions": ["v1beta1"],
56-
"clientConfig": {
57-
"service": {
58-
"name": "operator-webhook",
59-
"namespace": "{{ .Release.Namespace }}",
60-
"path": "/resource-conversion"
61-
}
62-
}
63-
}
64-
}
57+
.spec.conversion.webhook.clientConfig.service.namespace = "{{ .Release.Namespace }}"
6558
' "${crd_file}" > "${target}"; then
6659
echo "ERROR: yq failed to process ${filename}"
6760
rm -f "${target}"

hack/update-codegen.sh

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,21 @@ group "CRD Gen"
5858
# Install tools at the versions pinned in hack/tools.go (go.mod).
5959
GOFLAGS=-mod=mod go install sigs.k8s.io/controller-tools/cmd/controller-gen
6060
GOFLAGS=-mod=mod go install github.com/mikefarah/yq/v4
61-
export PATH="$(go env GOPATH)/bin:$PATH"
61+
go_bin="$(go env GOBIN)"
62+
if [[ -z "${go_bin}" ]]; then
63+
go_bin="$(go env GOPATH)/bin"
64+
fi
65+
export PATH="${go_bin}:$PATH"
6266

6367
GOFLAGS=-mod=mod controller-gen \
6468
crd:allowDangerousTypes=true,ignoreUnexportedFields=true,headerFile="${REPO_ROOT_DIR}/hack/boilerplate/boilerplate.yaml.txt" \
6569
paths="${REPO_ROOT_DIR}/pkg/apis/..." \
6670
output:crd:dir="${REPO_ROOT_DIR}/config/crd/bases"
6771

72+
group "Update CRD conversion webhooks"
73+
74+
"${REPO_ROOT_DIR}/hack/update-crd-conversion.sh"
75+
6876
group "Sync CRDs to Helm chart"
6977

7078
"${REPO_ROOT_DIR}/hack/sync-helm-crds.sh"

hack/update-crd-conversion.sh

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2026 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+
# Adds the conversion webhook stanza that controller-gen does not emit for the
18+
# operator's single-version CRDs. The release operator.yaml consumes these bases
19+
# directly, so this must run before any chart or release manifest generation.
20+
21+
set -o errexit
22+
set -o nounset
23+
set -o pipefail
24+
25+
REPO_ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
26+
CRD_BASES_DIR="${REPO_ROOT_DIR}/config/crd/bases"
27+
28+
echo "Updating CRD conversion webhooks in ${CRD_BASES_DIR}"
29+
30+
for crd_file in \
31+
"${CRD_BASES_DIR}/operator.knative.dev_knativeeventings.yaml" \
32+
"${CRD_BASES_DIR}/operator.knative.dev_knativeservings.yaml"; do
33+
if [[ ! -f "${crd_file}" ]]; then
34+
echo "ERROR: CRD file not found: ${crd_file}" >&2
35+
exit 1
36+
fi
37+
38+
echo " Updating $(basename "${crd_file}")"
39+
tmp="$(mktemp "${crd_file}.XXXXXX")"
40+
if ! awk '
41+
function emit_conversion() {
42+
print " conversion:"
43+
print " strategy: Webhook"
44+
print " webhook:"
45+
print " conversionReviewVersions: [\"v1beta1\"]"
46+
print " clientConfig:"
47+
print " service:"
48+
print " name: operator-webhook"
49+
print " namespace: knative-operator"
50+
print " path: /resource-conversion"
51+
}
52+
53+
$0 == " conversion:" {
54+
emit_conversion()
55+
inserted = 1
56+
skipping = 1
57+
next
58+
}
59+
60+
skipping && $0 == " versions:" {
61+
skipping = 0
62+
print
63+
next
64+
}
65+
66+
skipping {
67+
next
68+
}
69+
70+
$0 == " versions:" && !inserted {
71+
emit_conversion()
72+
inserted = 1
73+
print
74+
next
75+
}
76+
77+
{
78+
print
79+
}
80+
81+
END {
82+
if (!inserted) {
83+
print "ERROR: could not find CRD spec.versions anchor" > "/dev/stderr"
84+
exit 1
85+
}
86+
}
87+
' "${crd_file}" > "${tmp}"; then
88+
rm -f "${tmp}"
89+
exit 1
90+
fi
91+
mv "${tmp}" "${crd_file}"
92+
done
93+
94+
echo "Done. CRD conversion webhooks updated."

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" "${TMP_DIFFROOT}/config"
34+
mkdir -p "${TMP_DIFFROOT}/pkg" "${TMP_DIFFROOT}/config/crd"
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/crd/bases" "${TMP_DIFFROOT}/config/crd/"
3637
cp -aR "${REPO_ROOT_DIR}/config/charts" "${TMP_DIFFROOT}/config/"
3738

3839
# TODO(mattmoor): We should be able to rm -rf pkg/client/ and vendor/
@@ -42,9 +43,12 @@ echo "Diffing ${REPO_ROOT_DIR} against freshly generated codegen"
4243
ret=0
4344
diff -Nupr --no-dereference "${REPO_ROOT_DIR}/pkg" "${TMP_DIFFROOT}/pkg" || ret=1
4445
diff -Nupr --no-dereference "${REPO_ROOT_DIR}/vendor" "${TMP_DIFFROOT}/vendor" || ret=1
46+
diff -Nupr --no-dereference "${REPO_ROOT_DIR}/config/crd/bases" "${TMP_DIFFROOT}/config/crd/bases" || ret=1
4547
diff -Nupr --no-dereference "${REPO_ROOT_DIR}/config/charts" "${TMP_DIFFROOT}/config/charts" || ret=1
4648

4749
# Restore working tree state
50+
rm -fr "${REPO_ROOT_DIR}/config/crd/bases"
51+
cp -aR "${TMP_DIFFROOT}/config/crd/bases" "${REPO_ROOT_DIR}/config/crd/"
4852
rm -fr "${REPO_ROOT_DIR}/config/charts"
4953
cp -aR "${TMP_DIFFROOT}/config/charts" "${REPO_ROOT_DIR}/config/"
5054
rm -fr "${TMP_DIFFROOT}/config"

0 commit comments

Comments
 (0)