Skip to content

Commit 705a605

Browse files
committed
[Configuration]Support configuration for X509 client certificate in kube config file
1 parent d9ae510 commit 705a605

File tree

12 files changed

+1254
-123
lines changed

12 files changed

+1254
-123
lines changed

code-check/code-static-check.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Usage:
2+
# sh ./code-static-check.sh ${source_dir}
3+
4+
cppcheck --enable=all $*
5+

examples/create_pod/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api
2-
LIBS:=-L../../kubernetes/build -lkubernetes -lcurl -lpthread -lssl -lz
1+
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config
2+
LIBS:=-L../../kubernetes/build -lkubernetes -lcurl -lyaml -lpthread -lssl -lz
33
CFLAGS:=-g
44

55
all:

examples/create_pod/main.c

+23-58
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1+
#include <kube_config.h>
12
#include <apiClient.h>
23
#include <CoreV1API.h>
34
#include <malloc.h>
45
#include <stdio.h>
56
#include <errno.h>
67

7-
// kubectl proxy server
8-
#define K8S_APISERVER_BASEPATH "http://localhost:8001"
9-
10-
// Alternately from within a Kubernetes cluster:
11-
// #define K8S_APISERVER_BASEPATH https://your.server.here
12-
13-
#define K8S_TOKEN_FILE_IN_CLUSTER "/var/run/secrets/kubernetes.io/serviceaccount/token"
14-
#define K8S_TOKEN_BUF_SIZE 1024
15-
#define K8S_AUTH_KEY "Authorization"
16-
#define K8S_AUTH_VALUE_TEMPLATE "Bearer %s"
17-
18-
apiClient_t *g_k8sAPIConnector;
19-
208
void create_a_pod(apiClient_t * apiClient)
219
{
2210
char *namespace = "default";
@@ -54,57 +42,34 @@ void create_a_pod(apiClient_t * apiClient)
5442
v1_pod_free(apod);
5543
}
5644

57-
int loadK8sConfigInCluster(char *token, int token_buf_size)
58-
{
59-
static char fname[] = "loadK8sConfigInCluster()";
60-
61-
FILE *fp;
62-
fp = fopen(K8S_TOKEN_FILE_IN_CLUSTER, "r");
63-
64-
if (fp == NULL) {
65-
if (errno == ENOENT) {
66-
printf("%s: The file %s does not exist.", fname, K8S_TOKEN_FILE_IN_CLUSTER);
67-
return (-1);
68-
} else {
69-
printf("%s: Failed to open file %s.", fname, K8S_TOKEN_FILE_IN_CLUSTER);
70-
return (-1);
71-
}
72-
}
73-
74-
while (fgets(token, token_buf_size, fp) != NULL) {
75-
;
76-
}
77-
78-
printf("%s\n", token);
79-
80-
fclose(fp);
81-
82-
return 0;
83-
}
84-
85-
int init_k8s_connector(const char *token_out_of_cluster)
45+
int main(int argc, char *argv[])
8646
{
87-
list_t *apiKeys;
88-
apiKeys = list_create();
89-
90-
char *keyToken = strdup(K8S_AUTH_KEY);
9147

92-
char valueToken[K8S_TOKEN_BUF_SIZE];
93-
memset(valueToken, 0, sizeof(valueToken));
48+
int rc = 0;
9449

95-
sprintf(valueToken, K8S_AUTH_VALUE_TEMPLATE, token_out_of_cluster);
50+
char *baseName = NULL;
51+
sslConfig_t *sslConfig = NULL;
52+
list_t *apiKeys = NULL;
53+
apiClient_t *k8sApiClient = NULL;
9654

97-
keyValuePair_t *keyPairToken = keyValuePair_create(keyToken, valueToken);
98-
list_addElement(apiKeys, keyPairToken);
55+
rc = load_kube_config(&baseName, &sslConfig, &apiKeys, NULL);
56+
if (0 == rc) {
57+
k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys);
58+
} else {
59+
printf("Cannot load kubernetes configuration.\n");
60+
return -1;
61+
}
9962

100-
g_k8sAPIConnector = apiClient_create_with_base_path(K8S_APISERVER_BASEPATH, NULL, apiKeys);
101-
}
63+
if (k8sApiClient) {
64+
create_a_pod(k8sApiClient);
65+
}
10266

103-
int main(int argc, char *argv[])
104-
{
105-
init_k8s_connector(argv[1]);
67+
free_client_config(baseName, sslConfig, apiKeys);
68+
baseName = NULL;
69+
sslConfig = NULL;
70+
apiKeys = NULL;
10671

107-
create_a_pod(g_k8sAPIConnector);
72+
apiClient_free(k8sApiClient);
73+
k8sApiClient = NULL;
10874

109-
apiClient_free(g_k8sAPIConnector);
11075
}

examples/list_pod/Makefile

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api
2-
LIBS:=-L../../kubernetes/build -lkubernetes -lcurl -lpthread -lssl -lz
1+
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config
2+
LIBS:=-L../../kubernetes/build -lkubernetes -lcurl -lyaml -lpthread -lssl -lz
33
CFLAGS:=-g
44

55
all:

examples/list_pod/main.c

+26-61
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
1+
#include <kube_config.h>
12
#include <apiClient.h>
23
#include <CoreV1API.h>
34
#include <malloc.h>
45
#include <stdio.h>
56
#include <errno.h>
67

