Skip to content

Commit d737dd8

Browse files
committed
Add examples that uses bazel generated c kubernetes library
1 parent 66c5243 commit d737dd8

File tree

3 files changed

+191
-2
lines changed

3 files changed

+191
-2
lines changed

examples/BUILD

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
load("@rules_cc//cc:defs.bzl", "cc_binary")
2-
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake")
3-
load("@rules_foreign_cc//foreign_cc:defs.bzl", "make")
2+
load("@rules_foreign_cc//foreign_cc:defs.bzl", "cmake", "make")
3+
44
cmake(
55
name = "kube_c",
66
build_args = [
@@ -11,3 +11,25 @@ cmake(
1111
lib_source = "//:kubernetes",
1212
out_shared_libs = ["libkubernetes.so"],
1313
)
14+
15+
# create lib files (.so or .a)
16+
# Example: bazel build list_pod_lib
17+
cc_library(
18+
name = "list_pod_lib",
19+
srcs = ["bazel/list_pod.c"],
20+
deps = [":kube_c"],
21+
)
22+
23+
# create and run executable file.
24+
# Example: bazel run list_pod
25+
cc_binary(
26+
name = "list_pod",
27+
srcs = ["bazel/list_pod.c"],
28+
deps = [":kube_c"],
29+
)
30+
31+
cc_binary(
32+
name = "create_pod",
33+
srcs = ["bazel/create_pod.c"],
34+
deps = [":kube_c"],
35+
)

examples/bazel/create_pod.c

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
2+
#include "kubernetes/config/kube_config.h"
3+
#include "kubernetes/include/apiClient.h"
4+
#include "kubernetes/api/CoreV1API.h"
5+
#include <malloc.h>
6+
#include <stdio.h>
7+
#include <errno.h>
8+
9+
void create_a_pod(apiClient_t * apiClient)
10+
{
11+
char *namespace = "default";
12+
13+
v1_pod_t *podinfo = calloc(1, sizeof(v1_pod_t));
14+
podinfo->api_version = strdup("v1");
15+
podinfo->kind = strdup("Pod");
16+
podinfo->spec = calloc(1, sizeof(v1_pod_spec_t));
17+
18+
podinfo->metadata = calloc(1, sizeof(v1_object_meta_t));
19+
/* set pod name */
20+
podinfo->metadata->name = strdup("test-pod-6");
21+
22+
/* set containers for pod */
23+
list_t *containerlist = list_createList();
24+
v1_container_t *con = calloc(1, sizeof(v1_container_t));
25+
con->name = strdup("my-container");
26+
con->image = strdup("ubuntu:latest");
27+
con->image_pull_policy = strdup("IfNotPresent");
28+
29+
/* set command for container */
30+
list_t *commandlist = list_createList();
31+
char *cmd = strdup("sleep");
32+
list_addElement(commandlist, cmd);
33+
con->command = commandlist;
34+
35+
list_t *arglist = list_createList();
36+
char *arg1 = strdup("3600");
37+
list_addElement(arglist, arg1);
38+
con->args = arglist;
39+
40+
/* set volume mounts for container */
41+
list_t *volumemounts = list_createList();
42+
v1_volume_mount_t *volmou = calloc(1, sizeof(v1_volume_mount_t));
43+
volmou->mount_path = strdup("/test");
44+
volmou->name = strdup("test");
45+
list_addElement(volumemounts, volmou);
46+
con->volume_mounts = volumemounts;
47+
48+
list_addElement(containerlist, con);
49+
podinfo->spec->containers = containerlist;
50+
51+
/* set volumes for pod */
52+
list_t *volumelist = list_createList();
53+
v1_volume_t *volume = calloc(1, sizeof(v1_volume_t));
54+
volume->name = strdup("test");
55+
56+
v1_host_path_volume_source_t *hostPath = calloc(1, sizeof(v1_host_path_volume_source_t));
57+
hostPath->path = strdup("/test");
58+
volume->host_path = hostPath;
59+
60+
list_addElement(volumelist, volume);
61+
podinfo->spec->volumes = volumelist;
62+
63+
/* call API in libkubernetes to create pod */
64+
v1_pod_t *apod = CoreV1API_createNamespacedPod(apiClient, namespace, podinfo, NULL, NULL, NULL, NULL);
65+
printf("code=%ld\n", apiClient->response_code);
66+
67+
v1_pod_free(apod);
68+
v1_pod_free(podinfo);
69+
}
70+
71+
int main(int argc, char *argv[])
72+
{
73+
74+
int rc = 0;
75+
76+
char *baseName = NULL;
77+
sslConfig_t *sslConfig = NULL;
78+
list_t *apiKeys = NULL;
79+
apiClient_t *k8sApiClient = NULL;
80+
81+
rc = load_kube_config(&baseName, &sslConfig, &apiKeys, NULL);
82+
if (0 == rc) {
83+
k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys);
84+
} else {
85+
printf("Cannot load kubernetes configuration.\n");
86+
return -1;
87+
}
88+
89+
if (k8sApiClient) {
90+
create_a_pod(k8sApiClient);
91+
}
92+
93+
free_client_config(baseName, sslConfig, apiKeys);
94+
baseName = NULL;
95+
sslConfig = NULL;
96+
apiKeys = NULL;
97+
98+
apiClient_free(k8sApiClient);
99+
k8sApiClient = NULL;
100+
apiClient_unsetupGlobalEnv();
101+
102+
return 0;
103+
}

examples/bazel/list_pod.c

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#include "kubernetes/config/kube_config.h"
2+
#include "kubernetes/api/CoreV1API.h"
3+
#include "stdio.h"
4+
5+
void list_pod(apiClient_t * apiClient)
6+
{
7+
v1_pod_list_t *pod_list = NULL;
8+
pod_list = CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */
9+
NULL, /* pretty */
10+
NULL, /* allowWatchBookmarks */
11+
NULL, /* continue */
12+
NULL, /* fieldSelector */
13+
NULL, /* labelSelector */
14+
NULL, /* limit */
15+
NULL, /* resourceVersion */
16+
NULL, /* resourceVersionMatch */
17+
NULL, /* sendInitialEvents */
18+
NULL, /* timeoutSeconds */
19+
NULL /* watch */
20+
);
21+
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
22+
if (pod_list) {
23+
printf("Get pod list:\n");
24+
listEntry_t *listEntry = NULL;
25+
v1_pod_t *pod = NULL;
26+
list_ForEach(listEntry, pod_list->items) {
27+
pod = listEntry->data;
28+
printf("\tThe pod name: %s\n", pod->metadata->name);
29+
}
30+
v1_pod_list_free(pod_list);
31+
pod_list = NULL;
32+
} else {
33+
printf("Cannot get any pod.\n");
34+
}
35+
}
36+
37+
int main()
38+
{
39+
char *basePath = NULL;
40+
sslConfig_t *sslConfig = NULL;
41+
list_t *apiKeys = NULL;
42+
int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */
43+
if (rc != 0) {
44+
printf("Cannot load kubernetes configuration.\n");
45+
return -1;
46+
}
47+
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
48+
if (!apiClient) {
49+
printf("Cannot create a kubernetes client.\n");
50+
return -1;
51+
}
52+
53+
list_pod(apiClient);
54+
55+
apiClient_free(apiClient);
56+
apiClient = NULL;
57+
free_client_config(basePath, sslConfig, apiKeys);
58+
basePath = NULL;
59+
sslConfig = NULL;
60+
apiKeys = NULL;
61+
apiClient_unsetupGlobalEnv();
62+
63+
return 0;
64+
}

0 commit comments

Comments
 (0)