...
1"""Contains macros for working with sigs.k8s.io/controller-tools in Bazel"""
2GO = "@go_sdk//:bin/go"
3
4CONTROLLER_GEN = "@io_k8s_sigs_controller_tools//cmd/controller-gen"
5
6# Common pieces for both generation macros
7ARGS = [
8 "$(location %s)" % GO,
9 "$(location %s)" % CONTROLLER_GEN,
10]
11
12DATA = [
13 GO,
14 CONTROLLER_GEN,
15]
16
17def gen_rbac(name, rbac_out_path, role_name, pkg = "", outpath_relative_to_repo_root = False, paths = "./..."):
18 """Generates RBAC manifests for a given Go package containing Kubebuilder annotations.
19
20 Args:
21 name: The name of the produced rule, and the name this rule will be callable from.
22 e.g., name = "gen_talaria_rbac" in controllers/talaria/BUILD.bazel is callable
23 as //controllers/talaria:gen_talaria_rbac.
24 rbac_out_path: Path to generate RBAC manifests to
25 role_name: metadata.name for produced Role/ClusterRole. Pick something identifiable.
26 pkg: Relative path from repo root to Go package to generate RBAC from. Don't provide
27 if this is being defined in the desired package.
28 outpath_relative_to_repo_root: Whether or not the rbac_out_path should be evaluated as being
29 relative from the pkg or the repository root.
30 paths: The path expression for which Go package(s) to generate RBAC manifests for.
31 Defafults to recursively generating RBAC for everything under pkg.
32 """
33
34 native.sh_binary(
35 name = name,
36 srcs = ["//hack/tools/controller-gen:gen-rbac.sh"],
37 args = ARGS + [
38 pkg if pkg != "" else native.package_name(),
39 role_name,
40 rbac_out_path,
41 str(outpath_relative_to_repo_root),
42 paths,
43 ],
44 data = DATA,
45 )
46
47def gen_crds(name, crd_out_path, pkg = "", outpath_relative_to_repo_root = False, paths = "./...", gen_webhooks = False):
48 """Generates CRD manifests for a given Go package containing Kubebuilder annotations.
49
50 Args:
51 name: The name of the produced rule, and the name this rule will be callable from.
52 e.g., name = "gen_crds" in controllers/join-operator/BUILD.bazel is callable
53 as //controllers/join-operator:gen_crds.
54 crd_out_path: Path to generate RBAC manifests to
55 pkg: Relative path from repo root to Go package to generate RBAC from. Don't provide
56 if this is being defined in the desired package.
57 outpath_relative_to_repo_root: Whether or not the crd_out_path should be evaluated as being
58 relative from the pkg or the repository root.
59 paths: The path expression for which Go package(s) to generate RBAC manifests for.
60 Defafults to recursively generating RBAC for everything under pkg.
61 gen_webhooks: whether or not to genrate webhook configuration as well
62 """
63
64 native.sh_binary(
65 name = name,
66 srcs = ["//hack/tools/controller-gen:gen-crds.sh"],
67 args = ARGS + [
68 pkg if pkg != "" else native.package_name(),
69 crd_out_path,
70 str(outpath_relative_to_repo_root),
71 paths,
72 str(gen_webhooks),
73 ],
74 data = DATA,
75 )
76
77# TODO: give this rule the same love as the above rules
78def gen_code(directory, name = "gen_code"):
79 """Generates code for a given controller-runtime project (e.g., DeepCopy)"""
80 native.sh_binary(
81 name = name,
82 srcs = ["//hack/tools/controller-gen:gen-code.sh"],
83 args = ARGS + [directory],
84 data = DATA,
85 )
View as plain text