Skip to content

Commit 1d79e0f

Browse files
committed
add example for load_kube_config_buffer
1 parent 70f05e5 commit 1d79e0f

File tree

7 files changed

+281
-0
lines changed

7 files changed

+281
-0
lines changed

examples/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ all:
22
cd create_pod; make
33
cd list_pod_with_invalid_kubeconfig; make
44
cd list_pod; make
5+
cd list_pod_buffer; make
56
cd list_pod_incluster; make
67
cd delete_pod; make
78
cd exec_provider; make
@@ -18,6 +19,7 @@ clean:
1819
cd create_pod; make clean
1920
cd list_pod_with_invalid_kubeconfig; make clean
2021
cd list_pod; make clean
22+
cd list_pod_buffer; make clean
2123
cd list_pod_incluster; make clean
2224
cd delete_pod; make clean
2325
cd exec_provider; make clean
@@ -35,6 +37,7 @@ test:
3537
kubectl wait --for=condition=ready --all pod -n default --timeout=60s
3638
cd list_pod_with_invalid_kubeconfig; make test
3739
cd list_pod; make test
40+
cd list_pod_buffer; make test
3841
cd delete_pod; make test
3942
kubectl wait --for=delete pod/test-pod-6 -n default --timeout=120s
4043
cd list_secret; make test
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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+
8+
void list_pod(apiClient_t * apiClient)
9+
{
10+
v1_pod_list_t *pod_list = NULL;
11+
pod_list = CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */
12+
NULL, /* pretty */
13+
NULL, /* allowWatchBookmarks */
14+
NULL, /* continue */
15+
NULL, /* fieldSelector */
16+
NULL, /* labelSelector */
17+
NULL, /* limit */
18+
NULL, /* resourceVersion */
19+
NULL, /* resourceVersionMatch */
20+
NULL, /* sendInitialEvents */
21+
NULL, /* timeoutSeconds */
22+
NULL /* watch */
23+
);
24+
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
25+
if (pod_list) {
26+
printf("Get pod list:\n");
27+
listEntry_t *listEntry = NULL;
28+
v1_pod_t *pod = NULL;
29+
list_ForEach(listEntry, pod_list->items) {
30+
pod = listEntry->data;
31+
printf("\tThe pod name: %s\n", pod->metadata->name);
32+
}
33+
v1_pod_list_free(pod_list);
34+
pod_list = NULL;
35+
} else {
36+
printf("Cannot get any pod.\n");
37+
}
38+
}
39+
40+
int main(int argc, char *argv[])
41+
{
42+
int rc = 0;
43+
44+
char *baseName = NULL;
45+
sslConfig_t *sslConfig = NULL;
46+
list_t *apiKeys = NULL;
47+
apiClient_t *k8sApiClient = NULL;
48+
49+
rc = load_kube_config(&baseName, &sslConfig, &apiKeys, "./config_with_exec_provider");
50+
if (0 == rc) {
51+
k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys);
52+
} else {
53+
printf("Cannot load kubernetes configuration.\n");
54+
return -1;
55+
}
56+
57+
if (k8sApiClient) {
58+
list_pod(k8sApiClient);
59+
}
60+
61+
free_client_config(baseName, sslConfig, apiKeys);
62+
baseName = NULL;
63+
sslConfig = NULL;
64+
apiKeys = NULL;
65+
66+
apiClient_free(k8sApiClient);
67+
k8sApiClient = NULL;
68+
apiClient_unsetupGlobalEnv();
69+
70+
return rc;
71+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#define _GNU_SOURCE
2+
#include <stdlib.h>
3+
#include <stdio.h>
4+
#include <string.h>
5+
6+
#define ENV_EXEC_CLIENT_CERTIFICATE_DATA "exec_client_certificate_data"
7+
#define ENV_EXEC_CLIENT_PRIVATE_KEY "exec_client_private_key"
8+
9+
char token_template[] = "\
10+
{\
11+
\"apiVersion\": \"client.authentication.k8s.io/v1beta1\",\
12+
\"kind\": \"ExecCredential\",\
13+
\"status\": {\
14+
\"token\": \"%s\"\
15+
}\
16+
}";
17+
18+
char certificate_template[] = "\
19+
{\
20+
\"apiVersion\": \"client.authentication.k8s.io/v1beta1\",\
21+
\"kind\": \"ExecCredential\",\
22+
\"status\": {\
23+
\"clientCertificateData\": \"%s\",\
24+
\"clientKeyData\": \"%s\"\
25+
}\
26+
}";
27+
28+
int main(int argc, char *argv[])
29+
{
30+
const char *client_certificate_data = secure_getenv(ENV_EXEC_CLIENT_CERTIFICATE_DATA);
31+
const char *client_private_key = secure_getenv(ENV_EXEC_CLIENT_PRIVATE_KEY);
32+
33+
if ((4 == argc) && argv[3]) {
34+
// token is passed by command line argument
35+
printf(token_template, argv[3]);
36+
} else if ((client_certificate_data) && strlen(client_certificate_data) > 0 && (client_private_key) && strlen(client_private_key) > 0) {
37+
// client certificate and private key are passed by environment variables
38+
printf(certificate_template, client_certificate_data, client_private_key);
39+
} else {
40+
printf("Cannot get authentication data\n");
41+
}
42+
43+
return 0;
44+
}

examples/list_pod_buffer/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
list_pod_buffer_bin
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
find_package(${pkgName} CONFIG REQUIRED COMPONENTS ${pkgName})
2+
3+
add_executable(list_pod_buffer main.c)
4+
target_link_libraries(list_pod_buffer PRIVATE ${pkgName}::${pkgName})

