kubernetes-retired / cluster-registry

[EOL] Cluster Registry API
https://kubernetes.github.io/cluster-registry/
Apache License 2.0
238 stars 94 forks source link

can't import the api library #161

Closed baodongli closed 6 years ago

baodongli commented 6 years ago

/sig multicluster

Hi, I'm trying to use the cluster registry api in my golang project. I imported it as

import "k8s.io/cluster-registry/pkg/apis/clusterregistry"

In the bazel BUILD file, added the following dependency: "@io_k8s_cluster_registry//pkg/apis/clusterregistry:go_default_library"

When building the project, bazel threw the following error: no such package '@io_k8s_cluster_registry//pkg/apis/clusterregistry': The repository could not be resolved and referenced by ...

Can someone let me know what I did wrong?

perotinus commented 6 years ago

@baodongli Did you add an entry for the cluster-registry to your bazel WORKSPACE file, in the root of your repository? You'll need something like this:

http_archive(
    name = "io_bazel_rules_go",
    url = "https://github.com/bazelbuild/rules_go/releases/download/0.8.1/rules_go-0.8.1.tar.gz",
    sha256 = "90bb270d0a92ed5c83558b2797346917c46547f6f7103e648941ecdb6b9d0e72",
)
load("@io_bazel_rules_go//go:def.bzl", "go_rules_dependencies", "go_register_toolchains")
go_rules_dependencies()
go_register_toolchains()

go_repository(
    name = "io_k8s_cluster_registry",
    tag = "v0.0.1',
    importpath = "k8s.io/cluster-registry",
)

https://github.com/bazelbuild/rules_go/blob/master/go/workspace.rst#go_repository has some more details about go_repository.

You may need to add WORKSPACE entries for the transitive dependencies of the cluster-registry as well.

baodongli commented 6 years ago

Thanks! I'm new to bazel build. I added the repo in the WORKSPACE file, and now I'm getting this error: ERROR: /home/devuser/.cache/bazel/_bazel_devuser/4035ef574743c353882c5b18cf766ff3/external/io_k8s_cluster_registry/vendor/k8s.io/apimachinery/pkg/labels/BUILD:23:1: error loading package '@io_k8s_cluster_registry//vendor/k8s.io/apimachinery/pkg/util/sets': Extension file not found. Unable to load package for '@io_kubernetes_build//defs:go.bzl': The repository could not be resolved and referenced by '@io_k8s_cluster_registry//vendor/k8s.io/apimachinery/pkg/labels:go_default_library'.

I then took the following from the cluster_registry and added to pilot's WORKSPACE: http_archive( name = "io_kubernetes_build", sha256 = "8e49ac066fbaadd475bd63762caa90f81cd1880eba4cc25faa93355ef5fa2739", strip_prefix = "repo-infra-e26fc85d14a1d3dc25569831acc06919673c545a", urls = ["https://github.com/kubernetes/repo-infra/archive/e26fc85d14a1d3dc25569831acc06919673c545a.tar.gz"], )

With that, I started getting lots of 'method redeclared' and 'inconsistent definitions' errors. I guess that I must be using different versions of kubernetes between pilot (the project I'm trying to build) and the cluster-registry?

perotinus commented 6 years ago

Is https://github.com/kelseyhightower/pilot the project you're referring to?

Can you include an example of one of the errors you were getting? It seems like the target you're trying to build in the cluster registry is depending on its own vendored versions of deps rather than the ones in your workspace. You may be able to fix that by adding the build_file_generation = on parameter to the go_repository rule, which forces Bazel to regenerate the BUILD files there. You might also need to regenerate BUILD files in pilot, if it has generated BUILD files, but I'm not sure how that project is set up.

baodongli commented 6 years ago

Thanks a bunch! I added the build_file_generation = on, and other dependent repos into the workspace file, and it worked now.

By the way, this is istio pilot.