Skip to content

Commit 568bccb

Browse files
committed
[Example] Add an example for configmap
1 parent 74913dd commit 568bccb

File tree

5 files changed

+192
-1
lines changed

5 files changed

+192
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ git clone https://libwebsockets.org/repo/libwebsockets --depth 1 --branch v4.2-s
2121
cd libwebsockets
2222
mkdir build
2323
cd build
24-
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lib ..
24+
cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
2525
make
2626
sudo make install
2727

examples/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ all:
1010
cd multi_thread; make
1111
cd exec_pod; make
1212
cd list_secret; make
13+
cd configmap; make
1314

1415
clean:
1516
cd create_pod; make clean
@@ -23,12 +24,14 @@ clean:
2324
cd multi_thread; make clean
2425
cd exec_pod; make clean
2526
cd list_secret; make clean
27+
cd configmap; make clean
2628

2729
test:
2830
cd create_pod; make test; sleep 10
2931
cd list_pod; make test
3032
cd delete_pod; make test
3133
cd list_secret; make test
34+
cd configmap; make test
3235
cd generic; make test
3336
cd multi_thread; make test; sleep 10
3437
kubectl describe po test-pod-8

examples/configmap/.gitignore

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

examples/configmap/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config
2+
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml -lwebsockets -L/usr/local/lib
3+
CFLAGS:=-g
4+
BIN:=configmap_bin
5+
6+
all:
7+
gcc main.c $(CFLAGS) $(INCLUDE) $(LIBS) -o $(BIN)
8+
clean:
9+
rm ./$(BIN)
10+
test:
11+
./$(BIN)

