Skip to content

Commit 2887dd5

Browse files
committed
* Fix memory leak when kubeconfig is invalid
* Add a test for this case
1 parent 5911cc0 commit 2887dd5

File tree

5 files changed

+93
-8
lines changed

5 files changed

+93
-8
lines changed

examples/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ clean:
3131
test:
3232
cd create_pod; make test;
3333
kubectl wait --for=condition=ready --all pod -n default --timeout=60s
34+
cd list_pod_with_invalid_kubeconfig; make test
3435
cd list_pod; make test
3536
cd delete_pod; make test
3637
kubectl wait --for=delete pod/test-pod-6 -n default --timeout=120s
@@ -46,6 +47,7 @@ test:
4647
memcheck:
4748
cd create_pod; make memcheck;
4849
kubectl wait --for=condition=ready --all pod -n default --timeout=60s
50+
cd list_pod_with_invalid_kubeconfig; make memcheck
4951
cd list_pod; make memcheck
5052
cd delete_pod; make memcheck
5153
kubectl wait --for=delete pod/test-pod-6 -n default --timeout=120s
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
list_pod_with_invalid_kubeconfig
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_pod_with_invalid_kubeconfig
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)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <config/kube_config.h>
2+
#include <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+
0, /* allowWatchBookmarks */
11+
NULL, /* continue */
12+
NULL, /* fieldSelector */
13+
NULL, /* labelSelector */
14+
0, /* limit */
15+
NULL, /* resourceVersion */
16+
NULL, /* resourceVersionMatch */
17+
0, /* timeoutSeconds */
18+
0 /* watch */
19+
);
20+
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
21+
if (pod_list) {
22+
printf("Get pod list:\n");
23+
listEntry_t *listEntry = NULL;
24+
v1_pod_t *pod = NULL;
25+
list_ForEach(listEntry, pod_list->items) {
26+
pod = listEntry->data;
27+
printf("\tThe pod name: %s\n", pod->metadata->name);
28+
}
29+
v1_pod_list_free(pod_list);
30+
pod_list = NULL;
31+
} else {
32+
printf("Cannot get any pod.\n");
33+
}
34+
}
35+
36+
int main()
37+
{
38+
char *basePath = NULL;
39+
sslConfig_t *sslConfig = NULL;
40+
list_t *apiKeys = NULL;
41+
int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, "non-existent-file");
42+
if (rc != 0) {
43+
printf("Cannot load kubernetes configuration.\n");
44+
/* Return 0 to avoid Github/Action check failures.
45+
You should return a non-zero value in a production environment. */
46+
return 0;
47+
}
48+
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
49+
if (!apiClient) {
50+
printf("Cannot create a kubernetes client.\n");
51+
/* Return 0 to avoid Github/Action check failures.
52+
You should return a non-zero value in a production environment. */
53+
return 0;
54+
}
55+
56+
list_pod(apiClient);
57+
58+
apiClient_free(apiClient);
59+
apiClient = NULL;
60+
free_client_config(basePath, sslConfig, apiKeys);
61+
basePath = NULL;
62+
sslConfig = NULL;
63+
apiKeys = NULL;
64+
apiClient_unsetupGlobalEnv();
65+
66+
return 0;
67+
}

kubernetes/config/kube_config_yaml.c

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,14 +425,6 @@ int kubeyaml_load_kubeconfig(kubeconfig_t * kubeconfig)
425425
{
426426
static char fname[] = "kubeyaml_load_kubeconfig()";
427427

428-
yaml_parser_t parser;
429-
yaml_document_t document;
430-
431-
int done = 0;
432-
433-
/* Create the Parser object. */
434-
yaml_parser_initialize(&parser);
435-
436428
/* Set a file input. */
437429
FILE *input = NULL;
438430
if (kubeconfig->fileName) {
@@ -446,8 +438,14 @@ int kubeyaml_load_kubeconfig(kubeconfig_t * kubeconfig)
446438
return -1;
447439
}
448440

441+
yaml_parser_t parser;
442+
yaml_document_t document;
443+
444+
/* Create the Parser object. */
445+
yaml_parser_initialize(&parser);
449446
yaml_parser_set_input_file(&parser, input);
450447

448+
int done = 0;
451449
while (!done) {
452450

453451
if (!yaml_parser_load(&parser, &document)) {

0 commit comments

Comments
 (0)