1
+ #include <kube_config.h>
1
2
#include <apiClient.h>
2
3
#include <CoreV1API.h>
3
4
#include <malloc.h>
4
5
#include <stdio.h>
5
6
#include <errno.h>
6
7
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
-
20
8
void list_pod (apiClient_t * apiClient )
21
9
{
22
10
v1_pod_list_t * pod_list = NULL ;
@@ -31,71 +19,48 @@ void list_pod(apiClient_t * apiClient)
31
19
0 , /* timeoutSeconds */
32
20
0 /* watch */
33
21
);
34
- printf ("return code=%ld\n" , apiClient -> response_code );
22
+ printf ("The return code of HTTP request =%ld\n" , apiClient -> response_code );
35
23
if (pod_list ) {
36
- printf ("Get pod list. \n" );
24
+ printf ("Get pod list: \n" );
37
25
listEntry_t * listEntry = NULL ;
38
26
v1_pod_t * pod = NULL ;
39
27
list_ForEach (listEntry , pod_list -> items ) {
40
28
pod = listEntry -> data ;
41
- printf ("pod name= %s\n" , pod -> metadata -> name );
29
+ printf ("\tThe pod name: %s\n" , pod -> metadata -> name );
42
30
}
43
31
} else {
44
- printf ("Cannot list any pod.\n" );
32
+ printf ("Cannot get any pod.\n" );
45
33
}
46
34
}
47
35
48
- int loadK8sConfigInCluster ( char * token , int token_buf_size )
36
+ int main ( int argc , char * argv [] )
49
37
{
50
- static char fname [] = "loadK8sConfigInCluster()" ;
38
+ int rc = 0 ;
51
39
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 ;
54
44
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 ;
63
51
}
64
52
65
- while ( fgets ( token , token_buf_size , fp ) != NULL ) {
66
- ;
53
+ if ( k8sApiClient ) {
54
+ list_pod ( k8sApiClient ) ;
67
55
}
68
56
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 ;
97
61
98
- list_pod (g_k8sAPIConnector );
62
+ apiClient_free (k8sApiClient );
63
+ k8sApiClient = NULL ;
99
64
100
- apiClient_free ( g_k8sAPIConnector ) ;
65
+ return rc ;
101
66
}
0 commit comments