Skip to content

Commit 798d40b

Browse files
committed
updating functionality to verify bazel build files
1 parent dd1e209 commit 798d40b

File tree

4 files changed

+102
-43
lines changed

4 files changed

+102
-43
lines changed

.kazelcfg.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"GoPrefix": "k8s.io/kops",
3+
"AddSourcesRules": true
4+
}

hack/go_install_from_commit.sh

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2017 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+
16+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
PKG=$1
21+
COMMIT=$2
22+
export GOPATH=$3
23+
export GOBIN="$GOPATH/bin"
24+
25+
go get -d -u "${PKG}"
26+
cd "${GOPATH}/src/${PKG}"
27+
git checkout -q "${COMMIT}"
28+
go install "${PKG}"

hack/update-bazel.sh

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
16+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
KOPS_ROOT=$(git rev-parse --show-toplevel)
21+
# https://github.com/kubernetes/test-infra/issues/5699#issuecomment-348350792
22+
cd ${KOPS_ROOT}
23+
TMP_GOPATH=$(mktemp -d)
24+
25+
# manually remove BUILD file for k8s.io/apimachinery/pkg/util/sets/BUILD if it
26+
# exists; there is a specific set-gen rule that breaks importing
27+
# ref: https://github.com/kubernetes/kubernetes/blob/4e2f5e2212b05a305435ef96f4b49dc0932e1264/staging/src/k8s.io/apimachinery/pkg/util/sets/BUILD#L23-L49
28+
# rm -f ${KOPS_ROOT}/vendor/k8s.io/apimachinery/pkg/util/sets/{BUILD,BUILD.bazel}
29+
30+
"${KOPS_ROOT}/hack/go_install_from_commit.sh" \
31+
github.com/bazelbuild/bazel-gazelle/cmd/gazelle \
32+
eaa1e87d2a3ca716780ca6650ef5b9b9663b8773 \
33+
"${TMP_GOPATH}"
34+
35+
"${TMP_GOPATH}/bin/gazelle" fix \
36+
-external=vendored \
37+
-mode=fix \
38+
-proto=disable \
39+
-repo_root="${KOPS_ROOT}"

hack/verify-bazel.sh

+31-43
Original file line numberDiff line numberDiff line change
@@ -17,49 +17,37 @@ set -o errexit
1717
set -o nounset
1818
set -o pipefail
1919

20-
export KUBE_ROOT=$(dirname "${BASH_SOURCE}")/..
21-
22-
# Example: kube::util::trap_add 'echo "in trap DEBUG"' DEBUG
23-
# See: http://stackoverflow.com/questions/3338030/multiple-bash-traps-for-the-same-signal
24-
trap_add() {
25-
local trap_add_cmd
26-
trap_add_cmd=$1
27-
shift
28-
29-
for trap_add_name in "$@"; do
30-
local existing_cmd
31-
local new_cmd
32-
33-
# Grab the currently defined trap commands for this trap
34-
existing_cmd=`trap -p "${trap_add_name}" | awk -F"'" '{print $2}'`
35-
36-
if [[ -z "${existing_cmd}" ]]; then
37-
new_cmd="${trap_add_cmd}"
38-
else
39-
new_cmd="${trap_add_cmd};${existing_cmd}"
40-
fi
41-
42-
# Assign the test
43-
trap "${new_cmd}" "${trap_add_name}"
44-
done
45-
}
46-
47-
_tmpdir="$(mktemp -d -t verify-bazel.XXXXXX)"
48-
trap_add "rm -rf ${_tmpdir}" EXIT
49-
50-
_tmp_gopath="${_tmpdir}/go"
51-
_tmp_kuberoot="${_tmp_gopath}/src/k8s.io/kops"
52-
mkdir -p "${_tmp_kuberoot}/.."
53-
cp -a "${KUBE_ROOT}" "${_tmp_kuberoot}/.."
54-
55-
cd "${_tmp_kuberoot}"
56-
GOPATH="${_tmp_gopath}" bazel run //:gazelle
57-
58-
diff=$(diff -Naupr "${KUBE_ROOT}" "${_tmp_kuberoot}" || true)
20+
KOPS_ROOT=$(git rev-parse --show-toplevel)
21+
TMP_GOPATH=$(mktemp -d)
22+
cd "${KOPS_ROOT}"
23+
24+
"${KOPS_ROOT}/hack/go_install_from_commit.sh" \
25+
github.com/bazelbuild/bazel-gazelle/cmd/gazelle \
26+
eaa1e87d2a3ca716780ca6650ef5b9b9663b8773 \
27+
"${TMP_GOPATH}"
28+
29+
30+
gazelle_diff=$("${TMP_GOPATH}/bin/gazelle" fix \
31+
-external=vendored \
32+
-mode=diff \
33+
-proto=disable \
34+
-repo_root="${KOPS_ROOT}")
35+
36+
if [[ -n "${gazelle_diff}" ]]; then
37+
echo "${gazelle_diff}" >&2
38+
echo >&2
39+
echo "Run ./hack/update-bazel.sh" >&2
40+
exit 1
41+
fi
5942

60-
if [[ -n "${diff}" ]]; then
61-
echo "${diff}"
62-
echo
63-
echo "Run make bazel-gazelle"
43+
# Make sure there are no BUILD files outside vendor - we should only have
44+
# BUILD.bazel files.
45+
old_build_files=$(find . -name BUILD \( -type f -o -type l \) \
46+
-not -path './vendor/*' | sort)
47+
if [[ -n "${old_build_files}" ]]; then
48+
echo "One or more BUILD files found in the tree:" >&2
49+
echo "${old_build_files}" >&2
50+
echo >&2
51+
echo "Only BUILD.bazel is allowed." >&2
6452
exit 1
6553
fi

0 commit comments

Comments
 (0)