Skip to content

Commit e490db4

Browse files
committed
[make][pre-commit]Check CRD schema to avoid update issues
The new crd-schema-check make target compares the CRD schema of the patch with the schema on the tip of main and report errors on non backward compatible changes. This make target now also run in pre-commit both locally and in CI. This make target uses https://github.com/openshift/crd-schema-checker to do the actual checking. Related: OSPRH-11833
1 parent c38568c commit e490db4

4 files changed

+50
-0
lines changed

.pre-commit-config.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ repos:
1919
entry: make
2020
args: ['operator-lint']
2121
pass_filenames: false
22+
- id: make-crd-schema-check
23+
name: make-crd-schema-check
24+
language: system
25+
entry: make
26+
args: ['crd-schema-check']
27+
pass_filenames: false
2228

2329
- repo: https://github.com/dnephin/pre-commit-golang
2430
rev: v0.5.1

Makefile

+8
Original file line numberDiff line numberDiff line change
@@ -365,3 +365,11 @@ run-with-webhook: export METRICS_PORT?=8080
365365
run-with-webhook: export HEALTH_PORT?=8081
366366
run-with-webhook: manifests generate fmt vet ## Run a controller from your host.
367367
/bin/bash hack/run_with_local_webhook.sh
368+
369+
CRD_SCHEMA_CHECKER_VERSION ?= release-4.16
370+
BRANCH=main
371+
372+
PHONY: crd-schema-check
373+
crd-schema-check: manifests
374+
INSTALL_DIR=$(LOCALBIN) CRD_SCHEMA_CHECKER_VERSION=$(CRD_SCHEMA_CHECKER_VERSION) hack/build-crd-schema-checker.sh
375+
INSTALL_DIR=$(LOCALBIN) BASE_REF="$${PULL_BASE_SHA:-$(BRANCH)}" hack/crd-schema-checker.sh

hack/build-crd-schema-checker.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
set -euxo pipefail
3+
4+
if [ -f "$INSTALL_DIR/crd-schema-checker" ]; then
5+
exit 0
6+
fi
7+
8+
mkdir -p "$INSTALL_DIR/git-tmp"
9+
git clone https://github.com/openshift/crd-schema-checker.git \
10+
-b "$CRD_SCHEMA_CHECKER_VERSION" "$INSTALL_DIR/git-tmp"
11+
pushd "$INSTALL_DIR/git-tmp"
12+
GOWORK=off make
13+
cp crd-schema-checker "$INSTALL_DIR/"
14+
popd
15+
rm -rf "$INSTALL_DIR/git-tmp"

hack/crd-schema-checker.sh

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
set -euxo pipefail
3+
4+
CHECKER=$INSTALL_DIR/crd-schema-checker
5+
6+
TMP_DIR=$(mktemp -d)
7+
8+
function cleanup {
9+
rm -rf "$TMP_DIR"
10+
}
11+
12+
trap cleanup EXIT
13+
14+
15+
for crd in config/crd/bases/*.yaml; do
16+
mkdir -p "$(dirname "$TMP_DIR/$crd")"
17+
git show "$BASE_REF:$crd" > "$TMP_DIR/$crd"
18+
$CHECKER check-manifests \
19+
--existing-crd-filename="$TMP_DIR/$crd" \
20+
--new-crd-filename="$crd"
21+
done

0 commit comments

Comments
 (0)