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

Commit ff2cbf0

Browse files
nlopezgichrislovecnm
authored andcommitted
Enable building kubectl hermetically as part of toolchain setup (#229)
* adding option to build kubectl from src * trying to build kubectl * remove kwargs from http_file call * using a newer version of kubernetes * use src version of kubectl in rules * run buildifier * fixing issue with e2e tests not finding tool * moving validation of toolchain attrs to toolchain rule * missin period * more missing periods * fixing resolution of kubectl path * fixning last issue with kubectl binary resolution
1 parent 2ced0eb commit ff2cbf0

File tree

7 files changed

+120
-15
lines changed

7 files changed

+120
-15
lines changed

k8s/k8s.bzl

+6-4
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ py_library(
3535
strip_prefix = "PyYAML-3.12/lib/yaml",
3636
)
3737

38-
# Register the default kubectl toolchain that expects the 'kubectl'
39-
# executable to be in the PATH
38+
# Register the default kubectl toolchain targets for supported platforms
39+
# note these work with the autoconfigured toolchain
4040
native.register_toolchains(
4141
"@io_bazel_rules_k8s//toolchains/kubectl:kubectl_linux_toolchain",
4242
"@io_bazel_rules_k8s//toolchains/kubectl:kubectl_osx_toolchain",
4343
"@io_bazel_rules_k8s//toolchains/kubectl:kubectl_windows_toolchain",
4444
)
4545

46-
# WORKSPACE target to configure the kubectl tool
47-
kubectl_configure(name = "local_k8s_config")
46+
excludes = native.existing_rules().keys()
47+
if "k8s_config" not in excludes:
48+
# WORKSPACE target to configure the kubectl tool
49+
kubectl_configure(name = "k8s_config", build_srcs = True)

k8s/object.bzl

+5-1
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,13 @@ def _common_impl(ctx):
173173
namespace_arg = "--namespace=\"" + namespace_arg + "\""
174174

175175
kubectl_tool_info = ctx.toolchains["@io_bazel_rules_k8s//toolchains/kubectl:toolchain_type"].kubectlinfo
176+
kubectl_tool = kubectl_tool_info.tool_path
177+
if not kubectl_tool_info.tool_path:
178+
kubectl_tool = _runfiles(ctx, kubectl_tool_info.tool_target.files.to_list()[0])
179+
files += kubectl_tool_info.tool_target.files.to_list()
176180

177181
substitutions = {
178-
"%{kubectl_tool}": kubectl_tool_info.tool_path,
182+
"%{kubectl_tool}": kubectl_tool,
179183
"%{cluster}": cluster_arg,
180184
"%{context}": context_arg,
181185
"%{user}": user_arg,

toolchains/kubectl/BUILD

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ toolchain(
3232
"@bazel_tools//platforms:linux",
3333
"@bazel_tools//platforms:x86_64",
3434
],
35-
toolchain = "@local_k8s_config//:toolchain",
35+
toolchain = "@k8s_config//:toolchain",
3636
toolchain_type = ":toolchain_type",
3737
)
3838

@@ -42,7 +42,7 @@ toolchain(
4242
"@bazel_tools//platforms:osx",
4343
"@bazel_tools//platforms:x86_64",
4444
],
45-
toolchain = "@local_k8s_config//:toolchain",
45+
toolchain = "@k8s_config//:toolchain",
4646
toolchain_type = ":toolchain_type",
4747
)
4848

@@ -52,6 +52,6 @@ toolchain(
5252
"@bazel_tools//platforms:windows",
5353
"@bazel_tools//platforms:x86_64",
5454
],
55-
toolchain = "@local_k8s_config//:toolchain",
55+
toolchain = "@k8s_config//:toolchain",
5656
toolchain_type = ":toolchain_type",
5757
)
File renamed without changes.

toolchains/kubectl/BUILD.target.tpl

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Copyright 2018 The Bazel Authors. All rights reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
"""
15+
This BUILD file is auto-generated from toolchains/kubectl/BUILD.tpl
16+
"""
17+
18+
package(default_visibility = ["//visibility:public"])
19+
20+
load("@io_bazel_rules_k8s//toolchains/kubectl:kubectl_toolchain.bzl", "kubectl_toolchain")
21+
22+
kubectl_toolchain(
23+
name = "toolchain",
24+
tool_target = "%{KUBECTL_TARGET}",
25+
)

toolchains/kubectl/kubectl_configure.bzl

