Skip to content

Commit

Permalink
[make][pre-commit]Check CRD schema to avoid update issues
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gibizer committed Nov 22, 2024
1 parent 48b09a8 commit cda4a46
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ repos:
entry: make
args: ['operator-lint']
pass_filenames: false
- id: make-crd-schema-check
name: make-crd-schema-check
language: system
entry: make
args: ['crd-schema-check']
pass_filenames: false

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
Expand Down
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,8 @@ run-with-webhook: manifests generate fmt vet ## Run a controller from your host.
.PHONY: webhook-cleanup
webhook-cleanup:
/bin/bash hack/clean_local_webhook.sh

PHONY: crd-schema-check
crd-schema-check: manifests
INSTALL_DIR=$(LOCALBIN) CRD_SCHEMA_CHECKER_VERSION=release-4.16 hack/build-crd-schema-checker.sh
INSTALL_DIR=$(LOCALBIN) BRANCH=$(BRANCH) hack/crd-schema-checker.sh
15 changes: 15 additions & 0 deletions hack/build-crd-schema-checker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -euxo pipefail

if [ -f "$INSTALL_DIR/crd-schema-checker" ]; then
exit 0
fi

mkdir -p "$INSTALL_DIR/git-tmp"
git clone https://github.com/openshift/crd-schema-checker.git \
-b "$CRD_SCHEMA_CHECKER_VERSION" "$INSTALL_DIR/git-tmp"
pushd "$INSTALL_DIR/git-tmp"
GOWORK=off make
cp crd-schema-checker "$INSTALL_DIR/"
popd
rm -rf "$INSTALL_DIR/git-tmp"
31 changes: 31 additions & 0 deletions hack/crd-schema-checker.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash
set -euxo pipefail

if [ -z "$INSTALL_DIR" ]; then
echo "define INSTALL_DIR"
exit 1
fi

if [ -z "$BRANCH" ]; then
echo "define BRANCH"
exit 1
fi

CHECKER=$INSTALL_DIR/crd-schema-checker

TMP_DIR=$(mktemp -d)

function cleanup {
rm -rf "$TMP_DIR"
}

trap cleanup EXIT


for crd in config/crd/bases/*.yaml; do
mkdir -p "$(dirname "$TMP_DIR/$crd")"
git show "$BRANCH:$crd" > "$TMP_DIR/$crd"
$CHECKER check-manifests \
--existing-crd-filename="$TMP_DIR/$crd" \
--new-crd-filename="$crd"
done

0 comments on commit cda4a46

Please sign in to comment.