examples/configmap/main.c

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#include <kube_config.h>
2+
#include <apiClient.h>
3+
#include <CoreV1API.h>
4+
#include <malloc.h>
5+
#include <stdio.h>
6+
#include <errno.h>
7+
#include <unistd.h>
8+
9+
void create_configmap(apiClient_t * apiClient, const char *name, const char *namespace_)
10+
{
11+
char *api_version = strdup("v1");
12+
char *kind = strdup("ConfigMap");
13+
14+
list_t *data = list_create();
15+
keyValuePair_t *kv = keyValuePair_create(strdup("worker1"), strdup("1"));
16+
list_addElement(data, kv);
17+
kv = keyValuePair_create(strdup("worker2"), strdup("2"));
18+
list_addElement(data, kv);
19+
20+
v1_object_meta_t *meta = v1_object_meta_create(NULL,
21+
NULL,
22+
NULL,
23+
0,
24+
NULL,
25+
NULL,
26+
NULL,
27+
0,
28+
NULL,
29+
NULL,
30+
strdup(name),
31+
strdup(namespace_),
32+
NULL,
33+
NULL,
34+
NULL,
35+
NULL);
36+
37+
v1_config_map_t *body = v1_config_map_create(api_version,
38+
NULL,
39+
data,
40+
kind,
41+
meta);
42+
43+
v1_config_map_t *ret_config_map = CoreV1API_createNamespacedConfigMap(apiClient,
44+
namespace_,
45+
body,
46+
NULL,
47+
NULL,
48+
NULL);
49+
50+
printf("%s: The return code of HTTP request=%ld\n", __func__, apiClient->response_code);
51+
52+
if (201 == apiClient->response_code) {
53+
printf("%s: Create the config map successfully.\n", __func__);
54+
} else {
55+
fprintf(stderr, "%s: Failed to create the config map.\n", __func__);
56+
return;
57+
}
58+
59+
if (ret_config_map) {
60+
v1_config_map_free(ret_config_map);
61+
ret_config_map = NULL;
62+
}
63+
if (body) {
64+
v1_config_map_free(body);
65+
body = NULL;
66+
}
67+
}
68+
69+
void list_configmap(apiClient_t * apiClient, const char *namespace_)
70+
{
71+
v1_config_map_list_t *config_map_list = CoreV1API_listNamespacedConfigMap(apiClient,
72+
namespace_, // char *namespace
73+
"true", // char *pretty
74+
0, // int allowWatchBookmarks
75+
NULL, // char * _continue
76+
NULL, // char * fieldSelector
77+
NULL, // char * labelSelector
78+
0, // int limit
79+
NULL, // char * resourceVersion
80+
0, // int timeoutSeconds
81+
0 //int watch
82+
);
83+
84+
printf("%s: The return code of HTTP request=%ld\n", __func__, apiClient->response_code);
85+
86+
if (200 == apiClient->response_code) {
87+
printf("%s: List the config maps successfully.\n", __func__);
88+
} else {
89+
fprintf(stderr, "%s: Failed to list the config maps.\n", __func__);
90+
return;
91+
}
92+
93+
if (config_map_list && config_map_list->items) {
94+
listEntry_t *config_map_list_entry = NULL;
95+
v1_config_map_t *config_map = NULL;
96+
list_ForEach(config_map_list_entry, config_map_list->items) {
97+
config_map = config_map_list_entry->data;
98+
printf("\tThe config map name: %s\n", config_map->metadata->name);
99+
100+
listEntry_t *data_entry = NULL;
101+
keyValuePair_t *pair = NULL;
102+
list_ForEach(data_entry, config_map->data) {
103+
pair = data_entry->data;
104+
printf("\tkey=%s, value=%s\n", pair->key, (char *) pair->value);
105+
}
106+
}
107+
v1_config_map_list_free(config_map_list);
108+
config_map_list = NULL;
109+
} else {
110+
fprintf(stderr, "%s: The config map list is invalid.\n", __func__);
111+
}
112+
}
113+
114+
void delete_configmap(apiClient_t * apiClient, const char *name, const char *namespace_)
115+
{
116+
v1_status_t *status = CoreV1API_deleteNamespacedConfigMap(apiClient,
117+
name, // char *name
118+
namespace_, // char *namespace
119+
NULL, // char *pretty
120+
NULL, // char *dryRun
121+
0, // int gracePeriodSeconds
122+
0, // int orphanDependents
123+
NULL, // char *propagationPolicy
124+
NULL // v1_delete_options_t *body
125+
);
126+
127+
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
128+
129+
if (200 == apiClient->response_code || 202 == apiClient->response_code) {
130+
printf("The config map is deleted successfully.\n");
131+
} else {
132+
if (status && status->message) {
133+
printf("Failed to delete the config map. The error message: %s\n", status->message);
134+
}
135+
}
136+
137+
if (status) {
138+
v1_status_free(status);
139+
status = NULL;
140+
}
141+
}
142+
143+
int main(int argc, char *argv[])
144+
{
145+
char *basePath = NULL;
146+
sslConfig_t *sslConfig = NULL;
147+
list_t *apiKeys = NULL;
148+
int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */
149+
if (rc != 0) {
150+
fprintf(stderr, "Cannot load kubernetes configuration.\n");
151+
return -1;
152+
}
153+
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
154+
if (!apiClient) {
155+
fprintf(stderr, "Cannot create a kubernetes client.\n");
156+
return -1;
157+
}
158+
159+
char *config_map_name = "cm1";
160+
char *namespace_ = "default";
161+
create_configmap(apiClient, config_map_name, namespace_);
162+
sleep(5);
163+
list_configmap(apiClient, namespace_);
164+
sleep(5);
165+
delete_configmap(apiClient, config_map_name, namespace_);
166+
167+
apiClient_free(apiClient);
168+
apiClient = NULL;
169+
free_client_config(basePath, sslConfig, apiKeys);
170+
basePath = NULL;
171+
sslConfig = NULL;
172+
apiKeys = NULL;
173+
apiClient_unsetupGlobalEnv();
174+
175+
return 0;
176+
}

0 commit comments

Comments
 (0)