Skip to content

Commit 18c9414

Browse files
committed
Add an example to list events
1 parent 1102730 commit 18c9414

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

examples/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ all:
1111
cd exec_pod; make
1212
cd list_secret; make
1313
cd configmap; make
14+
cd list_event; make
1415

1516
clean:
1617
cd create_pod; make clean
@@ -25,6 +26,7 @@ clean:
2526
cd exec_pod; make clean
2627
cd list_secret; make clean
2728
cd configmap; make clean
29+
cd list_event; make clean
2830

2931
test:
3032
cd create_pod; make test;
@@ -34,6 +36,7 @@ test:
3436
kubectl wait --for=delete pod/test-pod-6 -n default --timeout=120s
3537
cd list_secret; make test
3638
cd configmap; make test
39+
cd list_event; make test
3740
cd generic; make test
3841
cd multi_thread; make test;
3942
kubectl wait --for=condition=ready pod/test-pod-8 -n default --timeout=60s
@@ -48,6 +51,7 @@ memcheck:
4851
kubectl wait --for=delete pod/test-pod-6 -n default --timeout=120s
4952
cd list_secret; make memcheck
5053
cd configmap; make memcheck
54+
cd list_event; make test
5155
cd generic; make memcheck
5256
cd multi_thread; make memcheck;
5357
kubectl wait --for=condition=ready pod/test-pod-8 -n default --timeout=60s

examples/list_event/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
list_event_bin

examples/list_event/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
INCLUDE:=-I../../kubernetes/
2+
LIBS:=-L../../kubernetes/build -lyaml -lwebsockets -lkubernetes -L/usr/local/lib
3+
CFLAGS:=-g
4+
BIN:=list_event_bin
5+
6+
.PHONY : all clean test memcheck
7+
all:
8+
gcc main.c $(CFLAGS) $(INCLUDE) $(LIBS) -o $(BIN)
9+
10+
test:
11+
./$(BIN)
12+
13+
memcheck:
14+
valgrind --tool=memcheck --leak-check=full ./$(BIN)
15+
16+
clean:
17+
rm ./$(BIN)

examples/list_event/main.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <config/kube_config.h>
2+
#include <api/CoreV1API.h>
3+
#include <stdio.h>
4+
5+
#include <config/kube_config.h>
6+
#include <api/CoreV1API.h>
7+
#include <stdio.h>
8+
9+
void list_event(apiClient_t * apiClient)
10+
{
11+
core_v1_event_list_t *event_list = CoreV1API_listNamespacedEvent(apiClient, "default", /*namespace */
12+
"true", /* pretty */
13+
0, /* allowWatchBookmarks */
14+
NULL, /* continue */
15+
NULL, /* fieldSelector */
16+
NULL, /* labelSelector */
17+
0, /* limit */
18+
NULL, /* resourceVersion */
19+
NULL, /* resourceVersionMatch */
20+
0, /* timeoutSeconds */
21+
0 /* watch */
22+
);
23+
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
24+
if (event_list) {
25+
if (event_list->items) {
26+
listEntry_t *listEntry = NULL;
27+
core_v1_event_t *event = NULL;
28+
list_ForEach(listEntry, event_list->items) {
29+
event = listEntry->data;
30+
if (event) {
31+
if (event->type) {
32+
printf("Event Type: %s\n", event->type);
33+
}
34+
if (event->message) {
35+
printf("Event Message: %s\n", event->message);
36+
}
37+
}
38+
}
39+
} else {
40+
fprintf(stderr, "There are no events in event list.\n");
41+
}
42+
core_v1_event_list_free(event_list);
43+
event_list = NULL;
44+
} else {
45+
fprintf(stderr, "Cannot get event list.\n");
46+
}
47+
}
48+
49+
int main()
50+
{
51+
char *basePath = NULL;
52+
sslConfig_t *sslConfig = NULL;
53+
list_t *apiKeys = NULL;
54+
int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */
55+
if (rc != 0) {
56+
fprintf(stderr, "Cannot load kubernetes configuration.\n");
57+
return -1;
58+
}
59+
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
60+
if (!apiClient) {
61+
fprintf(stderr, "Cannot create a kubernetes client.\n");
62+
return -1;
63+
}
64+
65+
list_event(apiClient);
66+
67+
apiClient_free(apiClient);
68+
apiClient = NULL;
69+
free_client_config(basePath, sslConfig, apiKeys);
70+
basePath = NULL;
71+
sslConfig = NULL;
72+
apiKeys = NULL;
73+
apiClient_unsetupGlobalEnv();
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)