update-bazel.sh 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #!/usr/bin/env bash
  2. # Copyright 2016 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. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
  19. source "${KUBE_ROOT}/hack/lib/init.sh"
  20. # Ensure that we find the binaries we build before anything else.
  21. export GOBIN="${KUBE_OUTPUT_BINPATH}"
  22. PATH="${GOBIN}:${PATH}"
  23. # Install tools we need, but only from vendor/...
  24. pushd "${KUBE_ROOT}" >/dev/null
  25. GO111MODULE=on GOFLAGS=-mod=vendor go install github.com/bazelbuild/bazel-gazelle/cmd/gazelle
  26. GO111MODULE=on GOFLAGS=-mod=vendor go install github.com/bazelbuild/buildtools/buildozer
  27. GO111MODULE=on GOFLAGS=-mod=vendor go install k8s.io/repo-infra/cmd/kazel
  28. popd >/dev/null
  29. # Find all of the staging repos.
  30. while IFS='' read -r repo; do staging_repos+=("${repo}"); done <\
  31. <(cd "${KUBE_ROOT}/staging/src" && find k8s.io -mindepth 1 -maxdepth 1 -type d | LC_ALL=C LANG=C sort)
  32. # Save the staging repos into a Starlark list that can be used by Bazel rules.
  33. (
  34. cat "${KUBE_ROOT}/hack/boilerplate/boilerplate.generatebzl.txt"
  35. echo "# This file is autogenerated by hack/update-bazel.sh."
  36. # avoid getting caught by the boilerplate checker
  37. rev <<<".TIDE TON OD #"
  38. echo
  39. echo "staging_repos = ["
  40. for repo in "${staging_repos[@]}"; do
  41. echo " \"${repo}\","
  42. done
  43. echo "]"
  44. ) >"${KUBE_ROOT}/staging/repos_generated.bzl"
  45. # Ensure there's a BUILD file at vendor/ so the all-srcs rule in the root
  46. # BUILD.bazel file is isolated from changes under vendor/.
  47. touch "${KUBE_ROOT}/vendor/BUILD"
  48. # Ensure there's a BUILD file at the root of each staging repo. This prevents
  49. # the package-srcs glob in vendor/BUILD from following the symlinks
  50. # from vendor/k8s.io into staging. Following these symlinks through
  51. # vendor results in files being excluded from the kubernetes-src tarball
  52. # generated by Bazel.
  53. for repo in "${staging_repos[@]}"; do
  54. touch "${KUBE_ROOT}/staging/src/${repo}/BUILD"
  55. done
  56. # Run gazelle to update Go rules in BUILD files.
  57. # filter out known pkg-config error (see buildozer workaround below)
  58. # NOTE: the `|| exit "${PIPESTATUS[0]}"` is to ignore grep errors
  59. # while preserving the exit code of gazelle
  60. gazelle fix \
  61. -external=vendored \
  62. -mode=fix \
  63. -repo_root "${KUBE_ROOT}" \
  64. "${KUBE_ROOT}" \
  65. 2>&1 | grep -Ev "vendor/github.com/seccomp/libseccomp-golang/seccomp(_internal)?.go: pkg-config not supported: #cgo pkg-config: libseccomp" || (exit "${PIPESTATUS[0]}")
  66. # Run kazel to update the pkg-srcs and all-srcs rules as well as handle
  67. # Kubernetes code generators.
  68. kazel
  69. # make targets in vendor manual
  70. # buildozer exits 3 when no changes are made ¯\_(ツ)_/¯
  71. # https://github.com/bazelbuild/buildtools/tree/master/buildozer#error-code
  72. buildozer -quiet 'add tags manual' '//vendor/...:%go_binary' '//vendor/...:%go_test' && ret=$? || ret=$?
  73. if [[ $ret != 0 && $ret != 3 ]]; then
  74. exit 1
  75. fi
  76. # mark all ./test/integration/* targets as integration
  77. # see comment above re: buildozer exit codes
  78. buildozer -quiet 'add tags integration' '//test/integration/...:%go_test' && ret=$? || ret=$?
  79. if [[ $ret != 0 && $ret != 3 ]]; then
  80. exit 1
  81. fi
  82. # restrict ./vendor/github.com/prometheus/* targets visibility
  83. # see comment above re: buildozer exit codes
  84. buildozer -quiet 'set visibility //staging/src/k8s.io/component-base/metrics:prometheus_import_allow_list' '//vendor/github.com/prometheus/...:go_default_library' && ret=$? || ret=$?
  85. if [[ $ret != 0 && $ret != 3 ]]; then
  86. exit 1
  87. fi
  88. # we need to set this because gazelle doesn't support pkg-config, which would set this link option
  89. # see comment above re: buildozer exit codes
  90. buildozer -quiet 'set clinkopts select({"@io_bazel_rules_go//go/platform:linux":["-lseccomp",],"//conditions:default":[],})' //vendor/github.com/seccomp/libseccomp-golang:go_default_library && ret=$? || ret=$?
  91. if [[ $ret != 0 && $ret != 3 ]]; then
  92. exit 1
  93. fi