automate removing bazel in hack/update-bazel.sh

This commit is contained in:
Benjamin Elder 2021-02-28 14:15:57 -08:00
parent 4880b996b5
commit a1f2787897

View File

@ -18,99 +18,26 @@ set -o nounset
set -o pipefail
KUBE_ROOT=$(dirname "${BASH_SOURCE[0]}")/..
source "${KUBE_ROOT}/hack/lib/init.sh"
# Ensure that we find the binaries we build before anything else.
export GOBIN="${KUBE_OUTPUT_BINPATH}"
PATH="${GOBIN}:${PATH}"
# delete all bazel related files
find "${KUBE_ROOT}" \( \
-name BUILD \
-o -name BUILD.bazel \
-o -name '*.bzl' \
\) \
-delete
pushd "${KUBE_ROOT}/hack/tools" >/dev/null
GO111MODULE=on go install github.com/bazelbuild/bazel-gazelle/cmd/gazelle
GO111MODULE=on go install github.com/bazelbuild/buildtools/buildozer
GO111MODULE=on go install k8s.io/repo-infra/cmd/kazel
popd >/dev/null
# remove additional one-off bazel related files
# NOTE: most of these will be pairs of symlinked location in "${KUBE_ROOT}/"
# and the actual location in "${KUBE_ROOT}/build/root/"
rm -f \
"${KUBE_ROOT}/build/root/BUILD.root" \
"${KUBE_ROOT}/WORKSPACE" \
"${KUBE_ROOT}/build/root/WORKSPACE" \
"${KUBE_ROOT}/.bazelrc" \
"${KUBE_ROOT}/build/root/.bazelrc" \
"${KUBE_ROOT}/.bazelversion" \
"${KUBE_ROOT}/build/root/.bazelversion" \
"${KUBE_ROOT}/.kazelcfg.json" \
"${KUBE_ROOT}/build/root/.kazelcfg.json"
# Find all of the staging repos.
while IFS='' read -r repo; do staging_repos+=("${repo}"); done <\
<(cd "${KUBE_ROOT}/staging/src" && find k8s.io -mindepth 1 -maxdepth 1 -type d | LC_ALL=C LANG=C sort)
# Save the staging repos into a Starlark list that can be used by Bazel rules.
(
cat "${KUBE_ROOT}/hack/boilerplate/boilerplate.generatebzl.txt"
echo "# This file is autogenerated by hack/update-bazel.sh."
# avoid getting caught by the boilerplate checker
rev <<<".TIDE TON OD #"
echo
echo "staging_repos = ["
for repo in "${staging_repos[@]}"; do
echo " \"${repo}\","
done
echo "]"
) >"${KUBE_ROOT}/staging/repos_generated.bzl"
# Ensure there's a BUILD file at vendor/ so the all-srcs rule in the root
# BUILD.bazel file is isolated from changes under vendor/.
touch "${KUBE_ROOT}/vendor/BUILD"
# Ensure there's a BUILD file at the root of each staging repo. This prevents
# the package-srcs glob in vendor/BUILD from following the symlinks
# from vendor/k8s.io into staging. Following these symlinks through
# vendor results in files being excluded from the kubernetes-src tarball
# generated by Bazel.
for repo in "${staging_repos[@]}"; do
touch "${KUBE_ROOT}/staging/src/${repo}/BUILD"
done
# Warning: Please be careful when ignoring these pkg-config errors.
# As gazelle doesn't support pkg-config, if we suppress these errors here, we actually need to modify the script below to handle it manually.
# For instance, in the case of `libseccomp`, we set the link option `-lseccomp` to make it work properly.
KNOWN_PKG_CONFIG_ERRORS=(
"vendor/github.com/seccomp/libseccomp-golang/seccomp(_internal)?.go: pkg-config not supported: #cgo pkg-config: libseccomp"
"vendor/github.com/google/cadvisor/nvm/machine_libipmctl.go: pkg-config not supported: #cgo pkg-config: libipmctl"
)
# Run gazelle to update Go rules in BUILD files.
# filter out known pkg-config error (see buildozer workaround below)
# NOTE: the `|| exit "${PIPESTATUS[0]}"` is to ignore grep errors
# while preserving the exit code of gazelle
gazelle fix \
-external=vendored \
-mode=fix \
-repo_root "${KUBE_ROOT}" \
"${KUBE_ROOT}" \
2>&1 | grep -Ev "$(kube::util::join \| "${KNOWN_PKG_CONFIG_ERRORS[@]}")" || (exit "${PIPESTATUS[0]}")
# Run kazel to update the pkg-srcs and all-srcs rules as well as handle
# Kubernetes code generators.
kazel
# make targets in vendor manual
# buildozer exits 3 when no changes are made ¯\_(ツ)_/¯
# https://github.com/bazelbuild/buildtools/tree/master/buildozer#error-code
buildozer -quiet 'add tags manual' '//vendor/...:%go_binary' '//vendor/...:%go_test' && ret=$? || ret=$?
if [[ $ret != 0 && $ret != 3 ]]; then
exit 1
fi
# mark all ./test/integration/* targets as integration
# see comment above re: buildozer exit codes
buildozer -quiet 'add tags integration' '//test/integration/...:%go_test' && ret=$? || ret=$?
if [[ $ret != 0 && $ret != 3 ]]; then
exit 1
fi
# restrict ./vendor/github.com/prometheus/* targets visibility
# see comment above re: buildozer exit codes
buildozer -quiet 'set visibility //build/visible_to:vendor_githubcom_prometheus_CONSUMERS' '//vendor/github.com/prometheus/...:go_default_library' && ret=$? || ret=$?
if [[ $ret != 0 && $ret != 3 ]]; then
exit 1
fi
# we need to set this because gazelle doesn't support pkg-config, which would set this link option
# see comment above re: buildozer exit codes
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=$?
if [[ $ret != 0 && $ret != 3 ]]; then
exit 1
fi
# Avoid bazel stuff in tools/ directory
rm hack/tools/BUILD