Bzlmod extension for kubectl
Module Extensions
kubectl
kubectl = use_extension("@rules_kubectl//kubectl:extensions.bzl", "kubectl")
kubectl.toolchain(name, version)
Module extension for fetching kubectl.
TAG CLASSES
toolchain
Fetch kubectl and register toolchains for it.
Each tag creates a host repository (default @kubectl) exposing the host platform's binary as @{name}//:kubectl. That target runs kubectl from the directory bazel run was invoked in, so relative paths such as -f ./deploy.yaml resolve as they would for a locally installed kubectl.
kubectl = use_extension("@rules_kubectl//kubectl:extensions.bzl", "kubectl")
kubectl.toolchain()
use_repo(kubectl, "kubectl")
bazel run @kubectl -- get pods --namespace my-namespace
bazel run @kubectl -- apply -f ./deploy/configmap.yaml
rules_kubectl registers the toolchains itself; the root module does not need a register_toolchains call. The registered toolchains serve the version requested by the root module's first toolchain tag (or the default when the root module has none).
Versions
rules_kubectl ships checksums for a single default version. Requesting any other version makes the extension download the upstream .sha256 files for that release while it is evaluated. On Bazel 8.5 and newer the digests are then stored as facts in MODULE.bazel.lock, so the lookup only happens once per version and airgapped builds work from the lockfile. Older Bazel versions repeat the lookup whenever the extension is re-evaluated.
kubectl.toolchain(version = "1.36.5")
Several versions can live side by side by giving each tag its own repository name. Only the first tag in the root module decides which version the registered toolchains serve:
kubectl.toolchain()
kubectl.toolchain(name = "kubectl_legacy", version = "1.35.6")
use_repo(kubectl, "kubectl", "kubectl_legacy")
Attributes
| Name | Description | Type | Mandatory | Default |
|---|---|---|---|---|
| name | The name of the host repository. | Name | optional | "kubectl" |
| version | The version of kubectl to fetch, without a leading v. | String | optional | "1.37.1" |