├── .github └── workflows │ └── main.yaml ├── .gitignore ├── BUILD ├── LICENSE ├── README.md ├── WORKSPACE ├── archive.bzl ├── defs.bzl ├── docs ├── BUILD └── defs.md ├── examples ├── .bazelversion ├── BUILD ├── README ├── WORKSPACE ├── base │ └── echoapp │ │ ├── BUILD │ │ ├── echo.pod.yaml │ │ └── kustomization.yaml ├── proj-1 │ └── cluster-a │ │ ├── stage │ │ ├── BUILD │ │ └── kustomization.yaml │ │ └── tools │ │ ├── BUILD │ │ ├── kustomization.yaml │ │ ├── subdir │ │ └── password.secret.yaml │ │ └── the-map.configmap.yaml ├── proj-2 │ └── cluster-b │ │ └── prod │ │ ├── BUILD │ │ ├── golden.yaml │ │ └── kustomization.yaml └── proj-3 │ └── cluster-c │ └── prod │ ├── BUILD │ └── kustomization.yaml ├── exec.sh ├── execpwd.sh ├── fixgolden.sh ├── tests ├── BUILD ├── WORKSPACE └── b6i-compat │ └── yyz │ └── default │ ├── BUILD │ ├── deployment.yaml │ ├── golden.yaml │ ├── golden.yaml.hash │ ├── kustomization.yaml │ ├── service.yaml │ └── wrap.bzl ├── tools └── gke_kubeconfig.sh └── workspace.bzl /.github/workflows/main.yaml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | pull_request: 8 | branches: 9 | - main 10 | 11 | jobs: 12 | build: 13 | runs-on: ubuntu-latest 14 | 15 | steps: 16 | - name: checkout 17 | uses: actions/checkout@v1 18 | 19 | - name: deps-go 20 | uses: actions/setup-go@v3 21 | 22 | - name: deps 23 | run: | 24 | curl -LO "https://github.com/bazelbuild/bazelisk/releases/download/v1.7.5/bazelisk-linux-amd64" 25 | mkdir -p "${GITHUB_WORKSPACE}/bin/" 26 | mv bazelisk-linux-amd64 "${GITHUB_WORKSPACE}/bin/bazel" 27 | chmod +x "${GITHUB_WORKSPACE}/bin/bazel" 28 | 29 | - name: gen 30 | run: | 31 | set -eux 32 | 33 | # Enforce documentation is up-to-date. 34 | ${GITHUB_WORKSPACE}/bin/bazel build //docs && cat ${GITHUB_WORKSPACE}/bazel-bin/docs/defs.md > ${GITHUB_WORKSPACE}/docs/defs.md 35 | git diff-index --name-status HEAD 36 | git diff 37 | git diff-index --quiet HEAD 38 | 39 | # Enforce license headers are up-to-date. 40 | GOBIN=/usr/local/bin/ go install github.com/google/addlicense@latest 41 | addlicense -check -c "BenchSci Analytics Inc." -l apache *.* tools 42 | 43 | - name: examples 44 | working-directory: examples 45 | run: ${GITHUB_WORKSPACE}/bin/bazel test ... 46 | 47 | - name: tests 48 | working-directory: tests 49 | run: ${GITHUB_WORKSPACE}/bin/bazel test ... 50 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /bazel-* 2 | /examples/bazel-* 3 | /tests/bazel-* 4 | -------------------------------------------------------------------------------- /BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | sh_library( 4 | name = "fixgolden", 5 | srcs = ["fixgolden.sh"], 6 | ) 7 | 8 | filegroup( 9 | name = "exec", 10 | srcs = ["exec.sh"], 11 | ) 12 | 13 | filegroup( 14 | name = "execpwd", 15 | srcs = ["execpwd.sh"], 16 | ) 17 | 18 | sh_binary( 19 | name = "kustomize", 20 | srcs = [":execpwd"], 21 | args = ["$(location @kustomize//:file)"], 22 | data = ["@kustomize//:file"], 23 | ) 24 | 25 | sh_binary( 26 | name = "kubectl_bin", 27 | srcs = select({ 28 | "@bazel_tools//src/conditions:darwin_arm64": ["@kubectl_darwin_arm64//file"], 29 | "@bazel_tools//src/conditions:darwin_x86_64": ["@kubectl_darwin//file"], 30 | "@bazel_tools//src/conditions:linux_aarch64": ["@kubectl_linux_arm64//file"], 31 | "@bazel_tools//src/conditions:linux_x86_64": ["@kubectl_linux//file"], 32 | }), 33 | ) 34 | 35 | sh_binary( 36 | name = "kubectl", 37 | srcs = [":execpwd"], 38 | args = ["$(location :kubectl_bin)"], 39 | data = [":kubectl_bin"], 40 | ) 41 | 42 | sh_binary( 43 | name = "gcloud", 44 | srcs = [":execpwd"], 45 | args = ["$(location @gcloud)"], 46 | data = ["@gcloud"], 47 | ) 48 | 49 | # Exported for documentation (see //tools:docs). 50 | exports_files(["defs.bzl"]) 51 | -------------------------------------------------------------------------------- /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_kustomize 2 | 3 | The rules define a collection of Bazel macros for working with 4 | [kustomize](https://kustomize.io/). These rules are intentionally lightweight 5 | and intended to be composable with other Kubernetes related rules. 6 | 7 | The rules are tested against Bazel 5.4 and supported on the following host 8 | platforms: 9 | 10 | * Linux, macOS 11 | * amd64, arm64 12 | 13 | Patches to support additional platforms are welcome. 14 | 15 | # Setup 16 | 17 | After setup, see the [examples](./examples/) directory and the [API documentation](./docs/defs.md). 18 | 19 | ```bzl 20 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") 21 | 22 | 23 | http_archive( 24 | name = "com_benchsci_rules_kustomize", 25 | sha256 = "7c2308991352d534c4ad3d9a0e37eee402ccb8a7c96eeb718c18c52f81c45fb1", 26 | strip_prefix = "rules_kustomize-4b979472aae1953799a616e45a10748acafcd982", 27 | urls = ["https://github.com/benchsci/rules_kustomize/archive/4b979472aae1953799a616e45a10748acafcd982.zip"], 28 | ) 29 | 30 | load("@com_benchsci_rules_kustomize//:workspace.bzl", "download_kustomize_deps") 31 | 32 | download_kustomize_deps() 33 | 34 | # Optional for some handy GKE related utilities. 35 | load("@com_benchsci_rules_kustomize//:workspace.bzl", "download_gcloud_deps") 36 | 37 | download_gcloud_deps() 38 | ``` 39 | 40 | These rules make a best-effort attempt to track the stable releases of its 41 | dependencies. If you need to lock to different versions you can specify them 42 | explictly in your WORKSPACE file by copying and modifying the contents of the 43 | helper functions in [workspace.bzl](./workspace.bzl) (e.g. the body of the 44 | `download_kustomize_deps` function) 45 | 46 | # Additional tools: 47 | 48 | The underlying tools that the macros use are exposed as `run` commands:: 49 | 50 | bazel run @com_benchsci_rules_kustomize//:kubectl 51 | bazel run @com_benchsci_rules_kustomize//:kustomize 52 | bazel run @com_benchsci_rules_kustomize//:gcloud 53 | 54 | You can alias these tools into your own Bazel repository via 55 | [`alias`](https://docs.bazel.build/versions/master/be/general.html#alias). For 56 | example: 57 | 58 | alias( 59 | name = "kubectl", 60 | actual = "@com_benchsci_rules_kustomize//:kubectl", 61 | ) 62 | -------------------------------------------------------------------------------- /WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "com_benchsci_rules_kustomize") 2 | 3 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file") 4 | load("@//:workspace.bzl", "download_gcloud_deps", "download_kustomize_deps") 5 | 6 | download_kustomize_deps() 7 | 8 | download_gcloud_deps() 9 | 10 | http_archive( 11 | name = "io_bazel_stardoc", 12 | sha256 = "6d07d18c15abb0f6d393adbd6075cd661a2219faab56a9517741f0fc755f6f3c", 13 | strip_prefix = "stardoc-0.4.0", 14 | urls = ["https://github.com/bazelbuild/stardoc/archive/refs/tags/0.4.0.tar.gz"], 15 | ) 16 | 17 | load("@io_bazel_stardoc//:setup.bzl", "stardoc_repositories") 18 | 19 | stardoc_repositories() 20 | -------------------------------------------------------------------------------- /archive.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2021 BenchSci Analytics Inc. 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 | # Forked from snippets described in 16 | # https://github.com/bazelbuild/rules_k8s/issues/298 17 | load("@bazel_tools//tools/build_defs/repo:utils.bzl", "patch", "workspace_and_buildfile") 18 | 19 | def os_name(repository_ctx): 20 | os_name = repository_ctx.os.name.lower() 21 | if os_name.startswith("mac os"): 22 | return "darwin" 23 | elif os_name.find("windows") != -1: 24 | return "windows" 25 | elif os_name.startswith("linux"): 26 | return "linux" 27 | else: 28 | fail("Unsupported operating system: " + os_name) 29 | 30 | def cpu_name(repository_ctx): 31 | cpu_name = repository_ctx.os.arch.lower() 32 | if cpu_name.startswith("x86") or cpu_name.startswith("amd64") or cpu_name.startswith("k8"): 33 | return "" 34 | elif cpu_name.startswith("arm64") or cpu_name.startswith("aarch64"): 35 | return "_arm64" 36 | else: 37 | fail("Unsupported cpu arch: " + cpu_name) 38 | 39 | _http_archive_by_os_attrs = { 40 | "url": attr.string_dict(), 41 | "urls": attr.string_list_dict(), 42 | "sha256": attr.string_dict(), 43 | "strip_prefix": attr.string_dict(), 44 | "type": attr.string(), 45 | "build_file": attr.label(allow_single_file = True), 46 | "build_file_content": attr.string(), 47 | "patches": attr.label_list(default = []), 48 | "patch_tool": attr.string(default = "patch"), 49 | "patch_args": attr.string_list(default = ["-p0"]), 50 | "patch_cmds": attr.string_list(default = []), 51 | "workspace_file": attr.label(allow_single_file = True), 52 | "workspace_file_content": attr.string(), 53 | } 54 | 55 | def _http_archive_by_os_impl(ctx): 56 | """Implementation of the http_archive rule.""" 57 | if not ctx.attr.url and not ctx.attr.urls: 58 | fail("At least one of url and urls must be provided") 59 | if ctx.attr.build_file and ctx.attr.build_file_content: 60 | fail("Only one of build_file and build_file_content can be provided.") 61 | 62 | host = "".join([os_name(ctx), cpu_name(ctx)]) 63 | url = ctx.attr.url.get(host) 64 | urls = ctx.attr.urls.get(host) 65 | sha256 = ctx.attr.sha256.get(host) or "" 66 | strip_prefix = ctx.attr.strip_prefix.get(host) or "" 67 | 68 | all_urls = [] 69 | if url: 70 | all_urls = url 71 | if urls: 72 | all_urls = [urls] + all_urls 73 | 74 | ctx.download_and_extract( 75 | all_urls, 76 | "", 77 | sha256, 78 | ctx.attr.type, 79 | strip_prefix, 80 | ) 81 | patch(ctx) 82 | workspace_and_buildfile(ctx) 83 | 84 | http_archive_by_os = repository_rule( 85 | implementation = _http_archive_by_os_impl, 86 | attrs = _http_archive_by_os_attrs, 87 | ) 88 | -------------------------------------------------------------------------------- /defs.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2021 BenchSci Analytics Inc. 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 | def kubeconfig(): 16 | """Macro for genrule target named "kubeconfig" intended to uniquely identify a Kubernetes cluster. 17 | 18 | This macro creates two rules: "kubeconfig" and "kubectl". The latter is a 19 | convenience run rule that invokes kubectl with the sibling configuration. 20 | 21 | These rules are meant to be set in the `cfg` parameter of the `apply` rule 22 | documented below. 23 | """ 24 | 25 | # kubectl has a nasty habbit of re-writing the kubeconfig in the source 26 | # tree. Clone the contents into a generated file so the source tree isn't 27 | # mutated. 28 | native.genrule( 29 | name = "kubeconfig", 30 | srcs = ["kubeconfig.yaml"], 31 | outs = ["kubeconfig_copy.yaml"], 32 | cmd = "cat '$(location :kubeconfig.yaml)' > $@", 33 | # This visibility is intended to prevent accidental usage outside this 34 | # package and subpackages. DO NOT CHANGE. 35 | visibility = [":__subpackages__"], 36 | ) 37 | 38 | native.sh_binary( 39 | name = "kubectl", 40 | srcs = ["@com_benchsci_rules_kustomize//:exec"], 41 | data = [ 42 | ":kubeconfig", 43 | "@com_benchsci_rules_kustomize//:kubectl_bin", 44 | ], 45 | args = [ 46 | "$(location @com_benchsci_rules_kustomize//:kubectl_bin)", 47 | "--kubeconfig=$(location :kubeconfig)", 48 | ], 49 | visibility = ["//visibility:public"], 50 | ) 51 | 52 | def _base_cmd(cfg, src, namespace, tags, subcmd, *extra_args): 53 | args = [ 54 | "$(location @com_benchsci_rules_kustomize//:kubectl_bin)", 55 | "--kubeconfig=$(location {})".format(cfg), 56 | subcmd, 57 | "-f=$(location {})".format(src), 58 | ] 59 | if namespace != None: 60 | args += ["--namespace=" + namespace] 61 | 62 | args += list(extra_args) 63 | 64 | return dict( 65 | srcs = ["@com_benchsci_rules_kustomize//:exec"], 66 | data = [ 67 | "@com_benchsci_rules_kustomize//:kubectl_bin", 68 | cfg, 69 | src, 70 | ], 71 | tags = tags, 72 | args = args, 73 | # Required to allow aliasing run targets. 74 | visibility = ["//visibility:public"], 75 | ) 76 | 77 | def apply(name, src, cfg, namespace = None, tags = None): 78 | """Performs a dry-run `apply` via `kubectl` against a resource file. 79 | 80 | This is intended to be combined with the output of a `kustomization` rule 81 | though `src` can reference any file accepted by `kubectl apply`. This 82 | macro defines two additional rules: 83 | 84 | * `.run` actually performs the apply. 85 | * `.diff` outputs the diff against the existing configuration. 86 | 87 | See: 88 | 89 | * https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply 90 | * https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#diff 91 | 92 | Args: 93 | name: A unique name for this rule. 94 | src: A label. 95 | cfg: A label referencing a kubeconfig file pointing to desired target 96 | cluster. 97 | namespace: Optionally restrict operations to a specific namespace in the 98 | cluster defined by `cfg`. 99 | tags: Sets tags on the rule. The `requires-network` tag must be among 100 | the tags. 101 | """ 102 | if tags == None: 103 | tags = ["requires-network"] 104 | if "requires-network" not in tags: 105 | fail("Apply target tags must contain 'requires-network'.") 106 | 107 | native.sh_binary(name = name, **_base_cmd(cfg, src, namespace, tags, "apply", "--dry-run=server")) 108 | native.sh_binary(name = name + ".run", **_base_cmd(cfg, src, namespace, tags, "apply")) 109 | native.sh_binary(name = name + ".diff", **_base_cmd(cfg, src, namespace, tags, "diff")) 110 | 111 | def kustomization( 112 | name, 113 | srcs, 114 | out, 115 | golden = None, 116 | visibility = None, 117 | autofix = True, 118 | plugin_dir = None, 119 | tags = ["block-network"]): 120 | """Builds a kustomization defined by the input srcs. 121 | 122 | The output is a YAML multi-doc comprised of all the resources defined by 123 | the customization. This macro will create additional rules with the 124 | following suffixes: 125 | 126 | * `.autofix` is a run target generated if `golden` is set. It synchronizes the output of 127 | * `.golden` is a test target generated if `golden` is set. 128 | 129 | See: 130 | 131 | * https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#kustomization 132 | * https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#kustomization-root 133 | 134 | Args: 135 | name: A unique name for this rule. 136 | srcs: Source inputs to run `kustomize build` against. These are any 137 | valid Bazel labels representing. 138 | 139 | Note that the Bazel glob() function can be used to specify which source 140 | files to include and which to exclude, e.g. 141 | `glob(["*.yaml"], exclude=["golden.yaml"])`. 142 | out: The name of the output file. 143 | golden: Identify a file containing the expected output of the build. 144 | Defining this parameter creates a test rule named `.golden` that 145 | verifies the output is identical to the contents of the named file. 146 | Golden files are a materialized view of resources and can be useful if 147 | your kustomizations have many transitive dependencies. 148 | autofix: Toggle creation of a `.autofix` rule if `golden` is also set. 149 | plugin_dir: TODO 150 | visibility: The visibility of this rule. 151 | tags: Sets tags on the rule. The `block-network` tag is strongly 152 | recommended (but not enforced) to ensure hermeticity and 153 | reproducibility. 154 | """ 155 | kwargs = {} 156 | if visibility: 157 | kwargs["visibility"] = visibility 158 | 159 | native.filegroup( 160 | name = name + ".srcs", 161 | srcs = srcs, 162 | **kwargs 163 | ) 164 | 165 | # Used only to be able to generate a path to the file below. 166 | native.filegroup( 167 | name = name + ".main", 168 | srcs = ["kustomization.yaml"], 169 | visibility = ["//visibility:private"], 170 | ) 171 | 172 | build_cmd = [ 173 | "./$(location @kustomize//:file) ", 174 | "build", 175 | # This can be dangerous, but Bazel forces us to be explicit in 176 | # defining dependencies and we use the "block-network" tag below to 177 | # disallow fetches. 178 | "--load-restrictor=LoadRestrictionsNone", 179 | ] 180 | if plugin_dir: 181 | build_cmd = ["KUSTOMIZE_PLUGIN_HOME=$$( realpath {} )".format(plugin_dir)] + build_cmd + ["--enable_alpha_plugins"] 182 | 183 | native.genrule( 184 | name = name, 185 | srcs = [ 186 | ":" + name + ".srcs", 187 | ":" + name + ".main", 188 | ], 189 | outs = [out], 190 | cmd = " ".join(build_cmd + [ 191 | "$$( dirname '$(location :{}.main)' )".format(name), 192 | "> \"$@\"", 193 | ]), 194 | # Ideally we'd use something like: 195 | # 196 | # kbin = "{}//:kustomize".format(native.repository_name()) 197 | # 198 | # See https://github.com/bazelbuild/bazel/issues/4092 199 | tools = ["@kustomize//:file"], 200 | tags = tags, 201 | **kwargs 202 | ) 203 | 204 | if golden != None: 205 | if autofix: 206 | native.sh_binary( 207 | name = name + ".autofix", 208 | srcs = ["@com_benchsci_rules_kustomize//:fixgolden"], 209 | data = [":" + name], 210 | args = ["$(location :{})".format(name), golden], 211 | visibility = ["//visibility:private"], 212 | ) 213 | native.sh_test( 214 | name = name + ".golden", 215 | srcs = ["@com_benchsci_rules_kustomize//:exec"], 216 | data = [":" + name, golden], 217 | args = [ 218 | "diff", 219 | "-I", 220 | "^#.*", 221 | "$(location :{})".format(name), 222 | "$(location {})".format(golden), 223 | ], 224 | visibility = ["//visibility:private"], 225 | ) 226 | -------------------------------------------------------------------------------- /docs/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = ["//visibility:public"]) 2 | 3 | load("@io_bazel_stardoc//stardoc:stardoc.bzl", "stardoc") 4 | 5 | stardoc( 6 | name = "docs", 7 | out = "defs.md", 8 | input = "//:defs.bzl", 9 | ) 10 | -------------------------------------------------------------------------------- /docs/defs.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | ## apply 6 | 7 |
 8 | apply(name, src, cfg, namespace, tags)
 9 | 
