From 8deb2c2abb5e4b7c8bb310abec13db2f022b071c Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Fri, 22 Nov 2024 11:30:48 +0100 Subject: [PATCH 1/2] [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 --- .pre-commit-config.yaml | 6 ++++++ Makefile | 7 +++++++ hack/build-crd-schema-checker.sh | 15 +++++++++++++++ hack/crd-schema-checker.sh | 21 +++++++++++++++++++++ 4 files changed, 49 insertions(+) create mode 100755 hack/build-crd-schema-checker.sh create mode 100755 hack/crd-schema-checker.sh diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 01b4ed01a..56ad6d089 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -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 diff --git a/Makefile b/Makefile index faf4fb948..49be76e7e 100644 --- a/Makefile +++ b/Makefile @@ -477,3 +477,10 @@ 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 + +CRD_SCHEMA_CHECKER_VERSION ?= release-4.16 + +PHONY: crd-schema-check +crd-schema-check: manifests + INSTALL_DIR=$(LOCALBIN) CRD_SCHEMA_CHECKER_VERSION=$(CRD_SCHEMA_CHECKER_VERSION) hack/build-crd-schema-checker.sh + INSTALL_DIR=$(LOCALBIN) BASE_REF="$${PULL_BASE_SHA:-$(BRANCH)}" hack/crd-schema-checker.sh diff --git a/hack/build-crd-schema-checker.sh b/hack/build-crd-schema-checker.sh new file mode 100755 index 000000000..12bf3be9a --- /dev/null +++ b/hack/build-crd-schema-checker.sh @@ -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" diff --git a/hack/crd-schema-checker.sh b/hack/crd-schema-checker.sh new file mode 100755 index 000000000..c431ae50b --- /dev/null +++ b/hack/crd-schema-checker.sh @@ -0,0 +1,21 @@ +#!/bin/bash +set -euxo pipefail + +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 "$BASE_REF:$crd" > "$TMP_DIR/$crd" + $CHECKER check-manifests \ + --existing-crd-filename="$TMP_DIR/$crd" \ + --new-crd-filename="$crd" +done From ce9cdc7e12a05fe9451fa9ccd751b79ad5391e00 Mon Sep 17 00:00:00 2001 From: Balazs Gibizer Date: Fri, 22 Nov 2024 12:57:18 +0100 Subject: [PATCH 2/2] DNM:test make crd-schema-check --- .pre-commit-config.yaml | 1 + apis/bases/core.openstack.org_openstackcontrolplanes.yaml | 4 ++++ apis/core/v1beta1/openstackcontrolplane_types.go | 3 +++ .../crd/bases/core.openstack.org_openstackcontrolplanes.yaml | 4 ++++ 4 files changed, 12 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 56ad6d089..2c1c5fbd7 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -31,6 +31,7 @@ repos: entry: make args: ['crd-schema-check'] pass_filenames: false + verbose: true - repo: https://github.com/pre-commit/pre-commit-hooks rev: v4.4.0 diff --git a/apis/bases/core.openstack.org_openstackcontrolplanes.yaml b/apis/bases/core.openstack.org_openstackcontrolplanes.yaml index 669280c40..09eb2afed 100644 --- a/apis/bases/core.openstack.org_openstackcontrolplanes.yaml +++ b/apis/bases/core.openstack.org_openstackcontrolplanes.yaml @@ -10879,6 +10879,8 @@ spec: format: int32 minimum: 0 type: integer + foo: + type: integer nodeSelector: additionalProperties: type: string @@ -14696,6 +14698,8 @@ spec: type: string type: object type: array + required: + - foo type: object type: object type: object diff --git a/apis/core/v1beta1/openstackcontrolplane_types.go b/apis/core/v1beta1/openstackcontrolplane_types.go index 6add4f982..b46408cdb 100644 --- a/apis/core/v1beta1/openstackcontrolplane_types.go +++ b/apis/core/v1beta1/openstackcontrolplane_types.go @@ -511,6 +511,9 @@ type RabbitmqTemplate struct { // +operator-sdk:csv:customresourcedefinitions:type=spec // NodeSelector to target subset of worker nodes running this service NodeSelector *map[string]string `json:"nodeSelector,omitempty"` + + // +kubebuilder:validation:Required + Foo int `json:"foo"` } // OvnSection defines the desired state of OVN services diff --git a/config/crd/bases/core.openstack.org_openstackcontrolplanes.yaml b/config/crd/bases/core.openstack.org_openstackcontrolplanes.yaml index 669280c40..09eb2afed 100644 --- a/config/crd/bases/core.openstack.org_openstackcontrolplanes.yaml +++ b/config/crd/bases/core.openstack.org_openstackcontrolplanes.yaml @@ -10879,6 +10879,8 @@ spec: format: int32 minimum: 0 type: integer + foo: + type: integer nodeSelector: additionalProperties: type: string @@ -14696,6 +14698,8 @@ spec: type: string type: object type: array + required: + - foo type: object type: object type: object