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

Commit d724919

Browse files
authored
Allow downloading prebuilt binary in kubectl toolchain (#253)
* 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
1 parent 59b62a9 commit d724919

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

toolchains/kubectl/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ rule. At the moment, the tool's configuration does one of the following:
66

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

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

96+
### Download a custom kubectl binary
97+
98+
If you want to download a standard binary released by the kubernetes project,
99+
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).
100+
For example, if you want to use kubectl v1.10.0 on a x86 64 bit Linux platform,
101+
add to your `WORKSPACE` file the following lines (Note: The call to
102+
`kubectl_configure` must be before the call to `k8s_repositories`):
103+
104+
```python
105+
load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")
106+
107+
git_repository(
108+
name = "io_bazel_rules_docker",
109+
commit = "{HEAD}",
110+
remote = "https://github.com/bazelbuild/rules_docker.git",
111+
)
112+
113+
load(
114+
"@io_bazel_rules_docker//container:container.bzl",
115+
container_repositories = "repositories",
116+
)
117+
118+
container_repositories()
119+
120+
# This requires rules_docker to be fully instantiated before
121+
# it is pulled in.
122+
git_repository(
123+
name = "io_bazel_rules_k8s",
124+
commit = "{HEAD}",
125+
remote = "https://github.com/bazelbuild/rules_k8s.git",
126+
)
127+
128+
load("@io_bazel_rules_k8s//toolchains/kubectl:kubectl_configure.bzl", "kubectl_configure")
129+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_file")
130+
# Download the v1.10.0 kubectl binary for the Linux x86 64 bit platform.
131+
http_file(
132+
name="k8s_binary",
133+
downloaded_file_path = "kubectl",
134+
sha256="49f7e5791d7cd91009c728eb4dc1dbf9ee1ae6a881be6b970e631116065384c3",
135+
executable=True,
136+
urls=["https://storage.googleapis.com/kubernetes-release/release/v1.10.0/bin/linux/amd64/kubectl"],
137+
)
138+
# Configure the kubectl toolchain to use the downloaded prebuilt v1.10.0
139+
# kubectl binary.
140+
kubectl_configure(name="k8s_config", kubectl_path="@k8s_binary//file")
141+
k8s_repositories()
142+
```
143+
144+
145+
94146
## Using the kubectl toolchain
95147

96148
The information below will be helpful if:

toolchains/kubectl/kubectl_configure.bzl

+22-5
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ load(":defaults.bzl",
3030

3131
def _impl(repository_ctx):
3232
substitutions = None
33-
label = None
3433
if repository_ctx.attr.build_srcs:
3534
kubectl_target = "@io_kubernetes//cmd/kubectl:kubectl"
3635
substitutions = {"%{KUBECTL_TARGET}": "%s" % kubectl_target}
3736
template = Label("@io_bazel_rules_k8s//toolchains/kubectl:BUILD.target.tpl")
37+
elif repository_ctx.attr.kubectl_path != None:
38+
substitutions = {"%{KUBECTL_TARGET}": "%s" % repository_ctx.attr.kubectl_path}
39+
template = Label("@io_bazel_rules_k8s//toolchains/kubectl:BUILD.target.tpl")
3840
else:
3941
kubectl_tool_path = repository_ctx.which("kubectl") or ""
4042
substitutions = {"%{KUBECTL_TOOL}": "%s" % kubectl_tool_path}
@@ -44,12 +46,18 @@ def _impl(repository_ctx):
4446
"BUILD",
4547
template,
4648
substitutions,
47-
False,
49+
False
4850
)
4951

5052
_kubectl_configure = repository_rule(
5153
implementation = _impl,
5254
attrs = {
55+
"kubectl_path": attr.label(
56+
allow_single_file=True,
57+
mandatory=False,
58+
doc = "Optional. Path to a prebuilt custom kubectl binary file or" +
59+
" label. Can't be used together with attribute 'build_srcs'.",
60+
),
5361
"build_srcs": attr.bool(
5462
doc = "Optional. Set to true to build kubectl from sources.",
5563
default = False,
@@ -96,18 +104,24 @@ def kubectl_configure(name, **kwargs):
96104
Required Args
97105
name: A unique name for this rule.
98106
Default Args:
99-
build_srcs: Optional. Set to true to build kubectl from sources. Default: False
100-
k8s_commit: Otional. Commit / release tag at which to build kubectl
107+
build_srcs: Optional. Set to true to build kubectl from sources. Default: False.
108+
Can't be specified if kubectl_path is specified.
109+
k8s_commit: Optional. Commit / release tag at which to build kubectl
101110
from. Default is defined as k8s_tag in :defaults.bzl.
102111
k8s_sha256: Optional. sha256 of commit at which to build kubectl from.
103112
Default is defined as k8s_sha256 in :defaults.bzl.
113+
kubectl_path: Optional. Use the kubectl binary at the given path or label.
114+
This can't be used with 'build_srcs'.
104115
Note: Not all versions/commits of kubernetes project can be used to compile
105116
kubectl from an external repo. Notably, we have only tested with v1.13.0-beta.1
106117
or above. Note this rule has a hardcoded pointer to io_kubernetes_build repo
107118
if your commit (above v1.13.0-beta.1) does not work due to problems,
108119
related to @io_kubernetes_build repo, please send a PR to update these values.
109120
"""
110121
build_srcs = False
122+
if "build_srcs" in kwargs and "kubectl_path" in kwargs:
123+
fail("Attributes 'build_srcs' and 'kubectl_path' can't be specified at"+
124+
" the same time")
111125
if "build_srcs" in kwargs and kwargs["build_srcs"]:
112126
build_srcs = True
113127
_ensure_all_provided("kubectl_configure",
@@ -143,4 +157,7 @@ def kubectl_configure(name, **kwargs):
143157
k8s_repo_tools_commit
144158
)],
145159
)
146-
_kubectl_configure(name = name, build_srcs = build_srcs)
160+
if "kubectl_path" in kwargs:
161+
_kubectl_configure(name = name, kubectl_path=kwargs["kubectl_path"])
162+
else:
163+
_kubectl_configure(name = name, build_srcs = build_srcs)

toolchains/kubectl/kubectl_toolchain.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ KubectlInfo = provider(
1919
doc = "Information about how to invoke the kubectl tool.",
2020
fields = {
2121
"tool_path": "Path to the kubectl executable",
22-
"tool_target": "Target to build kubectl executable",
22+
"tool_target": "A kubectl executable target built from source or downloaded.",
2323
},
2424
)
2525

@@ -42,7 +42,7 @@ kubectl_toolchain = rule(
4242
mandatory = False,
4343
),
4444
"tool_target": attr.label(
45-
doc = "Target to build kubectl from source.",
45+
doc = "Target to build kubectl from source or a downloaded kubectl binary.",
4646
mandatory = False,
4747
),
4848
},

0 commit comments

Comments
 (0)