ob/rules_kubebuilder

Unable to get configuration working

eudaimos opened this issue · 10 comments

@ob creating an issue working with this repo (which is awesome that you put this together) - hoping you can help with a little troubleshooting

Essentially bazel is saying it cannot download kubebuilder or find the @rules_kubebuilder//controller-gen:toolchain - here are 3 different command tries with basically the same response:

bazel query //...

➜  kb-operator-sbox bazel query //...
INFO: Repository kubebuilder_sdk instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule _kubebuilder_download_sdk defined at:
  /private/var/tmp/_bazel_jeff/77a1926f3e1f14e13b1c87bbae4eb719/external/rules_kubebuilder/kubebuilder/sdk.bzl:34:44: in <toplevel>
WARNING: Download from https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.1/kubebuilder_2.3.1_darwin-x86_64.tar.gz failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 404 Not Found
ERROR: An error occurred during the fetch of repository 'kubebuilder_sdk':
   Traceback (most recent call last):
	File "/private/var/tmp/_bazel_jeff/77a1926f3e1f14e13b1c87bbae4eb719/external/rules_kubebuilder/kubebuilder/sdk.bzl", line 23, column 29, in _kubebuilder_download_sdk_impl
		ctx.download_and_extract(
Error in download_and_extract: java.io.IOException: Error downloading [https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.1/kubebuilder_2.3.1_darwin-x86_64.tar.gz] to /private/var/tmp/_bazel_jeff/77a1926f3e1f14e13b1c87bbae4eb719/external/kubebuilder_sdk/kubebuilder_2.3.1_darwin-x86_64.tar.gz: GET returned 404 Not Found
ERROR: Evaluation of query "//..." failed due to BUILD file errors
Loading: 0 packages loaded

bazel run //:kubebuilder -- --help

➜  kb-operator-sbox bazel run //:kubebuilder -- --help
INFO: Repository kubebuilder_sdk instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule _kubebuilder_download_sdk defined at:
  /private/var/tmp/_bazel_jeff/77a1926f3e1f14e13b1c87bbae4eb719/external/rules_kubebuilder/kubebuilder/sdk.bzl:34:44: in <toplevel>
WARNING: Download from https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.1/kubebuilder_2.3.1_darwin-x86_64.tar.gz failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 404 Not Found
ERROR: An error occurred during the fetch of repository 'kubebuilder_sdk':
   Traceback (most recent call last):
	File "/private/var/tmp/_bazel_jeff/77a1926f3e1f14e13b1c87bbae4eb719/external/rules_kubebuilder/kubebuilder/sdk.bzl", line 23, column 29, in _kubebuilder_download_sdk_impl
		ctx.download_and_extract(
Error in download_and_extract: java.io.IOException: Error downloading [https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.1/kubebuilder_2.3.1_darwin-x86_64.tar.gz] to /private/var/tmp/_bazel_jeff/77a1926f3e1f14e13b1c87bbae4eb719/external/kubebuilder_sdk/kubebuilder_2.3.1_darwin-x86_64.tar.gz: GET returned 404 Not Found
ERROR: /Users/jeff/dev/scratch/kb-operator-sbox/BUILD.bazel:9:12: //:kubebuilder depends on @kubebuilder_sdk//:bin/kubebuilder in repository @kubebuilder_sdk which failed to fetch. no such package '@kubebuilder_sdk//': java.io.IOException: Error downloading [https://github.com/kubernetes-sigs/kubebuilder/releases/download/v2.3.1/kubebuilder_2.3.1_darwin-x86_64.tar.gz] to /private/var/tmp/_bazel_jeff/77a1926f3e1f14e13b1c87bbae4eb719/external/kubebuilder_sdk/kubebuilder_2.3.1_darwin-x86_64.tar.gz: GET returned 404 Not Found
ERROR: Analysis of target '//:kubebuilder' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.566s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)
FAILED: Build did NOT complete successfully (1 packages loaded, 0 targets configured)

bazel build //api/v1beta1:crds

➜  kb-operator-sbox bazel build //api/v1beta1:crds
ERROR: While resolving toolchains for target //api/v1beta1:crds: no matching toolchains found for types @rules_kubebuilder//controller-gen:toolchain
ERROR: Analysis of target '//api/v1beta1:crds' failed; build aborted: no matching toolchains found for types @rules_kubebuilder//controller-gen:toolchain
INFO: Elapsed time: 0.140s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (2 packages loaded, 2 targets configured)

My configuration so far

WORSPACE file

workspace(name = "scratch_osdk_operator")

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
    name = "io_bazel_rules_go",
    sha256 = "b725e6497741d7fc2d55fcc29a276627d10e43fa5d0bb692692890ae30d98d00",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.24.3/rules_go-v0.24.3.tar.gz",
        "https://github.com/bazelbuild/rules_go/releases/download/v0.24.3/rules_go-v0.24.3.tar.gz",
    ],
)