10 | 11 | Performs a dry-run `apply` via `kubectl` against a resource file. 12 | 13 | This is intended to be combined with the output of a `kustomization` rule 14 | though `src` can reference any file accepted by `kubectl apply`. This 15 | macro defines two additional rules: 16 | 17 | * `.run` actually performs the apply. 18 | * `.diff` outputs the diff against the existing configuration. 19 | 20 | See: 21 | 22 | * https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#apply 23 | * https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands#diff 24 | 25 | 26 | **PARAMETERS** 27 | 28 | 29 | | Name | Description | Default Value | 30 | | :-------------: | :-------------: | :-------------: | 31 | | name | A unique name for this rule. | none | 32 | | src | A label. | none | 33 | | cfg | A label referencing a kubeconfig file pointing to desired target cluster. | none | 34 | | namespace | Optionally restrict operations to a specific namespace in the cluster defined by cfg. | None | 35 | | tags | Sets tags on the rule. The requires-network tag must be among the tags. | None | 36 | 37 | 38 | 39 | 40 | ## kubeconfig 41 | 42 |
43 | kubeconfig()
44 | 
45 | 46 | Macro for genrule target named "kubeconfig" intended to uniquely identify a Kubernetes cluster. 47 | 48 | This macro creates two rules: "kubeconfig" and "kubectl". The latter is a 49 | convenience run rule that invokes kubectl with the sibling configuration. 50 | 51 | These rules are meant to be set in the `cfg` parameter of the `apply` rule 52 | documented below. 53 | 54 | **PARAMETERS** 55 | 56 | 57 | 58 | 59 | 60 | ## kustomization 61 | 62 |
63 | kustomization(name, srcs, out, golden, visibility, autofix, plugin_dir, tags)
64 | 
65 | 66 | Builds a kustomization defined by the input srcs. 67 | 68 | The output is a YAML multi-doc comprised of all the resources defined by 69 | the customization. This macro will create additional rules with the 70 | following suffixes: 71 | 72 | * `.autofix` is a run target generated if `golden` is set. It synchronizes the output of 73 | * `.golden` is a test target generated if `golden` is set. 74 | 75 | See: 76 | 77 | * https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#kustomization 78 | * https://kubectl.docs.kubernetes.io/references/kustomize/glossary/#kustomization-root 79 | 80 | 81 | **PARAMETERS** 82 | 83 | 84 | | Name | Description | Default Value | 85 | | :-------------: | :-------------: | :-------------: | 86 | | name | A unique name for this rule. | none | 87 | | srcs | Source inputs to run kustomize build against. These are any valid Bazel labels representing.

