diff --git a/.bazelversion b/.bazelversion new file mode 100644 index 0000000..56b6be4 --- /dev/null +++ b/.bazelversion @@ -0,0 +1 @@ +8.3.1 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..4c35be2 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,3 @@ +# A custom merge driver for the Bazel lockfile. +# https://bazel.build/external/lockfile#automatic-resolution +MODULE.bazel.lock merge=bazel-lockfile-merge diff --git a/BUILD.bazel b/BUILD.bazel index 1319b64..f76a448 100644 --- a/BUILD.bazel +++ b/BUILD.bazel @@ -1,21 +1,15 @@ -load("@rules_pkg//:pkg.bzl", "pkg_tar") load("@rules_kubebuilder//kubebuilder:def.bzl", "kubebuilder") +load("@rules_pkg//:pkg.bzl", "pkg_tar") pkg_tar( name = "release", srcs = glob([ "*.bazel", - "*.bzl", ]) + [ "WORKSPACE", ], extension = "tar.gz", package_dir = "rules_kubebuilder", - deps = [ - "//controller-gen:release", - "//kubebuilder:release", - "//kustomize:release", - ], ) # This is just an example of how to run kubebuilder via bazel diff --git a/MODULE.bazel b/MODULE.bazel new file mode 100644 index 0000000..7def5a7 --- /dev/null +++ b/MODULE.bazel @@ -0,0 +1,75 @@ +############################################################################### +# Bazel now uses Bzlmod by default to manage external dependencies. +# Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. +# +# For more details, please check https://github.com/bazelbuild/bazel/issues/18958 +############################################################################### + +module( + name = "rules_kubebuilder", + version = "0.1.0", +) + +# Keep consistent with https://github.com/openai/openai/blob/master/MODULE.bazel +bazel_dep(name = "platforms", version = "1.0.0") +bazel_dep(name = "rules_go", version = "0.55.1") +bazel_dep(name = "rules_pkg", version = "1.0.1") + +# Create controller-gen and kustomize repos via local extensions so that +# this module can reference them from its packages (toolchains). +cg = use_extension("@rules_kubebuilder//controller-gen:extensions.bzl", "cg_ext") +cg.controller_gen( + arches = [ + "amd64", + "arm64", + ], + version = "0.19.0", +) # create controller_gen__ +use_repo( + cg, + "controller_gen_darwin_amd64", + "controller_gen_darwin_arm64", + "controller_gen_linux_amd64", + "controller_gen_linux_arm64", +) + +kz = use_extension("@rules_kubebuilder//kustomize:extensions.bzl", "kz_ext") +kz.kustomize( + arches = [ + "amd64", + "arm64", + ], + version = "5.7.1", +) # create kustomize__ +use_repo( + kz, + "kustomize_darwin_amd64", + "kustomize_darwin_arm64", + "kustomize_linux_amd64", + "kustomize_linux_arm64", +) + +# Create Kubebuilder SDK repo (used by tests) +kb = use_extension("@rules_kubebuilder//kubebuilder:extensions.bzl", "kb_ext") +kb.kubebuilder_sdk( + name = "kubebuilder_sdk", + version = "4.8.0", +) +use_repo(kb, "kubebuilder_sdk") + +# Register toolchains so that local builds/tests in this repository resolve +# the kustomize and controller-gen toolchains. When used as a dependency, +# only the root module's register_toolchains(...) is honored, so these lines +# are effectively ignored by downstream consumers. +register_toolchains( + # controller-gen toolchains + "@rules_kubebuilder//controller-gen:controller_gen_linux_amd64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_linux_arm64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_darwin_amd64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_darwin_arm64_toolchain", + # kustomize toolchains + "@rules_kubebuilder//kustomize:kustomize_linux_amd64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_linux_arm64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_darwin_amd64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_darwin_arm64_toolchain", +) diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock new file mode 100644 index 0000000..9e56f59 --- /dev/null +++ b/MODULE.bazel.lock @@ -0,0 +1,383 @@ +{ + "lockFileVersion": 18, + "registryFileHashes": { + "https://bcr.bazel.build/bazel_registry.json": "8a28e4aff06ee60aed2a8c281907fb8bcbf3b753c91fb5a5c57da3215d5b3497", + "https://bcr.bazel.build/modules/abseil-cpp/20210324.2/MODULE.bazel": "7cd0312e064fde87c8d1cd79ba06c876bd23630c83466e9500321be55c96ace2", + "https://bcr.bazel.build/modules/abseil-cpp/20211102.0/MODULE.bazel": "70390338f7a5106231d20620712f7cccb659cd0e9d073d1991c038eb9fc57589", + "https://bcr.bazel.build/modules/abseil-cpp/20230125.1/MODULE.bazel": "89047429cb0207707b2dface14ba7f8df85273d484c2572755be4bab7ce9c3a0", + "https://bcr.bazel.build/modules/abseil-cpp/20230802.0.bcr.1/MODULE.bazel": "1c8cec495288dccd14fdae6e3f95f772c1c91857047a098fad772034264cc8cb", + "https://bcr.bazel.build/modules/abseil-cpp/20230802.0/MODULE.bazel": "d253ae36a8bd9ee3c5955384096ccb6baf16a1b1e93e858370da0a3b94f77c16", + "https://bcr.bazel.build/modules/abseil-cpp/20230802.1/MODULE.bazel": "fa92e2eb41a04df73cdabeec37107316f7e5272650f81d6cc096418fe647b915", + "https://bcr.bazel.build/modules/abseil-cpp/20240116.1/MODULE.bazel": "37bcdb4440fbb61df6a1c296ae01b327f19e9bb521f9b8e26ec854b6f97309ed", + "https://bcr.bazel.build/modules/abseil-cpp/20240116.1/source.json": "9be551b8d4e3ef76875c0d744b5d6a504a27e3ae67bc6b28f46415fd2d2957da", + "https://bcr.bazel.build/modules/bazel_features/1.1.0/MODULE.bazel": "cfd42ff3b815a5f39554d97182657f8c4b9719568eb7fded2b9135f084bf760b", + "https://bcr.bazel.build/modules/bazel_features/1.1.1/MODULE.bazel": "27b8c79ef57efe08efccbd9dd6ef70d61b4798320b8d3c134fd571f78963dbcd", + "https://bcr.bazel.build/modules/bazel_features/1.11.0/MODULE.bazel": "f9382337dd5a474c3b7d334c2f83e50b6eaedc284253334cf823044a26de03e8", + "https://bcr.bazel.build/modules/bazel_features/1.15.0/MODULE.bazel": "d38ff6e517149dc509406aca0db3ad1efdd890a85e049585b7234d04238e2a4d", + "https://bcr.bazel.build/modules/bazel_features/1.17.0/MODULE.bazel": "039de32d21b816b47bd42c778e0454217e9c9caac4a3cf8e15c7231ee3ddee4d", + "https://bcr.bazel.build/modules/bazel_features/1.18.0/MODULE.bazel": "1be0ae2557ab3a72a57aeb31b29be347bcdc5d2b1eb1e70f39e3851a7e97041a", + "https://bcr.bazel.build/modules/bazel_features/1.19.0/MODULE.bazel": "59adcdf28230d220f0067b1f435b8537dd033bfff8db21335ef9217919c7fb58", + "https://bcr.bazel.build/modules/bazel_features/1.30.0/MODULE.bazel": "a14b62d05969a293b80257e72e597c2da7f717e1e69fa8b339703ed6731bec87", + "https://bcr.bazel.build/modules/bazel_features/1.30.0/source.json": "b07e17f067fe4f69f90b03b36ef1e08fe0d1f3cac254c1241a1818773e3423bc", + "https://bcr.bazel.build/modules/bazel_features/1.4.1/MODULE.bazel": "e45b6bb2350aff3e442ae1111c555e27eac1d915e77775f6fdc4b351b758b5d7", + "https://bcr.bazel.build/modules/bazel_features/1.9.1/MODULE.bazel": "8f679097876a9b609ad1f60249c49d68bfab783dd9be012faf9d82547b14815a", + "https://bcr.bazel.build/modules/bazel_skylib/1.0.3/MODULE.bazel": "bcb0fd896384802d1ad283b4e4eb4d718eebd8cb820b0a2c3a347fb971afd9d8", + "https://bcr.bazel.build/modules/bazel_skylib/1.1.1/MODULE.bazel": "1add3e7d93ff2e6998f9e118022c84d163917d912f5afafb3058e3d2f1545b5e", + "https://bcr.bazel.build/modules/bazel_skylib/1.2.0/MODULE.bazel": "44fe84260e454ed94ad326352a698422dbe372b21a1ac9f3eab76eb531223686", + "https://bcr.bazel.build/modules/bazel_skylib/1.2.1/MODULE.bazel": "f35baf9da0efe45fa3da1696ae906eea3d615ad41e2e3def4aeb4e8bc0ef9a7a", + "https://bcr.bazel.build/modules/bazel_skylib/1.3.0/MODULE.bazel": "20228b92868bf5cfc41bda7afc8a8ba2a543201851de39d990ec957b513579c5", + "https://bcr.bazel.build/modules/bazel_skylib/1.4.1/MODULE.bazel": "a0dcb779424be33100dcae821e9e27e4f2901d9dfd5333efe5ac6a8d7ab75e1d", + "https://bcr.bazel.build/modules/bazel_skylib/1.4.2/MODULE.bazel": "3bd40978e7a1fac911d5989e6b09d8f64921865a45822d8b09e815eaa726a651", + "https://bcr.bazel.build/modules/bazel_skylib/1.5.0/MODULE.bazel": "32880f5e2945ce6a03d1fbd588e9198c0a959bb42297b2cfaf1685b7bc32e138", + "https://bcr.bazel.build/modules/bazel_skylib/1.6.1/MODULE.bazel": "8fdee2dbaace6c252131c00e1de4b165dc65af02ea278476187765e1a617b917", + "https://bcr.bazel.build/modules/bazel_skylib/1.7.0/MODULE.bazel": "0db596f4563de7938de764cc8deeabec291f55e8ec15299718b93c4423e9796d", + "https://bcr.bazel.build/modules/bazel_skylib/1.7.1/MODULE.bazel": "3120d80c5861aa616222ec015332e5f8d3171e062e3e804a2a0253e1be26e59b", + "https://bcr.bazel.build/modules/bazel_skylib/1.7.1/source.json": "f121b43eeefc7c29efbd51b83d08631e2347297c95aac9764a701f2a6a2bb953", + "https://bcr.bazel.build/modules/buildozer/7.1.2/MODULE.bazel": "2e8dd40ede9c454042645fd8d8d0cd1527966aa5c919de86661e62953cd73d84", + "https://bcr.bazel.build/modules/buildozer/7.1.2/source.json": "c9028a501d2db85793a6996205c8de120944f50a0d570438fcae0457a5f9d1f8", + "https://bcr.bazel.build/modules/gazelle/0.32.0/MODULE.bazel": "b499f58a5d0d3537f3cf5b76d8ada18242f64ec474d8391247438bf04f58c7b8", + "https://bcr.bazel.build/modules/gazelle/0.33.0/MODULE.bazel": "a13a0f279b462b784fb8dd52a4074526c4a2afe70e114c7d09066097a46b3350", + "https://bcr.bazel.build/modules/gazelle/0.34.0/MODULE.bazel": "abdd8ce4d70978933209db92e436deb3a8b737859e9354fb5fd11fb5c2004c8a", + "https://bcr.bazel.build/modules/gazelle/0.36.0/MODULE.bazel": "e375d5d6e9a6ca59b0cb38b0540bc9a05b6aa926d322f2de268ad267a2ee74c0", + "https://bcr.bazel.build/modules/gazelle/0.36.0/source.json": "0823f097b127e0201ae55d85647c94095edfe27db0431a7ae880dcab08dfaa04", + "https://bcr.bazel.build/modules/google_benchmark/1.8.2/MODULE.bazel": "a70cf1bba851000ba93b58ae2f6d76490a9feb74192e57ab8e8ff13c34ec50cb", + "https://bcr.bazel.build/modules/googletest/1.11.0/MODULE.bazel": "3a83f095183f66345ca86aa13c58b59f9f94a2f81999c093d4eeaa2d262d12f4", + "https://bcr.bazel.build/modules/googletest/1.14.0.bcr.1/MODULE.bazel": "22c31a561553727960057361aa33bf20fb2e98584bc4fec007906e27053f80c6", + "https://bcr.bazel.build/modules/googletest/1.14.0.bcr.1/source.json": "41e9e129f80d8c8bf103a7acc337b76e54fad1214ac0a7084bf24f4cd924b8b4", + "https://bcr.bazel.build/modules/googletest/1.14.0/MODULE.bazel": "cfbcbf3e6eac06ef9d85900f64424708cc08687d1b527f0ef65aa7517af8118f", + "https://bcr.bazel.build/modules/jsoncpp/1.9.5/MODULE.bazel": "31271aedc59e815656f5736f282bb7509a97c7ecb43e927ac1a37966e0578075", + "https://bcr.bazel.build/modules/jsoncpp/1.9.5/source.json": "4108ee5085dd2885a341c7fab149429db457b3169b86eb081fa245eadf69169d", + "https://bcr.bazel.build/modules/libpfm/4.11.0/MODULE.bazel": "45061ff025b301940f1e30d2c16bea596c25b176c8b6b3087e92615adbd52902", + "https://bcr.bazel.build/modules/platforms/0.0.10/MODULE.bazel": "8cb8efaf200bdeb2150d93e162c40f388529a25852b332cec879373771e48ed5", + "https://bcr.bazel.build/modules/platforms/0.0.11/MODULE.bazel": "0daefc49732e227caa8bfa834d65dc52e8cc18a2faf80df25e8caea151a9413f", + "https://bcr.bazel.build/modules/platforms/0.0.4/MODULE.bazel": "9b328e31ee156f53f3c416a64f8491f7eb731742655a47c9eec4703a71644aee", + "https://bcr.bazel.build/modules/platforms/0.0.5/MODULE.bazel": "5733b54ea419d5eaf7997054bb55f6a1d0b5ff8aedf0176fef9eea44f3acda37", + "https://bcr.bazel.build/modules/platforms/0.0.6/MODULE.bazel": "ad6eeef431dc52aefd2d77ed20a4b353f8ebf0f4ecdd26a807d2da5aa8cd0615", + "https://bcr.bazel.build/modules/platforms/0.0.7/MODULE.bazel": "72fd4a0ede9ee5c021f6a8dd92b503e089f46c227ba2813ff183b71616034814", + "https://bcr.bazel.build/modules/platforms/0.0.8/MODULE.bazel": "9f142c03e348f6d263719f5074b21ef3adf0b139ee4c5133e2aa35664da9eb2d", + "https://bcr.bazel.build/modules/platforms/1.0.0/MODULE.bazel": "f05feb42b48f1b3c225e4ccf351f367be0371411a803198ec34a389fb22aa580", + "https://bcr.bazel.build/modules/platforms/1.0.0/source.json": "f4ff1fd412e0246fd38c82328eb209130ead81d62dcd5a9e40910f867f733d96", + "https://bcr.bazel.build/modules/protobuf/21.7/MODULE.bazel": "a5a29bb89544f9b97edce05642fac225a808b5b7be74038ea3640fae2f8e66a7", + "https://bcr.bazel.build/modules/protobuf/27.0/MODULE.bazel": "7873b60be88844a0a1d8f80b9d5d20cfbd8495a689b8763e76c6372998d3f64c", + "https://bcr.bazel.build/modules/protobuf/27.1/MODULE.bazel": "703a7b614728bb06647f965264967a8ef1c39e09e8f167b3ca0bb1fd80449c0d", + "https://bcr.bazel.build/modules/protobuf/29.0-rc2.bcr.1/MODULE.bazel": "52f4126f63a2f0bbf36b99c2a87648f08467a4eaf92ba726bc7d6a500bbf770c", + "https://bcr.bazel.build/modules/protobuf/29.0-rc2/MODULE.bazel": "6241d35983510143049943fc0d57937937122baf1b287862f9dc8590fc4c37df", + "https://bcr.bazel.build/modules/protobuf/29.0/MODULE.bazel": "319dc8bf4c679ff87e71b1ccfb5a6e90a6dbc4693501d471f48662ac46d04e4e", + "https://bcr.bazel.build/modules/protobuf/29.0/source.json": "b857f93c796750eef95f0d61ee378f3420d00ee1dd38627b27193aa482f4f981", + "https://bcr.bazel.build/modules/protobuf/3.19.0/MODULE.bazel": "6b5fbb433f760a99a22b18b6850ed5784ef0e9928a72668b66e4d7ccd47db9b0", + "https://bcr.bazel.build/modules/protobuf/3.19.2/MODULE.bazel": "532ffe5f2186b69fdde039efe6df13ba726ff338c6bc82275ad433013fa10573", + "https://bcr.bazel.build/modules/protobuf/3.19.6/MODULE.bazel": "9233edc5e1f2ee276a60de3eaa47ac4132302ef9643238f23128fea53ea12858", + "https://bcr.bazel.build/modules/pybind11_bazel/2.11.1/MODULE.bazel": "88af1c246226d87e65be78ed49ecd1e6f5e98648558c14ce99176da041dc378e", + "https://bcr.bazel.build/modules/pybind11_bazel/2.11.1/source.json": "be4789e951dd5301282729fe3d4938995dc4c1a81c2ff150afc9f1b0504c6022", + "https://bcr.bazel.build/modules/re2/2023-09-01/MODULE.bazel": "cb3d511531b16cfc78a225a9e2136007a48cf8a677e4264baeab57fe78a80206", + "https://bcr.bazel.build/modules/re2/2023-09-01/source.json": "e044ce89c2883cd957a2969a43e79f7752f9656f6b20050b62f90ede21ec6eb4", + "https://bcr.bazel.build/modules/rules_android/0.1.1/MODULE.bazel": "48809ab0091b07ad0182defb787c4c5328bd3a278938415c00a7b69b50c4d3a8", + "https://bcr.bazel.build/modules/rules_android/0.1.1/source.json": "e6986b41626ee10bdc864937ffb6d6bf275bb5b9c65120e6137d56e6331f089e", + "https://bcr.bazel.build/modules/rules_cc/0.0.1/MODULE.bazel": "cb2aa0747f84c6c3a78dad4e2049c154f08ab9d166b1273835a8174940365647", + "https://bcr.bazel.build/modules/rules_cc/0.0.10/MODULE.bazel": "ec1705118f7eaedd6e118508d3d26deba2a4e76476ada7e0e3965211be012002", + "https://bcr.bazel.build/modules/rules_cc/0.0.13/MODULE.bazel": "0e8529ed7b323dad0775ff924d2ae5af7640b23553dfcd4d34344c7e7a867191", + "https://bcr.bazel.build/modules/rules_cc/0.0.14/MODULE.bazel": "5e343a3aac88b8d7af3b1b6d2093b55c347b8eefc2e7d1442f7a02dc8fea48ac", + "https://bcr.bazel.build/modules/rules_cc/0.0.15/MODULE.bazel": "6704c35f7b4a72502ee81f61bf88706b54f06b3cbe5558ac17e2e14666cd5dcc", + "https://bcr.bazel.build/modules/rules_cc/0.0.16/MODULE.bazel": "7661303b8fc1b4d7f532e54e9d6565771fea666fbdf839e0a86affcd02defe87", + "https://bcr.bazel.build/modules/rules_cc/0.0.2/MODULE.bazel": "6915987c90970493ab97393024c156ea8fb9f3bea953b2f3ec05c34f19b5695c", + "https://bcr.bazel.build/modules/rules_cc/0.0.6/MODULE.bazel": "abf360251023dfe3efcef65ab9d56beefa8394d4176dd29529750e1c57eaa33f", + "https://bcr.bazel.build/modules/rules_cc/0.0.8/MODULE.bazel": "964c85c82cfeb6f3855e6a07054fdb159aced38e99a5eecf7bce9d53990afa3e", + "https://bcr.bazel.build/modules/rules_cc/0.0.9/MODULE.bazel": "836e76439f354b89afe6a911a7adf59a6b2518fafb174483ad78a2a2fde7b1c5", + "https://bcr.bazel.build/modules/rules_cc/0.1.1/MODULE.bazel": "2f0222a6f229f0bf44cd711dc13c858dad98c62d52bd51d8fc3a764a83125513", + "https://bcr.bazel.build/modules/rules_cc/0.1.1/source.json": "d61627377bd7dd1da4652063e368d9366fc9a73920bfa396798ad92172cf645c", + "https://bcr.bazel.build/modules/rules_foreign_cc/0.9.0/MODULE.bazel": "c9e8c682bf75b0e7c704166d79b599f93b72cfca5ad7477df596947891feeef6", + "https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/MODULE.bazel": "40c97d1144356f52905566c55811f13b299453a14ac7769dfba2ac38192337a8", + "https://bcr.bazel.build/modules/rules_fuzzing/0.5.2/source.json": "c8b1e2c717646f1702290959a3302a178fb639d987ab61d548105019f11e527e", + "https://bcr.bazel.build/modules/rules_go/0.41.0/MODULE.bazel": "55861d8e8bb0e62cbd2896f60ff303f62ffcb0eddb74ecb0e5c0cbe36fc292c8", + "https://bcr.bazel.build/modules/rules_go/0.42.0/MODULE.bazel": "8cfa875b9aa8c6fce2b2e5925e73c1388173ea3c32a0db4d2b4804b453c14270", + "https://bcr.bazel.build/modules/rules_go/0.46.0/MODULE.bazel": "3477df8bdcc49e698b9d25f734c4f3a9f5931ff34ee48a2c662be168f5f2d3fd", + "https://bcr.bazel.build/modules/rules_go/0.55.1/MODULE.bazel": "a57a6fc59a74326c0b440d07cca209edf13c7d1a641e48cfbeab56e79f873609", + "https://bcr.bazel.build/modules/rules_go/0.55.1/source.json": "827a740c8959c9d20616889e7746cde4dcc6ee80d25146943627ccea0736328f", + "https://bcr.bazel.build/modules/rules_java/4.0.0/MODULE.bazel": "5a78a7ae82cd1a33cef56dc578c7d2a46ed0dca12643ee45edbb8417899e6f74", + "https://bcr.bazel.build/modules/rules_java/5.3.5/MODULE.bazel": "a4ec4f2db570171e3e5eb753276ee4b389bae16b96207e9d3230895c99644b86", + "https://bcr.bazel.build/modules/rules_java/6.0.0/MODULE.bazel": "8a43b7df601a7ec1af61d79345c17b31ea1fedc6711fd4abfd013ea612978e39", + "https://bcr.bazel.build/modules/rules_java/6.4.0/MODULE.bazel": "e986a9fe25aeaa84ac17ca093ef13a4637f6107375f64667a15999f77db6c8f6", + "https://bcr.bazel.build/modules/rules_java/6.5.2/MODULE.bazel": "1d440d262d0e08453fa0c4d8f699ba81609ed0e9a9a0f02cd10b3e7942e61e31", + "https://bcr.bazel.build/modules/rules_java/7.10.0/MODULE.bazel": "530c3beb3067e870561739f1144329a21c851ff771cd752a49e06e3dc9c2e71a", + "https://bcr.bazel.build/modules/rules_java/7.12.2/MODULE.bazel": "579c505165ee757a4280ef83cda0150eea193eed3bef50b1004ba88b99da6de6", + "https://bcr.bazel.build/modules/rules_java/7.2.0/MODULE.bazel": "06c0334c9be61e6cef2c8c84a7800cef502063269a5af25ceb100b192453d4ab", + "https://bcr.bazel.build/modules/rules_java/7.3.2/MODULE.bazel": "50dece891cfdf1741ea230d001aa9c14398062f2b7c066470accace78e412bc2", + "https://bcr.bazel.build/modules/rules_java/7.6.1/MODULE.bazel": "2f14b7e8a1aa2f67ae92bc69d1ec0fa8d9f827c4e17ff5e5f02e91caa3b2d0fe", + "https://bcr.bazel.build/modules/rules_java/8.12.0/MODULE.bazel": "8e6590b961f2defdfc2811c089c75716cb2f06c8a4edeb9a8d85eaa64ee2a761", + "https://bcr.bazel.build/modules/rules_java/8.12.0/source.json": "cbd5d55d9d38d4008a7d00bee5b5a5a4b6031fcd4a56515c9accbcd42c7be2ba", + "https://bcr.bazel.build/modules/rules_jvm_external/4.4.2/MODULE.bazel": "a56b85e418c83eb1839819f0b515c431010160383306d13ec21959ac412d2fe7", + "https://bcr.bazel.build/modules/rules_jvm_external/5.1/MODULE.bazel": "33f6f999e03183f7d088c9be518a63467dfd0be94a11d0055fe2d210f89aa909", + "https://bcr.bazel.build/modules/rules_jvm_external/5.2/MODULE.bazel": "d9351ba35217ad0de03816ef3ed63f89d411349353077348a45348b096615036", + "https://bcr.bazel.build/modules/rules_jvm_external/5.3/MODULE.bazel": "bf93870767689637164657731849fb887ad086739bd5d360d90007a581d5527d", + "https://bcr.bazel.build/modules/rules_jvm_external/6.1/MODULE.bazel": "75b5fec090dbd46cf9b7d8ea08cf84a0472d92ba3585b476f44c326eda8059c4", + "https://bcr.bazel.build/modules/rules_jvm_external/6.3/MODULE.bazel": "c998e060b85f71e00de5ec552019347c8bca255062c990ac02d051bb80a38df0", + "https://bcr.bazel.build/modules/rules_jvm_external/6.3/source.json": "6f5f5a5a4419ae4e37c35a5bb0a6ae657ed40b7abc5a5189111b47fcebe43197", + "https://bcr.bazel.build/modules/rules_kotlin/1.9.0/MODULE.bazel": "ef85697305025e5a61f395d4eaede272a5393cee479ace6686dba707de804d59", + "https://bcr.bazel.build/modules/rules_kotlin/1.9.6/MODULE.bazel": "d269a01a18ee74d0335450b10f62c9ed81f2321d7958a2934e44272fe82dcef3", + "https://bcr.bazel.build/modules/rules_kotlin/1.9.6/source.json": "2faa4794364282db7c06600b7e5e34867a564ae91bda7cae7c29c64e9466b7d5", + "https://bcr.bazel.build/modules/rules_license/0.0.3/MODULE.bazel": "627e9ab0247f7d1e05736b59dbb1b6871373de5ad31c3011880b4133cafd4bd0", + "https://bcr.bazel.build/modules/rules_license/0.0.7/MODULE.bazel": "088fbeb0b6a419005b89cf93fe62d9517c0a2b8bb56af3244af65ecfe37e7d5d", + "https://bcr.bazel.build/modules/rules_license/1.0.0/MODULE.bazel": "a7fda60eefdf3d8c827262ba499957e4df06f659330bbe6cdbdb975b768bb65c", + "https://bcr.bazel.build/modules/rules_license/1.0.0/source.json": "a52c89e54cc311196e478f8382df91c15f7a2bfdf4c6cd0e2675cc2ff0b56efb", + "https://bcr.bazel.build/modules/rules_pkg/0.7.0/MODULE.bazel": "df99f03fc7934a4737122518bb87e667e62d780b610910f0447665a7e2be62dc", + "https://bcr.bazel.build/modules/rules_pkg/1.0.1/MODULE.bazel": "5b1df97dbc29623bccdf2b0dcd0f5cb08e2f2c9050aab1092fd39a41e82686ff", + "https://bcr.bazel.build/modules/rules_pkg/1.0.1/source.json": "bd82e5d7b9ce2d31e380dd9f50c111d678c3bdaca190cb76b0e1c71b05e1ba8a", + "https://bcr.bazel.build/modules/rules_proto/4.0.0/MODULE.bazel": "a7a7b6ce9bee418c1a760b3d84f83a299ad6952f9903c67f19e4edd964894e06", + "https://bcr.bazel.build/modules/rules_proto/5.3.0-21.7/MODULE.bazel": "e8dff86b0971688790ae75528fe1813f71809b5afd57facb44dad9e8eca631b7", + "https://bcr.bazel.build/modules/rules_proto/6.0.2/MODULE.bazel": "ce916b775a62b90b61888052a416ccdda405212b6aaeb39522f7dc53431a5e73", + "https://bcr.bazel.build/modules/rules_proto/7.0.2/MODULE.bazel": "bf81793bd6d2ad89a37a40693e56c61b0ee30f7a7fdbaf3eabbf5f39de47dea2", + "https://bcr.bazel.build/modules/rules_proto/7.0.2/source.json": "1e5e7260ae32ef4f2b52fd1d0de8d03b606a44c91b694d2f1afb1d3b28a48ce1", + "https://bcr.bazel.build/modules/rules_python/0.10.2/MODULE.bazel": "cc82bc96f2997baa545ab3ce73f196d040ffb8756fd2d66125a530031cd90e5f", + "https://bcr.bazel.build/modules/rules_python/0.23.1/MODULE.bazel": "49ffccf0511cb8414de28321f5fcf2a31312b47c40cc21577144b7447f2bf300", + "https://bcr.bazel.build/modules/rules_python/0.25.0/MODULE.bazel": "72f1506841c920a1afec76975b35312410eea3aa7b63267436bfb1dd91d2d382", + "https://bcr.bazel.build/modules/rules_python/0.28.0/MODULE.bazel": "cba2573d870babc976664a912539b320cbaa7114cd3e8f053c720171cde331ed", + "https://bcr.bazel.build/modules/rules_python/0.31.0/MODULE.bazel": "93a43dc47ee570e6ec9f5779b2e64c1476a6ce921c48cc9a1678a91dd5f8fd58", + "https://bcr.bazel.build/modules/rules_python/0.4.0/MODULE.bazel": "9208ee05fd48bf09ac60ed269791cf17fb343db56c8226a720fbb1cdf467166c", + "https://bcr.bazel.build/modules/rules_python/0.40.0/MODULE.bazel": "9d1a3cd88ed7d8e39583d9ffe56ae8a244f67783ae89b60caafc9f5cf318ada7", + "https://bcr.bazel.build/modules/rules_python/0.40.0/source.json": "939d4bd2e3110f27bfb360292986bb79fd8dcefb874358ccd6cdaa7bda029320", + "https://bcr.bazel.build/modules/rules_shell/0.2.0/MODULE.bazel": "fda8a652ab3c7d8fee214de05e7a9916d8b28082234e8d2c0094505c5268ed3c", + "https://bcr.bazel.build/modules/rules_shell/0.3.0/MODULE.bazel": "de4402cd12f4cc8fda2354fce179fdb068c0b9ca1ec2d2b17b3e21b24c1a937b", + "https://bcr.bazel.build/modules/rules_shell/0.3.0/source.json": "c55ed591aa5009401ddf80ded9762ac32c358d2517ee7820be981e2de9756cf3", + "https://bcr.bazel.build/modules/stardoc/0.5.1/MODULE.bazel": "1a05d92974d0c122f5ccf09291442580317cdd859f07a8655f1db9a60374f9f8", + "https://bcr.bazel.build/modules/stardoc/0.5.3/MODULE.bazel": "c7f6948dae6999bf0db32c1858ae345f112cacf98f174c7a8bb707e41b974f1c", + "https://bcr.bazel.build/modules/stardoc/0.5.6/MODULE.bazel": "c43dabc564990eeab55e25ed61c07a1aadafe9ece96a4efabb3f8bf9063b71ef", + "https://bcr.bazel.build/modules/stardoc/0.7.0/MODULE.bazel": "05e3d6d30c099b6770e97da986c53bd31844d7f13d41412480ea265ac9e8079c", + "https://bcr.bazel.build/modules/stardoc/0.7.1/MODULE.bazel": "3548faea4ee5dda5580f9af150e79d0f6aea934fc60c1cc50f4efdd9420759e7", + "https://bcr.bazel.build/modules/stardoc/0.7.1/source.json": "b6500ffcd7b48cd72c29bb67bcac781e12701cc0d6d55d266a652583cfcdab01", + "https://bcr.bazel.build/modules/upb/0.0.0-20220923-a547704/MODULE.bazel": "7298990c00040a0e2f121f6c32544bab27d4452f80d9ce51349b1a28f3005c43", + "https://bcr.bazel.build/modules/zlib/1.2.11/MODULE.bazel": "07b389abc85fdbca459b69e2ec656ae5622873af3f845e1c9d80fe179f3effa0", + "https://bcr.bazel.build/modules/zlib/1.2.12/MODULE.bazel": "3b1a8834ada2a883674be8cbd36ede1b6ec481477ada359cd2d3ddc562340b27", + "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.5/MODULE.bazel": "eec517b5bbe5492629466e11dae908d043364302283de25581e3eb944326c4ca", + "https://bcr.bazel.build/modules/zlib/1.3.1.bcr.5/source.json": "22bc55c47af97246cfc093d0acf683a7869377de362b5d1c552c2c2e16b7a806", + "https://bcr.bazel.build/modules/zlib/1.3.1/MODULE.bazel": "751c9940dcfe869f5f7274e1295422a34623555916eb98c174c1e945594bf198" + }, + "selectedYankedVersions": {}, + "moduleExtensions": { + "//controller-gen:extensions.bzl%cg_ext": { + "general": { + "bzlTransitiveDigest": "yeYb4fQOVBf7WOc1zFgTyh/mBWI4NQuVMDPk6TFv2uo=", + "usagesDigest": "wOvjBKNTJr/dAVS+n2fsikp4c8yrUR0Dj13hKskdlCE=", + "recordedFileInputs": {}, + "recordedDirentsInputs": {}, + "envVariables": {}, + "generatedRepoSpecs": { + "controller_gen_linux_amd64": { + "repoRuleId": "@@//controller-gen:repo.bzl%_download_controller_gen", + "attributes": { + "version": "0.19.0", + "os": "linux", + "arch": "amd64", + "urls": [] + } + }, + "controller_gen_linux_arm64": { + "repoRuleId": "@@//controller-gen:repo.bzl%_download_controller_gen", + "attributes": { + "version": "0.19.0", + "os": "linux", + "arch": "arm64", + "urls": [] + } + }, + "controller_gen_darwin_amd64": { + "repoRuleId": "@@//controller-gen:repo.bzl%_download_controller_gen", + "attributes": { + "version": "0.19.0", + "os": "darwin", + "arch": "amd64", + "urls": [] + } + }, + "controller_gen_darwin_arm64": { + "repoRuleId": "@@//controller-gen:repo.bzl%_download_controller_gen", + "attributes": { + "version": "0.19.0", + "os": "darwin", + "arch": "arm64", + "urls": [] + } + } + }, + "moduleExtensionMetadata": { + "explicitRootModuleDirectDeps": [ + "controller_gen_linux_amd64", + "controller_gen_linux_arm64", + "controller_gen_darwin_amd64", + "controller_gen_darwin_arm64" + ], + "explicitRootModuleDirectDevDeps": [], + "useAllRepos": "NO", + "reproducible": false + }, + "recordedRepoMappingEntries": [ + [ + "", + "rules_kubebuilder", + "" + ] + ] + } + }, + "//kubebuilder:extensions.bzl%kb_ext": { + "general": { + "bzlTransitiveDigest": "VA6PzIZ1twkSvL+8Er89L8doTfsZ4qcMtDvA9ce9hiU=", + "usagesDigest": "pbUstmx+aQgGQp8yrbydVfMOUG1+J2xdrIRr/1eRUtU=", + "recordedFileInputs": {}, + "recordedDirentsInputs": {}, + "envVariables": {}, + "generatedRepoSpecs": { + "kubebuilder_sdk": { + "repoRuleId": "@@//kubebuilder:sdk.bzl%_kubebuilder_download_sdk", + "attributes": { + "version": "4.8.0" + } + } + }, + "moduleExtensionMetadata": { + "explicitRootModuleDirectDeps": [ + "kubebuilder_sdk" + ], + "explicitRootModuleDirectDevDeps": [], + "useAllRepos": "NO", + "reproducible": false + }, + "recordedRepoMappingEntries": [ + [ + "", + "rules_kubebuilder", + "" + ] + ] + } + }, + "//kustomize:extensions.bzl%kz_ext": { + "general": { + "bzlTransitiveDigest": "0rcc724NzhWCZmSA+lXIqxvA/H8c19v4h4jEo/oopj8=", + "usagesDigest": "Dr+eIfYpSrqdYGJgBhlMTg1RO28dh9+9wfvCQJ7QlK8=", + "recordedFileInputs": {}, + "recordedDirentsInputs": {}, + "envVariables": {}, + "generatedRepoSpecs": { + "kustomize_linux_amd64": { + "repoRuleId": "@@//kustomize:repo.bzl%_download_kustomize", + "attributes": { + "version": "5.7.1", + "os": "linux", + "arch": "amd64", + "urls": [] + } + }, + "kustomize_linux_arm64": { + "repoRuleId": "@@//kustomize:repo.bzl%_download_kustomize", + "attributes": { + "version": "5.7.1", + "os": "linux", + "arch": "arm64", + "urls": [] + } + }, + "kustomize_darwin_amd64": { + "repoRuleId": "@@//kustomize:repo.bzl%_download_kustomize", + "attributes": { + "version": "5.7.1", + "os": "darwin", + "arch": "amd64", + "urls": [] + } + }, + "kustomize_darwin_arm64": { + "repoRuleId": "@@//kustomize:repo.bzl%_download_kustomize", + "attributes": { + "version": "5.7.1", + "os": "darwin", + "arch": "arm64", + "urls": [] + } + } + }, + "moduleExtensionMetadata": { + "explicitRootModuleDirectDeps": [ + "kustomize_linux_amd64", + "kustomize_linux_arm64", + "kustomize_darwin_amd64", + "kustomize_darwin_arm64" + ], + "explicitRootModuleDirectDevDeps": [], + "useAllRepos": "NO", + "reproducible": false + }, + "recordedRepoMappingEntries": [ + [ + "", + "rules_kubebuilder", + "" + ] + ] + } + }, + "@@rules_kotlin+//src/main/starlark/core/repositories:bzlmod_setup.bzl%rules_kotlin_extensions": { + "general": { + "bzlTransitiveDigest": "hUTp2w+RUVdL7ma5esCXZJAFnX7vLbVfLd7FwnQI6bU=", + "usagesDigest": "QI2z8ZUR+mqtbwsf2fLqYdJAkPOHdOV+tF2yVAUgRzw=", + "recordedFileInputs": {}, + "recordedDirentsInputs": {}, + "envVariables": {}, + "generatedRepoSpecs": { + "com_github_jetbrains_kotlin_git": { + "repoRuleId": "@@rules_kotlin+//src/main/starlark/core/repositories:compiler.bzl%kotlin_compiler_git_repository", + "attributes": { + "urls": [ + "https://github.com/JetBrains/kotlin/releases/download/v1.9.23/kotlin-compiler-1.9.23.zip" + ], + "sha256": "93137d3aab9afa9b27cb06a824c2324195c6b6f6179d8a8653f440f5bd58be88" + } + }, + "com_github_jetbrains_kotlin": { + "repoRuleId": "@@rules_kotlin+//src/main/starlark/core/repositories:compiler.bzl%kotlin_capabilities_repository", + "attributes": { + "git_repository_name": "com_github_jetbrains_kotlin_git", + "compiler_version": "1.9.23" + } + }, + "com_github_google_ksp": { + "repoRuleId": "@@rules_kotlin+//src/main/starlark/core/repositories:ksp.bzl%ksp_compiler_plugin_repository", + "attributes": { + "urls": [ + "https://github.com/google/ksp/releases/download/1.9.23-1.0.20/artifacts.zip" + ], + "sha256": "ee0618755913ef7fd6511288a232e8fad24838b9af6ea73972a76e81053c8c2d", + "strip_version": "1.9.23-1.0.20" + } + }, + "com_github_pinterest_ktlint": { + "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_file", + "attributes": { + "sha256": "01b2e0ef893383a50dbeb13970fe7fa3be36ca3e83259e01649945b09d736985", + "urls": [ + "https://github.com/pinterest/ktlint/releases/download/1.3.0/ktlint" + ], + "executable": true + } + }, + "rules_android": { + "repoRuleId": "@@bazel_tools//tools/build_defs/repo:http.bzl%http_archive", + "attributes": { + "sha256": "cd06d15dd8bb59926e4d65f9003bfc20f9da4b2519985c27e190cddc8b7a7806", + "strip_prefix": "rules_android-0.1.1", + "urls": [ + "https://github.com/bazelbuild/rules_android/archive/v0.1.1.zip" + ] + } + } + }, + "recordedRepoMappingEntries": [ + [ + "rules_kotlin+", + "bazel_tools", + "bazel_tools" + ] + ] + } + } + } +} diff --git a/README.md b/README.md index 0fbbdf0..a7ecc35 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,70 @@ These bazel rules download and make available the [Kubebuilder SDK](https://github.com/kubernetes-sigs/kubebuilder) for building kubernetes operators in bazel. +## Installation + +### Using with Bzlmod (MODULE.bazel) + +With Bzlmod, repository creation and toolchain registration must be separated: + +- Module extensions may create external repositories but may not call `register_toolchains()`. +- Toolchains must be registered in the root `MODULE.bazel` via the `register_toolchains(...)` directive. + +This repo provides module extensions to download the Kubebuilder SDK, controller-gen, and kustomize. Register the toolchains directly using labels from this repo. + +Note: Recent Kubebuilder releases only include the `kubebuilder` binary in the SDK tarball. The `etcd`, `kube-apiserver`, and `kubectl` binaries are not bundled and must be provided separately. Collect these binaries into a single directory at runtime and set `KUBEBUILDER_ASSETS` to that directory for tests that use envtest. + +Example `MODULE.bazel` snippet: + +```starlark +module(name = "your_module") + +bazel_dep(name = "rules_kubebuilder", version = "0.1.0") + +# Create the SDK repo(s) via the module extension +kb = use_extension("@rules_kubebuilder//kubebuilder:extensions.bzl", "kb_ext") +kb.kubebuilder_sdk(name = "kubebuilder_sdk_4_8_0", version = "4.8.0") +use_repo(kb, "kubebuilder_sdk_4_8_0") + +# Download controller-gen binaries for both arches +cg = use_extension("@rules_kubebuilder//controller-gen:extensions.bzl", "cg_ext") +cg.controller_gen(version = "0.17.1", arches = ["amd64", "arm64"]) # creates repos named controller_gen__ +use_repo(cg, + "controller_gen_linux_amd64", + "controller_gen_linux_arm64", + "controller_gen_darwin_amd64", + "controller_gen_darwin_arm64", +) + +# Download kustomize binaries for both arches +kz = use_extension("@rules_kubebuilder//kustomize:extensions.bzl", "kz_ext") +kz.kustomize(version = "5.7.1", arches = ["amd64", "arm64"]) # creates repos named kustomize__ +use_repo(kz, + "kustomize_linux_amd64", + "kustomize_linux_arm64", + "kustomize_darwin_amd64", + "kustomize_darwin_arm64", +) + +# Register toolchains at the root (don’t do this from an extension) +register_toolchains( + # controller-gen + "@rules_kubebuilder//controller-gen:controller_gen_linux_amd64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_linux_arm64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_darwin_amd64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_darwin_arm64_toolchain", + # kustomize + "@rules_kubebuilder//kustomize:kustomize_linux_amd64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_linux_arm64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_darwin_amd64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_darwin_arm64_toolchain", +) +``` + +Note: The macros in `controller-gen/deps.bzl` and `kustomize/deps.bzl` call `native.register_toolchains(...)` and are intended for WORKSPACE-based setups. Do not call these macros from a module extension. + +### WORKSPACE (legacy Bazel, obsolete from Bazel 8.0+) + To use these rules, add the following to your `WORKSPACE` file: ```starlark @@ -15,35 +79,66 @@ git_repository( load("@rules_kubebuilder//kubebuilder:sdk.bzl", "kubebuilder_register_sdk") -kubebuilder_register_sdk(version = "2.3.1") +kubebuilder_register_sdk(version = "4.8.0") load("@rules_kubebuilder//controller-gen:deps.bzl", "controller_gen_register_toolchain") - -controller_gen_register_toolchain() - load("@rules_kubebuilder//kustomize:deps.bzl", "kustomize_register_toolchain") -kustomize_register_toolchain() +# Downloads binaries from GitHub for linux/darwin and amd64/arm64 by default, +# and registers the matching toolchains. +controller_gen_register_toolchain(version = "0.17.1", arches = ["amd64", "arm64"]) # controller-gen +kustomize_register_toolchain(version = "5.7.1", arches = ["amd64", "arm64"]) # kustomize ``` -And in your `go_test()` files, add `etcd` as a data dependency like this: +## Kubebuilder tests + +In your `go_test()` rules, include your asset binaries as data and set +`KUBEBUILDER_ASSETS` for the test itself. For example: ```starlark go_test( name = "go_default_test", srcs = ["apackage_test.go"], + # Provide your own binaries (e.g., go_binary targets or prebuilt files) data = [ - "@kubebuilder_sdk//:bin/etcd", + ":etcd_bin", + ":kube_apiserver_bin", + ":kubectl_bin", ], + # Point this to a directory on disk or an output created by your build. + # For example, you can assemble a directory via a genrule and reference + # its path here, or keep a stable path in your repo. + env = {"KUBEBUILDER_ASSETS": "/path/to/your/assets/dir"}, embed = [":go_default_library"], - deps = [ - ], ) ``` -You'll need to run the test as: + +Alternatively, you can pass the environment variable on the command line: ```shell -bazel test --test_env=KUBEBUILDER_ASSETS=$(bazel info execution_root 2>/dev/null)/$(bazel run @kubebuilder_sdk//:pwd 2>/dev/null) //... +bazel test --test_env=KUBEBUILDER_ASSETS=/path/to/your/assets/dir //... +``` + +### Macro: Assemble envtest assets + +To make assembling assets easier, this repo includes a small macro that copies +your binaries into a single directory and provides a helper to print that path. + +```starlark +load("@rules_kubebuilder//kubebuilder:assets.bzl", "kubebuilder_assets") + +# Assume you build or provide these binaries somewhere in your repo +# (they can be go_binary targets or prebuilt files). +kubebuilder_assets( + name = "envtest", # creates etcd, kube-apiserver, kubectl, and envtest_assets_pwd + etcd = ":etcd_bin", + kube_apiserver = ":kube_apiserver_bin", + kubectl = ":kubectl_bin", +) + +# In CI or locally, set KUBEBUILDER_ASSETS using the helper (prints the +# directory containing the assembled etcd/kube-apiserver/kubectl files): +# bazel test --test_env=KUBEBUILDER_ASSETS=$(bazel run //path:envtest_assets_pwd) //... ``` You can also add the following to `BUILD.bazel` at the root of your workspace: @@ -64,7 +159,7 @@ bazel run //:kubebuilder -- --help 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): ```starlark -load("@io_bazel_rules_go//go:def.bzl", "go_library") +load("@rules_go//go:def.bzl", "go_library") load( "@rules_kubebuilder//controller-gen:controller-gen.bzl", "controller_gen_crd", @@ -97,15 +192,16 @@ controller_gen_object( deps = DEPS, ) -# keep go_library( name = "go_default_library", + # keep srcs = [ - "generated_sources", - "srcs", + ":generated_sources", + ":srcs", ], importpath = "yourdomain.com/your-operator/api/v1alpha1", visibility = ["//visibility:public"], + # keep deps = DEPS, ) @@ -119,16 +215,113 @@ controller_gen_crd( ) ``` -## Developers +The `deps` passed to the `controller_gen_*` rules are converted to a GOPATH +using the `go_path` rule from `@rules_go//go:def.bzl`. For more complex use +cases, you can instead pass a target derived from a `go_path` rule as the +`gopath_dep` argument. This can be useful if you need to run controller-gen +using Go source files from many directories. The above example translated to +use `gopath_dep` is as follows: -The toolchain that describes `controller-gen` needs to be built and the binaries committed so that -they can be used. Fortunately Go supports cross compiling so in order to build the controller, you'll -need to get and install Go either from [their download page](https://golang.org/doc/install) or from -homebrew by running +```starlark +load("@rules_go//go:def.bzl", "go_library", "go_path") +load( + "@rules_kubebuilder//controller-gen:controller-gen.bzl", + "controller_gen_crd", + "controller_gen_object", + "controller_gen_rbac", +) -```shell -brew install golang +DEPS = [ + "@io_k8s_api//core/v1:go_default_library", + "@io_k8s_apimachinery//pkg/api/resource:go_default_library", + "@io_k8s_apimachinery//pkg/apis/meta/v1:go_default_library", + "@io_k8s_apimachinery//pkg/runtime:go_default_library", + "@io_k8s_apimachinery//pkg/runtime/schema:go_default_library", + "@io_k8s_sigs_controller_runtime//pkg/scheme:go_default_library", +] + +filegroup( + name = "srcs", + srcs = glob(["*.go"], exclude=["*_test.go"]), +) + +go_path( + name = "gopath", + deps = [":go_default_library"], +) + +controller_gen_object( + name = "generated_sources", + srcs = [ + ":srcs", + ], + # Still must pass deps to `controller_gen_object`, since + # `:go_default_library` needs the output of this rule to build. + deps = DEPS, +) + +go_library( + name = "go_default_library", + # keep + srcs = [ + ":generated_sources", + ":srcs", + ], + importpath = "yourdomain.com/your-operator/api/v1alpha1", + visibility = ["//visibility:public"], + # keep + deps = DEPS, +) + +controller_gen_crd( + name = "crds", + srcs = [ + ":srcs", + ], + visibility = ["//visibility:public"], + # But non-object dependencies can use `:gopath` instead. + gopath_dep = ":gopath", +) + +controller_gen_rbac( + name = "rbacs", + srcs = [ + ":srcs", + ], + visibility = ["//visibility:public"], + # But non-object dependencies can use `:gopath` instead. + gopath_dep = ":gopath", +) +``` + +## Kustomize + +This repo also exposes a rule `kustomize` to run basic Kustomize commands, as +well as a `kustomize_bin` target to use from genrules to run arbitrary +Kustomize commands: + +```starlark +@load("rules_kubebuilder//kustomize:kustomize.bzl", "kustomize") +kustomize( + name = "example", + srcs = ["kustomization.yaml", "base.yaml"], +) +genrule( + name = "example_genrule", + srcs = ["kustomization.yaml", "base.yaml"], + outs = ["rendered.yaml"], + cmd = """ +set -e +$(location @rules_kubebuilder//kustomize:kustomize_bin) build \ + --load-restrictor=LoadRestrictionsNone $(SRCS) > $@ +""", + tools = ["@rules_kubebuilder//kustomize:kustomize_bin"], +) ``` -After that you can run the script in `scripts/build-controller-gen.sh` which will compile `controller-gen` -for both Linux and macOS. +## Historical Notes + +Previously this repo committed prebuilt binaries for controller-gen and kustomize. +This has been replaced with repository rules that download the official release +artifacts from GitHub at fetch time. No binaries are stored in Git, and arm64 +is supported alongside amd64. diff --git a/WORKSPACE b/WORKSPACE index 3276db5..e46a189 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -2,7 +2,7 @@ workspace(name = "rules_kubebuilder") load("@rules_kubebuilder//kubebuilder:sdk.bzl", "kubebuilder_register_sdk") -kubebuilder_register_sdk(version = "2.3.1") +kubebuilder_register_sdk(version = "4.8.0") ## Next section is for testing in this same MP. @@ -22,7 +22,7 @@ load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies") rules_pkg_dependencies() http_archive( - name = "io_bazel_rules_go", + name = "rules_go", sha256 = "b4433651f57560237681cb9caa969106aba614f5b1e66fefa5834c42b8013b42", urls = [ "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.24.6/rules_go-v0.24.6.tar.gz", @@ -30,7 +30,7 @@ http_archive( ], ) -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") +load("@rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") go_rules_dependencies() diff --git a/controller-gen/BUILD.bazel b/controller-gen/BUILD.bazel index 4488322..8b1690b 100644 --- a/controller-gen/BUILD.bazel +++ b/controller-gen/BUILD.bazel @@ -1,13 +1,4 @@ load("@rules_kubebuilder//controller-gen:controller-gen-toolchain.bzl", "controller_gen_toolchain") -load("@rules_pkg//:pkg.bzl", "pkg_tar") - -exports_files( - [ - "bin/controller-gen.linux", - "bin/controller-gen.darwin", - ], - visibility = ["//visibility:public"], -) filegroup( name = "srcs", @@ -18,53 +9,75 @@ filegroup( visibility = ["//visibility:public"], ) -pkg_tar( - name = "release-bin", - srcs = glob(["bin/*"]), - package_dir = "/bin", +toolchain_type(name = "toolchain") + +# Toolchains reference externally fetched binaries. See deps.bzl and +# extensions.bzl for repository creation macros. + +# Linux x86_64 +controller_gen_toolchain( + name = "controller_gen_linux_amd64", + controller_gen_bin = "@controller_gen_linux_amd64//:controller-gen", ) -pkg_tar( - name = "release", - srcs = [ - "BUILD.bazel", - ":srcs", +toolchain( + name = "controller_gen_linux_amd64_toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", ], - package_dir = "controller-gen", + toolchain = ":controller_gen_linux_amd64", + toolchain_type = ":toolchain", visibility = ["//visibility:public"], - deps = [":release-bin"], ) -toolchain_type(name = "toolchain") - +# Linux arm64 controller_gen_toolchain( - name = "controller_gen_linux", - controller_gen_bin = "@rules_kubebuilder//controller-gen:bin/controller-gen.linux", + name = "controller_gen_linux_arm64", + controller_gen_bin = "@controller_gen_linux_arm64//:controller-gen", ) +toolchain( + name = "controller_gen_linux_arm64_toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], + toolchain = ":controller_gen_linux_arm64", + toolchain_type = ":toolchain", + visibility = ["//visibility:public"], +) + +# macOS x86_64 controller_gen_toolchain( - name = "controller_gen_darwin", - controller_gen_bin = "@rules_kubebuilder//controller-gen:bin/controller-gen.darwin", + name = "controller_gen_darwin_amd64", + controller_gen_bin = "@controller_gen_darwin_amd64//:controller-gen", ) toolchain( - name = "controller_gen_linux_toolchain", + name = "controller_gen_darwin_amd64_toolchain", exec_compatible_with = [ - "@platforms//os:linux", + "@platforms//os:osx", "@platforms//cpu:x86_64", ], - toolchain = ":controller_gen_linux", + toolchain = ":controller_gen_darwin_amd64", toolchain_type = ":toolchain", visibility = ["//visibility:public"], ) +# macOS arm64 +controller_gen_toolchain( + name = "controller_gen_darwin_arm64", + controller_gen_bin = "@controller_gen_darwin_arm64//:controller-gen", +) + toolchain( - name = "controller_gen_darwin_toolchain", + name = "controller_gen_darwin_arm64_toolchain", exec_compatible_with = [ "@platforms//os:osx", - "@platforms//cpu:x86_64", + "@platforms//cpu:aarch64", ], - toolchain = ":controller_gen_darwin", + toolchain = ":controller_gen_darwin_arm64", toolchain_type = ":toolchain", visibility = ["//visibility:public"], ) diff --git a/controller-gen/bin/controller-gen.darwin b/controller-gen/bin/controller-gen.darwin deleted file mode 100755 index 18a8019..0000000 Binary files a/controller-gen/bin/controller-gen.darwin and /dev/null differ diff --git a/controller-gen/bin/controller-gen.linux b/controller-gen/bin/controller-gen.linux deleted file mode 100755 index 7c9aa66..0000000 Binary files a/controller-gen/bin/controller-gen.linux and /dev/null differ diff --git a/controller-gen/controller-gen.bzl b/controller-gen/controller-gen.bzl index 1784b4f..2550aa0 100644 --- a/controller-gen/controller-gen.bzl +++ b/controller-gen/controller-gen.bzl @@ -1,8 +1,8 @@ """ Rules to run controller-gen """ -load("@io_bazel_rules_go//go:def.bzl", "go_context", "go_path") -load("@io_bazel_rules_go//go/private:providers.bzl", "GoPath") +load("@rules_go//go:def.bzl", "go_context", "go_path") +load("@rules_go//go/private:providers.bzl", "GoPath") def _controller_gen_action(ctx, cg_cmd, outputs, output_path): """ Run controller-gen in the sandbox. @@ -25,6 +25,23 @@ def _controller_gen_action(ctx, cg_cmd, outputs, output_path): if ctx.attr.gopath_dep: gopath = "$(pwd)/" + ctx.bin_dir.path + "/" + ctx.attr.gopath_dep[GoPath].gopath + # Compute paths argument: either explicit file list or user-provided pattern + if hasattr(ctx.attr, "paths") and ctx.attr.paths: + paths_value = ctx.attr.paths + else: + paths_value = "{{{files}}}".format(files = ",".join([f.path for f in ctx.files.srcs])) + + # Compute paths argument: either explicit file list or user-provided pattern + if hasattr(ctx.attr, "paths") and ctx.attr.paths: + paths_value = ctx.attr.paths + else: + paths_value = "{{{files}}}".format(files = ",".join([f.path for f in ctx.files.srcs])) + + # Allow rules to pass through additional controller-gen args (e.g. extra output rules) + extra_args = [] + if hasattr(ctx.attr, "extra_args") and ctx.attr.extra_args: + extra_args = ctx.attr.extra_args + cmd = """ source <($PWD/{godir}/go env) && export PATH=$GOROOT/bin:$PWD/{godir}:$PATH && @@ -36,11 +53,12 @@ def _controller_gen_action(ctx, cg_cmd, outputs, output_path): godir = go_ctx.go.path[:-1 - len(go_ctx.go.basename)], gopath = gopath, cmd = "$(pwd)/" + cg_info.controller_gen_bin.path, - args = "{cg_cmd} paths={{{files}}} output:dir={outputpath}".format( - cg_cmd = cg_cmd, - files = ",".join([f.path for f in ctx.files.srcs]), - outputpath = output_path, - ), + args = " ".join([ + "{cg_cmd}".format(cg_cmd = cg_cmd), + "paths={paths}".format(paths = paths_value), + # Keep default output to declared directory so Bazel tracks outputs. + "output:dir={outputpath}".format(outputpath = output_path), + ] + extra_args), ) ctx.actions.run_shell( mnemonic = "ControllerGen", @@ -55,11 +73,23 @@ def _controller_gen_action(ctx, cg_cmd, outputs, output_path): ) def _inputs(ctx, go_ctx): - inputs = (ctx.files.srcs + go_ctx.sdk.srcs + go_ctx.sdk.tools + - go_ctx.sdk.headers + go_ctx.stdlib.libs) + # Build a depset of all required inputs; avoid list+depset concatenation. + inputs = depset( + direct = ctx.files.srcs, + transitive = [ + go_ctx.sdk.srcs, + go_ctx.sdk.tools, + go_ctx.sdk.headers, + go_ctx.stdlib.libs, + ], + ) if ctx.attr.gopath_dep: - inputs += ctx.attr.gopath_dep.files.to_list() + inputs = depset(transitive = [inputs, ctx.attr.gopath_dep.files]) + + # Include header file if present + if hasattr(ctx.attr, "headerFile") and ctx.file.headerFile: + inputs = depset(direct = [ctx.file.headerFile], transitive = [inputs]) return inputs def _env(): @@ -71,6 +101,8 @@ def _controller_gen_crd_impl(ctx): outputdir = ctx.actions.declare_directory(ctx.label.name) cg_cmd = "crd" extra_args = [] + if hasattr(ctx.attr, "headerFile") and ctx.file.headerFile: + extra_args.append("headerFile={}".format(ctx.file.headerFile.path)) if ctx.attr.trivialVersions: extra_args.append("trivialVersions=true") if ctx.attr.preserveUnknownFields: @@ -90,8 +122,11 @@ def _controller_gen_crd_impl(ctx): def _controller_gen_object_impl(ctx): output = ctx.actions.declare_file("zz_generated.deepcopy.go") + cg_cmd = "object" + if hasattr(ctx.attr, "headerFile") and ctx.file.headerFile: + cg_cmd += ":headerFile={}".format(ctx.file.headerFile.path) - _controller_gen_action(ctx, "object", [output], output.dirname) + _controller_gen_action(ctx, cg_cmd, [output], output.dirname) return DefaultInfo( files = depset([output]), @@ -101,6 +136,8 @@ def _controller_gen_rbac_impl(ctx): outputdir = ctx.actions.declare_directory("rbac") cg_cmd = "rbac" extra_args = [] + if hasattr(ctx.attr, "headerFile") and ctx.file.headerFile: + extra_args.append("headerFile={}".format(ctx.file.headerFile.path)) if ctx.attr.roleName: extra_args.append("roleName={}".format(ctx.attr.roleName)) if len(extra_args) > 0: @@ -128,13 +165,17 @@ COMMON_ATTRS = { mandatory = True, doc = "Source files passed to controller-gen", ), + "paths": attr.string( + default = "", + doc = "If set, overrides srcs with a controller-gen paths=... pattern (e.g., ./...).", + ), "gopath_dep": attr.label( providers = [GoPath], mandatory = False, doc = "Go lang dependencies, automatically bundled in a go_path() by the macro.", ), "_go_context_data": attr.label( - default = "@io_bazel_rules_go//:go_context_data", + default = "@rules_go//:go_context_data", doc = "Internal, context for go compilation.", ), } @@ -143,7 +184,7 @@ def _crd_extra_attrs(): ret = COMMON_ATTRS ret.update({ "trivialVersions": attr.bool( - default = True, + default = False, ), "preserveUnknownFields": attr.bool( default = False, @@ -152,6 +193,13 @@ def _crd_extra_attrs(): ), "maxDescLen": attr.int( ), + "headerFile": attr.label( + allow_single_file = True, + ), + # Additional controller-gen args, e.g. ["output:crd:artifacts:config="] + "extra_args": attr.string_list( + doc = "Additional args appended to controller-gen invocation.", + ), }) return ret @@ -161,6 +209,9 @@ def _rbac_extra_attrs(): "roleName": attr.string( default = "manager-role", ), + "headerFile": attr.label( + allow_single_file = True, + ), }) return ret @@ -170,7 +221,7 @@ def _webhook_extra_attrs(): def _toolchains(): return [ - "@io_bazel_rules_go//go:toolchain", + "@rules_go//go:toolchain", "@rules_kubebuilder//controller-gen:toolchain", ] @@ -182,9 +233,18 @@ _controller_gen_crd = rule( "The output directory will be the name of the rule.", ) +def _object_extra_attrs(): + ret = COMMON_ATTRS + ret.update({ + "headerFile": attr.label( + allow_single_file = True, + ), + }) + return ret + _controller_gen_object = rule( implementation = _controller_gen_object_impl, - attrs = COMMON_ATTRS, + attrs = _object_extra_attrs(), toolchains = _toolchains(), doc = "Run the code generating portion of controller-gen. " + "You can use the name of this rule as part of the `srcs` attribute " + @@ -228,7 +288,7 @@ def controller_gen_object(name, **kwargs): def controller_gen_rbac(name, **kwargs): _maybe_add_gopath_dep(name, kwargs) _controller_gen_rbac(name = name, **kwargs) - + def controller_gen_webhook(name, **kwargs): _maybe_add_gopath_dep(name, kwargs) _controller_gen_webhook(name = name, **kwargs) diff --git a/controller-gen/deps.bzl b/controller-gen/deps.bzl index 3adb7b4..ff68887 100644 --- a/controller-gen/deps.bzl +++ b/controller-gen/deps.bzl @@ -1,8 +1,41 @@ -""" Dependencies for controller-gen +"""Controller-gen repositories and toolchains (WORKSPACE helper). + +This macro defines external repositories to download controller-gen binaries +for the supported OS/architectures and registers the corresponding toolchains. + +Args: + version: Controller-tools release version to use (e.g. "0.17.1"). + arches: List of architectures to support (subset of ["amd64", "arm64"]). """ -def controller_gen_register_toolchain(name = None): - native.register_toolchains( - "@rules_kubebuilder//controller-gen:controller_gen_linux_toolchain", - "@rules_kubebuilder//controller-gen:controller_gen_darwin_toolchain", - ) +load("@rules_kubebuilder//controller-gen:repo.bzl", "controller_gen_download") + +def _controller_gen_repo_name(os, arch): + return "controller_gen_%s_%s" % (os, arch) + +def controller_gen_register_toolchain(version = "0.17.1", arches = ["amd64", "arm64"]): + for os in ["linux", "darwin"]: + for arch in arches: + repo_name = _controller_gen_repo_name(os, arch) + controller_gen_download( + name = repo_name, + version = version, + os = os, + arch = arch, + ) + + # Register toolchains for each defined combo + tcs = [] + if "amd64" in arches: + tcs += [ + "@rules_kubebuilder//controller-gen:controller_gen_linux_amd64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_darwin_amd64_toolchain", + ] + if "arm64" in arches: + tcs += [ + "@rules_kubebuilder//controller-gen:controller_gen_linux_arm64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_darwin_arm64_toolchain", + ] + + if tcs: + native.register_toolchains(*tcs) diff --git a/controller-gen/extensions.bzl b/controller-gen/extensions.bzl new file mode 100644 index 0000000..0652fde --- /dev/null +++ b/controller-gen/extensions.bzl @@ -0,0 +1,42 @@ +"""Bzlmod module extension for controller-gen binaries. + +Creates external repositories containing controller-gen binaries for +specified OS/arch combinations. Does not register toolchains. +""" + +load("@rules_kubebuilder//controller-gen:repo.bzl", "controller_gen_download") + +_controller_gen_tag = tag_class( + attrs = { + "version": attr.string(default = "0.17.1"), + "arches": attr.string_list(default = ["amd64", "arm64"]), + # Repos are always created for linux and darwin; override by adding/removing use_repo if needed + }, +) + +def _cg_ext_impl(mctx): + # For each module using this extension, create repos per requested arches + for mod in mctx.modules: + for t in mod.tags.controller_gen: + for os in ["linux", "darwin"]: + for arch in t.arches: + name = "controller_gen_%s_%s" % (os, arch) + controller_gen_download(name = name, version = t.version, os = os, arch = arch) + + # Surface created repo names for convenience + names = [] + for mod in mctx.modules: + for t in mod.tags.controller_gen: + for os in ["linux", "darwin"]: + for arch in t.arches: + names.append("controller_gen_%s_%s" % (os, arch)) + + return mctx.extension_metadata( + root_module_direct_deps = names, + root_module_direct_dev_deps = [], + ) + +cg_ext = module_extension( + implementation = _cg_ext_impl, + tag_classes = {"controller_gen": _controller_gen_tag}, +) diff --git a/controller-gen/repo.bzl b/controller-gen/repo.bzl new file mode 100644 index 0000000..a4edaf6 --- /dev/null +++ b/controller-gen/repo.bzl @@ -0,0 +1,45 @@ +"""Repository rule to download controller-gen binaries by OS/arch. + +Provides a repository containing a single executable file `controller-gen` at +the repo root. Intended to be used by toolchain definitions. +""" + +def _controller_gen_url(version, os, arch): + return "https://github.com/kubernetes-sigs/controller-tools/releases/download/v%s/controller-gen-%s-%s" % (version, os, arch) + +def _write_build(repo_ctx): + repo_ctx.file( + "BUILD.bazel", + content = """ +package(default_visibility = ["//visibility:public"]) +exports_files(["controller-gen"]) +""", + executable = False, + ) + +def _download_controller_gen_impl(repo_ctx): + version = repo_ctx.attr.version + os = repo_ctx.attr.os + arch = repo_ctx.attr.arch + + urls = repo_ctx.attr.urls + if not urls: + urls = [_controller_gen_url(version, os, arch)] + + # Download the platform-specific binary directly as 'controller-gen' + repo_ctx.download(urls, output = "controller-gen", executable = True) + + _write_build(repo_ctx) + +_download_controller_gen = repository_rule( + implementation = _download_controller_gen_impl, + attrs = { + "version": attr.string(default = "0.17.1"), + "os": attr.string(mandatory = True), + "arch": attr.string(mandatory = True), + "urls": attr.string_list(default = []), + }, +) + +def controller_gen_download(name, version, os, arch, urls = None): + _download_controller_gen(name = name, version = version, os = os, arch = arch, urls = urls or []) diff --git a/kubebuilder/BUILD.sdk.bazel b/kubebuilder/BUILD.sdk.bazel index 8866a87..7ec1b95 100644 --- a/kubebuilder/BUILD.sdk.bazel +++ b/kubebuilder/BUILD.sdk.bazel @@ -1,10 +1,10 @@ load("@rules_kubebuilder//kubebuilder:sdk.bzl", "kubebuilder_pwd") exports_files([ - "bin/etcd", - "bin/kube-apiserver", + # As of newer Kubebuilder releases, only the kubebuilder binary is + # included in the upstream tarball. etcd, kube-apiserver, and kubectl + # are no longer bundled and must be provided separately by users. "bin/kubebuilder", - "bin/kubectl", ]) kubebuilder_pwd( diff --git a/kubebuilder/assets.bzl b/kubebuilder/assets.bzl new file mode 100644 index 0000000..0b8aa77 --- /dev/null +++ b/kubebuilder/assets.bzl @@ -0,0 +1,79 @@ +"""Utilities to assemble envtest assets into a single directory. + +This macro copies user-provided etcd, kube-apiserver, and kubectl binaries +into a single output directory suitable for use as KUBEBUILDER_ASSETS. + +It also creates a `_assets_pwd` executable that prints the directory path, +so you can set KUBEBUILDER_ASSETS via: + + bazel run //path:NAME_assets_pwd +""" + +load("@rules_kubebuilder//kubebuilder:sdk.bzl", "kubebuilder_pwd") + +def kubebuilder_assets(name, *, etcd, kube_apiserver, kubectl, visibility = None): + """Assemble envtest assets into a single directory. + + Args: + name: Base name for generated targets. + etcd: Label of etcd binary. + kube_apiserver: Label of kube-apiserver binary. + kubectl: Label of kubectl binary. + visibility: Optional visibility list applied to created targets. + + Creates: + - genrule `name + "_assets"` producing files under `_assets/`: + - `_assets/etcd` + - `_assets/kube-apiserver` + - `_assets/kubectl` + - target `name + "_assets_pwd"` printing the directory path + """ + + if visibility == None: + visibility = ["//visibility:public"] + + # Place outputs in the package's bin directory with canonical filenames + # expected by envtest. Keeping them in the same directory allows + # KUBEBUILDER_ASSETS to be set to that directory. + out_etcd = "etcd" + out_kas = "kube-apiserver" + out_kubectl = "kubectl" + + native.genrule( + name = name + "_assets", + srcs = [], + tools = [ + etcd, + kube_apiserver, + kubectl, + ], + outs = [ + out_etcd, + out_kas, + out_kubectl, + ], + # All outs live under the same directory; $(@D) resolves to that dir + # for the first output. + cmd = ( + "mkdir -p $(@D) && " + + "cp $(location {etcd}) $(@D)/{out_etcd} && " + + "cp $(location {kas}) $(@D)/{out_kas} && " + + "cp $(location {kubectl}) $(@D)/{out_kubectl}" + ).format( + etcd = etcd, + kas = kube_apiserver, + kubectl = kubectl, + out_etcd = out_etcd, + out_kas = out_kas, + out_kubectl = out_kubectl, + ), + visibility = visibility, + ) + + # Create a helper that prints the assembled directory path. + kubebuilder_pwd( + name = name + "_assets_pwd", + srcs = [":" + out_etcd], + kubebuilder_binary = ":" + out_etcd, + visibility = visibility, + ) diff --git a/kubebuilder/def.bzl b/kubebuilder/def.bzl index bb53c4d..0c90d96 100644 --- a/kubebuilder/def.bzl +++ b/kubebuilder/def.bzl @@ -31,7 +31,7 @@ _kubebuilder = rule( "_kubebuilder": attr.label( default = "@kubebuilder_sdk//:bin/kubebuilder", allow_single_file = True, - cfg = "host", + cfg = "exec", executable = True, ), "_runner": attr.label( diff --git a/kubebuilder/extensions.bzl b/kubebuilder/extensions.bzl new file mode 100644 index 0000000..ef9111c --- /dev/null +++ b/kubebuilder/extensions.bzl @@ -0,0 +1,61 @@ +"""Bzlmod module extension for kubebuilder SDK. + +This extension only creates external repositories (downloads the SDK). +It does NOT register toolchains. In Bzlmod, register toolchains in the +root MODULE.bazel using `register_toolchains(...)` with the labels from +this repo (e.g. @rules_kubebuilder//controller-gen:..., @rules_kubebuilder//kustomize:...). + +Example usage in MODULE.bazel: + + bazel_dep(name = "rules_kubebuilder", version = "0.1.0") + + kb = use_extension("@rules_kubebuilder//kubebuilder:extensions.bzl", "kb_ext") + kb.kubebuilder_sdk(name = "kubebuilder_sdk_4_8_0", version = "4.8.0") + use_repo(kb, "kubebuilder_sdk_4_8_0") + + # Register toolchains at the root (do NOT call from an extension) + register_toolchains( + # controller-gen + "@rules_kubebuilder//controller-gen:controller_gen_linux_amd64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_linux_arm64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_darwin_amd64_toolchain", + "@rules_kubebuilder//controller-gen:controller_gen_darwin_arm64_toolchain", + # kustomize + "@rules_kubebuilder//kustomize:kustomize_linux_amd64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_linux_arm64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_darwin_amd64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_darwin_arm64_toolchain", + ) +""" + +load("@rules_kubebuilder//kubebuilder:sdk.bzl", "kubebuilder_download_sdk") + +_kubebuilder_sdk_tag = tag_class( + attrs = { + "name": attr.string(mandatory = True), + "version": attr.string(mandatory = True), + # Future: add urls/strip_prefix overrides if needed + }, +) + +def _kb_ext_impl(mctx): + # Create requested SDK repos during module resolution + for mod in mctx.modules: + for t in mod.tags.kubebuilder_sdk: + kubebuilder_download_sdk( + name = t.name, + version = t.version, + ) + + # Optionally, guide Bazel on likely imports (for editor UX only) + return mctx.extension_metadata( + root_module_direct_deps = [t.name for mod in mctx.modules for t in mod.tags.kubebuilder_sdk], + root_module_direct_dev_deps = [], + ) + +kb_ext = module_extension( + implementation = _kb_ext_impl, + tag_classes = { + "kubebuilder_sdk": _kubebuilder_sdk_tag, + }, +) diff --git a/kubebuilder/generate_sdk_list.py b/kubebuilder/generate_sdk_list.py index e29c372..1a639f3 100644 --- a/kubebuilder/generate_sdk_list.py +++ b/kubebuilder/generate_sdk_list.py @@ -1,9 +1,8 @@ -import requests +import json import os from dataclasses import dataclass -from tqdm import tqdm -from typing import List -import json + +import requests @dataclass @@ -11,6 +10,8 @@ class ReleaseItem: version: str linux_amd64_checksum: str darwin_amd64_checksum: str + linux_arm64_checksum: str | None = None + darwin_arm64_checksum: str | None = None def main(): @@ -20,45 +21,98 @@ def main(): def fetch_release_items(): if "GH_ACCESS_TOKEN" not in os.environ: - 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") + 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" + ) exit(1) access_token = os.getenv("GH_ACCESS_TOKEN") - releases_response = requests.get('https://api.github.com/repos/kubernetes-sigs/kubebuilder/releases', headers={ - "Accept": "application/vnd.github+json", - "Authorization": f"token {access_token}" - }) + releases_response = requests.get( + "https://api.github.com/repos/kubernetes-sigs/kubebuilder/releases", + headers={ + "Accept": "application/vnd.github+json", + "Authorization": f"token {access_token}", + }, + ) items = [] - for release in tqdm(releases_response.json()): - checksum_asset = [x for x in release["assets"] - if x["name"] == "checksums.txt"][0] - checksum_response = requests.get( - checksum_asset["browser_download_url"]) + for release in releases_response.json(): + checksum_asset = [x for x in release["assets"] if x["name"] == "checksums.txt"][0] + checksum_response = requests.get(checksum_asset["browser_download_url"]) checksum_dic = checksum_text_to_dic(checksum_response.text) - linux_amd64_key = get_key_containing( - list(checksum_dic.keys()), "linux_amd64") - darwin_amd64_key = get_key_containing( - list(checksum_dic.keys()), "darwin_amd64") - - items.append(ReleaseItem( - release["tag_name"], checksum_dic[linux_amd64_key], checksum_dic[darwin_amd64_key])) + version_no_v = release["tag_name"].lstrip("v") + + def pick_checksum(os_id: str, arch_id: str): + # Prefer legacy tarball filenames that include the version. + legacy_name = f"kubebuilder_{version_no_v}_{os_id}_{arch_id}.tar.gz" + if legacy_name in checksum_dic: + return checksum_dic[legacy_name] + + # Fall back to v4+ plain binary filenames. + binary_name = f"kubebuilder_{os_id}_{arch_id}" + if binary_name in checksum_dic: + return checksum_dic[binary_name] + + # Last resort: find a single sensible candidate containing the platform, excluding SBOM/JSON/signature files. + candidates = [ + k + for k in checksum_dic.keys() + if f"_{os_id}_{arch_id}" in k + and not k.endswith(".json") + and ".sbom." not in k + and not k.endswith(".sig") + ] + if len(candidates) == 1: + return checksum_dic[candidates[0]] + # Prefer tarballs if multiple + tgz = [k for k in candidates if k.endswith(".tar.gz")] + if tgz: + return checksum_dic[tgz[0]] + # Prefer exact binary name next + if binary_name in candidates: + return checksum_dic[binary_name] + return None + + linux_amd64 = pick_checksum("linux", "amd64") + darwin_amd64 = pick_checksum("darwin", "amd64") + linux_arm64 = pick_checksum("linux", "arm64") + darwin_arm64 = pick_checksum("darwin", "arm64") + + # Require amd64; arm64 optional + if not linux_amd64 or not darwin_amd64: + # Skip releases that don't conform to expected artifact layout + continue + + items.append( + ReleaseItem( + release["tag_name"], + linux_amd64, + darwin_amd64, + linux_arm64, + darwin_arm64, + ) + ) return items -def generate_sdk_list(items: List[ReleaseItem]): +def generate_sdk_list(items: list[ReleaseItem]): result = """## Useful constants. Generated by generate_sdk_list.py -DEFAULT_VERSION = "2.3.1" +DEFAULT_VERSION = "4.8.0" SDK_VERSION_SHA256 = { """ sdk_dic = {} for item in items: - sdk_dic[item.version.replace("v","")] = { + entry = { "linux_amd64": item.linux_amd64_checksum, "darwin_amd64": item.darwin_amd64_checksum, } + if item.linux_arm64_checksum: + entry["linux_arm64"] = item.linux_arm64_checksum + if item.darwin_arm64_checksum: + entry["darwin_arm64"] = item.darwin_arm64_checksum + sdk_dic[item.version.replace("v", "")] = entry result += remove_first_line(json.dumps(sdk_dic, indent=4)) with open("sdk_list.bzl", "w") as f: f.write(result) diff --git a/kubebuilder/kubebuilder-runner.sh.template b/kubebuilder/kubebuilder-runner.sh.template index 759d165..75f3e91 100644 --- a/kubebuilder/kubebuilder-runner.sh.template +++ b/kubebuilder/kubebuilder-runner.sh.template @@ -2,6 +2,19 @@ KUBEBUILDER_SHORT_PATH="@@KUBEBUILDER_SHORT_PATH@@" -kubebuilder_short_path=$(readlink $KUBEBUILDER_SHORT_PATH) +# Resolve kubebuilder path to an absolute path before changing directories. +# Because Bazel runfile symlinks often point to relative targets, the path +# printed by readlink may become invalid after the cd, causing the wrapper to +# fail to locate the binary. The script should resolve to an absolute path +# before changing directories. +# Prefer, in order: readlink -f, realpath, then readlink. +kubebuilder_abs_path="" +if kubebuilder_abs_path=$(readlink -f "$KUBEBUILDER_SHORT_PATH" 2>/dev/null); then + : +elif kubebuilder_abs_path=$(realpath "$KUBEBUILDER_SHORT_PATH" 2>/dev/null); then + : +else + kubebuilder_abs_path=$(readlink "$KUBEBUILDER_SHORT_PATH") || exit 1 +fi -cd "$BUILD_WORKSPACE_DIRECTORY" && "$kubebuilder_short_path" "$@" +cd "$BUILD_WORKSPACE_DIRECTORY" && "$kubebuilder_abs_path" "$@" diff --git a/kubebuilder/sdk.bzl b/kubebuilder/sdk.bzl index 63fc55f..22a1a1c 100644 --- a/kubebuilder/sdk.bzl +++ b/kubebuilder/sdk.bzl @@ -13,11 +13,11 @@ def _kubebuilder_download_sdk_impl(ctx): fail("Unknown version {}".format(version)) sha256 = SDK_VERSION_SHA256[version][platform] urls = [url.format(version = version, platform = platform) for url in ctx.attr.urls] - strip_prefix = ctx.attr.strip_prefix.format(version = version, platform = platform) - ctx.download_and_extract( + ctx.download( url = urls, - stripPrefix = strip_prefix, sha256 = sha256, + output = "kubebuilder", + executable = True, ) ctx.template( "BUILD.bazel", @@ -28,13 +28,12 @@ def _kubebuilder_download_sdk_impl(ctx): _kubebuilder_download_sdk = repository_rule( _kubebuilder_download_sdk_impl, attrs = { - "version": attr.string(default = "2.3.1"), + "version": attr.string(default = "4.8.0"), "urls": attr.string_list( default = [ - "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v{version}/kubebuilder_{version}_{platform}.tar.gz", + "https://github.com/kubernetes-sigs/kubebuilder/releases/download/v{version}/kubebuilder_{platform}", ], ), - "strip_prefix": attr.string(default = "kubebuilder_{version}_{platform}"), }, ) @@ -42,15 +41,26 @@ def kubebuilder_download_sdk(name, **kwargs): _kubebuilder_download_sdk(name = name, **kwargs) def _detect_host_platform(ctx): + # Determine OS if ctx.os.name == "linux": - host = "linux_amd64" + os_id = "linux" elif ctx.os.name == "mac os x": - host = "darwin_amd64" + os_id = "darwin" else: fail("Unsupported operating system: " + ctx.os.name) - return host -def kubebuilder_register_sdk(version = "2.3.1"): + # Determine CPU architecture + arch = ctx.os.arch + if arch == "x86_64" or arch == "amd64": + arch_id = "amd64" + elif arch == "aarch64" or arch == "arm64": + arch_id = "arm64" + else: + fail("Unsupported CPU architecture: " + arch) + + return "{}_{}".format(os_id, arch_id) + +def kubebuilder_register_sdk(version = "4.8.0"): kubebuilder_download_sdk( name = "kubebuilder_sdk", version = version, diff --git a/kubebuilder/sdk_list.bzl b/kubebuilder/sdk_list.bzl index ff2dcf0..b607b66 100644 --- a/kubebuilder/sdk_list.bzl +++ b/kubebuilder/sdk_list.bzl @@ -1,126 +1,192 @@ ## Useful constants. Generated by generate_sdk_list.py -DEFAULT_VERSION = "2.3.1" +DEFAULT_VERSION = "4.7.1" SDK_VERSION_SHA256 = { + "4.8.0": { + "linux_amd64": "4f784e52845db48320cd37b579ae331149a8a61a2e13ed04230034c97a017897", + "darwin_amd64": "3ef6fb437d04468742a082be1d5621852cd7278fe1e4693f2c13c37b1a391019", + "linux_arm64": "9b6b5e37c84600a387396a5635badcc59cbc3b8ed3ae5f17b1fcabec0a7b61ed", + "darwin_arm64": "4719a3e0ea3a1fac9718b49e8deb63f79e091a7fd30e06e779880eba726a2ba2", + }, + "4.7.1": { + "linux_amd64": "83ae391dc7aecc0b7d85d6c375b40207272538efa46233eada39e3d82ddc07a6", + "darwin_amd64": "f0031dbf157cdc15048ba092f1daf8b6abced8bca92d0e23ad8e3667b1fd30bb", + "linux_arm64": "4a75b077a5ac5d64a4ef6c4cbac1448b834a34b4cb3891c9532ab0674aa746d0", + "darwin_arm64": "6c3edc949f644d74d7dacdfd0e3b9e88c5d92c3226bf7fe23976d0b4af485d46", + }, + "4.7.0": { + "linux_amd64": "e7cdfac83cf32ccf67ea07e7d256cee3129b6783577836fb85c4341d5d7f03e7", + "darwin_amd64": "a5e63af93fe71f5056568c044c05720f30ddda7eb5afd72cc027903183f8abc3", + "linux_arm64": "20228f6bc8374735181a580376a442ac565699628d60b0acce1c00908ea512ed", + "darwin_arm64": "b90b7fdcd365005320188ab2b39eed19dd4abfc6424b18d7033f13e137d44802", + }, + "4.6.0": { + "linux_amd64": "73b05e081316e675eaca815dc62f13264a580d4d7940c381ae69e14b990d8128", + "darwin_amd64": "10ac1656fe75e5be83331a12d7244d7e82d86aa34b05badaa4348e7b979f2287", + "linux_arm64": "324b7a9a2ad53f93c8008eaf2d8472e365b1577a9766bf421fd86131e99adab6", + "darwin_arm64": "8e72996ec5fb466026b3643054a8a8af0e22aa2f4bf9a9b08d7422fd95c5bbf0", + }, + "4.5.2": { + "linux_amd64": "7a691c7c286b4c1a3f57b2ca9c85e3856662f850a4fa4cc6feb1aaae9273cf1c", + "darwin_amd64": "9e1613b62566fe89b644b17afa42ecd13e1a391b8485575bdedbe62a1499d75d", + "linux_arm64": "77ae785ea75d76ad191caa6bf69001faade8019ed771a19a0f174def3baf29ed", + "darwin_arm64": "0aab8fbac91421f7daef718f50e6027e6866a8c0dbb2a725969e87b2bec694b1", + }, + "4.5.1": { + "linux_amd64": "42f5b6f0e9ecbc7e0a8514a6b6e2e9fd78767deea4123477354ebcb1fd42d6cc", + "darwin_amd64": "26261361a7159fb4ba4bef0017de838f1250c001802b6b0056ac028c0e21aa48", + "linux_arm64": "de94401324b3c080e1f14290f68ddd06453baf20460ed0f444e86b9370a11608", + "darwin_arm64": "49e6f3d7db934dde636fae243bcfaec9bbb08431c4ad95f5211985c7dcf7f4b4", + }, + "4.5.0": { + "linux_amd64": "c3f4e83bea4327dcfabca20ef00ce3281b1706f33e1f4430ee99e1d637449175", + "darwin_amd64": "e331e8c3c42073f7b9dc3e368ec7c6efadaf67d5796530d64648184d57550c55", + "linux_arm64": "e851698027d444c59449c19418e06772e1ca4cc6e1d91c11ab3bd6cdb065a817", + "darwin_arm64": "5e9dbf3631824bc9d4958aeb967ae09d9cfd615e4b7933715f0bd72ef5698ba3", + }, + "4.4.0": { + "linux_amd64": "02b4f232b919e2ffdc15347a3a36715f829cdebe775914d150842a48ef99e4bd", + "darwin_amd64": "824d3b7852ba482e03ec9edc6a8b4e306500291a229492b09f5d6e0d68f0b649", + "linux_arm64": "f20bd63f65c1150c586c235c59f803c549e171a4e86f4d5f844d21a9c6a1ac17", + "darwin_arm64": "5ee0a350064c05e710f2e7672cbec7bd0c61bf257154227c96f4f8ad9917454c", + }, + "4.3.1": { + "linux_amd64": "42cc63d37285d69cf7f308fd6b58d5be85a5d96a47ca60e17a20f4e86643697b", + "darwin_amd64": "2af6f781fbef8a39262b8d7f5ffb3775e5715930dbb7bc77f2866934e8762e27", + "linux_arm64": "b9e087f7df8d0565008efc3f4ec1b5247d6ce78fb419ac7db3f1dbb3f7796204", + "darwin_arm64": "ab89e28171e01b5f65b40eee45dfcefc392bb46a5ce212878fda9b381e65ab9b", + }, + "4.3.0": { + "linux_amd64": "abbea590681ba05870e5f3bc0232e021577c9ebceeb46705b7d2c2023378e981", + "darwin_amd64": "413c8ff27fe4c6ecfbcec0b3503f64e85c69d7aaac2eef5771ef06c7a51af88b", + "linux_arm64": "9aad0901d5e02268c9f66d192593a996811edaf4cf556d88f416e6f2630d1af4", + "darwin_arm64": "6191dd55ca9c76fdfb14ba76fdc32672778be9b990404b4fdc5f054f151ddc63", + }, + "4.2.0": { + "linux_amd64": "287abc072ecbdf6387d98b7c0695bcbadc91996fe577b70b529de1a4089b46ae", + "darwin_amd64": "8834d5703befb83cb128661dba611e11b2df450fb2ff006f16951657898b6e02", + "linux_arm64": "d2404629ec92399f207194529591e34f5770866b2043b2a7615f04bced80a589", + "darwin_arm64": "b3605a198b8f647142581e42f6b42078b0a63a331b3708bd96ce819704ab81bb", + }, + "4.1.1": { + "linux_amd64": "48130ea24ed9d48a99277baa37c41673f6bcfaba2d747cc6213b97a2f00bef78", + "darwin_amd64": "9402fa13b552fe1012dd606a1e5158f14713f65e085ee912fcfc39ee41aa8324", + "linux_arm64": "670d1570dc9b0b349c615dbdc91084c4e3b32eedc57f3a4f98091a090c32d1b6", + "darwin_arm64": "20420d8fb4eee3ee3db4d4c2302e5355a12bb47ba9e6f6b6f672f5a8b44e16e1", + }, + "4.1.0": { + "linux_amd64": "881749712318cc1035067944d8d971546ca5a090e14a52f74f9a33efee758b95", + "darwin_amd64": "8ec79cd494a7bd530e089307033ed0c9b7e4e8bab8b284faf6221b7be52bb59f", + "linux_arm64": "8acb1eb930c302fca4263a724f81d5d7725372db220850128195b8908814526c", + "darwin_arm64": "7d438a777a231b08e24e37709611259577f8f1c2559b3f9cf5fd06e546597860", + }, + "4.0.0": { + "linux_amd64": "da79077245c0f6d18d4654354837751239b1b7c14ad6deaa14e2e335400fe6f2", + "darwin_amd64": "223ffe60a4bb9cce4fccdf1d05c0fa9cca05d0d8dac89302667aa20d28f54e65", + "linux_arm64": "19b3cc9d3e6d2b8524a5975f93a2530b9c9ef97821e7661c04c404e6829a8c8e", + "darwin_arm64": "85ed6580d070e61e23f4cc9780c1cb74e3f1af326ff800446dd886b9306218ee", + }, + "3.15.1": { + "linux_amd64": "b1f04e198035beb65ba7439196541ffb231c3c9feae07ac82149fcbb1576ae7a", + "darwin_amd64": "a5e6fbc3ccc801e2abea0089ec4713a55d574314e137c335314bf4ff888cd498", + "linux_arm64": "0c457625a7e5f8ca552da902e4d1e6f9925741ad00dc7a673dbbf2dcaa0a5e30", + "darwin_arm64": "f4edbe49371c7bb88079d298490a93cbc9834efb32b242dca6fdad219726fc61", + }, + "3.15.0": { + "linux_amd64": "51fb03cd3435c72c512100b6bf39c13147a69c376661aed5215cbcd435c433d8", + "darwin_amd64": "b5dba4d878e75115ba6a2926bf44153a49bfb02c7f55e90aef68f3eee2206ed6", + "linux_arm64": "e27e1e38d841f0ec072b3ae505113cb41184b3d2fdb38d58e6ebedc17b500d2e", + "darwin_arm64": "d3b74de4246af67b86f012f7004880a8ad25e7d1ea90bf708a5b275b9bce5fb3", + }, + "3.14.2": { + "linux_amd64": "4424cde82d8f523ca200dd94cbe1d3514411b0739046e41ebc53862ea199c904", + "darwin_amd64": "6bf204d06a6c01e23214a0fbdee76550224c9b9aa53783793347660b05871221", + "linux_arm64": "e5782947c99d41d8c36d5ac19c48d7a941316fc0ed19b2958232a327f3bc46e0", + "darwin_arm64": "c6d9c0bc3bdfa002f0463d43641eba3e4e642c9d2514c5f3f3ce626abfe95c83", + }, + "3.14.1": { + "linux_amd64": "6af06e8b2eff7e56abf95239eebee7f260714560961165d26c420810de8257cf", + "darwin_amd64": "19043b414d8bba6e372fe4720ef9305ab37bbe681cd6448379ab046dd7217173", + "linux_arm64": "f5dc08da67141d31ec47e602373ea219804f5d14fcd84fb0d3ec69b6ca24db53", + "darwin_arm64": "9e896618dbc69d5f96f9154bbacb136ed81c3864274fe3fff878608eb7eaf074", + }, + "3.14.0": { + "linux_amd64": "702dce3c115f78a5c3e7372787a2894ab80cb6f2c7bd66fd83cbc88c79bc8768", + "darwin_amd64": "aaebeff03c88b74f35766de6026f9d7114752a8fa5424cec0e393aceca01a423", + "linux_arm64": "441a0e3023024e3b97c92f7f76cd1b926da0f01b32c909ed8df83579410ae5ee", + "darwin_arm64": "47970ec6e2c2356ad54e13d15aa022ac3b4b2c0030b8286a9298c3c4193bf7f9", + }, + "3.13.0": { + "linux_amd64": "ceaa07c801d245cfa96bf8003c26cc267b9e9668886efa4a50cc82997d4d639d", + "darwin_amd64": "15000fa2962574a366c90bb8a303b5717de821d5bb87ba8e9b4d2f27add918bd", + "linux_arm64": "93652c13ddf01c3e4b259082cfb90fc60f72980977a91e7f70de2d80bb3f87bd", + "darwin_arm64": "ecb7b0f4de0ee17e7eead09960b31165cb11fa320d17b7ab4109ddeac1de2091", + }, + "3.12.0": { + "linux_amd64": "c835bdf3572b61a1641c2445e1b119dc7887cbc4c593e71fafc00a7d04acd8c9", + "darwin_amd64": "ee7e857d890aeeea5715a721599980cd7797d949ea6859674c2df061dffa6e5b", + "linux_arm64": "278a342438af004e04ed33cf0ecb83bfc7e11a8412ce00ca553e77aed42b5de7", + "darwin_arm64": "3aede52ef2d75497f271a6829788152887fa56582de335ff854c47b17c3e11f7", + }, + "3.11.1": { + "linux_amd64": "ca8dac24944ca97d6deecada35ef301b3cf67ec4bc25a5cdeccf38eaa7495285", + "darwin_amd64": "3bb01c7d743975e47cc44dc348fb8b4cdfb3851e300f5cea5bdb6025d2f99f60", + "linux_arm64": "213e821514dd3221e8a8bb8b45da049b7fd7b089ad4e24aaf56288b8f2637dc4", + "darwin_arm64": "2ea30d35d6f8faebe457e596fa5dfd3598169c1d8215c44df4ef9bd9b7a7827a", + }, + "3.11.0": { + "linux_amd64": "4278df20b5159aec35ef19f9fd769711f81bdf304e848a8fad516c7b5d4e099a", + "darwin_amd64": "fe2e26cb0419f87ea69e10832870495e23abdd190aaa6e73ac841a198671bbd4", + "linux_arm64": "64fb711150bcf9de6656b85cd78bbe1fa55fc1c52b0b6dfaf1ed01cde937ad51", + "darwin_arm64": "7d336f3b8d5e6f2009082aafa299900c7183e8d03c651e04d4ab78256deab828", + }, + "3.10.0": { + "linux_amd64": "d9ba5517a8cc8acaa9cf46c62525db7c5b2d3fd160618904a7796491e3f1ea21", + "darwin_amd64": "f27ac711e33ba794398ea6bd5b7447fc297f4c0f8c7a6bd01755f22e515cbf3a", + "linux_arm64": "7ac513787b4870e3a390b711fffb7d7e519638335f8338abba32f5796c047252", + "darwin_arm64": "6d47e6d15508738b07da360529baefc246e589b0ebe8d54a6a8818a30ca24e90", + }, + "3.9.1": { + "linux_amd64": "aa20234e12e8d74c988482c21f8bb00be12b859e377a51d2358dac4017f3623b", + "darwin_amd64": "81a20c7076479042c9fa8fa814462fd1349d63e9ff86c789c4267118ef4ceaf2", + "linux_arm64": "7d6b1841beb12a6e899b52db1e96082ce6464b0aa815569b2ee05fcf0267de1d", + "darwin_arm64": "a48ff7b589e4787db60de3b17d2f6f6a1f75ac33b2c93d32d37a9e91f42378d7", + }, + "3.9.0": { + "linux_amd64": "2dfd6eebabd7e6b05c02ca1f5e5ecd9be38f462a2754dea039318c6565517c3f", + "darwin_amd64": "943c102e23c78c9e772d0c3470bd685a87ad396d39f24c0f21d36a8a218aa808", + "linux_arm64": "32c2cc01e02e7e8ea4d950447c614c16aec6f0973b88ff8336984fa0f0e6a12c", + "darwin_arm64": "756d5c6cdd19efc3c1bb313b2666a9ee88effd06986811037cf8a18bd72e316e", + }, + "3.8.0": { + "linux_amd64": "27d22a91e293c916012d60b66e42e72e7f0234fded7cc5c73eb28343cca7a869", + "darwin_amd64": "6ad9e48197afdf6f2352bb8b824f5fe87369df881dcaa0fcb2261fb69add9082", + "linux_arm64": "06d48bf9a580472c2b84771eb4a3b4d3b1583e880bf937339a1f04a3387abfe5", + "darwin_arm64": "f98de66ae5e83a8aebb2d023dac3f886f64c0d73979a0b79a1a002d5aa90208f", + }, + "3.7.0": { + "linux_amd64": "b413f3229a27d83bdfe525c20144e8bab5c15fc8771717c41d8fb9fd68898dd8", + "darwin_amd64": "235ca6649f05f7dfff2152a9adbf8fbee15b71b8553d4c04df954fc5e121756d", + "linux_arm64": "2f6f57d100ae9c89edf250043f0b883a6debbac0688868bacd4979dc9b8d4d0b", + "darwin_arm64": "d2c7c533db79e748d5a3539b942cd0518eeb8993a20f971f13084a66280d6e50", + }, + "3.6.0": { + "linux_amd64": "d5a9f639cb98298817bcab32b6046eb45cb912b56217a75d729646512a436ff4", + "darwin_amd64": "c5f1d912ba08e2745c034e1d531bf8af956f316e76e7f02afccb05f377aee5a4", + "linux_arm64": "15e1cdf90ffcfefbd81aac0f620f8239f13902e80592e1239733e5ba607a5ec7", + "darwin_arm64": "4b117e547d4b8557b7f72f7c508885733f13e1100710379cdd35daab0c74b4ac", + }, "3.5.0": { "linux_amd64": "271e59de861684156515b8aa53eafa0cd242017595652c52a072b1812bbd3775", - "darwin_amd64": "b75c1f1532fc27187097f9ccb6c4d8e8ea751e7e9a238a1c1f2c58b171eadad7" + "darwin_amd64": "b75c1f1532fc27187097f9ccb6c4d8e8ea751e7e9a238a1c1f2c58b171eadad7", + "linux_arm64": "6c7a8238138b309d2fb84f316f800da804ece829c67304ead144a0291f282f1a", + "darwin_arm64": "dfde9dda51d4f32dd16499103198ebe45430cf71c59d4fe4c0614212273a4bd1", }, "3.4.1": { "linux_amd64": "4b9c6b3afec09bd1c1f1a67ed11c275854b9cbddc1654ebdb77c3b0b95f4e065", - "darwin_amd64": "6c4600ff2a4ed02c073f0c9d305bf89948e02b3767d2d5a810d0138d269c133d" - }, - "3.4.0": { - "linux_amd64": "cf90dad16d27a9c31db11f2c326aad926036610a75be03f342e28136ae64cf47", - "darwin_amd64": "4d77ba9e9fc214dbd16077fa069bf40606bfe57c359b3e24fbf38919922d7b5f" - }, - "3.3.0": { - "linux_amd64": "f9fb1a8e329dfe3be635ccdec3c7d3a6df4092ba13243bfcb89111b12343eb4f", - "darwin_amd64": "d5c3ed529d8156005ce15de43f6f505abc4beb63e9b0ac9e215edaecb64c2ce3" - }, - "3.2.0": { - "linux_amd64": "102bb0f586dcb50951aded67856483a2ee114057c56475b3cda6051a12832a72", - "darwin_amd64": "871a0f21af99d895ef534fb11704eb0e4a1f8b55df58906f625702faa65f64e6" - }, - "3.1.0": { - "linux_amd64": "a4af6a2110cf506855cdc86d0291c6e76203ae9fb3c918f7fdc05e7962e4b488", - "darwin_amd64": "2a6c5e0e276b65cfbe173fca07b318ecff1752bf947002641b808c4a40187f2c" - }, - "3.0.0": { - "linux_amd64": "d762c5cd954f9eb8620c64d27fc413e383e8100108d9edaee2211457165d2dff", - "darwin_amd64": "c8bcbb32de6a6e17676a21ce202d2da7d97f342582f42d4dfe672260b7f2c100" - }, - "3.0.0-rc.0": { - "linux_amd64": "5dc229842edc6e35d51c6f8ee3c8dbfcd7989665ebaa0d27a79bdd6e6fe80e39", - "darwin_amd64": "af0c319343b58a340eb16c3c3d389027dd6dd741e733d4809c6ed4d6fa4a60d3" - }, - "3.0.0-beta.1": { - "linux_amd64": "ae80ae35b75fda988ad23cb48ae658d653d802484b21d9c55dd54ddeadfa65b3", - "darwin_amd64": "3dc49db6550b3256132db2554e34ec029ce70042c61dd2f534acadee3365b8a7" - }, - "3.0.0-beta.0": { - "linux_amd64": "8178d61b1e8214f8d0871719614143f42f032655b77a64ea4e5c364fec2c8acd", - "darwin_amd64": "991cbd04b162f31f40f5eb430286f23ac38b16bd57f0c85fcc85b2a00adef8f9" - }, - "2.3.2": { - "linux_amd64": "c65b86f497726fcb8b887d55c10a68e38a503aa736c0d02b2cce11df267ad91d", - "darwin_amd64": "7e7dc6f5b106909a13a24beea73427940a6ed81a8dea66d65b185dc2c71dcc28" - }, - "3.0.0-alpha.1": { - "linux_amd64": "dab17609a0b7a8d89244cd2c739c52bb777067c61b867d50a9def869c98cd560", - "darwin_amd64": "bf4baab7587747f084d9b6e308a5e07fb56cdc032b907c81bfb230558a301052" - }, - "3.0.0-alpha.0": { - "linux_amd64": "099faba3dda57585bbfaa9b090fa3bc724c124858f5d5c78dc8a507297456d0d", - "darwin_amd64": "76abdc522426bf70cdd5118400563aa1bb62638eaa0e4c8486d30f4178403867" - }, - "2.3.1": { - "linux_amd64": "ff496970f209706763f2aba2bdcefc2de8d00085b3b972b5790117b59ea4ed10", - "darwin_amd64": "39314d45053b6c31eb17e35c9b8d923f8a38277a9a136448345dd4b7f0f308f4" - }, - "2.3.0": { - "linux_amd64": "a8ffea619f8d6e6c9fab2df8543cf0912420568e3979f44340a7613de5944141", - "darwin_amd64": "b44f6c7ba1edf0046354ff29abffee3410d156fabae6eb3fa62da806988aa8bd" - }, - "2.2.0": { - "linux_amd64": "9ef35a4a4e92408f7606f1dd1e68fe986fa222a88d34e40ecc07b6ffffcc8c12", - "darwin_amd64": "5ccb9803d391e819b606b0c702610093619ad08e429ae34401b3e4d448dd2553" - }, - "2.1.0": { - "linux_amd64": "79820786964eaecba1e90c413d8399600fde7917dfd1b0b74d6536b33f020077", - "darwin_amd64": "3ab68c805a7dea461023e2c52b906c0ecac6ecb1b9a597e4a98611bce9c1fe56" - }, - "2.0.1": { - "linux_amd64": "e8d287535c79013bfebcee22f748153686128926ae5992f023725e7b17996a04", - "darwin_amd64": "a2cd518da553584aee2e8a74818da1521f5dd4a9a4a97c8e18b2634e8a8266ca" - }, - "2.0.0": { - "linux_amd64": "858d84aa3e8bb6528d7dd4dbab4e8fceb59c8ea7905918bc72dc719d784c40f3", - "darwin_amd64": "4679b5d57c857a0d3c763e9eefb4c797804a184fcae7827a3ca9abc990accc70" - }, - "2.0.0-rc.0": { - "linux_amd64": "d4e5f8adf005e59ad1b2b8bf88f9e6841d500a473fb6f7dde7b17ca94b12e1df", - "darwin_amd64": "970c74143287731c60bb171a1c90f2fb18c99b5e989af38db0d5094b96ded02a" - }, - "2.0.0-beta.0": { - "linux_amd64": "52a81e9277b24ad3693551be142cad05efc0e4d5a35a1f99749e847d6b5f952a", - "darwin_amd64": "8ade93ba2821ff8e87a9266e91409a1a8ef7a8c6afe7143e68c356b675df17f4" - }, - "2.0.0-alpha.4": { - "linux_amd64": "623849d15580544790805326532d1d3e0e6bb92da770e908048049dda724be4d", - "darwin_amd64": "e0937eb925cf0e68efa26b15e0c57feb4b7dbb0176f1acd69a68a78c5c925081" - }, - "2.0.0-alpha.3": { - "linux_amd64": "ff6c1eb39eac6fac5c63a773f4690643603955e8672991d8da12fce0b3708d28", - "darwin_amd64": "eb401e5aab74a8344e9cb58cb9263ed0dce74b218e6de9fd71d0009ee08ac93c" - }, - "2.0.0-alpha.2": { - "linux_amd64": "d9d3f471cb54f4e802ed763d0706720be45f65c7bd36e45ab28db7840028bf72", - "darwin_amd64": "795e49c0c9111f27307f63af9271f736efc1d2c8d2379be0087ff26321a7719c" - }, - "2.0.0-alpha.1": { - "linux_amd64": "8647fcac60166ae211b9e198596452003c5887dd5b0d8b79427aa4fe3357f125", - "darwin_amd64": "c19887e34f5ff008d89ae3e86aeecd0560002c0dacd36f870b46741e9426b551" - }, - "2.0.0-alpha.0": { - "linux_amd64": "edee6582e8cf0c2836d509ed3939b7c3a1006c28bb57f22f2a3d4a5f4699ae99", - "darwin_amd64": "9405023af5ddbadf5196cf450c412e3d328c995a86f097f69222c26110c003b2" - }, - "1.0.8": { - "linux_amd64": "7dfaf7a38d69c77f046e85aeabfe96f7cdd88634feab3ecc0bb03f9da7e69ad3", - "darwin_amd64": "a2a2db0db67c61e7ae958b9ff393e44a7032cd11b5e46acf9bd0a547e3fb9915" - }, - "1.0.7": { - "linux_amd64": "9f7f55708429122b2111a5012977265522228764e756e0094f0128605f85b462", - "darwin_amd64": "f58b9b33a3a61be2313c1747c91c46436deede87d20ad13287618d5e54e9d315" - }, - "1.0.6": { - "linux_amd64": "a622eebdcc95984d3a3d87d0acd2d7c02b463f4db23b1b22020a2bc77c032668", - "darwin_amd64": "0a056b58af62e4204cc65e0a7a39236396acff1935fe10effd52bf226ea6dbc3" + "darwin_amd64": "6c4600ff2a4ed02c073f0c9d305bf89948e02b3767d2d5a810d0138d269c133d", + "linux_arm64": "802aaa112d9890d30744e20058b7fff0d6a8741ee37804855e7191dd494c9e74", + "darwin_arm64": "8f20303a8d3ed579d38160b92a7314f793892daa33cbc366b4f340bbb0308bc2", }, - "1.0.5": { - "linux_amd64": "e729c74f6a1b3ab9c416642f372b84df60d07c84f64302f1f7e3fe7a9f9411e5", - "darwin_amd64": "a510ef34d947764f57385e11990ba70560bf2d520f56849160174d2c18544063" - } -} \ No newline at end of file +} diff --git a/kustomize/BUILD.bazel b/kustomize/BUILD.bazel index 7a71e5e..a905133 100644 --- a/kustomize/BUILD.bazel +++ b/kustomize/BUILD.bazel @@ -1,13 +1,5 @@ load("@rules_kubebuilder//kustomize:kustomize-toolchain.bzl", "kustomize_toolchain") -load("@rules_pkg//:pkg.bzl", "pkg_tar") - -exports_files( - [ - "bin/kustomize.linux", - "bin/kustomize.darwin", - ], - visibility = ["//visibility:public"], -) +load("@rules_kubebuilder//kustomize:kustomize.bzl", "kustomize_bin") filegroup( name = "srcs", @@ -18,53 +10,81 @@ filegroup( visibility = ["//visibility:public"], ) -pkg_tar( - name = "release-bin", - srcs = glob(["bin/*"]), - package_dir = "/bin", +toolchain_type(name = "toolchain") + +# Toolchains reference externally fetched binaries. See deps.bzl and +# extensions.bzl for repository creation macros. + +# Linux x86_64 +kustomize_toolchain( + name = "kustomize_linux_amd64", + kustomize_bin = "@kustomize_linux_amd64//:kustomize", ) -pkg_tar( - name = "release", - srcs = [ - "BUILD.bazel", - ":srcs", +toolchain( + name = "kustomize_linux_amd64_toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:x86_64", ], - package_dir = "kustomize", + toolchain = ":kustomize_linux_amd64", + toolchain_type = ":toolchain", visibility = ["//visibility:public"], - deps = [":release-bin"], ) -toolchain_type(name = "toolchain") - +# Linux arm64 kustomize_toolchain( - name = "kustomize_linux", - kustomize_bin = "@rules_kubebuilder//kustomize:bin/kustomize.linux", + name = "kustomize_linux_arm64", + kustomize_bin = "@kustomize_linux_arm64//:kustomize", +) + +toolchain( + name = "kustomize_linux_arm64_toolchain", + exec_compatible_with = [ + "@platforms//os:linux", + "@platforms//cpu:aarch64", + ], + toolchain = ":kustomize_linux_arm64", + toolchain_type = ":toolchain", + visibility = ["//visibility:public"], ) +# macOS x86_64 kustomize_toolchain( - name = "kustomize_darwin", - kustomize_bin = "@rules_kubebuilder//kustomize:bin/kustomize.darwin", + name = "kustomize_darwin_amd64", + kustomize_bin = "@kustomize_darwin_amd64//:kustomize", ) toolchain( - name = "kustomize_linux_toolchain", + name = "kustomize_darwin_amd64_toolchain", exec_compatible_with = [ - "@platforms//os:linux", + "@platforms//os:osx", "@platforms//cpu:x86_64", ], - toolchain = ":kustomize_linux", + toolchain = ":kustomize_darwin_amd64", toolchain_type = ":toolchain", visibility = ["//visibility:public"], ) +# macOS arm64 +kustomize_toolchain( + name = "kustomize_darwin_arm64", + kustomize_bin = "@kustomize_darwin_arm64//:kustomize", +) + toolchain( - name = "kustomize_darwin_toolchain", + name = "kustomize_darwin_arm64_toolchain", exec_compatible_with = [ "@platforms//os:osx", - "@platforms//cpu:x86_64", + "@platforms//cpu:aarch64", ], - toolchain = ":kustomize_darwin", + toolchain = ":kustomize_darwin_arm64", toolchain_type = ":toolchain", visibility = ["//visibility:public"], ) + +# Public wrapper to expose the active kustomize as a single label usable from genrule +kustomize_bin( + name = "kustomize_bin", + visibility = ["//visibility:public"], +) diff --git a/kustomize/bin/kustomize.darwin b/kustomize/bin/kustomize.darwin deleted file mode 100755 index c81b943..0000000 Binary files a/kustomize/bin/kustomize.darwin and /dev/null differ diff --git a/kustomize/bin/kustomize.linux b/kustomize/bin/kustomize.linux deleted file mode 100755 index 6a94f36..0000000 Binary files a/kustomize/bin/kustomize.linux and /dev/null differ diff --git a/kustomize/deps.bzl b/kustomize/deps.bzl index b6e2dea..aff385a 100644 --- a/kustomize/deps.bzl +++ b/kustomize/deps.bzl @@ -1,8 +1,39 @@ -""" Dependencies for kustomize +"""Kustomize repositories and toolchains (WORKSPACE helper). + +This macro defines external repositories to download kustomize binaries +for the supported OS/architectures and registers the corresponding toolchains. + +Args: + version: Kustomize release version to use (e.g. "5.7.1"). + arches: List of architectures to support (subset of ["amd64", "arm64"]). """ -def kustomize_register_toolchain(name = None): - native.register_toolchains( - "@rules_kubebuilder//kustomize:kustomize_linux_toolchain", - "@rules_kubebuilder//kustomize:kustomize_darwin_toolchain", - ) +load("@rules_kubebuilder//kustomize:repo.bzl", "kustomize_download") + +def _kustomize_repo_name(os, arch): + return "kustomize_%s_%s" % (os, arch) + +def kustomize_register_toolchain(version = "5.7.1", arches = ["amd64", "arm64"]): + for os in ["linux", "darwin"]: + for arch in arches: + kustomize_download( + name = _kustomize_repo_name(os, arch), + version = version, + os = os, + arch = arch, + ) + + tcs = [] + if "amd64" in arches: + tcs += [ + "@rules_kubebuilder//kustomize:kustomize_linux_amd64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_darwin_amd64_toolchain", + ] + if "arm64" in arches: + tcs += [ + "@rules_kubebuilder//kustomize:kustomize_linux_arm64_toolchain", + "@rules_kubebuilder//kustomize:kustomize_darwin_arm64_toolchain", + ] + + if tcs: + native.register_toolchains(*tcs) diff --git a/kustomize/extensions.bzl b/kustomize/extensions.bzl new file mode 100644 index 0000000..375639d --- /dev/null +++ b/kustomize/extensions.bzl @@ -0,0 +1,35 @@ +"""Bzlmod module extension for kustomize binaries.""" + +load("@rules_kubebuilder//kustomize:repo.bzl", "kustomize_download") + +_kustomize_tag = tag_class( + attrs = { + "version": attr.string(default = "5.7.1"), + "arches": attr.string_list(default = ["amd64", "arm64"]), + }, +) + +def _kz_ext_impl(mctx): + for mod in mctx.modules: + for t in mod.tags.kustomize: + for os in ["linux", "darwin"]: + for arch in t.arches: + name = "kustomize_%s_%s" % (os, arch) + kustomize_download(name = name, version = t.version, os = os, arch = arch) + + names = [] + for mod in mctx.modules: + for t in mod.tags.kustomize: + for os in ["linux", "darwin"]: + for arch in t.arches: + names.append("kustomize_%s_%s" % (os, arch)) + + return mctx.extension_metadata( + root_module_direct_deps = names, + root_module_direct_dev_deps = [], + ) + +kz_ext = module_extension( + implementation = _kz_ext_impl, + tag_classes = {"kustomize": _kustomize_tag}, +) diff --git a/kustomize/kustomize.bzl b/kustomize/kustomize.bzl index 6bbd907..2b1078d 100644 --- a/kustomize/kustomize.bzl +++ b/kustomize/kustomize.bzl @@ -44,3 +44,63 @@ kustomize = rule( ], doc = "", ) + +def _kustomize_bin_impl(ctx): + out = ctx.actions.declare_file(ctx.label.name) + kustomize_info = ctx.toolchains["@rules_kubebuilder//kustomize:toolchain"].kustomize_info + + script = _make_script(kustomize_info.kustomize_bin.path) + + ctx.actions.write(output = out, content = script, is_executable = True) + + # Merge runfiles: (a) your tool, (b) the bash runfiles lib’s runfiles + rf = ctx.runfiles(files = [kustomize_info.kustomize_bin]) + rf = rf.merge(ctx.attr._bash_runfiles[DefaultInfo].default_runfiles) + + return DefaultInfo( + files = depset([out]), + executable = out, + runfiles = rf, + ) + +def _make_script(path): + no_external = path + if path.startswith("external/"): + no_external = path[len("external/"):] + return """#!/usr/bin/env bash +set -euo pipefail + +# --- begin runfiles.bash initialization v3 --- +set +e; f=bazel_tools/tools/bash/runfiles/runfiles.bash +# shellcheck disable=SC1090 +source "${{RUNFILES_DIR:-/dev/null}}/$f" 2>/dev/null || \ +source "$(grep -sm1 "^$f " "${{RUNFILES_MANIFEST_FILE:-/dev/null}}" | cut -f2- -d' ')" 2>/dev/null || \ +source "$0.runfiles/$f" 2>/dev/null || \ +source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ +source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \ +{{ echo>&2 "ERROR: cannot find $f"; exit 1; }}; f=; set -e +# --- end runfiles.bash initialization v3 --- + +tool="$(rlocation "{runfile1}")" +if [[ -z "${{tool:-}}" || ! -x "${{tool:-}}" ]]; then + tool="$(rlocation "{runfile2}")" +fi +if [[ -z "${{tool:-}}" || ! -x "${{tool:-}}" ]]; then + echo "ERROR: kustomize binary not found in runfiles: tried {runfile1} and {runfile2}" >&2 + exit 1 +fi +exec "${{tool?}}" "$@" +""".format(runfile1 = no_external, runfile2 = path) + +kustomize_bin = rule( + implementation = _kustomize_bin_impl, + attrs = { + # Pull in the Bash runfiles library + "_bash_runfiles": attr.label( + default = Label("@bazel_tools//tools/bash/runfiles"), + ), + }, + toolchains = ["@rules_kubebuilder//kustomize:toolchain"], + executable = True, + doc = "Executable wrapper that runs the kustomize binary from the active toolchain.", +) diff --git a/kustomize/repo.bzl b/kustomize/repo.bzl new file mode 100644 index 0000000..5fb6e60 --- /dev/null +++ b/kustomize/repo.bzl @@ -0,0 +1,58 @@ +"""Repository rule to download kustomize binaries by OS/arch.""" + +def _kustomize_url(version, os, arch): + # Note: the tag path encodes the '/v' as '%2Fv' in release URLs. + return ( + "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%%2Fv%s/kustomize_v%s_%s_%s.tar.gz" % (version, version, os, arch) + ) + +def _write_build(repo_ctx): + repo_ctx.file( + "BUILD.bazel", + content = """ +package(default_visibility = ["//visibility:public"]) +exports_files(["kustomize"]) +""", + executable = False, + ) + +def _download_kustomize_impl(repo_ctx): + version = repo_ctx.attr.version + os = repo_ctx.attr.os + arch = repo_ctx.attr.arch + + urls = repo_ctx.attr.urls + if not urls: + urls = [_kustomize_url(version, os, arch)] + repo_ctx.download_and_extract(urls, stripPrefix = "") + + candidates = [ + "kustomize", + "bin/kustomize", + "kustomize_v%s_%s_%s/kustomize" % (version, os, arch), + ] + found = None + for p in candidates: + if repo_ctx.path(p).exists: + found = p + break + if not found: + fail("kustomize binary not found after extracting %s" % urls[0]) + + if found != "kustomize": + repo_ctx.symlink(found, "kustomize") + + _write_build(repo_ctx) + +_download_kustomize = repository_rule( + implementation = _download_kustomize_impl, + attrs = { + "version": attr.string(default = "5.7.1"), + "os": attr.string(mandatory = True), + "arch": attr.string(mandatory = True), + "urls": attr.string_list(default = []), + }, +) + +def kustomize_download(name, version, os, arch, urls = None): + _download_kustomize(name = name, version = version, os = os, arch = arch, urls = urls or []) diff --git a/scripts/build-controller-gen.sh b/scripts/build-controller-gen.sh deleted file mode 100755 index c7baacd..0000000 --- a/scripts/build-controller-gen.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -VERSION="0.4.1" - -ROOT=$(git rev-parse --show-toplevel) -DEST="$ROOT/controller-gen/bin" - -test -d "$ROOT/build/tmp" && rm -rf "$ROOT/build/tmp" -mkdir -p "$ROOT/build/tmp" -cd "$ROOT/build/tmp" - -# Fetch and unpack v0.3.0 -echo "Downloading and extracting controller-tools version $VERSION" -curl -sL "https://github.com/kubernetes-sigs/controller-tools/archive/v${VERSION}.tar.gz" | tar xfz - - -cd "controller-tools-$VERSION" - -mkdir -p "$ROOT/bin" -echo "Building controller-gen" -GOOS=darwin GOARCH=amd64 go build ./cmd/controller-gen -mv controller-gen "$DEST/controller-gen.darwin" -GOOS=linux GOARCH=amd64 go build ./cmd/controller-gen -mv controller-gen "$DEST/controller-gen.linux" - -echo "Binaries built:" -file "$DEST"/* diff --git a/scripts/get-kustomize.sh b/scripts/get-kustomize.sh deleted file mode 100755 index cd98cba..0000000 --- a/scripts/get-kustomize.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/bash - -set -euo pipefail - -VERSION="3.8.4" - -ROOT=$(git rev-parse --show-toplevel) -DEST="$ROOT/km/bin" - -mkdir -p "$ROOT/build/tmp" -cd "$ROOT/build/tmp" - -echo Fetching Kustomize version "$VERSION" -for OS in linux darwin; do - curl -s -O -L "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv${VERSION}/kustomize_v${VERSION}_${OS}_amd64.tar.gz" - - test -e kustomize_v${VERSION}_${OS}_amd64.tar.gz || { - echo failed to download kustomize - exit 1 - } - - tar xfx kustomize_v${VERSION}_${OS}_amd64.tar.gz - - mv kustomize "$DEST/kustomize.$OS" -done - -echo done. -file "$DEST"/* - diff --git a/test/BUILD.bazel b/test/BUILD.bazel index 984cfbf..1f10c08 100644 --- a/test/BUILD.bazel +++ b/test/BUILD.bazel @@ -1,23 +1,58 @@ -load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") +load("@rules_go//go:def.bzl", "go_binary", "go_library", "go_test") +load("@rules_kubebuilder//kubebuilder:assets.bzl", "kubebuilder_assets") go_library( name = "go_default_library", - srcs = [ - "apackage.go", - ], + srcs = ["apackage.go"], importpath = "apackage", visibility = ["//visibility:public"], deps = [ ], ) +kubebuilder_assets( + name = "envtest", + etcd = ":dummy_etcd", + kube_apiserver = ":dummy_kube_apiserver", + kubectl = ":dummy_kubectl", +) + go_test( - name = "go_default_test", + name = "test_test", srcs = ["apackage_test.go"], data = [ - "@kubebuilder_sdk//:bin/etcd", + ":envtest_assets_pwd", ], - embed = [":go_default_library"], + embed = [":test"], deps = [ + "@rules_go//go/runfiles:go_default_library", ], ) + +go_library( + name = "test", + srcs = ["apackage.go"], + importpath = "go.openai.org/project/bazel/third_party/rules_kubebuilder/test", + visibility = ["//visibility:public"], +) + +# Dummy binaries to stand in for the assets that Kubebuilder used to +# bundle (etcd, kube-apiserver, kubectl). These are provided as data to +# demonstrate wiring without relying on upstream bundles. +go_binary( + name = "dummy_etcd", + srcs = ["dummy_etcd_main.go"], + visibility = ["//visibility:public"], +) + +go_binary( + name = "dummy_kube_apiserver", + srcs = ["dummy_kube_apiserver_main.go"], + visibility = ["//visibility:public"], +) + +go_binary( + name = "dummy_kubectl", + srcs = ["dummy_kubectl_main.go"], + visibility = ["//visibility:public"], +) diff --git a/test/apackage_test.go b/test/apackage_test.go index c308eb7..53b4c27 100644 --- a/test/apackage_test.go +++ b/test/apackage_test.go @@ -1,8 +1,12 @@ package apackage import ( - "os" - "testing" + "os" + "os/exec" + "strings" + "testing" + + "github.com/bazelbuild/rules_go/go/runfiles" ) func TestAFunct(t *testing.T) { @@ -14,9 +18,36 @@ func TestAFunct(t *testing.T) { // See https://pkg.go.dev/sigs.k8s.io/controller-runtime/pkg/envtest func TestEnvironment(t *testing.T) { - kbAssets := os.Getenv("KUBEBUILDER_ASSETS") - if kbAssets == "" { - t.Errorf("Did not find KUBEBUILDER_ASSETS environment variable set") - } + // Demonstrate end-to-end usage of the kubebuilder_assets macro by + // locating and running the generated *_assets_pwd helper to obtain + // the assembled assets directory, then setting KUBEBUILDER_ASSETS. + rf, err := runfiles.New() + if err != nil { + t.Fatalf("runfiles.New(): %v", err) + } + ws := os.Getenv("TEST_WORKSPACE") + if ws == "" { + ws = "rules_kubebuilder" + } + helperPath, err := rf.Rlocation(ws + "/test/envtest_assets_pwd.bash") + if err != nil { + t.Fatalf("Rlocation: %v", err) + } + out, err := exec.Command(helperPath).CombinedOutput() + if err != nil { + t.Fatalf("running envtest_assets_pwd: %v, out=%s", err, string(out)) + } + dir := strings.TrimSpace(string(out)) + if dir == "" { + t.Fatalf("envtest_assets_pwd returned empty path") + } + if err := os.Setenv("KUBEBUILDER_ASSETS", dir); err != nil { + t.Fatalf("Setenv: %v", err) + } + + kbAssets := os.Getenv("KUBEBUILDER_ASSETS") + if kbAssets == "" { + t.Errorf("Did not find KUBEBUILDER_ASSETS environment variable set") + } } diff --git a/test/dummy_etcd_main.go b/test/dummy_etcd_main.go new file mode 100644 index 0000000..e13f27f --- /dev/null +++ b/test/dummy_etcd_main.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func main() { + // Dummy etcd replacement for tests + fmt.Println("dummy etcd running (no-op)") +} + diff --git a/test/dummy_kube_apiserver_main.go b/test/dummy_kube_apiserver_main.go new file mode 100644 index 0000000..1308d07 --- /dev/null +++ b/test/dummy_kube_apiserver_main.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func main() { + // Dummy kube-apiserver replacement for tests + fmt.Println("dummy kube-apiserver running (no-op)") +} + diff --git a/test/dummy_kubectl_main.go b/test/dummy_kubectl_main.go new file mode 100644 index 0000000..9cb4e5b --- /dev/null +++ b/test/dummy_kubectl_main.go @@ -0,0 +1,9 @@ +package main + +import "fmt" + +func main() { + // Dummy kubectl replacement for tests + fmt.Println("dummy kubectl running (no-op)") +} +