http_archive(
    name = "bazel_gazelle",
    sha256 = "b85f48fa105c4403326e9525ad2b2cc437babaa6e15a3fc0b1dbab0ab064bc7c",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
        "https://github.com/bazelbuild/bazel-gazelle/releases/download/v0.22.2/bazel-gazelle-v0.22.2.tar.gz",
    ],
)

load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")
load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies", "go_repository")

go_rules_dependencies()

go_register_toolchains()

gazelle_dependencies()

http_archive(
    name = "rules_proto",
    sha256 = "602e7161d9195e50246177e7c55b2f39950a9cf7366f74ed5f22fd45750cd208",
    strip_prefix = "rules_proto-97d8af4dc474595af3900dd85cb3a29ad28cc313",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_proto/archive/97d8af4dc474595af3900dd85cb3a29ad28cc313.tar.gz",
        "https://github.com/bazelbuild/rules_proto/archive/97d8af4dc474595af3900dd85cb3a29ad28cc313.tar.gz",
    ],
)

load("@rules_proto//proto:repositories.bzl", "rules_proto_dependencies", "rules_proto_toolchains")

# added after bazel complained about missing dependency
http_archive(
    name = "rules_pkg",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.3.0/rules_pkg-0.3.0.tar.gz",
        "https://github.com/bazelbuild/rules_pkg/releases/download/0.3.0/rules_pkg-0.3.0.tar.gz",
    ],
    sha256 = "6b5969a7acd7b60c02f816773b06fcf32fbe8ba0c7919ccdc2df4f8fb923804a",
)
load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")
rules_pkg_dependencies()


load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
    name = "rules_kubebuilder",
    remote = "https://github.com/ob/rules_kubebuilder",
    commit = "d5372a02bcc5abdcfbb8e68f18bde6e9495b3019", # would be better I think if there was a tag
)

load("@rules_kubebuilder//kubebuilder:sdk.bzl", "kubebuilder_register_sdk")

kubebuilder_register_sdk(version = "2.3.1")

root BUILD.bazel file

load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_library")
load("@bazel_gazelle//:def.bzl", "gazelle")

# gazelle:prefix github.com/scratch/kb-operator-sbox/my-test-operator
gazelle(name = "gazelle")

load("@rules_kubebuilder//kubebuilder:def.bzl", "kubebuilder")

kubebuilder(name = "kubebuilder")

go_library(
    name = "my-test-operator_lib",
    srcs = ["main.go"],
    importpath = "github.com/scratch/kb-operator-sbox/my-test-operator",
    visibility = ["//visibility:private"],
    deps = [
        "@io_k8s_apimachinery//pkg/runtime:go_default_library",
        "@io_k8s_client_go//kubernetes/scheme:go_default_library",
        "@io_k8s_client_go//plugin/pkg/client/auth/gcp:go_default_library",
        "@io_k8s_sigs_controller_runtime//:go_default_library",
        "@io_k8s_sigs_controller_runtime//pkg/log/zap:go_default_library",
    ],
)

go_binary(
    name = "my-test-operator",
    embed = [":my-test-operator_lib"],
    visibility = ["//visibility:public"],
)

BUILD.bazel file at api/v1beta1:

load("@io_bazel_rules_go//go:def.bzl", "go_library")

# GENERATED BY bazel run //:gazelle - left for posterity
# go_library(
#     name = "v1beta1",
#     srcs = [
#         "groupversion_info.go",
#         "project_types.go",
#         "zz_generated.deepcopy.go",
#     ],
#     importpath = "github.com/rollbar/rollbar-k8s/my-test-operator/api/v1beta1",
#     visibility = ["//visibility:public"],
#     deps = [
#         "@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",
#     ],
# )

load(
    "@rules_kubebuilder//controller-gen:controller-gen.bzl",
    "controller_gen_crd",
    "controller_gen_object",
    "controller_gen_rbac",
)

filegroup(
    name = "srcs",
    # srcs = [
        # "groupversion_info.go",
        # your source files here, except for zz_generated_deepcopy.go
    # ] + 
    srcs = glob(["*.go"], exclude=["zz_generated*.go"]),
)

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",
]

controller_gen_object(
    name = "generated_sources",
    srcs = [
        ":srcs",
    ],
    deps = DEPS,
)

# keep
go_library(
    name = "go_default_library",
    srcs = [
        "generated_sources",
        "srcs",
    ],
    importpath = "github.com/scratch/kb-operator-sbox/my-test-operator/api/v1beta1",
    visibility = ["//visibility:public"],
    deps = DEPS,
)

controller_gen_crd(
    name = "crds",
    srcs = [
        ":srcs",
    ],
    visibility = ["//visibility:public"],
    deps = DEPS,
)
ob commented

