├── .github └── workflows │ └── main.yaml ├── .gitignore ├── BUILD.bazel ├── LICENSE ├── README.md ├── WORKSPACE ├── controller-gen ├── BUILD.bazel ├── bin │ ├── controller-gen.darwin │ └── controller-gen.linux ├── controller-gen-toolchain.bzl ├── controller-gen.bzl └── deps.bzl ├── kubebuilder ├── BUILD.bazel ├── BUILD.sdk.bazel ├── def.bzl ├── generate_sdk_list.py ├── kubebuilder-runner.sh.template ├── kubebuilder_pwd.bash.in ├── sdk.bzl └── sdk_list.bzl ├── kustomize ├── BUILD.bazel ├── bin │ ├── kustomize.darwin │ └── kustomize.linux ├── deps.bzl ├── kustomize-toolchain.bzl └── kustomize.bzl ├── scripts ├── build-controller-gen.sh └── get-kustomize.sh └── test ├── BUILD.bazel ├── apackage.go └── apackage_test.go /.github/workflows/main.yaml: -------------------------------------------------------------------------------- 1 | name: Release 2 | on: 3 | push: 4 | # Sequence of patterns matched against refs/tags 5 | tags: 6 | - 'v*' # Push events to matching v*, i.e. v1.0, v20.15.10 7 | 8 | jobs: 9 | build: 10 | name: Create Release 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Checkout code 14 | uses: actions/checkout@v2 15 | - name: Create Release 16 | id: create_release 17 | uses: actions/create-release@v1 18 | env: 19 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token 20 | with: 21 | tag_name: ${{ github.ref }} 22 | release_name: Release ${{ github.ref }} 23 | draft: false 24 | prerelease: false 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bazel-* 2 | 3 | build/tmp 4 | -------------------------------------------------------------------------------- /BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@rules_pkg//:pkg.bzl", "pkg_tar") 2 | load("@rules_kubebuilder//kubebuilder:def.bzl", "kubebuilder") 3 | 4 | pkg_tar( 5 | name = "release", 6 | srcs = glob([ 7 | "*.bazel", 8 | "*.bzl", 9 | ]) + [ 10 | "WORKSPACE", 11 | ], 12 | extension = "tar.gz", 13 | package_dir = "rules_kubebuilder", 14 | deps = [ 15 | "//controller-gen:release", 16 | "//kubebuilder:release", 17 | "//kustomize:release", 18 | ], 19 | ) 20 | 21 | # This is just an example of how to run kubebuilder via bazel 22 | kubebuilder(name = "kubebuilder") 23 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # rules_kubebuilder 2 | 3 | These bazel rules download and make available the [Kubebuilder SDK](https://github.com/kubernetes-sigs/kubebuilder) for building kubernetes operators in bazel. 4 | 5 | To use these rules, add the following to your `WORKSPACE` file: 6 | 7 | ```starlark 8 | load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository") 9 | 10 | git_repository( 11 | name = "rules_kubebuilder", 12 | branch = "main", 13 | remote = "https://github.com/ob/rules_kubebuilder.git", 14 | ) 15 | 16 | load("@rules_kubebuilder//kubebuilder:sdk.bzl", "kubebuilder_register_sdk") 17 | 18 | kubebuilder_register_sdk(version = "2.3.1") 19 | 20 | load("@rules_kubebuilder//controller-gen:deps.bzl", "controller_gen_register_toolchain") 21 | 22 | controller_gen_register_toolchain() 23 | 24 | load("@rules_kubebuilder//kustomize:deps.bzl", "kustomize_register_toolchain") 25 | 26 | kustomize_register_toolchain() 27 | ``` 28 | 29 | And in your `go_test()` files, add `etcd` as a data dependency like this: 30 | 31 | ```starlark 32 | go_test( 33 | name = "go_default_test", 34 | srcs = ["apackage_test.go"], 35 | data = [ 36 | "@kubebuilder_sdk//:bin/etcd", 37 | ], 38 | embed = [":go_default_library"], 39 | deps = [ 40 | ], 41 | ) 42 | ``` 43 | You'll need to run the test as: 44 | 45 | ```shell 46 | bazel test --test_env=KUBEBUILDER_ASSETS=$(bazel info execution_root 2>/dev/null)/$(bazel run @kubebuilder_sdk//:pwd 2>/dev/null) //... 47 | ``` 48 | 49 | You can also add the following to `BUILD.bazel` at the root of your workspace: 50 | 51 | ```starlark 52 | load("@rules_kubebuilder//kubebuilder:def.bzl", "kubebuilder") 53 | kubebuilder(name = "kubebuilder") 54 | ``` 55 | 56 | to be able to run `kubebuilder` like so: 57 | 58 | ```shell 59 | bazel run //:kubebuilder -- --help 60 | ``` 61 | 62 | ## Controller-gen 63 | 64 | In order to use `controller-gen` you will need to do something like the following in your `api/v1alpha1` directory (essentially where the `*_type.go` files are): 65 | 66 | ```starlark 67 | load("@io_bazel_rules_go//go:def.bzl", "go_library") 68 | load( 69 | "@rules_kubebuilder//controller-gen:controller-gen.bzl", 70 | "controller_gen_crd", 71 | "controller_gen_object", 72 | "controller_gen_rbac", 73 | ) 74 | 75 | filegroup( 76 | name = "srcs", 77 | srcs = [ 78 | "groupversion_info.go", 79 | # your source files here, except for zz_generated_deepcopy.go 80 | ], 81 | ) 82 | 83 | DEPS = [ 84 | "@io_k8s_api//core/v1:go_default_library", 85 | "@io_k8s_apimachinery//pkg/api/resource:go_default_library", 86 | "@io_k8s_apimachinery//pkg/apis/meta/v1:go_default_library", 87 | "@io_k8s_apimachinery//pkg/runtime:go_default_library", 88 | "@io_k8s_apimachinery//pkg/runtime/schema:go_default_library", 89 | "@io_k8s_sigs_controller_runtime//pkg/scheme:go_default_library", 90 | ] 91 | 92 | controller_gen_object( 93 | name = "generated_sources", 94 | srcs = [ 95 | ":srcs", 96 | ], 97 | deps = DEPS, 98 | ) 99 | 100 | # keep 101 | go_library( 102 | name = "go_default_library", 103 | srcs = [ 104 | "generated_sources", 105 | "srcs", 106 | ], 107 | importpath = "yourdomain.com/your-operator/api/v1alpha1", 108 | visibility = ["//visibility:public"], 109 | deps = DEPS, 110 | ) 111 | 112 | controller_gen_crd( 113 | name = "crds", 114 | srcs = [ 115 | ":srcs", 116 | ], 117 | visibility = ["//visibility:public"], 118 | deps = DEPS, 119 | ) 120 | ``` 121 | 122 | ## Developers 123 | 124 | The toolchain that describes `controller-gen` needs to be built and the binaries committed so that 125 | they can be used. Fortunately Go supports cross compiling so in order to build the controller, you'll 126 | need to get and install Go either from [their download page](https://golang.org/doc/install) or from 127 | homebrew by running 128 | 129 | ```shell 130 | brew install golang 131 | ``` 132 | 133 | After that you can run the script in `scripts/build-controller-gen.sh` which will compile `controller-gen` 134 | for both Linux and macOS. 135 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "rules_kubebuilder") 2 | 3 | load("@rules_kubebuilder//kubebuilder:sdk.bzl", "kubebuilder_register_sdk") 4 | 5 | kubebuilder_register_sdk(version = "2.3.1") 6 | 7 | ## Next section is for testing in this same MP. 8 | 9 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") 10 | 11 | http_archive( 12 | name = "rules_pkg", 13 | sha256 = "6b5969a7acd7b60c02f816773b06fcf32fbe8ba0c7919ccdc2df4f8fb923804a", 14 | urls = [ 15 | "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.3.0/rules_pkg-0.3.0.tar.gz", 16 | "https://github.com/bazelbuild/rules_pkg/releases/download/0.3.0/rules_pkg-0.3.0.tar.gz", 17 | ], 18 | ) 19 | 20 | load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") 21 | 22 | rules_pkg_dependencies() 23 | 24 | http_archive( 25 | name = "io_bazel_rules_go", 26 | sha256 = "b4433651f57560237681cb9caa969106aba614f5b1e66fefa5834c42b8013b42", 27 | urls = [ 28 | "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.24.6/rules_go-v0.24.6.tar.gz", 29 | "https://github.com/bazelbuild/rules_go/releases/download/v0.24.6/rules_go-v0.24.6.tar.gz", 30 | ], 31 | ) 32 | 33 | load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") 34 | 35 | go_rules_dependencies() 36 | 37 | go_register_toolchains() 38 | -------------------------------------------------------------------------------- /controller-gen/BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@rules_kubebuilder//controller-gen:controller-gen-toolchain.bzl", "controller_gen_toolchain") 2 | load("@rules_pkg//:pkg.bzl", "pkg_tar") 3 | 4 | exports_files( 5 | [ 6 | "bin/controller-gen.linux", 7 | "bin/controller-gen.darwin", 8 | ], 9 | visibility = ["//visibility:public"], 10 | ) 11 | 12 | filegroup( 13 | name = "srcs", 14 | srcs = glob([ 15 | "*.bzl", 16 | "**/*.bzl", 17 | ]), 18 | visibility = ["//visibility:public"], 19 | ) 20 | 21 | pkg_tar( 22 | name = "release-bin", 23 | srcs = glob(["bin/*"]), 24 | package_dir = "/bin", 25 | ) 26 | 27 | pkg_tar( 28 | name = "release", 29 | srcs = [ 30 | "BUILD.bazel", 31 | ":srcs", 32 | ], 33 | package_dir = "controller-gen", 34 | visibility = ["//visibility:public"], 35 | deps = [":release-bin"], 36 | ) 37 | 38 | toolchain_type(name = "toolchain") 39 | 40 | controller_gen_toolchain( 41 | name = "controller_gen_linux", 42 | controller_gen_bin = "@rules_kubebuilder//controller-gen:bin/controller-gen.linux", 43 | ) 44 | 45 | controller_gen_toolchain( 46 | name = "controller_gen_darwin", 47 | controller_gen_bin = "@rules_kubebuilder//controller-gen:bin/controller-gen.darwin", 48 | ) 49 | 50 | toolchain( 51 | name = "controller_gen_linux_toolchain", 52 | exec_compatible_with = [ 53 | "@platforms//os:linux", 54 | "@platforms//cpu:x86_64", 55 | ], 56 | toolchain = ":controller_gen_linux", 57 | toolchain_type = ":toolchain", 58 | visibility = ["//visibility:public"], 59 | ) 60 | 61 | toolchain( 62 | name = "controller_gen_darwin_toolchain", 63 | exec_compatible_with = [ 64 | "@platforms//os:osx", 65 | "@platforms//cpu:x86_64", 66 | ], 67 | toolchain = ":controller_gen_darwin", 68 | toolchain_type = ":toolchain", 69 | visibility = ["//visibility:public"], 70 | ) 71 | -------------------------------------------------------------------------------- /controller-gen/bin/controller-gen.darwin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ob/rules_kubebuilder/6be1418107fc4b763c43878928096aaac3ea2354/controller-gen/bin/controller-gen.darwin -------------------------------------------------------------------------------- /controller-gen/bin/controller-gen.linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ob/rules_kubebuilder/6be1418107fc4b763c43878928096aaac3ea2354/controller-gen/bin/controller-gen.linux -------------------------------------------------------------------------------- /controller-gen/controller-gen-toolchain.bzl: -------------------------------------------------------------------------------- 1 | """ Toolchain definitions for controller-gen 2 | """ 3 | 4 | ControllerGenInfo = provider( 5 | doc = "Information about how to invoke controller-gen", 6 | fields = ["controller_gen_bin"], 7 | ) 8 | 9 | def _controller_gen_toolchain_impl(ctx): 10 | toolchain_info = platform_common.ToolchainInfo( 11 | controller_gen_info = ControllerGenInfo( 12 | controller_gen_bin = ctx.file.controller_gen_bin, 13 | ), 14 | ) 15 | return [toolchain_info] 16 | 17 | controller_gen_toolchain = rule( 18 | implementation = _controller_gen_toolchain_impl, 19 | attrs = { 20 | "controller_gen_bin": attr.label(allow_single_file = True), 21 | }, 22 | ) 23 | -------------------------------------------------------------------------------- /controller-gen/controller-gen.bzl: -------------------------------------------------------------------------------- 1 | """ Rules to run controller-gen 2 | """ 3 | 4 | load("@io_bazel_rules_go//go:def.bzl", "go_context", "go_path") 5 | load("@io_bazel_rules_go//go/private:providers.bzl", "GoPath") 6 | 7 | def _controller_gen_action(ctx, cg_cmd, outputs, output_path): 8 | """ Run controller-gen in the sandbox. 9 | 10 | This function sets up the necessary dependencies in the Bazel sandbox to 11 | run controller-gen (which compiles Go code), then creates an action that 12 | runs it. 13 | 14 | Args: 15 | ctx - The Rule's context 16 | cg_cmd - The controller-gen subcommand to run 17 | output - List of File/Dir produced by the action (delcare dependencies) 18 | output_path - Path of outputs 19 | """ 20 | 21 | # TODO: what should GOPATH be if there are no dependencies? 22 | go_ctx = go_context(ctx) 23 | cg_info = ctx.toolchains["@rules_kubebuilder//controller-gen:toolchain"].controller_gen_info 24 | gopath = "" 25 | if ctx.attr.gopath_dep: 26 | gopath = "$(pwd)/" + ctx.bin_dir.path + "/" + ctx.attr.gopath_dep[GoPath].gopath 27 | 28 | cmd = """ 29 | source <($PWD/{godir}/go env) && 30 | export PATH=$GOROOT/bin:$PWD/{godir}:$PATH && 31 | export GOPATH={gopath} && 32 | mkdir -p .gocache && 33 | export GOCACHE=$PWD/.gocache && 34 | {cmd} {args} 35 | """.format( 36 | godir = go_ctx.go.path[:-1 - len(go_ctx.go.basename)], 37 | gopath = gopath, 38 | cmd = "$(pwd)/" + cg_info.controller_gen_bin.path, 39 | args = "{cg_cmd} paths={{{files}}} output:dir={outputpath}".format( 40 | cg_cmd = cg_cmd, 41 | files = ",".join([f.path for f in ctx.files.srcs]), 42 | outputpath = output_path, 43 | ), 44 | ) 45 | ctx.actions.run_shell( 46 | mnemonic = "ControllerGen", 47 | outputs = outputs, 48 | inputs = _inputs(ctx, go_ctx), 49 | env = _env(), 50 | command = cmd, 51 | tools = [ 52 | go_ctx.go, 53 | cg_info.controller_gen_bin, 54 | ], 55 | ) 56 | 57 | def _inputs(ctx, go_ctx): 58 | inputs = (ctx.files.srcs + go_ctx.sdk.srcs + go_ctx.sdk.tools + 59 | go_ctx.sdk.headers + go_ctx.stdlib.libs) 60 | 61 | if ctx.attr.gopath_dep: 62 | inputs += ctx.attr.gopath_dep.files.to_list() 63 | return inputs 64 | 65 | def _env(): 66 | return { 67 | "GO111MODULE": "off", # explicitly relying on passed in go_path to not download modules while doing codegen 68 | } 69 | 70 | def _controller_gen_crd_impl(ctx): 71 | outputdir = ctx.actions.declare_directory(ctx.label.name) 72 | cg_cmd = "crd" 73 | extra_args = [] 74 | if ctx.attr.trivialVersions: 75 | extra_args.append("trivialVersions=true") 76 | if ctx.attr.preserveUnknownFields: 77 | extra_args.append("preserveUnknownFields=true") 78 | if ctx.attr.crdVersions: 79 | fail("Unsuppored argument, please file a feature request") 80 | if ctx.attr.maxDescLen: 81 | fail("Unsupported argument, please file a feature request") 82 | if len(extra_args) > 0: 83 | cg_cmd += ":{args}".format(args = ",".join(extra_args)) 84 | 85 | _controller_gen_action(ctx, cg_cmd, [outputdir], outputdir.path) 86 | 87 | return DefaultInfo( 88 | files = depset([outputdir]), 89 | ) 90 | 91 | def _controller_gen_object_impl(ctx): 92 | output = ctx.actions.declare_file("zz_generated.deepcopy.go") 93 | 94 | _controller_gen_action(ctx, "object", [output], output.dirname) 95 | 96 | return DefaultInfo( 97 | files = depset([output]), 98 | ) 99 | 100 | def _controller_gen_rbac_impl(ctx): 101 | outputdir = ctx.actions.declare_directory("rbac") 102 | cg_cmd = "rbac" 103 | extra_args = [] 104 | if ctx.attr.roleName: 105 | extra_args.append("roleName={}".format(ctx.attr.roleName)) 106 | if len(extra_args) > 0: 107 | cg_cmd += ":{args}".format(args = ",".join(extra_args)) 108 | 109 | _controller_gen_action(ctx, cg_cmd, [outputdir], outputdir.path) 110 | 111 | return DefaultInfo( 112 | files = depset([outputdir]), 113 | ) 114 | 115 | def _controller_gen_webhook_impl(ctx): 116 | outputdir = ctx.actions.declare_directory(ctx.label.name) 117 | 118 | _controller_gen_action(ctx, "webhook", [outputdir], outputdir.path) 119 | 120 | return DefaultInfo( 121 | files = depset([outputdir]), 122 | ) 123 | 124 | COMMON_ATTRS = { 125 | "srcs": attr.label_list( 126 | allow_empty = False, 127 | allow_files = True, 128 | mandatory = True, 129 | doc = "Source files passed to controller-gen", 130 | ), 131 | "gopath_dep": attr.label( 132 | providers = [GoPath], 133 | mandatory = False, 134 | doc = "Go lang dependencies, automatically bundled in a go_path() by the macro.", 135 | ), 136 | "_go_context_data": attr.label( 137 | default = "@io_bazel_rules_go//:go_context_data", 138 | doc = "Internal, context for go compilation.", 139 | ), 140 | } 141 | 142 | def _crd_extra_attrs(): 143 | ret = COMMON_ATTRS 144 | ret.update({ 145 | "trivialVersions": attr.bool( 146 | default = True, 147 | ), 148 | "preserveUnknownFields": attr.bool( 149 | default = False, 150 | ), 151 | "crdVersions": attr.string_list( 152 | ), 153 | "maxDescLen": attr.int( 154 | ), 155 | }) 156 | return ret 157 | 158 | def _rbac_extra_attrs(): 159 | ret = COMMON_ATTRS 160 | ret.update({ 161 | "roleName": attr.string( 162 | default = "manager-role", 163 | ), 164 | }) 165 | return ret 166 | 167 | def _webhook_extra_attrs(): 168 | ret = COMMON_ATTRS 169 | return ret 170 | 171 | def _toolchains(): 172 | return [ 173 | "@io_bazel_rules_go//go:toolchain", 174 | "@rules_kubebuilder//controller-gen:toolchain", 175 | ] 176 | 177 | _controller_gen_crd = rule( 178 | implementation = _controller_gen_crd_impl, 179 | attrs = _crd_extra_attrs(), 180 | toolchains = _toolchains(), 181 | doc = "Run the CRD generating portion of controller-gen. " + 182 | "The output directory will be the name of the rule.", 183 | ) 184 | 185 | _controller_gen_object = rule( 186 | implementation = _controller_gen_object_impl, 187 | attrs = COMMON_ATTRS, 188 | toolchains = _toolchains(), 189 | doc = "Run the code generating portion of controller-gen. " + 190 | "You can use the name of this rule as part of the `srcs` attribute " + 191 | " of a `go_library` rule.", 192 | ) 193 | 194 | _controller_gen_rbac = rule( 195 | implementation = _controller_gen_rbac_impl, 196 | attrs = _rbac_extra_attrs(), 197 | toolchains = _toolchains(), 198 | doc = "Run the role binding generating portion of controller-gen. " + 199 | "The output directory will be `rbac`.", 200 | ) 201 | 202 | _controller_gen_webhook = rule( 203 | implementation = _controller_gen_webhook_impl, 204 | attrs = _webhook_extra_attrs(), 205 | toolchains = _toolchains(), 206 | doc = "Run the webhook generating portion of controller-gen. " + 207 | "The output directory will be the name of the rule.", 208 | ) 209 | 210 | def _maybe_add_gopath_dep(name, kwargs): 211 | if kwargs.get("deps", None): 212 | gopath_name = name + "_controller_gen" 213 | go_path( 214 | name = gopath_name, 215 | deps = kwargs["deps"], 216 | ) 217 | kwargs["gopath_dep"] = gopath_name 218 | kwargs.pop("deps") 219 | 220 | def controller_gen_crd(name, **kwargs): 221 | _maybe_add_gopath_dep(name, kwargs) 222 | _controller_gen_crd(name = name, **kwargs) 223 | 224 | def controller_gen_object(name, **kwargs): 225 | _maybe_add_gopath_dep(name, kwargs) 226 | _controller_gen_object(name = name, **kwargs) 227 | 228 | def controller_gen_rbac(name, **kwargs): 229 | _maybe_add_gopath_dep(name, kwargs) 230 | _controller_gen_rbac(name = name, **kwargs) 231 | 232 | def controller_gen_webhook(name, **kwargs): 233 | _maybe_add_gopath_dep(name, kwargs) 234 | _controller_gen_webhook(name = name, **kwargs) 235 | -------------------------------------------------------------------------------- /controller-gen/deps.bzl: -------------------------------------------------------------------------------- 1 | """ Dependencies for controller-gen 2 | """ 3 | 4 | def controller_gen_register_toolchain(name = None): 5 | native.register_toolchains( 6 | "@rules_kubebuilder//controller-gen:controller_gen_linux_toolchain", 7 | "@rules_kubebuilder//controller-gen:controller_gen_darwin_toolchain", 8 | ) 9 | -------------------------------------------------------------------------------- /kubebuilder/BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@rules_pkg//:pkg.bzl", "pkg_tar") 2 | 3 | exports_files( 4 | [ 5 | "kubebuilder-runner.sh.template", 6 | "kubebuilder_pwd.bash.in", 7 | ], 8 | visibility = ["//visibility:public"], 9 | ) 10 | 11 | filegroup( 12 | name = "srcs", 13 | srcs = glob([ 14 | "*.bzl", 15 | "**/*.bzl", 16 | ]), 17 | visibility = ["//visibility:public"], 18 | ) 19 | 20 | filegroup( 21 | name = "allfiles", 22 | srcs = glob(["**/*"]), 23 | ) 24 | 25 | pkg_tar( 26 | name = "release", 27 | srcs = [ 28 | ":allfiles", 29 | ], 30 | package_dir = "kubebuilder", 31 | visibility = ["//visibility:public"], 32 | ) 33 | -------------------------------------------------------------------------------- /kubebuilder/BUILD.sdk.bazel: -------------------------------------------------------------------------------- 1 | load("@rules_kubebuilder//kubebuilder:sdk.bzl", "kubebuilder_pwd") 2 | 3 | exports_files([ 4 | "bin/etcd", 5 | "bin/kube-apiserver", 6 | "bin/kubebuilder", 7 | "bin/kubectl", 8 | ]) 9 | 10 | kubebuilder_pwd( 11 | name = "pwd", 12 | srcs = [ 13 | "bin/kubebuilder", 14 | ], 15 | kubebuilder_binary = "bin/kubebuilder", 16 | ) 17 | -------------------------------------------------------------------------------- /kubebuilder/def.bzl: -------------------------------------------------------------------------------- 1 | """ Rules and Macros to allow running kubebuilder from within bazel. 2 | """ 3 | 4 | def _kubebuilder_impl(ctx): 5 | """Implementation of the _kubebuilder rule. 6 | 7 | Args: 8 | ctx: the repository_context 9 | """ 10 | 11 | out_file = ctx.actions.declare_file(ctx.label.name + ".bash") 12 | substitutions = { 13 | "@@KUBEBUILDER_SHORT_PATH@@": ctx.executable._kubebuilder.short_path, 14 | } 15 | ctx.actions.expand_template( 16 | template = ctx.file._runner, 17 | output = out_file, 18 | substitutions = substitutions, 19 | is_executable = True, 20 | ) 21 | runfiles = ctx.runfiles(files = [ctx.executable._kubebuilder]) 22 | return [DefaultInfo( 23 | files = depset([out_file]), 24 | runfiles = runfiles, 25 | executable = out_file, 26 | )] 27 | 28 | _kubebuilder = rule( 29 | implementation = _kubebuilder_impl, 30 | attrs = { 31 | "_kubebuilder": attr.label( 32 | default = "@kubebuilder_sdk//:bin/kubebuilder", 33 | allow_single_file = True, 34 | cfg = "host", 35 | executable = True, 36 | ), 37 | "_runner": attr.label( 38 | default = "@rules_kubebuilder//kubebuilder:kubebuilder-runner.sh.template", 39 | allow_single_file = True, 40 | ), 41 | }, 42 | executable = True, 43 | ) 44 | 45 | def kubebuilder(**kwargs): 46 | """ Macro to allow running kubebuilder from within Bazel. 47 | 48 | Args: 49 | **kwargs: the arguments to the macro. 50 | """ 51 | tags = kwargs.get("tags", []) 52 | if "manual" not in tags: 53 | tags.append("manual") 54 | kwargs["tags"] = tags 55 | _kubebuilder(**kwargs) 56 | -------------------------------------------------------------------------------- /kubebuilder/generate_sdk_list.py: -------------------------------------------------------------------------------- 1 | import requests 2 | import os 3 | from dataclasses import dataclass 4 | from tqdm import tqdm 5 | from typing import List 6 | import json 7 | 8 | 9 | @dataclass 10 | class ReleaseItem: 11 | version: str 12 | linux_amd64_checksum: str 13 | darwin_amd64_checksum: str 14 | 15 | 16 | def main(): 17 | items = fetch_release_items() 18 | generate_sdk_list(items) 19 | 20 | 21 | def fetch_release_items(): 22 | if "GH_ACCESS_TOKEN" not in os.environ: 23 | print("You need to get an access token following the instructions in \n https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token") 24 | exit(1) 25 | access_token = os.getenv("GH_ACCESS_TOKEN") 26 | releases_response = requests.get('https://api.github.com/repos/kubernetes-sigs/kubebuilder/releases', headers={ 27 | "Accept": "application/vnd.github+json", 28 | "Authorization": f"token {access_token}" 29 | }) 30 | 31 | items = [] 32 | for release in tqdm(releases_response.json()): 33 | checksum_asset = [x for x in release["assets"] 34 | if x["name"] == "checksums.txt"][0] 35 | checksum_response = requests.get( 36 | checksum_asset["browser_download_url"]) 37 | checksum_dic = checksum_text_to_dic(checksum_response.text) 38 | linux_amd64_key = get_key_containing( 39 | list(checksum_dic.keys()), "linux_amd64") 40 | darwin_amd64_key = get_key_containing( 41 | list(checksum_dic.keys()), "darwin_amd64") 42 | 43 | items.append(ReleaseItem( 44 | release["tag_name"], checksum_dic[linux_amd64_key], checksum_dic[darwin_amd64_key])) 45 | 46 | return items 47 | 48 | 49 | def generate_sdk_list(items: List[ReleaseItem]): 50 | result = """## Useful constants. Generated by generate_sdk_list.py 51 | 52 | DEFAULT_VERSION = "2.3.1" 53 | 54 | SDK_VERSION_SHA256 = { 55 | """ 56 | sdk_dic = {} 57 | for item in items: 58 | sdk_dic[item.version.replace("v","")] = { 59 | "linux_amd64": item.linux_amd64_checksum, 60 | "darwin_amd64": item.darwin_amd64_checksum, 61 | } 62 | result += remove_first_line(json.dumps(sdk_dic, indent=4)) 63 | with open("sdk_list.bzl", "w") as f: 64 | f.write(result) 65 | print("output: sdk_list.bzl") 66 | 67 | 68 | def checksum_text_to_dic(text): 69 | splitted_texts = text.split("\n") 70 | result = {} 71 | for pair in splitted_texts: 72 | split_pair = pair.split(" ") 73 | if len(split_pair) == 2: 74 | result[split_pair[1]] = split_pair[0] 75 | return result 76 | 77 | 78 | def get_key_containing(keys, word): 79 | cands = [key for key in keys if word in key] 80 | if len(cands) == 1: 81 | return cands[0] 82 | return "" 83 | 84 | 85 | def remove_first_line(text): 86 | tmp = text.split("\n") 87 | return "\n".join(tmp[1:]) 88 | 89 | 90 | if __name__ == "__main__": 91 | main() 92 | -------------------------------------------------------------------------------- /kubebuilder/kubebuilder-runner.sh.template: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | KUBEBUILDER_SHORT_PATH="@@KUBEBUILDER_SHORT_PATH@@" 4 | 5 | kubebuilder_short_path=$(readlink $KUBEBUILDER_SHORT_PATH) 6 | 7 | cd "$BUILD_WORKSPACE_DIRECTORY" && "$kubebuilder_short_path" "$@" 8 | -------------------------------------------------------------------------------- /kubebuilder/kubebuilder_pwd.bash.in: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -euo pipefail 4 | 5 | dirname '@@PWD@@' 6 | -------------------------------------------------------------------------------- /kubebuilder/sdk.bzl: -------------------------------------------------------------------------------- 1 | """Bazel rules for kubebuilder based projects 2 | """ 3 | 4 | load( 5 | "@rules_kubebuilder//kubebuilder:sdk_list.bzl", 6 | "SDK_VERSION_SHA256", 7 | ) 8 | 9 | def _kubebuilder_download_sdk_impl(ctx): 10 | platform = _detect_host_platform(ctx) 11 | version = ctx.attr.version 12 | if version not in SDK_VERSION_SHA256: 13 | fail("Unknown version {}".format(version)) 14 | sha256 = SDK_VERSION_SHA256[version][platform] 15 | urls = [url.format(version = version, platform = platform) for url in ctx.attr.urls] 16 | strip_prefix = ctx.attr.strip_prefix.format(version = version, platform = platform) 17 | ctx.download_and_extract( 18 | url = urls, 19 | stripPrefix = strip_prefix, 20 | sha256 = sha256, 21 | ) 22 | ctx.template( 23 | "BUILD.bazel", 24 | Label("@rules_kubebuilder//kubebuilder:BUILD.sdk.bazel"), 25 | executable = False, 26 | ) 27 | 28 | _kubebuilder_download_sdk = repository_rule( 29 | _kubebuilder_download_sdk_impl, 30 | attrs = { 31 | "version": attr.string(default = "2.3.1"), 32 | "urls": attr.string_list( 33 | default = [ 34 | "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v{version}/kubebuilder_{version}_{platform}.tar.gz", 35 | ], 36 | ), 37 | "strip_prefix": attr.string(default = "kubebuilder_{version}_{platform}"), 38 | }, 39 | ) 40 | 41 | def kubebuilder_download_sdk(name, **kwargs): 42 | _kubebuilder_download_sdk(name = name, **kwargs) 43 | 44 | def _detect_host_platform(ctx): 45 | if ctx.os.name == "linux": 46 | host = "linux_amd64" 47 | elif ctx.os.name == "mac os x": 48 | host = "darwin_amd64" 49 | else: 50 | fail("Unsupported operating system: " + ctx.os.name) 51 | return host 52 | 53 | def kubebuilder_register_sdk(version = "2.3.1"): 54 | kubebuilder_download_sdk( 55 | name = "kubebuilder_sdk", 56 | version = version, 57 | ) 58 | 59 | def _kubebuilder_pwd_impl(ctx): 60 | out_file = ctx.actions.declare_file(ctx.label.name + ".bash") 61 | exec_path = "$(execpath {})".format(ctx.attr.kubebuilder_binary.label) 62 | substitutions = { 63 | "@@PWD@@": ctx.expand_location(exec_path), 64 | } 65 | runfiles = None 66 | ctx.actions.expand_template( 67 | template = ctx.file._template, 68 | output = out_file, 69 | substitutions = substitutions, 70 | is_executable = True, 71 | ) 72 | return [DefaultInfo( 73 | files = depset([out_file]), 74 | runfiles = runfiles, 75 | executable = out_file, 76 | )] 77 | 78 | kubebuilder_pwd = rule( 79 | implementation = _kubebuilder_pwd_impl, 80 | attrs = { 81 | "srcs": attr.label_list(allow_files = True), 82 | "kubebuilder_binary": attr.label( 83 | mandatory = True, 84 | allow_single_file = True, 85 | ), 86 | "_template": attr.label( 87 | default = "@rules_kubebuilder//kubebuilder:kubebuilder_pwd.bash.in", 88 | allow_single_file = True, 89 | ), 90 | }, 91 | executable = True, 92 | ) 93 | -------------------------------------------------------------------------------- /kubebuilder/sdk_list.bzl: -------------------------------------------------------------------------------- 1 | ## Useful constants. Generated by generate_sdk_list.py 2 | 3 | DEFAULT_VERSION = "2.3.1" 4 | 5 | SDK_VERSION_SHA256 = { 6 | "3.5.0": { 7 | "linux_amd64": "271e59de861684156515b8aa53eafa0cd242017595652c52a072b1812bbd3775", 8 | "darwin_amd64": "b75c1f1532fc27187097f9ccb6c4d8e8ea751e7e9a238a1c1f2c58b171eadad7" 9 | }, 10 | "3.4.1": { 11 | "linux_amd64": "4b9c6b3afec09bd1c1f1a67ed11c275854b9cbddc1654ebdb77c3b0b95f4e065", 12 | "darwin_amd64": "6c4600ff2a4ed02c073f0c9d305bf89948e02b3767d2d5a810d0138d269c133d" 13 | }, 14 | "3.4.0": { 15 | "linux_amd64": "cf90dad16d27a9c31db11f2c326aad926036610a75be03f342e28136ae64cf47", 16 | "darwin_amd64": "4d77ba9e9fc214dbd16077fa069bf40606bfe57c359b3e24fbf38919922d7b5f" 17 | }, 18 | "3.3.0": { 19 | "linux_amd64": "f9fb1a8e329dfe3be635ccdec3c7d3a6df4092ba13243bfcb89111b12343eb4f", 20 | "darwin_amd64": "d5c3ed529d8156005ce15de43f6f505abc4beb63e9b0ac9e215edaecb64c2ce3" 21 | }, 22 | "3.2.0": { 23 | "linux_amd64": "102bb0f586dcb50951aded67856483a2ee114057c56475b3cda6051a12832a72", 24 | "darwin_amd64": "871a0f21af99d895ef534fb11704eb0e4a1f8b55df58906f625702faa65f64e6" 25 | }, 26 | "3.1.0": { 27 | "linux_amd64": "a4af6a2110cf506855cdc86d0291c6e76203ae9fb3c918f7fdc05e7962e4b488", 28 | "darwin_amd64": "2a6c5e0e276b65cfbe173fca07b318ecff1752bf947002641b808c4a40187f2c" 29 | }, 30 | "3.0.0": { 31 | "linux_amd64": "d762c5cd954f9eb8620c64d27fc413e383e8100108d9edaee2211457165d2dff", 32 | "darwin_amd64": "c8bcbb32de6a6e17676a21ce202d2da7d97f342582f42d4dfe672260b7f2c100" 33 | }, 34 | "3.0.0-rc.0": { 35 | "linux_amd64": "5dc229842edc6e35d51c6f8ee3c8dbfcd7989665ebaa0d27a79bdd6e6fe80e39", 36 | "darwin_amd64": "af0c319343b58a340eb16c3c3d389027dd6dd741e733d4809c6ed4d6fa4a60d3" 37 | }, 38 | "3.0.0-beta.1": { 39 | "linux_amd64": "ae80ae35b75fda988ad23cb48ae658d653d802484b21d9c55dd54ddeadfa65b3", 40 | "darwin_amd64": "3dc49db6550b3256132db2554e34ec029ce70042c61dd2f534acadee3365b8a7" 41 | }, 42 | "3.0.0-beta.0": { 43 | "linux_amd64": "8178d61b1e8214f8d0871719614143f42f032655b77a64ea4e5c364fec2c8acd", 44 | "darwin_amd64": "991cbd04b162f31f40f5eb430286f23ac38b16bd57f0c85fcc85b2a00adef8f9" 45 | }, 46 | "2.3.2": { 47 | "linux_amd64": "c65b86f497726fcb8b887d55c10a68e38a503aa736c0d02b2cce11df267ad91d", 48 | "darwin_amd64": "7e7dc6f5b106909a13a24beea73427940a6ed81a8dea66d65b185dc2c71dcc28" 49 | }, 50 | "3.0.0-alpha.1": { 51 | "linux_amd64": "dab17609a0b7a8d89244cd2c739c52bb777067c61b867d50a9def869c98cd560", 52 | "darwin_amd64": "bf4baab7587747f084d9b6e308a5e07fb56cdc032b907c81bfb230558a301052" 53 | }, 54 | "3.0.0-alpha.0": { 55 | "linux_amd64": "099faba3dda57585bbfaa9b090fa3bc724c124858f5d5c78dc8a507297456d0d", 56 | "darwin_amd64": "76abdc522426bf70cdd5118400563aa1bb62638eaa0e4c8486d30f4178403867" 57 | }, 58 | "2.3.1": { 59 | "linux_amd64": "ff496970f209706763f2aba2bdcefc2de8d00085b3b972b5790117b59ea4ed10", 60 | "darwin_amd64": "39314d45053b6c31eb17e35c9b8d923f8a38277a9a136448345dd4b7f0f308f4" 61 | }, 62 | "2.3.0": { 63 | "linux_amd64": "a8ffea619f8d6e6c9fab2df8543cf0912420568e3979f44340a7613de5944141", 64 | "darwin_amd64": "b44f6c7ba1edf0046354ff29abffee3410d156fabae6eb3fa62da806988aa8bd" 65 | }, 66 | "2.2.0": { 67 | "linux_amd64": "9ef35a4a4e92408f7606f1dd1e68fe986fa222a88d34e40ecc07b6ffffcc8c12", 68 | "darwin_amd64": "5ccb9803d391e819b606b0c702610093619ad08e429ae34401b3e4d448dd2553" 69 | }, 70 | "2.1.0": { 71 | "linux_amd64": "79820786964eaecba1e90c413d8399600fde7917dfd1b0b74d6536b33f020077", 72 | "darwin_amd64": "3ab68c805a7dea461023e2c52b906c0ecac6ecb1b9a597e4a98611bce9c1fe56" 73 | }, 74 | "2.0.1": { 75 | "linux_amd64": "e8d287535c79013bfebcee22f748153686128926ae5992f023725e7b17996a04", 76 | "darwin_amd64": "a2cd518da553584aee2e8a74818da1521f5dd4a9a4a97c8e18b2634e8a8266ca" 77 | }, 78 | "2.0.0": { 79 | "linux_amd64": "858d84aa3e8bb6528d7dd4dbab4e8fceb59c8ea7905918bc72dc719d784c40f3", 80 | "darwin_amd64": "4679b5d57c857a0d3c763e9eefb4c797804a184fcae7827a3ca9abc990accc70" 81 | }, 82 | "2.0.0-rc.0": { 83 | "linux_amd64": "d4e5f8adf005e59ad1b2b8bf88f9e6841d500a473fb6f7dde7b17ca94b12e1df", 84 | "darwin_amd64": "970c74143287731c60bb171a1c90f2fb18c99b5e989af38db0d5094b96ded02a" 85 | }, 86 | "2.0.0-beta.0": { 87 | "linux_amd64": "52a81e9277b24ad3693551be142cad05efc0e4d5a35a1f99749e847d6b5f952a", 88 | "darwin_amd64": "8ade93ba2821ff8e87a9266e91409a1a8ef7a8c6afe7143e68c356b675df17f4" 89 | }, 90 | "2.0.0-alpha.4": { 91 | "linux_amd64": "623849d15580544790805326532d1d3e0e6bb92da770e908048049dda724be4d", 92 | "darwin_amd64": "e0937eb925cf0e68efa26b15e0c57feb4b7dbb0176f1acd69a68a78c5c925081" 93 | }, 94 | "2.0.0-alpha.3": { 95 | "linux_amd64": "ff6c1eb39eac6fac5c63a773f4690643603955e8672991d8da12fce0b3708d28", 96 | "darwin_amd64": "eb401e5aab74a8344e9cb58cb9263ed0dce74b218e6de9fd71d0009ee08ac93c" 97 | }, 98 | "2.0.0-alpha.2": { 99 | "linux_amd64": "d9d3f471cb54f4e802ed763d0706720be45f65c7bd36e45ab28db7840028bf72", 100 | "darwin_amd64": "795e49c0c9111f27307f63af9271f736efc1d2c8d2379be0087ff26321a7719c" 101 | }, 102 | "2.0.0-alpha.1": { 103 | "linux_amd64": "8647fcac60166ae211b9e198596452003c5887dd5b0d8b79427aa4fe3357f125", 104 | "darwin_amd64": "c19887e34f5ff008d89ae3e86aeecd0560002c0dacd36f870b46741e9426b551" 105 | }, 106 | "2.0.0-alpha.0": { 107 | "linux_amd64": "edee6582e8cf0c2836d509ed3939b7c3a1006c28bb57f22f2a3d4a5f4699ae99", 108 | "darwin_amd64": "9405023af5ddbadf5196cf450c412e3d328c995a86f097f69222c26110c003b2" 109 | }, 110 | "1.0.8": { 111 | "linux_amd64": "7dfaf7a38d69c77f046e85aeabfe96f7cdd88634feab3ecc0bb03f9da7e69ad3", 112 | "darwin_amd64": "a2a2db0db67c61e7ae958b9ff393e44a7032cd11b5e46acf9bd0a547e3fb9915" 113 | }, 114 | "1.0.7": { 115 | "linux_amd64": "9f7f55708429122b2111a5012977265522228764e756e0094f0128605f85b462", 116 | "darwin_amd64": "f58b9b33a3a61be2313c1747c91c46436deede87d20ad13287618d5e54e9d315" 117 | }, 118 | "1.0.6": { 119 | "linux_amd64": "a622eebdcc95984d3a3d87d0acd2d7c02b463f4db23b1b22020a2bc77c032668", 120 | "darwin_amd64": "0a056b58af62e4204cc65e0a7a39236396acff1935fe10effd52bf226ea6dbc3" 121 | }, 122 | "1.0.5": { 123 | "linux_amd64": "e729c74f6a1b3ab9c416642f372b84df60d07c84f64302f1f7e3fe7a9f9411e5", 124 | "darwin_amd64": "a510ef34d947764f57385e11990ba70560bf2d520f56849160174d2c18544063" 125 | } 126 | } -------------------------------------------------------------------------------- /kustomize/BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@rules_kubebuilder//kustomize:kustomize-toolchain.bzl", "kustomize_toolchain") 2 | load("@rules_pkg//:pkg.bzl", "pkg_tar") 3 | 4 | exports_files( 5 | [ 6 | "bin/kustomize.linux", 7 | "bin/kustomize.darwin", 8 | ], 9 | visibility = ["//visibility:public"], 10 | ) 11 | 12 | filegroup( 13 | name = "srcs", 14 | srcs = glob([ 15 | "*.bzl", 16 | "**/*.bzl", 17 | ]), 18 | visibility = ["//visibility:public"], 19 | ) 20 | 21 | pkg_tar( 22 | name = "release-bin", 23 | srcs = glob(["bin/*"]), 24 | package_dir = "/bin", 25 | ) 26 | 27 | pkg_tar( 28 | name = "release", 29 | srcs = [ 30 | "BUILD.bazel", 31 | ":srcs", 32 | ], 33 | package_dir = "kustomize", 34 | visibility = ["//visibility:public"], 35 | deps = [":release-bin"], 36 | ) 37 | 38 | toolchain_type(name = "toolchain") 39 | 40 | kustomize_toolchain( 41 | name = "kustomize_linux", 42 | kustomize_bin = "@rules_kubebuilder//kustomize:bin/kustomize.linux", 43 | ) 44 | 45 | kustomize_toolchain( 46 | name = "kustomize_darwin", 47 | kustomize_bin = "@rules_kubebuilder//kustomize:bin/kustomize.darwin", 48 | ) 49 | 50 | toolchain( 51 | name = "kustomize_linux_toolchain", 52 | exec_compatible_with = [ 53 | "@platforms//os:linux", 54 | "@platforms//cpu:x86_64", 55 | ], 56 | toolchain = ":kustomize_linux", 57 | toolchain_type = ":toolchain", 58 | visibility = ["//visibility:public"], 59 | ) 60 | 61 | toolchain( 62 | name = "kustomize_darwin_toolchain", 63 | exec_compatible_with = [ 64 | "@platforms//os:osx", 65 | "@platforms//cpu:x86_64", 66 | ], 67 | toolchain = ":kustomize_darwin", 68 | toolchain_type = ":toolchain", 69 | visibility = ["//visibility:public"], 70 | ) 71 | -------------------------------------------------------------------------------- /kustomize/bin/kustomize.darwin: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ob/rules_kubebuilder/6be1418107fc4b763c43878928096aaac3ea2354/kustomize/bin/kustomize.darwin -------------------------------------------------------------------------------- /kustomize/bin/kustomize.linux: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ob/rules_kubebuilder/6be1418107fc4b763c43878928096aaac3ea2354/kustomize/bin/kustomize.linux -------------------------------------------------------------------------------- /kustomize/deps.bzl: -------------------------------------------------------------------------------- 1 | """ Dependencies for kustomize 2 | """ 3 | 4 | def kustomize_register_toolchain(name = None): 5 | native.register_toolchains( 6 | "@rules_kubebuilder//kustomize:kustomize_linux_toolchain", 7 | "@rules_kubebuilder//kustomize:kustomize_darwin_toolchain", 8 | ) 9 | -------------------------------------------------------------------------------- /kustomize/kustomize-toolchain.bzl: -------------------------------------------------------------------------------- 1 | """ Toolchain definitions for kustomize 2 | """ 3 | 4 | KustomizeInfo = provider( 5 | doc = "Information about how to invoke kustomize", 6 | fields = ["kustomize_bin"], 7 | ) 8 | 9 | def _kustomize_toolchain_impl(ctx): 10 | toolchain_info = platform_common.ToolchainInfo( 11 | kustomize_info = KustomizeInfo( 12 | kustomize_bin = ctx.file.kustomize_bin, 13 | ), 14 | ) 15 | return [toolchain_info] 16 | 17 | kustomize_toolchain = rule( 18 | implementation = _kustomize_toolchain_impl, 19 | attrs = { 20 | "kustomize_bin": attr.label(allow_single_file = True), 21 | }, 22 | ) 23 | -------------------------------------------------------------------------------- /kustomize/kustomize.bzl: -------------------------------------------------------------------------------- 1 | """ Rules to run kustomize 2 | """ 3 | 4 | def _kustomize_impl(ctx): 5 | output = ctx.actions.declare_file(ctx.label.name + ".yaml") 6 | tmpdir = ctx.actions.declare_directory(ctx.label.name + ".tmp") 7 | kustomize_info = ctx.toolchains["@rules_kubebuilder//kustomize:toolchain"].kustomize_info 8 | 9 | ctx.actions.run_shell( 10 | mnemonic = "Kustomize", 11 | outputs = [output, tmpdir], 12 | inputs = ctx.files.srcs, 13 | command = """ 14 | mkdir -p {tmp_path} && 15 | cp {srcs} {tmp_path} && 16 | {kustomize} build {tmp_path} > {output} 17 | """.format( 18 | kustomize = kustomize_info.kustomize_bin.path, 19 | srcs = " ".join(['"{}"'.format(f.path) for f in ctx.files.srcs]), 20 | tmp_path = tmpdir.short_path, 21 | output = output.path, 22 | ), 23 | tools = [ 24 | kustomize_info.kustomize_bin, 25 | ], 26 | ) 27 | 28 | return DefaultInfo( 29 | files = depset([output]), 30 | ) 31 | 32 | kustomize = rule( 33 | implementation = _kustomize_impl, 34 | attrs = { 35 | "srcs": attr.label_list( 36 | allow_empty = False, 37 | allow_files = True, 38 | mandatory = True, 39 | doc = "Source files passed to kustomize", 40 | ), 41 | }, 42 | toolchains = [ 43 | "@rules_kubebuilder//kustomize:toolchain", 44 | ], 45 | doc = "", 46 | ) 47 | -------------------------------------------------------------------------------- /scripts/build-controller-gen.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -euo pipefail 4 | 5 | VERSION="0.4.1" 6 | 7 | ROOT=$(git rev-parse --show-toplevel) 8 | DEST="$ROOT/controller-gen/bin" 9 | 10 | test -d "$ROOT/build/tmp" && rm -rf "$ROOT/build/tmp" 11 | mkdir -p "$ROOT/build/tmp" 12 | cd "$ROOT/build/tmp" 13 | 14 | # Fetch and unpack v0.3.0 15 | echo "Downloading and extracting controller-tools version $VERSION" 16 | curl -sL "https://github.com/kubernetes-sigs/controller-tools/archive/v${VERSION}.tar.gz" | tar xfz - 17 | 18 | cd "controller-tools-$VERSION" 19 | 20 | mkdir -p "$ROOT/bin" 21 | echo "Building controller-gen" 22 | GOOS=darwin GOARCH=amd64 go build ./cmd/controller-gen 23 | mv controller-gen "$DEST/controller-gen.darwin" 24 | GOOS=linux GOARCH=amd64 go build ./cmd/controller-gen 25 | mv controller-gen "$DEST/controller-gen.linux" 26 | 27 | echo "Binaries built:" 28 | file "$DEST"/* 29 | -------------------------------------------------------------------------------- /scripts/get-kustomize.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -euo pipefail 4 | 5 | VERSION="3.8.4" 6 | 7 | ROOT=$(git rev-parse --show-toplevel) 8 | DEST="$ROOT/km/bin" 9 | 10 | mkdir -p "$ROOT/build/tmp" 11 | cd "$ROOT/build/tmp" 12 | 13 | echo Fetching Kustomize version "$VERSION" 14 | for OS in linux darwin; do 15 | curl -s -O -L "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv${VERSION}/kustomize_v${VERSION}_${OS}_amd64.tar.gz" 16 | 17 | test -e kustomize_v${VERSION}_${OS}_amd64.tar.gz || { 18 | echo failed to download kustomize 19 | exit 1 20 | } 21 | 22 | tar xfx kustomize_v${VERSION}_${OS}_amd64.tar.gz 23 | 24 | mv kustomize "$DEST/kustomize.$OS" 25 | done 26 | 27 | echo done. 28 | file "$DEST"/* 29 | 30 | -------------------------------------------------------------------------------- /test/BUILD.bazel: -------------------------------------------------------------------------------- 1 | load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") 2 | 3 | go_library( 4 | name = "go_default_library", 5 | srcs = [ 6 | "apackage.go", 7 | ], 8 | importpath = "apackage", 9 | visibility = ["//visibility:public"], 10 | deps = [ 11 | ], 12 | ) 13 | 14 | go_test( 15 | name = "go_default_test", 16 | srcs = ["apackage_test.go"], 17 | data = [ 18 | "@kubebuilder_sdk//:bin/etcd", 19 | ], 20 | embed = [":go_default_library"], 21 | deps = [ 22 | ], 23 | ) 24 | -------------------------------------------------------------------------------- /test/apackage.go: -------------------------------------------------------------------------------- 1 | package apackage 2 | 3 | import "fmt" 4 | 5 | // Afunc ... 6 | func Afunc(b bool) error { 7 | if !b { 8 | return fmt.Errorf("This is why we can't have nice things") 9 | } 10 | return nil 11 | } 12 | -------------------------------------------------------------------------------- /test/apackage_test.go: -------------------------------------------------------------------------------- 1 | package apackage 2 | 3 | import ( 4 | "os" 5 | "testing" 6 | ) 7 | 8 | func TestAFunct(t *testing.T) { 9 | err := Afunc(true) 10 | if err != nil { 11 | t.Errorf("Should have worked") 12 | } 13 | } 14 | 15 | // See https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest 16 | func TestEnvironment(t *testing.T) { 17 | kbAssets := os.Getenv("KUBEBUILDER_ASSETS") 18 | if kbAssets == "" { 19 | t.Errorf("Did not find KUBEBUILDER_ASSETS environment variable set") 20 | } 21 | 22 | } 23 | --------------------------------------------------------------------------------