rules_kubectl

Bazel rules for a hermetic kubectl.

  • @kubectl//:kubectl runs the host platform's binary from the directory bazel run was invoked in.
  • @rules_kubectl//kubectl:toolchain_type is the toolchain type rules consume to run kubectl in an action.
  • @rules_kubectl//kubectl:current_toolchain exposes the resolved binary to genrule and friends as the KUBECTL (execpath) and KUBECTL_RLOCATIONPATH (rlocationpath) make variables.

Rules

kubectl_toolchain

load("@rules_kubectl//kubectl:defs.bzl", "kubectl_toolchain")

kubectl_toolchain(name, kubectl, version)

A kubectl toolchain.

Rules consume it through @rules_kubectl//kubectl:toolchain_type:

def _impl(ctx):
    toolchain = ctx.toolchains["@rules_kubectl//kubectl:toolchain_type"]
    ctx.actions.run(
        executable = toolchain.kubectl,
        ...
    )

The ToolchainInfo carries kubectl (the binary File) and version (a string, possibly empty).

@rules_kubectl//kubectl:current_toolchain exposes the binary through two make variables: KUBECTL is its execpath, for use in actions, and KUBECTL_RLOCATIONPATH is its rlocationpath, for looking it up in runfiles.

genrule(
    name = "pods",
    outs = ["pods.txt"],
    cmd = "$(KUBECTL) version --client > $@",
    toolchains = ["@rules_kubectl//kubectl:current_toolchain"],
)

sh_test(
    name = "pods_test",
    srcs = ["pods_test.sh"],
    data = ["@rules_kubectl//kubectl:current_toolchain"],
    env = {"KUBECTL": "$(KUBECTL_RLOCATIONPATH)"},
    toolchains = ["@rules_kubectl//kubectl:current_toolchain"],
)

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
kubectlA kubectl binary.Labelrequired
versionThe version of kubectl, without a leading v. Informational.Stringoptional""