make kube::util::find-binary not dependent on bazel-out/ structure

Implement an aspect that outputs go_build_mode metadata for go binaries,
and use that during binary selection.
This commit is contained in:
Mike Danese 2020-09-09 17:44:05 -07:00
parent d182ecc1f9
commit bdadb2a187
3 changed files with 77 additions and 14 deletions

View File

@ -12,7 +12,8 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_test") load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_context", "go_test")
load("@io_bazel_rules_go//go/platform:list.bzl", "GOOS_GOARCH")
# Defines a go_binary rule that enables cgo on platform builds targeting Linux, # Defines a go_binary rule that enables cgo on platform builds targeting Linux,
# and otherwise builds a pure go binary. # and otherwise builds a pure go binary.
@ -47,3 +48,64 @@ def go_test_conditional_pure(name, out, tags = [], **kwargs):
name = "name", name = "name",
actual = out, actual = out,
) )
_GO_BUILD_MODE_TMPL = "{goos}/{goarch}/pure={pure},static={static},msan={msan},race={race}\n"
def _go_build_mode_aspect_impl(target, ctx):
if (not hasattr(ctx.rule.attr, "_is_executable") or
not ctx.rule.attr._is_executable or
ctx.rule.attr.testonly):
# We only care about exporting platform info for executable targets
# that aren't testonly (e.g. kubectl and e2e.test).
return []
mode = go_context(ctx).mode
out = ctx.actions.declare_file(
target.files_to_run.executable.basename + ".go_build_mode",
sibling = target.files_to_run.executable,
)
ctx.actions.write(out, _GO_BUILD_MODE_TMPL.format(
goos = mode.goos,
goarch = mode.goarch,
pure = str(mode.pure).lower(),
static = str(mode.static).lower(),
msan = str(mode.msan).lower(),
race = str(mode.race).lower(),
))
return [OutputGroupInfo(default = depset([out]))]
# This aspect ouputs a *.go_build_mode metadata for go binaries. This metadata
# is used for executable selection e.g. in CI.
go_build_mode_aspect = aspect(
implementation = _go_build_mode_aspect_impl,
attrs = {
"goos": attr.string(
default = "auto",
values = ["auto"] + {goos: None for goos, _ in GOOS_GOARCH}.keys(),
),
"goarch": attr.string(
default = "auto",
values = ["auto"] + {goarch: None for _, goarch in GOOS_GOARCH}.keys(),
),
"pure": attr.string(
default = "auto",
values = ["auto", "on", "off"],
),
"static": attr.string(
default = "auto",
values = ["auto", "on", "off"],
),
"msan": attr.string(
default = "auto",
values = ["auto", "on", "off"],
),
"race": attr.string(
default = "auto",
values = ["auto", "on", "off"],
),
"_go_context_data": attr.label(default = "@io_bazel_rules_go//:go_context_data"),
},
toolchains = ["@io_bazel_rules_go//go:toolchain"],
)

View File

@ -22,6 +22,10 @@ build --define gotags=selinux
# This flag requires Bazel 0.5.0+ # This flag requires Bazel 0.5.0+
build --sandbox_fake_username build --sandbox_fake_username
# Output go_build_mode metadata for binaries. This metadata is used for
# executable selection e.g. in CI.
build --aspects //build:go.bzl%go_build_mode_aspect
# Enable go race detection. # Enable go race detection.
build:unit --@io_bazel_rules_go//go/config:race build:unit --@io_bazel_rules_go//go/config:race
test:unit --@io_bazel_rules_go//go/config:race test:unit --@io_bazel_rules_go//go/config:race

View File

@ -207,19 +207,16 @@ kube::util::find-binary-for-platform() {
"${KUBE_ROOT}/_output/dockerized/go/bin/${lookfor}" "${KUBE_ROOT}/_output/dockerized/go/bin/${lookfor}"
); );
fi fi
# Also search for binary in bazel build tree.
# The bazel go rules place some binaries in subtrees like # Also search for binary in bazel build tree if bazel-out/ exists.
# "bazel-bin/source/path/linux_amd64_pure_stripped/binaryname", so make sure if [[ -d "${KUBE_ROOT}/bazel-out" ]]; then
# the platform name is matched in the path. while IFS=$'\n' read -r bin_build_mode; do
while IFS=$'\n' read -r location; do if grep -q "${platform}" "${bin_build_mode}"; then
locations+=("$location"); # drop the extension to get the real binary path.
done < <(find "${KUBE_ROOT}/bazel-bin/" -type f -executable \ locations+=("${bin_build_mode%.*}")
\( -path "*/${platform/\//_}*/${lookfor}" -o -path "*/${lookfor}" \) 2>/dev/null || true) fi
# search for executables for non-GNU versions of find (eg. BSD) done < <(find "${KUBE_ROOT}/bazel-out/" -name "${lookfor}.go_build_mode")
while IFS=$'\n' read -r location; do fi
locations+=("$location");
done < <(find "${KUBE_ROOT}/bazel-bin/" -type f -perm -111 \
\( -path "*/${platform/\//_}*/${lookfor}" -o -path "*/${lookfor}" \) 2>/dev/null || true)
# List most recently-updated location. # List most recently-updated location.
local -r bin=$( (ls -t "${locations[@]}" 2>/dev/null || true) | head -1 ) local -r bin=$( (ls -t "${locations[@]}" 2>/dev/null || true) | head -1 )