From d737dd8f84be23d4c136a876d43cda04d6532665 Mon Sep 17 00:00:00 2001 From: joyanta55 Date: Tue, 19 Nov 2024 11:46:57 -0500 Subject: [PATCH 1/2] Add examples that uses bazel generated c kubernetes library --- examples/BUILD | 26 ++++++++- examples/bazel/create_pod.c | 103 ++++++++++++++++++++++++++++++++++++ examples/bazel/list_pod.c | 64 ++++++++++++++++++++++ 3 files changed, 191 insertions(+), 2 deletions(-) create mode 100644 examples/bazel/create_pod.c create mode 100644 examples/bazel/list_pod.c diff --git a/examples/BUILD b/examples/BUILD index fb7a362e..e72abc4f 100644 --- a/examples/BUILD +++ b/examples/BUILD @@ -1,6 +1,6 @@ load("@rules_cc//cc:defs.bzl", "cc_binary") -load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake") -load("@rules_foreign_cc//foreign_cc:defs.bzl", "make") +load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake", "make") + cmake( name = "kube_c", build_args = [ @@ -11,3 +11,25 @@ cmake( lib_source = "//:kubernetes", out_shared_libs = ["libkubernetes.so"], ) + +# create lib files (.so or .a) +# Example: bazel build list_pod_lib +cc_library( + name = "list_pod_lib", + srcs = ["bazel/list_pod.c"], + deps = [":kube_c"], +) + +# create and run executable file. +# Example: bazel run list_pod +cc_binary( + name = "list_pod", + srcs = ["bazel/list_pod.c"], + deps = [":kube_c"], +) + +cc_binary( + name = "create_pod", + srcs = ["bazel/create_pod.c"], + deps = [":kube_c"], +) diff --git a/examples/bazel/create_pod.c b/examples/bazel/create_pod.c new file mode 100644 index 00000000..1124c614 --- /dev/null +++ b/examples/bazel/create_pod.c @@ -0,0 +1,103 @@ + +#include "kubernetes/config/kube_config.h" +#include "kubernetes/include/apiClient.h" +#include "kubernetes/api/CoreV1API.h" +#include +#include +#include + +void create_a_pod(apiClient_t * apiClient) +{ + char *namespace = "default"; + + v1_pod_t *podinfo = calloc(1, sizeof(v1_pod_t)); + podinfo->api_version = strdup("v1"); + podinfo->kind = strdup("Pod"); + podinfo->spec = calloc(1, sizeof(v1_pod_spec_t)); + + podinfo->metadata = calloc(1, sizeof(v1_object_meta_t)); + /* set pod name */ + podinfo->metadata->name = strdup("test-pod-6"); + + /* set containers for pod */ + list_t *containerlist = list_createList(); + v1_container_t *con = calloc(1, sizeof(v1_container_t)); + con->name = strdup("my-container"); + con->image = strdup("ubuntu:latest"); + con->image_pull_policy = strdup("IfNotPresent"); + + /* set command for container */ + list_t *commandlist = list_createList(); + char *cmd = strdup("sleep"); + list_addElement(commandlist, cmd); + con->command = commandlist; + + list_t *arglist = list_createList(); + char *arg1 = strdup("3600"); + list_addElement(arglist, arg1); + con->args = arglist; + + /* set volume mounts for container */ + list_t *volumemounts = list_createList(); + v1_volume_mount_t *volmou = calloc(1, sizeof(v1_volume_mount_t)); + volmou->mount_path = strdup("/test"); + volmou->name = strdup("test"); + list_addElement(volumemounts, volmou); + con->volume_mounts = volumemounts; + + list_addElement(containerlist, con); + podinfo->spec->containers = containerlist; + + /* set volumes for pod */ + list_t *volumelist = list_createList(); + v1_volume_t *volume = calloc(1, sizeof(v1_volume_t)); + volume->name = strdup("test"); + + v1_host_path_volume_source_t *hostPath = calloc(1, sizeof(v1_host_path_volume_source_t)); + hostPath->path = strdup("/test"); + volume->host_path = hostPath; + + list_addElement(volumelist, volume); + podinfo->spec->volumes = volumelist; + + /* call API in libkubernetes to create pod */ + v1_pod_t *apod = CoreV1API_createNamespacedPod(apiClient, namespace, podinfo, NULL, NULL, NULL, NULL); + printf("code=%ld\n", apiClient->response_code); + + v1_pod_free(apod); + v1_pod_free(podinfo); +} + +int main(int argc, char *argv[]) +{ + + int rc = 0; + + char *baseName = NULL; + sslConfig_t *sslConfig = NULL; + list_t *apiKeys = NULL; + apiClient_t *k8sApiClient = NULL; + + rc = load_kube_config(&baseName, &sslConfig, &apiKeys, NULL); + if (0 == rc) { + k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys); + } else { + printf("Cannot load kubernetes configuration.\n"); + return -1; + } + + if (k8sApiClient) { + create_a_pod(k8sApiClient); + } + + free_client_config(baseName, sslConfig, apiKeys); + baseName = NULL; + sslConfig = NULL; + apiKeys = NULL; + + apiClient_free(k8sApiClient); + k8sApiClient = NULL; + apiClient_unsetupGlobalEnv(); + + return 0; +} \ No newline at end of file diff --git a/examples/bazel/list_pod.c b/examples/bazel/list_pod.c new file mode 100644 index 00000000..d74e6919 --- /dev/null +++ b/examples/bazel/list_pod.c @@ -0,0 +1,64 @@ +#include "kubernetes/config/kube_config.h" +#include "kubernetes/api/CoreV1API.h" +#include "stdio.h" + +void list_pod(apiClient_t * apiClient) +{ + v1_pod_list_t *pod_list = NULL; + pod_list = CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */ + NULL, /* pretty */ + NULL, /* allowWatchBookmarks */ + NULL, /* continue */ + NULL, /* fieldSelector */ + NULL, /* labelSelector */ + NULL, /* limit */ + NULL, /* resourceVersion */ + NULL, /* resourceVersionMatch */ + NULL, /* sendInitialEvents */ + NULL, /* timeoutSeconds */ + NULL /* watch */ + ); + printf("The return code of HTTP request=%ld\n", apiClient->response_code); + if (pod_list) { + printf("Get pod list:\n"); + listEntry_t *listEntry = NULL; + v1_pod_t *pod = NULL; + list_ForEach(listEntry, pod_list->items) { + pod = listEntry->data; + printf("\tThe pod name: %s\n", pod->metadata->name); + } + v1_pod_list_free(pod_list); + pod_list = NULL; + } else { + printf("Cannot get any pod.\n"); + } +} + +int main() +{ + char *basePath = NULL; + sslConfig_t *sslConfig = NULL; + list_t *apiKeys = NULL; + int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */ + if (rc != 0) { + printf("Cannot load kubernetes configuration.\n"); + return -1; + } + apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys); + if (!apiClient) { + printf("Cannot create a kubernetes client.\n"); + return -1; + } + + list_pod(apiClient); + + apiClient_free(apiClient); + apiClient = NULL; + free_client_config(basePath, sslConfig, apiKeys); + basePath = NULL; + sslConfig = NULL; + apiKeys = NULL; + apiClient_unsetupGlobalEnv(); + + return 0; +} \ No newline at end of file From 501550cf7ca936ed1d0efe07d32e17de72cd4ccd Mon Sep 17 00:00:00 2001 From: joyanta55 Date: Tue, 19 Nov 2024 15:45:28 -0500 Subject: [PATCH 2/2] Use existing examples --- .github/workflows/build.yml | 3 +- BUILD | 26 +++++++-- examples/BUILD | 37 ++++--------- examples/bazel/create_pod.c | 103 ------------------------------------ examples/bazel/list_pod.c | 64 ---------------------- 5 files changed, 35 insertions(+), 198 deletions(-) delete mode 100644 examples/bazel/create_pod.c delete mode 100644 examples/bazel/list_pod.c diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 7ac268e6..cb0bdcb1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -66,6 +66,5 @@ jobs: curl -LO "https://github.com/bazelbuild/bazelisk/releases/latest/download/bazelisk-linux-amd64" chmod +x bazelisk-linux-amd64 sudo mv bazelisk-linux-amd64 /usr/local/bin/bazel - cd examples/ - bazel build kube_c + bazel build kube_c_library \ No newline at end of file diff --git a/BUILD b/BUILD index 0a43b720..7e04f3cd 100644 --- a/BUILD +++ b/BUILD @@ -56,13 +56,33 @@ # deps = [":kube_c"], # ) -# Make sure you install the pre-requisites (libyaml,libwebsocket etc.) beforehand. A working example can be found here +# Make sure you install the pre-requisites (libyaml,libwebsocket etc.) beforehand. A working example can be found in the example directory. -# https://github.com/joyanta55/kubernetes_c_bazel/tree/main +load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library") +load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake", "make") -# In summary, the below filegroup allows to import kubernetes C client (i.e. lib_source = "@kubernetes_c_client//:kubernetes"), use cmake or make bazel rule provided by rules_foreign_cc (https://github.com/bazel-contrib/rules_foreign_cc) to build and use. filegroup( name = "kubernetes", srcs = glob(["kubernetes/**"]), visibility = ["//visibility:public"], ) + +cmake( + name = "kube_c", + build_args = [ + "--verbose", + "--", # <- Pass remaining options to the native tool. + "-j 1", + ], + lib_source = ":kubernetes", + out_shared_libs = ["libkubernetes.so"], +) + +# create lib files (.so or .a) +cc_library( + name = "kube_c_library", + hdrs = [":kubernetes"], # Explicitly add headers if needed + strip_include_prefix = "kubernetes", + visibility = ["//visibility:public"], + deps = [":kube_c"], +) diff --git a/examples/BUILD b/examples/BUILD index e72abc4f..e4145005 100644 --- a/examples/BUILD +++ b/examples/BUILD @@ -1,35 +1,20 @@ -load("@rules_cc//cc:defs.bzl", "cc_binary") -load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake", "make") - -cmake( - name = "kube_c", - build_args = [ - "--verbose", - "--", # <- Pass remaining options to the native tool. - "-j 1", - ], - lib_source = "//:kubernetes", - out_shared_libs = ["libkubernetes.so"], -) +# Example of using bazel rules on the existing examples. -# create lib files (.so or .a) -# Example: bazel build list_pod_lib -cc_library( - name = "list_pod_lib", - srcs = ["bazel/list_pod.c"], - deps = [":kube_c"], -) +load("@rules_cc//cc:defs.bzl", "cc_binary") # create and run executable file. -# Example: bazel run list_pod +# Run: bazel run //examples:list_pod cc_binary( name = "list_pod", - srcs = ["bazel/list_pod.c"], - deps = [":kube_c"], + srcs = [ + "list_pod/main.c", + ], + deps = ["//:kube_c_library"], #imported from BUILD file of root directory ) +# Run: bazel run //examples:list_event cc_binary( - name = "create_pod", - srcs = ["bazel/create_pod.c"], - deps = [":kube_c"], + name = "list_event", + srcs = ["list_event/main.c"], + deps = ["//:kube_c_library"], #imported from BUILD file of root directory ) diff --git a/examples/bazel/create_pod.c b/examples/bazel/create_pod.c deleted file mode 100644 index 1124c614..00000000 --- a/examples/bazel/create_pod.c +++ /dev/null @@ -1,103 +0,0 @@ - -#include "kubernetes/config/kube_config.h" -#include "kubernetes/include/apiClient.h" -#include "kubernetes/api/CoreV1API.h" -#include -#include -#include - -void create_a_pod(apiClient_t * apiClient) -{ - char *namespace = "default"; - - v1_pod_t *podinfo = calloc(1, sizeof(v1_pod_t)); - podinfo->api_version = strdup("v1"); - podinfo->kind = strdup("Pod"); - podinfo->spec = calloc(1, sizeof(v1_pod_spec_t)); - - podinfo->metadata = calloc(1, sizeof(v1_object_meta_t)); - /* set pod name */ - podinfo->metadata->name = strdup("test-pod-6"); - - /* set containers for pod */ - list_t *containerlist = list_createList(); - v1_container_t *con = calloc(1, sizeof(v1_container_t)); - con->name = strdup("my-container"); - con->image = strdup("ubuntu:latest"); - con->image_pull_policy = strdup("IfNotPresent"); - - /* set command for container */ - list_t *commandlist = list_createList(); - char *cmd = strdup("sleep"); - list_addElement(commandlist, cmd); - con->command = commandlist; - - list_t *arglist = list_createList(); - char *arg1 = strdup("3600"); - list_addElement(arglist, arg1); - con->args = arglist; - - /* set volume mounts for container */ - list_t *volumemounts = list_createList(); - v1_volume_mount_t *volmou = calloc(1, sizeof(v1_volume_mount_t)); - volmou->mount_path = strdup("/test"); - volmou->name = strdup("test"); - list_addElement(volumemounts, volmou); - con->volume_mounts = volumemounts; - - list_addElement(containerlist, con); - podinfo->spec->containers = containerlist; - - /* set volumes for pod */ - list_t *volumelist = list_createList(); - v1_volume_t *volume = calloc(1, sizeof(v1_volume_t)); - volume->name = strdup("test"); - - v1_host_path_volume_source_t *hostPath = calloc(1, sizeof(v1_host_path_volume_source_t)); - hostPath->path = strdup("/test"); - volume->host_path = hostPath; - - list_addElement(volumelist, volume); - podinfo->spec->volumes = volumelist; - - /* call API in libkubernetes to create pod */ - v1_pod_t *apod = CoreV1API_createNamespacedPod(apiClient, namespace, podinfo, NULL, NULL, NULL, NULL); - printf("code=%ld\n", apiClient->response_code); - - v1_pod_free(apod); - v1_pod_free(podinfo); -} - -int main(int argc, char *argv[]) -{ - - int rc = 0; - - char *baseName = NULL; - sslConfig_t *sslConfig = NULL; - list_t *apiKeys = NULL; - apiClient_t *k8sApiClient = NULL; - - rc = load_kube_config(&baseName, &sslConfig, &apiKeys, NULL); - if (0 == rc) { - k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys); - } else { - printf("Cannot load kubernetes configuration.\n"); - return -1; - } - - if (k8sApiClient) { - create_a_pod(k8sApiClient); - } - - free_client_config(baseName, sslConfig, apiKeys); - baseName = NULL; - sslConfig = NULL; - apiKeys = NULL; - - apiClient_free(k8sApiClient); - k8sApiClient = NULL; - apiClient_unsetupGlobalEnv(); - - return 0; -} \ No newline at end of file diff --git a/examples/bazel/list_pod.c b/examples/bazel/list_pod.c deleted file mode 100644 index d74e6919..00000000 --- a/examples/bazel/list_pod.c +++ /dev/null @@ -1,64 +0,0 @@ -#include "kubernetes/config/kube_config.h" -#include "kubernetes/api/CoreV1API.h" -#include "stdio.h" - -void list_pod(apiClient_t * apiClient) -{ - v1_pod_list_t *pod_list = NULL; - pod_list = CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */ - NULL, /* pretty */ - NULL, /* allowWatchBookmarks */ - NULL, /* continue */ - NULL, /* fieldSelector */ - NULL, /* labelSelector */ - NULL, /* limit */ - NULL, /* resourceVersion */ - NULL, /* resourceVersionMatch */ - NULL, /* sendInitialEvents */ - NULL, /* timeoutSeconds */ - NULL /* watch */ - ); - printf("The return code of HTTP request=%ld\n", apiClient->response_code); - if (pod_list) { - printf("Get pod list:\n"); - listEntry_t *listEntry = NULL; - v1_pod_t *pod = NULL; - list_ForEach(listEntry, pod_list->items) { - pod = listEntry->data; - printf("\tThe pod name: %s\n", pod->metadata->name); - } - v1_pod_list_free(pod_list); - pod_list = NULL; - } else { - printf("Cannot get any pod.\n"); - } -} - -int main() -{ - char *basePath = NULL; - sslConfig_t *sslConfig = NULL; - list_t *apiKeys = NULL; - int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */ - if (rc != 0) { - printf("Cannot load kubernetes configuration.\n"); - return -1; - } - apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys); - if (!apiClient) { - printf("Cannot create a kubernetes client.\n"); - return -1; - } - - list_pod(apiClient); - - apiClient_free(apiClient); - apiClient = NULL; - free_client_config(basePath, sslConfig, apiKeys); - basePath = NULL; - sslConfig = NULL; - apiKeys = NULL; - apiClient_unsetupGlobalEnv(); - - return 0; -} \ No newline at end of file