Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
Allow downloading prebuilt binary in kubectl toolchain (#253)
Browse files Browse the repository at this point in the history
* Allow downloading prebuilt binary in kubectl toolchain

Addresses customer request to download v1.10.0 in #245 because
v1.10.0 can't be built from source.

* Address review comments

* Doc fix
  • Loading branch information
smukherj1 authored Dec 20, 2018
1 parent 59b62a9 commit d724919
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 7 deletions.
52 changes: 52 additions & 0 deletions toolchains/kubectl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ rule. At the moment, the tool's configuration does one of the following:

1. Detect the path to the `kubectl` tool (default).
2. Build the `kubectl` tool from source.
3. Download a `kubectl` prebuilt binary not on the system path.

### Use a kubectl built from source
If you want to build the `kubectl` tool from source you will
need to add to your `WORKSPACE` file the following lines (Note:
The call to `kubectl_configure` must be before the call to
Expand Down Expand Up @@ -91,6 +93,56 @@ source repository is compatible with. Look at the `http_archive` invocation in
https://github.com/kubernetes/kubernetes/blob/{k8s_commit}/build/root/WORKSPACE
for the `@io_kubernetes_build` to get the commit pin, sha256 and prefix values.*

### Download a custom kubectl binary

If you want to download a standard binary released by the kubernetes project,
get the URL and SHA256 for the binary for your platform from [here](https://kubernetes.io/docs/tasks/tools/install-kubectl/#install-kubectl-binary-using-curl).
For example, if you want to use kubectl v1.10.0 on a x86 64 bit Linux platform,
add to your `WORKSPACE` file the following lines (Note: The call to
`kubectl_configure` must be before the call to `k8s_repositories`):

```python
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
name = "io_bazel_rules_docker",
commit = "{HEAD}",
remote = "https://github.com/bazelbuild/rules_docker.git",
)

load(
"@io_bazel_rules_docker//container:container.bzl",
container_repositories = "repositories",
)

container_repositories()

# This requires rules_docker to be fully instantiated before
# it is pulled in.
git_repository(
name = "io_bazel_rules_k8s",
commit = "{HEAD}",
remote = "https://github.com/bazelbuild/rules_k8s.git",
)

load("@io_bazel_rules_k8s//toolchains/kubectl:kubectl_configure.bzl", "kubectl_configure")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
# Download the v1.10.0 kubectl binary for the Linux x86 64 bit platform.
http_file(
name="k8s_binary",
downloaded_file_path = "kubectl",
sha256="49f7e5791d7cd91009c728eb4dc1dbf9ee1ae6a881be6b970e631116065384c3",
executable=True,
urls=["https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64/kubectl"],
)
# Configure the kubectl toolchain to use the downloaded prebuilt v1.10.0
# kubectl binary.
kubectl_configure(name="k8s_config", kubectl_path="@k8s_binary//file")
k8s_repositories()
```



## Using the kubectl toolchain

The information below will be helpful if:
Expand Down
27 changes: 22 additions & 5 deletions toolchains/kubectl/kubectl_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ load(":defaults.bzl",

def _impl(repository_ctx):
substitutions = None
label = None
if repository_ctx.attr.build_srcs:
kubectl_target = "@io_kubernetes//cmd/kubectl:kubectl"
substitutions = {"%{KUBECTL_TARGET}": "%s" % kubectl_target}
template = Label("@io_bazel_rules_k8s//toolchains/kubectl:BUILD.target.tpl")
elif repository_ctx.attr.kubectl_path != None:
substitutions = {"%{KUBECTL_TARGET}": "%s" % repository_ctx.attr.kubectl_path}
template = Label("@io_bazel_rules_k8s//toolchains/kubectl:BUILD.target.tpl")
else:
kubectl_tool_path = repository_ctx.which("kubectl") or ""
substitutions = {"%{KUBECTL_TOOL}": "%s" % kubectl_tool_path}
Expand All @@ -44,12 +46,18 @@ def _impl(repository_ctx):
"BUILD",
template,
substitutions,
False,
False
)

_kubectl_configure = repository_rule(
implementation = _impl,
attrs = {
"kubectl_path": attr.label(
allow_single_file=True,
mandatory=False,
doc = "Optional. Path to a prebuilt custom kubectl binary file or" +
" label. Can't be used together with attribute 'build_srcs'.",
),
"build_srcs": attr.bool(
doc = "Optional. Set to true to build kubectl from sources.",
default = False,
Expand Down Expand Up @@ -96,18 +104,24 @@ def kubectl_configure(name, **kwargs):
Required Args
name: A unique name for this rule.
Default Args:
build_srcs: Optional. Set to true to build kubectl from sources. Default: False
k8s_commit: Otional. Commit / release tag at which to build kubectl
build_srcs: Optional. Set to true to build kubectl from sources. Default: False.
Can't be specified if kubectl_path is specified.
k8s_commit: Optional. Commit / release tag at which to build kubectl
from. Default is defined as k8s_tag in :defaults.bzl.
k8s_sha256: Optional. sha256 of commit at which to build kubectl from.
Default is defined as k8s_sha256 in :defaults.bzl.
kubectl_path: Optional. Use the kubectl binary at the given path or label.
This can't be used with 'build_srcs'.
Note: Not all versions/commits of kubernetes project can be used to compile
kubectl from an external repo. Notably, we have only tested with v1.13.0-beta.1
or above. Note this rule has a hardcoded pointer to io_kubernetes_build repo
if your commit (above v1.13.0-beta.1) does not work due to problems,
related to @io_kubernetes_build repo, please send a PR to update these values.
"""
build_srcs = False
if "build_srcs" in kwargs and "kubectl_path" in kwargs:
fail("Attributes 'build_srcs' and 'kubectl_path' can't be specified at"+
" the same time")
if "build_srcs" in kwargs and kwargs["build_srcs"]:
build_srcs = True
_ensure_all_provided("kubectl_configure",
Expand Down Expand Up @@ -143,4 +157,7 @@ def kubectl_configure(name, **kwargs):
k8s_repo_tools_commit
)],
)
_kubectl_configure(name = name, build_srcs = build_srcs)
if "kubectl_path" in kwargs:
_kubectl_configure(name = name, kubectl_path=kwargs["kubectl_path"])
else:
_kubectl_configure(name = name, build_srcs = build_srcs)
4 changes: 2 additions & 2 deletions toolchains/kubectl/kubectl_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ KubectlInfo = provider(
doc = "Information about how to invoke the kubectl tool.",
fields = {
"tool_path": "Path to the kubectl executable",
"tool_target": "Target to build kubectl executable",
"tool_target": "A kubectl executable target built from source or downloaded.",
},
)

Expand All @@ -42,7 +42,7 @@ kubectl_toolchain = rule(
mandatory = False,
),
"tool_target": attr.label(
doc = "Target to build kubectl from source.",
doc = "Target to build kubectl from source or a downloaded kubectl binary.",
mandatory = False,
),
},
Expand Down

0 comments on commit d724919

Please sign in to comment.