|
| 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}" |
0 commit comments