The URL for kubebuilder was wrong.. I fixed it in fa93792

Can you try again?

ob commented

For the controller-gen toolchain, you are missing this bit in your WORKSPACE file:

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() 

I just added it to the README.

thanks @ob the bazel build //api/v1beta1:crds still isn't working due to a missing dependency:

➜  kb-operator-sbox bazel build //api/v1beta1:crds
DEBUG: Rule 'rules_kubebuilder' indicated that a canonical reproducible form can be obtained by modifying arguments shallow_since = "1605320413 -0800"
DEBUG: Repository rules_kubebuilder instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule git_repository defined at:
  /private/var/tmp/_bazel_jeff/77a1926f3e1f14e13b1c87bbae4eb719/external/bazel_tools/tools/build_defs/repo/git.bzl:199:33: in <toplevel>
ERROR: /Users/jeff/dev/scratch/kb-operator-sbox/api/v1beta1/BUILD.bazel:68:19: no such package '@io_k8s_sigs_controller_runtime//pkg/scheme': The repository '@io_k8s_sigs_controller_runtime' could not be resolved and referenced by '//api/v1beta1:crds_controller_gen'
ERROR: Analysis of target '//api/v1beta1:crds' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.116s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1 packages loaded, 3 targets configured)

I tried running both

$ bazel run //:gazelle -- update-repos --from_file=go.mod
$ bazel run //:gazelle

and I still don't see any of the go_repository macros added to the WORKSPACE file that I did in my other test project.

Do you know what might be happening?

ob commented
ERROR: /Users/jeff/dev/scratch/kb-operator-sbox/api/v1beta1/BUILD.bazel:68:19: no such package '@io_k8s_sigs_controller_runtime//pkg/scheme': The repository '@io_k8s_sigs_controller_runtime' could not be resolved and referenced by '//api/v1beta1:crds_controller_gen'

That means you're missing the go_repository() for @io_k8s_sigs_controller_runtime in your WORKSPACE.

Gazelle should add it when you run update-repos.

@ob thanks for getting back - whenever I try to run update-repos from_file=go.mod it doesn't work with a cryptic message as to why

➜  smuthin bazel run //:gazelle -- update-repos from_file=go.mod
DEBUG: Rule 'rules_kubebuilder' indicated that a canonical reproducible form can be obtained by modifying arguments shallow_since = "1605321094 -0800"
DEBUG: Repository rules_kubebuilder instantiated at:
  no stack (--record_rule_instantiation_callstack not enabled)
Repository rule git_repository defined at:
  /private/var/tmp/_bazel_jeff/91c4160ebe3e1c239aad446ab8765ec5/external/bazel_tools/tools/build_defs/repo/git.bzl:199:33: in <toplevel>
INFO: Analyzed target //:gazelle (1 packages loaded, 2 targets configured).
INFO: Found 1 target...
Target //:gazelle up-to-date:
  bazel-bin/gazelle-runner.bash
  bazel-bin/gazelle
INFO: Elapsed time: 0.145s, Critical Path: 0.01s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
gazelle: finding module version and sum for from_file=go.mod@latest: exit status 1:

not sure why it's looking for @latest nor why I'm getting the exit status 1 for the error

here is the current go.mod file

module github.com/scratch/kb-operator-sbox/my-test-operator

go 1.13

require (
	github.com/go-logr/logr v0.1.0
	github.com/onsi/ginkgo v1.11.0
	github.com/onsi/gomega v1.8.1
	k8s.io/apimachinery v0.17.2
	k8s.io/client-go v0.17.2
	sigs.k8s.io/controller-runtime v0.5.0
)
ob commented

Sorry, no idea what's going on here. You should try asking in Gazelle's issues.

ugh it was a type so sorry @ob - looking back at my command I forgot to include the -- before the from_file=go.mod - it seems to work now but I do have the manual step of adding build_file_proto_mode = "disable_global" to the following go_repositorys:

  • io_k8s_api
  • io_k8s_apimachinery
  • io_k8s_apiserver
  • io_k8s_sigs_controller_runtime

Last thing is it doesn't seem to work if I don't include the rules_pkg rules, so this seems like it's needed in the WORKSPACE instructions which may be obvious to most but it wasn't to me:

http_archive(
    name = "rules_pkg",
    sha256 = "6b5969a7acd7b60c02f816773b06fcf32fbe8ba0c7919ccdc2df4f8fb923804a",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.3.0/rules_pkg-0.3.0.tar.gz",
        "https://github.com/bazelbuild/rules_pkg/releases/download/0.3.0/rules_pkg-0.3.0.tar.gz",
    ],
)

load("@rules_pkg//:deps.bzl", "rules_pkg_dependencies")

rules_pkg_dependencies()
ob commented

You can add a #keep comment to prevent gazelle from overwriting it (see here)

You can also run gazelle with -build_file_proto_mode=disable_global (docs].