"""Contains macros for working with sigs.k8s.io/controller-tools in Bazel""" GO = "@go_sdk//:bin/go" CONTROLLER_GEN = "@io_k8s_sigs_controller_tools//cmd/controller-gen" # Common pieces for both generation macros ARGS = [ "$(location %s)" % GO, "$(location %s)" % CONTROLLER_GEN, ] DATA = [ GO, CONTROLLER_GEN, ] def gen_rbac(name, rbac_out_path, role_name, pkg = "", outpath_relative_to_repo_root = False, paths = "./..."): """Generates RBAC manifests for a given Go package containing Kubebuilder annotations. Args: name: The name of the produced rule, and the name this rule will be callable from. e.g., name = "gen_talaria_rbac" in controllers/talaria/BUILD.bazel is callable as //controllers/talaria:gen_talaria_rbac. rbac_out_path: Path to generate RBAC manifests to role_name: metadata.name for produced Role/ClusterRole. Pick something identifiable. pkg: Relative path from repo root to Go package to generate RBAC from. Don't provide if this is being defined in the desired package. outpath_relative_to_repo_root: Whether or not the rbac_out_path should be evaluated as being relative from the pkg or the repository root. paths: The path expression for which Go package(s) to generate RBAC manifests for. Defafults to recursively generating RBAC for everything under pkg. """ native.sh_binary( name = name, srcs = ["//hack/tools/controller-gen:gen-rbac.sh"], args = ARGS + [ pkg if pkg != "" else native.package_name(), role_name, rbac_out_path, str(outpath_relative_to_repo_root), paths, ], data = DATA, ) def gen_crds(name, crd_out_path, pkg = "", outpath_relative_to_repo_root = False, paths = "./...", gen_webhooks = False): """Generates CRD manifests for a given Go package containing Kubebuilder annotations. Args: name: The name of the produced rule, and the name this rule will be callable from. e.g., name = "gen_crds" in controllers/join-operator/BUILD.bazel is callable as //controllers/join-operator:gen_crds. crd_out_path: Path to generate RBAC manifests to pkg: Relative path from repo root to Go package to generate RBAC from. Don't provide if this is being defined in the desired package. outpath_relative_to_repo_root: Whether or not the crd_out_path should be evaluated as being relative from the pkg or the repository root. paths: The path expression for which Go package(s) to generate RBAC manifests for. Defafults to recursively generating RBAC for everything under pkg. gen_webhooks: whether or not to genrate webhook configuration as well """ native.sh_binary( name = name, srcs = ["//hack/tools/controller-gen:gen-crds.sh"], args = ARGS + [ pkg if pkg != "" else native.package_name(), crd_out_path, str(outpath_relative_to_repo_root), paths, str(gen_webhooks), ], data = DATA, ) # TODO: give this rule the same love as the above rules def gen_code(directory, name = "gen_code"): """Generates code for a given controller-runtime project (e.g., DeepCopy)""" native.sh_binary( name = name, srcs = ["//hack/tools/controller-gen:gen-code.sh"], args = ARGS + [directory], data = DATA, )