examples/list_pod_buffer/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
INCLUDE:=-I../../kubernetes/ -I/usr/local/include/kubernetes/
2+
LIBS:=-L../../kubernetes/build -lyaml -lwebsockets -lkubernetes -L/usr/local/lib
3+
CFLAGS:=-g
4+
BIN:=list_pod_buffer_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_pod_buffer/main.c

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
#include <config/kube_config.h>
2+
#include <api/CoreV1API.h>
3+
#include <stdio.h>
4+
5+
#define ENV_KUBECONFIG "KUBECONFIG"
6+
#ifndef _WIN32
7+
#define ENV_HOME "HOME"
8+
#else
9+
#define ENV_HOME "USERPROFILE"
10+
#endif
11+
12+
#define KUBE_CONFIG_DEFAULT_LOCATION "%s/.kube/config"
13+
14+
static char *getWorkingConfigFile(const char *configFileNamePassedIn)
15+
{
16+
char *configFileName = NULL;
17+
const char *kubeconfig_env = NULL;
18+
const char *homedir_env = NULL;
19+
20+
if (configFileNamePassedIn) {
21+
configFileName = strdup(configFileNamePassedIn);
22+
} else {
23+
#if defined(HAVE_SECURE_GETENV)
24+
kubeconfig_env = secure_getenv(ENV_KUBECONFIG);
25+
#elif defined(HAVE_GETENV)
26+
kubeconfig_env = getenv(ENV_KUBECONFIG);
27+
#endif
28+
if (kubeconfig_env) {
29+
configFileName = strdup(kubeconfig_env);
30+
} else {
31+
#if defined(HAVE_SECURE_GETENV)
32+
homedir_env = secure_getenv(ENV_HOME);
33+
#elif defined(HAVE_GETENV)
34+
homedir_env = getenv(ENV_HOME);
35+
#endif
36+
if (homedir_env) {
37+
int configFileNameSize = strlen(homedir_env) + strlen(KUBE_CONFIG_DEFAULT_LOCATION) + 1;
38+
configFileName = calloc(configFileNameSize, sizeof(char));
39+
if (configFileName) {
40+
snprintf(configFileName, configFileNameSize, KUBE_CONFIG_DEFAULT_LOCATION, homedir_env);
41+
}
42+
}
43+
}
44+
}
45+
46+
return configFileName;
47+
}
48+
49+
static char *getFileData(const char *filePath)
50+
{
51+
char *data = NULL;
52+
char *kubeConfigFile = getWorkingConfigFile(filePath);
53+
if (kubeConfigFile) {
54+
FILE *kubeFile = fopen(kubeConfigFile, "r");
55+
if (kubeFile) {
56+
fseek(kubeFile, 0, SEEK_END);
57+
long fsize = ftell(kubeFile);
58+
fseek(kubeFile, 0, SEEK_SET);
59+
60+
data = calloc(1, fsize + 1);
61+
if (data) {
62+
fread(data, 1, fsize, kubeFile);
63+
}
64+
65+
fclose(kubeFile);
66+
}
67+
free(kubeConfigFile);
68+
}
69+
70+
return data;
71+
}
72+
73+
void list_pod(apiClient_t * apiClient)
74+
{
75+
v1_pod_list_t *pod_list = NULL;
76+
pod_list = CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */
77+
NULL, /* pretty */
78+
NULL, /* allowWatchBookmarks */
79+
NULL, /* continue */
80+
NULL, /* fieldSelector */
81+
NULL, /* labelSelector */
82+
NULL, /* limit */
83+
NULL, /* resourceVersion */
84+
NULL, /* resourceVersionMatch */
85+
NULL, /* sendInitialEvents */
86+
NULL, /* timeoutSeconds */
87+
NULL /* watch */
88+
);
89+
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
90+
if (pod_list) {
91+
printf("Get pod list:\n");
92+
listEntry_t *listEntry = NULL;
93+
v1_pod_t *pod = NULL;
94+
list_ForEach(listEntry, pod_list->items) {
95+
pod = listEntry->data;
96+
printf("\tThe pod name: %s\n", pod->metadata->name);
97+
}
98+
v1_pod_list_free(pod_list);
99+
pod_list = NULL;
100+
} else {
101+
printf("Cannot get any pod.\n");
102+
}
103+
}
104+
105+
int main()
106+
{
107+
char *basePath = NULL;
108+
sslConfig_t *sslConfig = NULL;
109+
list_t *apiKeys = NULL;
110+
111+
char *dataBuffer = getFileData(NULL); /* NULL means loading configuration from $HOME/.kube/config */
112+
if (dataBuffer == NULL) {
113+
printf("Cannot get kubernetes configuration from file.\n");
114+
return -1;
115+
}
116+
117+
int rc = load_kube_config_buffer(&basePath, &sslConfig, &apiKeys, dataBuffer);
118+
if (rc != 0) {
119+
printf("Cannot load kubernetes configuration.\n");
120+
return -1;
121+
}
122+
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
123+
if (!apiClient) {
124+
printf("Cannot create a kubernetes client.\n");
125+
return -1;
126+
}
127+
128+
list_pod(apiClient);
129+
130+
apiClient_free(apiClient);
131+
apiClient = NULL;
132+
free_client_config(basePath, sslConfig, apiKeys);
133+
basePath = NULL;
134+
sslConfig = NULL;
135+
apiKeys = NULL;
136+
apiClient_unsetupGlobalEnv();
137+
free(dataBuffer);
138+
dataBuffer = NULL;
139+
140+
return 0;
141+
}

0 commit comments

Comments
 (0)