Note that the Bazel glob() function can be used to specify which source files to include and which to exclude, e.g. glob(["*.yaml"], exclude=["golden.yaml"]). | none | 88 | | out | The name of the output file. | none | 89 | | golden | Identify a file containing the expected output of the build. Defining this parameter creates a test rule named .golden that verifies the output is identical to the contents of the named file. Golden files are a materialized view of resources and can be useful if your kustomizations have many transitive dependencies. | None | 90 | | visibility | The visibility of this rule. | None | 91 | | autofix | Toggle creation of a .autofix rule if golden is also set. | True | 92 | | plugin_dir | TODO | None | 93 | | tags | Sets tags on the rule. The block-network tag is strongly recommended (but not enforced) to ensure hermeticity and reproducibility. | ["block-network"] | 94 | 95 | 96 | -------------------------------------------------------------------------------- /examples/.bazelversion: -------------------------------------------------------------------------------- 1 | 5.4.0 2 | -------------------------------------------------------------------------------- /examples/BUILD: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benchsci/rules_kustomize/918a63f10b495fedbf3909d0384dc303fd274898/examples/BUILD -------------------------------------------------------------------------------- /examples/README: -------------------------------------------------------------------------------- 1 | Build: 2 | 3 | bazel test //... 4 | 5 | You should see tests passing and output files like: 6 | 7 | $ find bazel-out/ -name '*.yaml' 8 | bazel-out/k8-fastbuild/bin/proj-1/cluster-a/stage/stage.yaml 9 | bazel-out/k8-fastbuild/bin/proj-1/cluster-a/tools/tools.yaml 10 | bazel-out/k8-fastbuild/bin/proj-2/cluster-b/prod/prod-b.yaml 11 | bazel-out/k8-fastbuild/bin/proj-3/cluster-c/prod/prod-c.yaml 12 | 13 | Note these build targets use "file_test" rules in order to also provide test 14 | coverage for kustomize() invocations that don't use the "golden" argument. 15 | -------------------------------------------------------------------------------- /examples/WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "examples") 2 | 3 | local_repository( 4 | name = "com_benchsci_rules_kustomize", 5 | path = "../", 6 | ) 7 | 8 | load("@com_benchsci_rules_kustomize//:workspace.bzl", "download_kustomize_deps") 9 | 10 | download_kustomize_deps() 11 | -------------------------------------------------------------------------------- /examples/base/echoapp/BUILD: -------------------------------------------------------------------------------- 1 | package(default_visibility = [ 2 | "//proj-1/cluster-a/stage:__subpackages__", 3 | "//proj-2/cluster-b/prod:__subpackages__", 4 | "//proj-3/cluster-c/prod:__subpackages__", 5 | ]) 6 | 7 | filegroup( 8 | name = "echoapp", 9 | srcs = [ 10 | "echo.pod.yaml", 11 | "kustomization.yaml", 12 | ], 13 | ) 14 | -------------------------------------------------------------------------------- /examples/base/echoapp/echo.pod.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Pod 3 | metadata: 4 | name: echo 5 | spec: 6 | containers: 7 | - name: echo 8 | image: k8s.gcr.io/echoserver:1.4 9 | -------------------------------------------------------------------------------- /examples/base/echoapp/kustomization.yaml: -------------------------------------------------------------------------------- 1 | resources: 2 | - echo.pod.yaml 3 | -------------------------------------------------------------------------------- /examples/proj-1/cluster-a/stage/BUILD: -------------------------------------------------------------------------------- 1 | load("@com_benchsci_rules_kustomize//:defs.bzl", "kustomization") 2 | load("@bazel_tools//tools/build_rules:test_rules.bzl", "file_test") 3 | 4 | kustomization( 5 | name = "stage", 6 | srcs = [ 7 | "kustomization.yaml", 8 | "//base/echoapp", 9 | ], 10 | out = "stage.yaml", 11 | ) 12 | 13 | golden = """apiVersion: v1 14 | kind: Pod 15 | metadata: 16 | name: echo 17 | namespace: stage 18 | spec: 19 | containers: 20 | - image: k8s.gcr.io/echoserver:1.4 21 | name: echo 22 | """ 23 | 24 | file_test( 25 | name = "golden", 26 | content = golden, 27 | file = ":stage", 28 | ) 29 | -------------------------------------------------------------------------------- /examples/proj-1/cluster-a/stage/kustomization.yaml: -------------------------------------------------------------------------------- 1 | namespace: stage 2 | resources: 3 | - ../../../base/echoapp 4 | -------------------------------------------------------------------------------- /examples/proj-1/cluster-a/tools/BUILD: -------------------------------------------------------------------------------- 1 | load("@com_benchsci_rules_kustomize//:defs.bzl", "kustomization") 2 | load("@bazel_tools//tools/build_rules:test_rules.bzl", "file_test") 3 | 4 | kustomization( 5 | name = "tools", 6 | srcs = glob(["**/*.yaml"]), 7 | out = "tools.yaml", 8 | ) 9 | 10 | golden = """apiVersion: v1 11 | data: 12 | altGreeting: Good Morning! 13 | enableRisky: "false" 14 | kind: ConfigMap 15 | metadata: 16 | name: the-map 17 | --- 18 | apiVersion: v1 19 | data: 20 | password: YWRtaW4= 21 | kind: Secret 22 | metadata: 23 | name: password 24 | type: Opaque 25 | """ 26 | 27 | file_test( 28 | name = "golden", 29 | content = golden, 30 | file = ":tools", 31 | ) 32 | -------------------------------------------------------------------------------- /examples/proj-1/cluster-a/tools/kustomization.yaml: -------------------------------------------------------------------------------- 1 | resources: 2 | - subdir/password.secret.yaml 3 | - the-map.configmap.yaml 4 | -------------------------------------------------------------------------------- /examples/proj-1/cluster-a/tools/subdir/password.secret.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Secret 3 | metadata: 4 | name: password 5 | type: Opaque 6 | data: 7 | password: YWRtaW4= 8 | -------------------------------------------------------------------------------- /examples/proj-1/cluster-a/tools/the-map.configmap.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: ConfigMap 3 | metadata: 4 | name: the-map 5 | data: 6 | altGreeting: "Good Morning!" 7 | enableRisky: "false" 8 | -------------------------------------------------------------------------------- /examples/proj-2/cluster-b/prod/BUILD: -------------------------------------------------------------------------------- 1 | load("@com_benchsci_rules_kustomize//:defs.bzl", "kustomization") 2 | load("@bazel_tools//tools/build_rules:test_rules.bzl", "file_test") 3 | 4 | kustomization( 5 | name = "prod", 6 | srcs = [ 7 | "kustomization.yaml", 8 | "//base/echoapp", 9 | ], 10 | out = "prod-b.yaml", 11 | golden = "golden.yaml", 12 | ) 13 | 14 | golden = """apiVersion: v1 15 | kind: Pod 16 | metadata: 17 | labels: 18 | cluster: b 19 | name: echo 20 | namespace: prod 21 | spec: 22 | containers: 23 | - image: k8s.gcr.io/echoserver:1.4 24 | name: echo 25 | """ 26 | 27 | file_test( 28 | name = "parallel_test", 29 | content = golden, 30 | file = ":prod", 31 | ) 32 | -------------------------------------------------------------------------------- /examples/proj-2/cluster-b/prod/golden.yaml: -------------------------------------------------------------------------------- 1 | # Code generated by fixgolden.sh. DO NOT EDIT. 2 | # See: bazel query 'attr(name, .autofix, ...)' 3 | # Original: c0436039be8acbb10e16b90250e6451c 4 | apiVersion: v1 5 | kind: Pod 6 | metadata: 7 | labels: 8 | cluster: b 9 | name: echo 10 | namespace: prod 11 | spec: 12 | containers: 13 | - image: k8s.gcr.io/echoserver:1.4 14 | name: echo 15 | -------------------------------------------------------------------------------- /examples/proj-2/cluster-b/prod/kustomization.yaml: -------------------------------------------------------------------------------- 1 | namespace: prod 2 | commonLabels: 3 | cluster: b 4 | 5 | resources: 6 | - ../../../base/echoapp 7 | -------------------------------------------------------------------------------- /examples/proj-3/cluster-c/prod/BUILD: -------------------------------------------------------------------------------- 1 | load("@com_benchsci_rules_kustomize//:defs.bzl", "kustomization") 2 | load("@bazel_tools//tools/build_rules:test_rules.bzl", "file_test") 3 | 4 | kustomization( 5 | name = "prod", 6 | srcs = [ 7 | "kustomization.yaml", 8 | "//base/echoapp", 9 | ], 10 | out = "prod-c.yaml", 11 | ) 12 | 13 | golden = """apiVersion: v1 14 | kind: Pod 15 | metadata: 16 | labels: 17 | cluster: c 18 | name: echo 19 | namespace: prod 20 | spec: 21 | containers: 22 | - image: k8s.gcr.io/echoserver:1.4 23 | name: echo 24 | """ 25 | 26 | file_test( 27 | name = "golden", 28 | content = golden, 29 | file = ":prod", 30 | ) 31 | -------------------------------------------------------------------------------- /examples/proj-3/cluster-c/prod/kustomization.yaml: -------------------------------------------------------------------------------- 1 | namespace: prod 2 | commonLabels: 3 | cluster: c 4 | 5 | resources: 6 | - ../../../base/echoapp 7 | -------------------------------------------------------------------------------- /exec.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Copyright 2021 Benchsci Analytics Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | exec "$@" 17 | -------------------------------------------------------------------------------- /execpwd.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # Copyright 2021 Benchsci Analytics Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | # See https://github.com/bazelbuild/bazel/issues/3325 17 | if [ -z "${BUILD_WORKING_DIRECTORY-}" ]; then 18 | echo "error BUILD_WORKING_DIRECTORY not set" 19 | exit 1 20 | fi 21 | 22 | cmd="$( pwd )/${1}" 23 | cd "${BUILD_WORKING_DIRECTORY}" 24 | shift 25 | 26 | exec "${cmd}" "$@" 27 | -------------------------------------------------------------------------------- /fixgolden.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # 3 | # Copyright 2021 BenchSci Analytics Inc. 4 | # 5 | # Licensed under the Apache License, Version 2.0 (the "License"); 6 | # you may not use this file except in compliance with the License. 7 | # You may obtain a copy of the License at 8 | # 9 | # http://www.apache.org/licenses/LICENSE-2.0 10 | # 11 | # Unless required by applicable law or agreed to in writing, software 12 | # distributed under the License is distributed on an "AS IS" BASIS, 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | # See the License for the specific language governing permissions and 15 | # limitations under the License. 16 | set -euo pipefail 17 | 18 | while(($#)); do 19 | SOURCE=$1 && shift 20 | GOLDEN="${BUILD_WORKSPACE_DIRECTORY}/$( dirname "${SOURCE}" )/$1" && shift 21 | echo "# Code generated by fixgolden.sh. DO NOT EDIT." > "$GOLDEN" 22 | echo "# See: bazel query 'attr(name, ".autofix", ...)'" >> "$GOLDEN" 23 | 24 | # Force merge conflicts to prevent automatically merging golden files which 25 | # aren't guaranteed to be correct (and in practice are often corrupt). 26 | if [[ "$OSTYPE" == "darwin"* ]]; then 27 | MD5=$( md5 -r "$SOURCE" | awk '{print $1}' ) 28 | else 29 | MD5=$( md5sum "$SOURCE" | awk '{print $1}' ) 30 | fi 31 | echo "# Original: ${MD5}" >> "$GOLDEN" 32 | 33 | cat "$SOURCE" >> "$GOLDEN" 34 | done 35 | -------------------------------------------------------------------------------- /tests/BUILD: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/benchsci/rules_kustomize/918a63f10b495fedbf3909d0384dc303fd274898/tests/BUILD -------------------------------------------------------------------------------- /tests/WORKSPACE: -------------------------------------------------------------------------------- 1 | workspace(name = "tests") 2 | 3 | local_repository( 4 | name = "com_benchsci_rules_kustomize", 5 | path = "../", 6 | ) 7 | 8 | load("@com_benchsci_rules_kustomize//:workspace.bzl", "download_kustomize_deps") 9 | 10 | download_kustomize_deps() 11 | -------------------------------------------------------------------------------- /tests/b6i-compat/yyz/default/BUILD: -------------------------------------------------------------------------------- 1 | load(":wrap.bzl", "namespace") 2 | 3 | namespace( 4 | name = "default", 5 | srcs = [ 6 | "deployment.yaml", 7 | "kustomization.yaml", 8 | "service.yaml", 9 | ], 10 | out = "default.yaml", 11 | ) 12 | -------------------------------------------------------------------------------- /tests/b6i-compat/yyz/default/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: the-deployment 5 | spec: 6 | replicas: 3 7 | selector: 8 | matchLabels: 9 | deployment: hello 10 | template: 11 | metadata: 12 | labels: 13 | deployment: hello 14 | spec: 15 | containers: 16 | - name: the-container 17 | image: monopole/hello:1 18 | ports: 19 | - containerPort: 8080 20 | -------------------------------------------------------------------------------- /tests/b6i-compat/yyz/default/golden.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | labels: 5 | app: hello 6 | name: the-service 7 | spec: 8 | ports: 9 | - port: 8666 10 | protocol: TCP 11 | targetPort: 8080 12 | selector: 13 | app: hello 14 | deployment: hello 15 | type: LoadBalancer 16 | --- 17 | apiVersion: apps/v1 18 | kind: Deployment 19 | metadata: 20 | labels: 21 | app: hello 22 | name: the-deployment 23 | spec: 24 | replicas: 3 25 | selector: 26 | matchLabels: 27 | app: hello 28 | deployment: hello 29 | template: 30 | metadata: 31 | labels: 32 | app: hello 33 | deployment: hello 34 | spec: 35 | containers: 36 | - image: monopole/hello:1 37 | name: the-container 38 | ports: 39 | - containerPort: 8080 40 | -------------------------------------------------------------------------------- /tests/b6i-compat/yyz/default/golden.yaml.hash: -------------------------------------------------------------------------------- 1 | 2f2b835f869f5bcce2e5f39c2a1a263c8c4d4628 2 | -------------------------------------------------------------------------------- /tests/b6i-compat/yyz/default/kustomization.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: kustomize.config.k8s.io/v1beta1 2 | kind: Kustomization 3 | 4 | commonLabels: 5 | app: hello 6 | 7 | resources: 8 | - deployment.yaml 9 | - service.yaml 10 | -------------------------------------------------------------------------------- /tests/b6i-compat/yyz/default/service.yaml: -------------------------------------------------------------------------------- 1 | kind: Service 2 | apiVersion: v1 3 | metadata: 4 | name: the-service 5 | spec: 6 | selector: 7 | deployment: hello 8 | type: LoadBalancer 9 | ports: 10 | - protocol: TCP 11 | port: 8666 12 | targetPort: 8080 13 | -------------------------------------------------------------------------------- /tests/b6i-compat/yyz/default/wrap.bzl: -------------------------------------------------------------------------------- 1 | load("@com_benchsci_rules_kustomize//:defs.bzl", "kustomization") 2 | 3 | def namespace(name, srcs, out, golden = "golden.yaml", visibility = None, transformers = None): 4 | kwargs = {"autofix": False} 5 | if transformers: 6 | kwargs.update({ 7 | # FIXME: this is likely broken (and also needs to be relative to 8 | # workspace root? 9 | "plugin_dir": "./gke/kustomize/plugin", 10 | }) 11 | srcs = list(srcs) + list(transformers) 12 | 13 | kustomization(name, srcs, out, golden, visibility, **kwargs) 14 | 15 | if not golden: 16 | return 17 | 18 | out_hash = "{}.hash".format(out) 19 | golden_hash = "{}.hash".format(golden) 20 | 21 | native.genrule( 22 | name = name + ".hash", 23 | srcs = [":" + name], 24 | outs = [out_hash], 25 | cmd = "out=($$(shasum \"$(location :{0})\")); echo $${{out[0]}} > \"$@\"".format(name), 26 | ) 27 | 28 | native.sh_test( 29 | name = name + ".golden_hash", 30 | srcs = ["@com_benchsci_rules_kustomize//:exec"], 31 | data = [":" + name + ".hash", golden_hash], 32 | args = [ 33 | "diff", 34 | "$(location :{}.hash)".format(name), 35 | "$(location {})".format(golden_hash), 36 | ], 37 | ) 38 | native.exports_files( 39 | [golden, golden_hash], 40 | visibility = ["//gke:__pkg__"], 41 | ) 42 | 43 | native.sh_binary( 44 | name = name + ".autofix", 45 | srcs = ["@com_benchsci_rules_kustomize//:fixgolden"], 46 | data = [":" + name, ":" + name + ".hash"], 47 | args = [ 48 | "$(location :{})".format(name), 49 | golden, 50 | "$(location :{}.hash)".format(name), 51 | golden_hash, 52 | ], 53 | ) 54 | -------------------------------------------------------------------------------- /tools/gke_kubeconfig.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2021 BenchSci Analytics Inc. 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | 16 | # Create a kubeconfig file suitable for use with the kubeconfig() macro. The 17 | # resulting config contains no secret material and are suitable to check into 18 | # version control directly (certificate data embedded in the configurations are 19 | # not private keys). 20 | set -euxo pipefail 21 | 22 | # Set "container/use_application_default_credentials" property via environment 23 | # variable since we don't want to mutate the user's settings silently. 24 | 25 | # See: 26 | # https://cloud.google.com/sdk/gcloud/reference/container/clusters/get-credentials 27 | # https://cloud.google.com/sdk/docs/properties 28 | export CLOUDSDK_CONTAINER_USE_APPLICATION_DEFAULT_CREDENTIALS="true" 29 | 30 | target="${1}" && shift 31 | gcloud="${1}" && shift 32 | 33 | # Remaining arguments are passed to "gcloud ... get-credentials". 34 | 35 | # Set KUBECONFIG to the staging area for the config files (gcloud SDK reads 36 | # this value to determine where to write). 37 | export KUBECONFIG=$( mktemp ) 38 | echo "# AUTOGENERATED BY gke_kubeconfig.sh DO NOT EDIT BY HAND" > "${target}" 39 | echo "# ARGS: $@" >> "${target}" 40 | "$gcloud" container clusters get-credentials "$@" 41 | cat "${KUBECONFIG}" >> "${target}" 42 | rm "${KUBECONFIG}" 43 | 44 | # Write a BUILD file to include kubeconfig and location if it doesn't exist 45 | # already. 46 | build=$( dirname "${target}" )/BUILD 47 | if [ -f "${build}" ]; then 48 | echo "BUILD file ${build} already exists, not overwriting." 49 | else 50 | cat << 'EOF' > "${build}" 51 | load("@com_benchsci_rules_kustomize//:defs.bzl", "kubeconfig") 52 | 53 | kubeconfig() 54 | EOF 55 | fi 56 | -------------------------------------------------------------------------------- /workspace.bzl: -------------------------------------------------------------------------------- 1 | # Copyright 2021 BenchSci Analytics Inc. 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 | load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file") 16 | load(":archive.bzl", "http_archive_by_os") 17 | 18 | def download_kustomize_deps(): 19 | http_archive_by_os( 20 | name = "kustomize", 21 | build_file_content = """package(default_visibility = ["//visibility:public"]) 22 | filegroup( 23 | name = "file", 24 | srcs = ["kustomize"], 25 | ) """, 26 | sha256 = { 27 | "darwin": "6fd57e78ed0c06b5bdd82750c5dc6d0f992a7b926d114fe94be46d7a7e32b63a", 28 | "darwin_arm64": "3c1e8b95cef4ff6e52d5f4b8c65b8d9d06b75f42d1cb40986c1d67729d82411a", 29 | "linux": "701e3c4bfa14e4c520d481fdf7131f902531bfc002cb5062dcf31263a09c70c9", 30 | "linux_arm64": "65665b39297cc73c13918f05bbe8450d17556f0acd16242a339271e14861df67", 31 | }, 32 | url = { 33 | "darwin": "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv4.5.7/kustomize_v4.5.7_darwin_amd64.tar.gz", 34 | "darwin_arm64": "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv4.5.7/kustomize_v4.5.7_darwin_arm64.tar.gz", 35 | "linux": "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv4.5.7/kustomize_v4.5.7_linux_amd64.tar.gz", 36 | "linux_arm64": "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv4.5.7/kustomize_v4.5.7_linux_arm64.tar.gz", 37 | }, 38 | ) 39 | 40 | http_file( 41 | name = "kubectl_darwin", 42 | executable = True, 43 | sha256 = "bb04d9450d9c9fa120956c5cc7c8dfaa700297038ff9c941741e730b02bbd1f3", 44 | urls = ["https://storage.googleapis.com/kubernetes-release/release/v1.29.2/bin/darwin/amd64/kubectl"], 45 | ) 46 | http_file( 47 | name = "kubectl_darwin_arm64", 48 | executable = True, 49 | sha256 = "ce030f86625df96560402573d86d4e6f4b8b956ca3e3b9df57cb8ccf2b9a540c", 50 | urls = ["https://storage.googleapis.com/kubernetes-release/release/v1.29.2/bin/darwin/arm64/kubectl"], 51 | ) 52 | http_file( 53 | name = "kubectl_linux", 54 | executable = True, 55 | sha256 = "7816d067740f47f949be826ac76943167b7b3a38c4f0c18b902fffa8779a5afa", 56 | urls = ["https://storage.googleapis.com/kubernetes-release/release/v1.29.2/bin/linux/amd64/kubectl"], 57 | ) 58 | http_file( 59 | name = "kubectl_linux_arm64", 60 | executable = True, 61 | sha256 = "3507ecb4224cf05ae2151a98d4932253624e7762159936d5347b19fe037655ca", 62 | urls = ["https://storage.googleapis.com/kubernetes-release/release/v1.29.2/bin/linux/arm64/kubectl"], 63 | ) 64 | 65 | def download_gcloud_deps(): 66 | http_archive_by_os( 67 | name = "gcloud", 68 | # Need to flush lines to create proper indentation 69 | build_file_content = """package(default_visibility = ["//visibility:public"])\nexports_files(["gcloud", "gsutil", "bq"])""", 70 | patch_cmds = [ 71 | "ln -s google-cloud-sdk/bin/gcloud gcloud", 72 | "ln -s google-cloud-sdk/bin/gsutil gsutil", 73 | "ln -s google-cloud-sdk/bin/bq bq", 74 | ], 75 | sha256 = { 76 | "darwin": "26db63460c7f71c7154c753fcb01030d1a509a3fade20f7ab0241f74e7986ec3", 77 | "darwin_arm64": "331b6a1f48c8e76f1a0ebfc25b290a944af9f3dae42a22704b297fc496518375", 78 | "linux": "de3525b315d7ea3c3696eba466ddd97313ccad45f6f684bcdd9224374baa6c08", 79 | "linux_arm64": "ce382528da5f5d5daf84f63ec4289ed60069e78f0cd8d9fa11c46ae5993854ea", 80 | }, 81 | url = { 82 | "darwin": "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-410.0.0-darwin-x86_64.tar.gz", 83 | "darwin_arm64": "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-410.0.0-darwin-arm.tar.gz", 84 | "linux": "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-410.0.0-linux-x86_64.tar.gz", 85 | "linux_arm64": "https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-410.0.0-linux-arm.tar.gz", 86 | }, 87 | ) 88 | --------------------------------------------------------------------------------