+69-6
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,79 @@ Defines a repository rule for configuring the kubectl tool.
1616
"""
1717

1818
def _impl(repository_ctx):
19-
20-
kubectl_tool_path = repository_ctx.which("kubectl")
19+
substitutions = None
20+
label = None
21+
if repository_ctx.attr.build_srcs:
22+
kubectl_target = "@io_kubernetes//cmd/kubectl:kubectl"
23+
substitutions = {"%{KUBECTL_TARGET}": "%s" % kubectl_target}
24+
template = Label("@io_bazel_rules_k8s//toolchains/kubectl:BUILD.target.tpl")
25+
else:
26+
kubectl_tool_path = repository_ctx.which("kubectl")
27+
substitutions = {"%{KUBECTL_TOOL}": "%s" % kubectl_tool_path}
28+
template = Label("@io_bazel_rules_k8s//toolchains/kubectl:BUILD.path.tpl")
2129

2230
repository_ctx.template(
2331
"BUILD",
24-
Label("@io_bazel_rules_k8s//toolchains/kubectl:BUILD.tpl"),
25-
{"%{KUBECTL_TOOL}": "%s" % kubectl_tool_path},
26-
False
32+
template,
33+
substitutions,
34+
False,
2735
)
2836

29-
kubectl_configure = repository_rule(
37+
_kubectl_configure = repository_rule(
3038
implementation = _impl,
39+
attrs = {
40+
"build_srcs": attr.bool(
41+
doc = "Optional. Set to true to build kubectl from sources.",
42+
default = False,
43+
mandatory = False,
44+
),
45+
},
3146
)
47+
48+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
49+
50+
def kubectl_configure(name, **kwargs):
51+
"""
52+
Creates an external repository with a kubectl_toolchain target
53+
properly configured.
54+
55+
Args:
56+
**kwargs:
57+
Required Args
58+
name: A unique name for this rule.
59+
Default Args:
60+
build_srcs: Optional. Set to true to build kubectl from sources. Default: False
61+
k8s_commit: Otional. Commit / release tag at which to build kubectl
62+
from. Default "v1.13.0-beta.1"
63+
k8s_sha256: Otional. sha256 of commit at which to build kubectl from.
64+
Default <valid sha for default version>.
65+
k8s_prefix: Otional. Prefix to strip from commit / release archive.
66+
Typically the same as the commit, or Kubernetes-<release tag>.
67+
Default <valid prefix for default version>.
68+
Note: Not all versions/commits of kubernetes project can be used to compile
69+
kubectl from an external repo. Notably, we have only tested with v1.13.0-beta.1
70+
or above. Note this rule has a hardcoded pointer to io_kubernetes_build repo
71+
if your commit (above v1.13.0-beta.1) does not work due to problems,
72+
related to @io_kubernetes_build repo, please send a PR to update these values.
73+
"""
74+
build_srcs = False
75+
if "build_srcs" in kwargs and kwargs["build_srcs"]:
76+
build_srcs = True
77+
78+
# We keep these defaults here as they are only used by in this macro and not by the repo rule.
79+
k8s_commit = kwargs["k8s_commit"] if "k8s_commit" in kwargs else "v1.13.0-beta.1"
80+
k8s_sha256 = kwargs["k8s_sha256"] if "k8s_sha256" in kwargs else "dfb39ce36284c1ce228954ca12bf016c09be61e40a875e8af4fff84e116bd3a7"
81+
k8s_prefix = kwargs["k8s_prefix"] if "k8s_prefix" in kwargs else "kubernetes-1.13.0-beta.1"
82+
http_archive(
83+
name = "io_kubernetes",
84+
sha256 = k8s_sha256,
85+
strip_prefix = k8s_prefix,
86+
urls = [("https://github.com/kubernetes/kubernetes/archive/%s.tar.gz" % k8s_commit)],
87+
)
88+
http_archive(
89+
name = "io_kubernetes_build",
90+
sha256 = "21160531ea8a9a4001610223ad815622bf60671d308988c7057168a495a7e2e8",
91+
strip_prefix = "repo-infra-b4bc4f1552c7fc1d4654753ca9b0e5e13883429f",
92+
urls = ["https://github.com/kubernetes/repo-infra/archive/b4bc4f1552c7fc1d4654753ca9b0e5e13883429f.tar.gz"],
93+
)
94+
_kubectl_configure(name = name, build_srcs = build_srcs)

toolchains/kubectl/kubectl_toolchain.bzl

+12-1
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,31 @@ 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",
2223
},
2324
)
2425

2526
def _kubectl_toolchain_impl(ctx):
27+
if not ctx.attr.tool_path and not ctx.attr.tool_target:
28+
fail("Invalid kubectl_toolchain. kubectl_toolchain must have either tool_path or tool_target.")
2629
toolchain_info = platform_common.ToolchainInfo(
2730
kubectlinfo = KubectlInfo(
2831
tool_path = ctx.attr.tool_path,
32+
tool_target = ctx.attr.tool_target,
2933
),
3034
)
3135
return [toolchain_info]
3236

3337
kubectl_toolchain = rule(
3438
implementation = _kubectl_toolchain_impl,
3539
attrs = {
36-
"tool_path": attr.string(),
40+
"tool_path": attr.string(
41+
doc = "Absolute path to a pre-installed kubectl binary.",
42+
mandatory = False,
43+
),
44+
"tool_target": attr.label(
45+
doc = "Target to build kubectl from source.",
46+
mandatory = False,
47+
),
3748
},
3849
)

0 commit comments

Comments
 (0)