@@ -16,16 +16,79 @@ Defines a repository rule for configuring the kubectl tool.
16
16
"""
17
17
18
18
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" )
21
29
22
30
repository_ctx .template (
23
31
"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 ,
27
35
)
28
36
29
- kubectl_configure = repository_rule (
37
+ _kubectl_configure = repository_rule (
30
38
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
+ },
31
46
)
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 )
0 commit comments