bazel

Build and test with Bazel

Building and testing Kubernetes with Bazel is supported but not yet default.

Go rules are managed by the gazelle tool, with some additional rules managed by the kazel tool. These tools are called via the hack/update-bazel.sh script.

Instructions for installing Bazel can be found here.

Several make rules have been created for common operations:

  • make bazel-build: builds all binaries in tree
  • make bazel-test: runs all unit tests
  • make bazel-test-integration: runs all integration tests
  • make bazel-release: builds release tarballs, Docker images (for server components), and Debian images

You can also interact with Bazel directly; for example, to run all kubectl unit tests, run

$ bazel test //pkg/kubectl/...

Planter

If you don’t want to install Bazel, you can instead try using the unofficial Planter tool, which runs Bazel inside a Docker container.

For example, you can run

$ ../test-infra/planter/planter.sh make bazel-test
$ ../test-infra/planter/planter.sh bazel build //cmd/kubectl

Continuous Integration

There are several bazel CI jobs: * ci-kubernetes-bazel-build: builds everything with Bazel * ci-kubernetes-bazel-test: runs unit tests in with Bazel

Similar jobs are run on all PRs; additionally, several of the e2e jobs use Bazel-built binaries when launching and testing Kubernetes clusters.

Known issues

Cross-compilation is not currently supported, so all binaries will be built for the host OS and architecture running Bazel. (For example, you can’t currently target linux/amd64 from macOS or linux/s390x from an amd64 machine.)

Additionally, native macOS support is still a work in progress. Using Planter is a possible workaround in the interim.

Bazel does not validate build environment, thus make sure that needed tools and development packages are installed in the system. Bazel builds require presence of make, gcc, g++, glibc and libstdc++ development headers and glibc static development libraries. Please check your distribution for exact names of the packages. Examples for some commonly used distributions are below:

Dependency Debian/Ubuntu CentOS OpenSuSE
Build essentials apt install build-essential yum groupinstall development zypper install -t pattern devel_C_C++
GCC C++ apt install g++ yum install gcc-c++ zypper install gcc-c++
GNU Libc static files apt install libc6-dev yum install glibc-static zypper install glibc-devel-static

Updating BUILD files

To update BUILD files, run:

$ ./hack/update-bazel.sh

To prevent Go rules from being updated, consult the gazelle documentation.

Note that much like Go files and gofmt, BUILD files have standardized, opinionated style rules, and running hack/update-bazel.sh will format them for you.

If you want to auto-format BUILD files in your editor, using something like Buildifier is recommended.

Updating the BUILD file for a package will be required when: * Files are added to or removed from a package * Import dependencies change for a package * A BUILD file has been updated and needs to be reformatted * A new BUILD file has been added (parent BUILD files will be updated)

Contacts

For help or discussion, join the #bazel channel on Kubernetes Slack.