From 7ecd80d1fe22e43c97949b6a1b0205830c155c1b Mon Sep 17 00:00:00 2001 From: guoyao <1015105054@qq.com> Date: Sat, 6 Nov 2021 09:37:28 +0800 Subject: [PATCH] add protobuf verification script Signed-off-by: guoyao <1015105054@qq.com> --- .github/workflows/ci.yml | 4 ++++ hack/update-estimator-protobuf.sh | 20 +++++++++++++++--- hack/verify-estimator-protobuf.sh | 34 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) create mode 100755 hack/verify-estimator-protobuf.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 343329a82..60eebf3cc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,6 +9,8 @@ jobs: name: lint runs-on: ubuntu-18.04 steps: + - name: Install Protoc + uses: arduino/setup-protoc@v1 - name: checkout code uses: actions/checkout@v2 - name: install Go @@ -21,6 +23,8 @@ jobs: run: hack/verify-staticcheck.sh - name: import alias run: hack/verify-import-aliases.sh + - name: protobuf + run: hack/verify-estimator-protobuf.sh codegen: name: codegen runs-on: ubuntu-18.04 diff --git a/hack/update-estimator-protobuf.sh b/hack/update-estimator-protobuf.sh index 1f16229b9..4c4d20c0d 100755 --- a/hack/update-estimator-protobuf.sh +++ b/hack/update-estimator-protobuf.sh @@ -4,6 +4,8 @@ set -o errexit set -o nounset set -o pipefail +KARMADA_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" + # Use `hack/generate-proto.sh` to generate proto files. export GOPATH=$(go env GOPATH | awk -F ':' '{print $1}') @@ -13,6 +15,17 @@ GO111MODULE=on go install golang.org/x/tools/cmd/goimports GO111MODULE=on go install k8s.io/code-generator/cmd/go-to-protobuf GO111MODULE=on go install github.com/gogo/protobuf/protoc-gen-gogo +#ref https://github.com/kubernetes/kubernetes/blob/master/hack/update-generated-protobuf-dockerized.sh +if [[ -z "$(which protoc)" || "$(protoc --version)" != "libprotoc 3."* ]]; then + echo "Generating protobuf requires protoc 3.0.0-beta1 or newer. Please download and" + echo "install the platform appropriate Protobuf package for your OS: " + echo + echo " https://github.com/protocolbuffers/protobuf/releases" + echo + echo "WARNING: Protobuf changes are not being validated" + exit 1 +fi + PACKAGES=( github.com/karmada-io/karmada/pkg/estimator/pb ) @@ -30,13 +43,14 @@ ${GOPATH}/bin/go-to-protobuf \ --go-header-file=./hack/boilerplate/boilerplate.go.txt \ --apimachinery-packages=$(IFS=, ; echo "${APIMACHINERY_PKGS[*]}") \ --packages=$(IFS=, ; echo "${PACKAGES[*]}") \ - --proto-import ./vendor \ - --proto-import ./third_party/protobuf/ + --proto-import="${KARMADA_ROOT}/vendor" \ + --proto-import="${KARMADA_ROOT}/third_party/protobuf" + # The `go-to-protobuf` tool will modify all import proto files in vendor, so we should use go mod vendor to prevent. go mod vendor -SERVICE_PROTO_FILES=$(find . -name "service.proto") +SERVICE_PROTO_FILES=$(find . -not -path *./_tmp/* -name "service.proto") for file in ${SERVICE_PROTO_FILES[*]}; do protoc \ diff --git a/hack/verify-estimator-protobuf.sh b/hack/verify-estimator-protobuf.sh new file mode 100755 index 000000000..e65fd5bbe --- /dev/null +++ b/hack/verify-estimator-protobuf.sh @@ -0,0 +1,34 @@ +#!/bin/bash + +set -o errexit +set -o nounset +set -o pipefail + +KARMADA_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd -P)" +_tmp="${KARMADA_ROOT}/_tmp" +ESTIMATORPB="pkg/estimator/pb" + +cleanup() { + rm -rf "${_tmp}" +} + +trap "cleanup" EXIT SIGINT + +cleanup + +mkdir -p "${_tmp}/${ESTIMATORPB}" + +cp -a "${KARMADA_ROOT}/${ESTIMATORPB}"/* "${_tmp}/${ESTIMATORPB}/" + +source "$KARMADA_ROOT/hack/update-estimator-protobuf.sh" + +echo "diffing ${ESTIMATORPB} against freshly generated estimator protobuf" +ret=0 +diff -Naupr "${KARMADA_ROOT}/${ESTIMATORPB}" "${_tmp}/${ESTIMATORPB}" || ret=$? +cp -a "${_tmp}/${ESTIMATORPB}"/* "${KARMADA_ROOT}/${ESTIMATORPB}/" +if [[ $ret -eq 0 ]]; then + echo "${ESTIMATORPB} is up to date." +else + echo "${ESTIMATORPB} is out of date. Please run hack/update-estimator-protobuf.sh to update." + exit 1 +fi