c icon indicating copy to clipboard operation
c copied to clipboard

BUILD.Bazel rule support in c k8s client

Open joyanta55 opened this issue 2 years ago • 4 comments

Hello All, For my Bazel project, I need to add c kubernetes-client source as my Bazel repo. But looks like the bazel build is not working, as there is no BUILD.Bazel in the c/kubernetes/ folder. So, just wondering if Bazel support has not implemented yet or I am doing something wrong.

Thanks in Advance.

joyanta55 avatar Oct 24 '22 19:10 joyanta55

This library currently uses cmake for building.

brendandburns avatar Oct 24 '22 21:10 brendandburns

This may help, I found it using web search so I know nothing about if it works for this :)

https://bazelbuild.github.io/rules_foreign_cc/main/cmake.html

brendandburns avatar Oct 24 '22 21:10 brendandburns

Hi @brendandburns , thanks a lot for your response. I actually tried the link you shared and then found out C k8s repo, doesn't contain BUILD.Bazel file, which is essential for adding this repo as Bazel repo. Bazel can build this library using cmake following the content of CMakeList.txt, but it also requires one BUILD.Bazel file, to do the cmake build.

Just asking, can we add this issue as PR, I can work on that.

joyanta55 avatar Oct 25 '22 13:10 joyanta55

The exact error I am getting when I run bazel build

no such package '@kube-client-c//kubernetes': BUILD file not found in directory 'kubernetes' of external repository @kube-client-c. Add a BUILD file to a directory to mark it as a package. and referenced by '//src:kube-client-c'

kube-client-c is my repo name.

joyanta55 avatar Oct 25 '22 15:10 joyanta55

I think adding a PR for this is fine. Feel free to send one.

Thanks

brendandburns avatar Oct 27 '22 21:10 brendandburns

Hi, I figured out a way to integrate this project in bazel.

In WORKSPACE file:

K8s_GENRULE_BUILD = """
package(default_visibility = ["//visibility:public"])
genrule(
    name = "kubernetes_genrule",
    srcs = glob(["**"]),
    outs = ["libkubernetes.so"],
    cmd = "ls external/kubernetes/kubernetes && cmake external/kubernetes/kubernetes && make && cp libkubernetes.so $(@D) && ls external/kubernetes/kubernetes"
)
cc_library(
    name = "kubernetes",
    srcs = ["libkubernetes.so"],
    copts=["-Iexternal/kubernetes/kubernetes"]

)
"""
git_repository(
    name = "kubernetes",
    build_file_content = K8s_GENRULE_BUILD,
    commit = "f5f12a807432824963bbea380cdf4d9ba412e00e",
    remote = "https://github.com/kubernetes-client/c.git",
    verbose = 1,
)

In the BUILD file, where you need to include this library:

package(default_visibility = ["//visibility:public"])
cc_library(
    name = "nvg_k8s_event_agent",
    srcs = ["sample.c"],
    hdrs = [
        "sample.h",
    ],
    deps = [
        "@kubernetes",
    ],
)

sample.c contents:

#include "kubernetes/config/kube_config.h"
#include "kubernetes/api/CoreV1API.h"
#include "kubernetes/model/core_v1_event.h"
#include "kubernetes/model/v1_object_reference.h"

int main(){return 0;}

So, closing this issue. Thanks

joyanta55 avatar Nov 09 '22 16:11 joyanta55

@joyantamishu Great ! So would you like to create a PR to commit your guide to the docs directory? I think it's very helpful for developers using bazel.

ityuhui avatar Nov 11 '22 02:11 ityuhui

Sure, sounds like a great idea. I will create the PR, and send it for your reviews.

joyanta55 avatar Nov 14 '22 14:11 joyanta55