verify-generated-protobuf.sh 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/env bash
  2. # Copyright 2015 The Kubernetes Authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # This script checks whether updating of the packages which explicitly
  16. # requesting generation is needed or not. We should run
  17. # `hack/update-generated-protobuf.sh` if those packages are out of date.
  18. # Usage: `hack/verify-generated-protobuf.sh`.
  19. set -o errexit
  20. set -o nounset
  21. set -o pipefail
  22. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  23. source "${KUBE_ROOT}/hack/lib/init.sh"
  24. kube::golang::setup_env
  25. APIROOTS=$({
  26. # gather the packages explicitly requesting generation
  27. git grep --files-with-matches -e '// +k8s:protobuf-gen=package' cmd pkg staging | xargs -n 1 dirname
  28. # add the root apimachinery pkg containing implicitly generated files (--apimachinery-packages)
  29. echo staging/src/k8s.io/apimachinery/pkg
  30. } | sort | uniq)
  31. _tmp="${KUBE_ROOT}/_tmp"
  32. cleanup() {
  33. rm -rf "${_tmp}"
  34. }
  35. trap "cleanup" EXIT SIGINT
  36. cleanup
  37. for APIROOT in ${APIROOTS}; do
  38. mkdir -p "${_tmp}/${APIROOT}"
  39. cp -a "${KUBE_ROOT}/${APIROOT}"/* "${_tmp}/${APIROOT}/"
  40. done
  41. KUBE_VERBOSE=3 "${KUBE_ROOT}/hack/update-generated-protobuf.sh"
  42. for APIROOT in ${APIROOTS}; do
  43. TMP_APIROOT="${_tmp}/${APIROOT}"
  44. echo "diffing ${APIROOT} against freshly generated protobuf"
  45. ret=0
  46. diff -Naupr -I 'Auto generated by' -x 'zz_generated.*' -x '.github' -x '.import-restrictions' "${KUBE_ROOT}/${APIROOT}" "${TMP_APIROOT}" || ret=$?
  47. cp -a "${TMP_APIROOT}"/* "${KUBE_ROOT}/${APIROOT}/"
  48. if [[ $ret -eq 0 ]]; then
  49. echo "${APIROOT} up to date."
  50. else
  51. echo "${APIROOT} is out of date. Please run hack/update-generated-protobuf.sh"
  52. exit 1
  53. fi
  54. done