7-
// kubectl proxy server
8-
#define K8S_APISERVER_BASEPATH "http://localhost:8001"
9-
10-
// Alternately from within a Kubernetes cluster:
11-
// #define K8S_APISERVER_BASEPATH https://your.server.here
12-
13-
#define K8S_TOKEN_FILE_IN_CLUSTER "/var/run/secrets/kubernetes.io/serviceaccount/token"
14-
#define K8S_TOKEN_BUF_SIZE 1024
15-
#define K8S_AUTH_KEY "Authorization"
16-
#define K8S_AUTH_VALUE_TEMPLATE "Bearer %s"
17-
18-
apiClient_t *g_k8sAPIConnector;
19-
208
void list_pod(apiClient_t * apiClient)
219
{
2210
v1_pod_list_t *pod_list = NULL;
@@ -31,71 +19,48 @@ void list_pod(apiClient_t * apiClient)
3119
0, /* timeoutSeconds */
3220
0 /* watch */
3321
);
34-
printf("return code=%ld\n", apiClient->response_code);
22+
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
3523
if (pod_list) {
36-
printf("Get pod list.\n");
24+
printf("Get pod list:\n");
3725
listEntry_t *listEntry = NULL;
3826
v1_pod_t *pod = NULL;
3927
list_ForEach(listEntry, pod_list->items) {
4028
pod = listEntry->data;
41-
printf("pod name=%s\n", pod->metadata->name);
29+
printf("\tThe pod name: %s\n", pod->metadata->name);
4230
}
4331
} else {
44-
printf("Cannot list any pod.\n");
32+
printf("Cannot get any pod.\n");
4533
}
4634
}
4735

48-
int loadK8sConfigInCluster(char *token, int token_buf_size)
36+
int main(int argc, char *argv[])
4937
{
50-
static char fname[] = "loadK8sConfigInCluster()";
38+
int rc = 0;
5139

52-
FILE *fp;
53-
fp = fopen(K8S_TOKEN_FILE_IN_CLUSTER, "r");
40+
char *baseName = NULL;
41+
sslConfig_t *sslConfig = NULL;
42+
list_t *apiKeys = NULL;
43+
apiClient_t *k8sApiClient = NULL;
5444

55-
if (fp == NULL) {
56-
if (errno == ENOENT) {
57-
printf("%s: The file %s does not exist.", fname, K8S_TOKEN_FILE_IN_CLUSTER);
58-
return (-1);
59-
} else {
60-
printf("%s: Failed to open file %s.", fname, K8S_TOKEN_FILE_IN_CLUSTER);
61-
return (-1);
62-
}
45+
rc = load_kube_config(&baseName, &sslConfig, &apiKeys, NULL);
46+
if (0 == rc) {
47+
k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys);
48+
} else {
49+
printf("Cannot load kubernetes configuration.\n");
50+
return -1;
6351
}
6452

65-
while (fgets(token, token_buf_size, fp) != NULL) {
66-
;
53+
if (k8sApiClient) {
54+
list_pod(k8sApiClient);
6755
}
6856

69-
printf("%s\n", token);
70-
71-
fclose(fp);
72-
73-
return 0;
74-
}
75-
76-
int init_k8s_connector(const char *token_out_of_cluster)
77-
{
78-
list_t *apiKeys;
79-
apiKeys = list_create();
80-
81-
char *keyToken = strdup(K8S_AUTH_KEY);
82-
83-
char valueToken[K8S_TOKEN_BUF_SIZE];
84-
memset(valueToken, 0, sizeof(valueToken));
85-
86-
sprintf(valueToken, K8S_AUTH_VALUE_TEMPLATE, token_out_of_cluster);
87-
88-
keyValuePair_t *keyPairToken = keyValuePair_create(keyToken, valueToken);
89-
list_addElement(apiKeys, keyPairToken);
90-
91-
g_k8sAPIConnector = apiClient_create_with_base_path(K8S_APISERVER_BASEPATH, NULL, apiKeys);
92-
}
93-
94-
int main(int argc, char *argv[])
95-
{
96-
init_k8s_connector(argv[1]);
57+
free_client_config(baseName, sslConfig, apiKeys);
58+
baseName = NULL;
59+
sslConfig = NULL;
60+
apiKeys = NULL;
9761

98-
list_pod(g_k8sAPIConnector);
62+
apiClient_free(k8sApiClient);
63+
k8sApiClient = NULL;
9964

100-
apiClient_free(g_k8sAPIConnector);
65+
return rc;
10166
}

kubernetes/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ set(CMAKE_VISIBILITY_INLINES_HIDDEN OFF)
99
set(CMAKE_BUILD_TYPE Debug)
1010

1111
set(pkgName "kubernetes")
12+
ADD_DEFINITIONS(-DOPENSSL)
1213

1314
find_package(CURL 7.58.0 REQUIRED)
1415
if(CURL_FOUND)
@@ -18,7 +19,11 @@ else(CURL_FOUND)
1819
message(FATAL_ERROR "Could not find the CURL library and development files.")
1920
endif()
2021

22+
2123
set(SRCS
24+
config/kube_config_model.c
25+
config/kube_config_yaml.c
26+
config/kube_config.c
2227
src/list.c
2328
src/apiKey.c
2429
src/apiClient.c
@@ -770,6 +775,9 @@ set(SRCS
770775
)
771776

772777
set(HDRS
778+
config/kube_config_model.h
779+
config/kube_config_yaml.h
780+
config/kube_config.h
773781
include/apiClient.h
774782
include/list.h
775783
include/keyValuePair.h

0 commit comments

Comments
 (0)