Skip to content

Commit 1d433b7

Browse files
authored
Merge pull request #79 from ityuhui/yh-exec-incluster
[Exec] Pod exec in cluster
2 parents fb28bbf + c8899ef commit 1d433b7

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

examples/exec_pod/main.c

+14-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <malloc.h>
33
#include <stdio.h>
44
#include <kube_exec.h>
5+
#include <incluster_config.h>
56

67
/*
78
* An example of call back function:
@@ -15,8 +16,13 @@ int main(int argc, char *argv[])
1516
{
1617
char *base_path = NULL;
1718
sslConfig_t *ssl_config = NULL;
18-
list_t *api_keys = NULL;
19-
int rc = load_kube_config(&base_path, &ssl_config, &api_keys, NULL); /* NULL means loading configuration from $HOME/.kube/config */
19+
list_t *api_tokens = NULL;
20+
21+
int rc = load_kube_config(&base_path, &ssl_config, &api_tokens, NULL); /* NULL means loading configuration from $HOME/.kube/config */
22+
/*
23+
* If you need exec pod in cluster:
24+
* int rc = load_incluster_config(&base_path, &ssl_config, &api_tokens);
25+
*/
2026
if (rc != 0) {
2127
printf("Cannot load kubernetes configuration.\n");
2228
return -1;
@@ -25,10 +31,11 @@ int main(int argc, char *argv[])
2531
/* The log level mask for libwebsokets */
2632
int wsc_log_mask = LLL_ERR | LLL_WARN;
2733
/*
28-
* If you need a detail log:*/
29-
//int wsc_log_mask = LLL_ERR | LLL_WARN | LLL_USER | LLL_NOTICE;
34+
* If you need a detailed log:
35+
* int wsc_log_mask = LLL_ERR | LLL_WARN | LLL_USER | LLL_NOTICE;
36+
*/
3037

31-
wsclient_t *wsc = wsclient_create(base_path, ssl_config, api_keys, wsc_log_mask);
38+
wsclient_t *wsc = wsclient_create(base_path, ssl_config, api_tokens, wsc_log_mask);
3239
if (!wsc) {
3340
fprintf(stderr, "Cannot create a websocket client.\n");
3441
return -1;
@@ -76,10 +83,10 @@ int main(int argc, char *argv[])
7683
end:
7784
/* Clean up */
7885
wsclient_free(wsc);
79-
free_client_config(base_path, ssl_config, api_keys);
86+
free_client_config(base_path, ssl_config, api_tokens);
8087
base_path = NULL;
8188
ssl_config = NULL;
82-
api_keys = NULL;
89+
api_tokens = NULL;
8390

8491
return 0;
8592
}

kubernetes/websocket/wsclient.c

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "wsclient.h"
2+
#include "../config/kube_config_common.h"
23
#include <stdio.h>
34
#include <stdlib.h>
45
#include <string.h>
@@ -10,7 +11,7 @@
1011
#define TTY_STDIN_NUMBER 0
1112
#define TTY_STDOUT_NUMBER 1
1213
#define WS_PROTOCOL_DELIM "://"
13-
#define WS_BASE_PATH_DELIM_CHAR ':' /* ip:port */
14+
#define WS_BASE_PATH_DELIM_CHAR ':' /* ip:port */
1415
#define WS_BASE_PATH_DELIM_LENGTH 1
1516

1617
static struct lws_context *g_lws_context;
@@ -73,7 +74,7 @@ static int get_server_address_and_port(char **p_ws_addr, int *p_ws_port, const c
7374
return 0;
7475
}
7576

76-
wsclient_t *wsclient_create(const char *base_path, sslConfig_t * ssl_config, list_t * apiKeys, int ws_log_mask)
77+
wsclient_t *wsclient_create(const char *base_path, sslConfig_t * ssl_config, list_t * api_tokens, int ws_log_mask)
7778
{
7879
if (!base_path) {
7980
fprintf(stderr, "%s: The base path is invalid.\n", __func__);
@@ -93,11 +94,12 @@ wsclient_t *wsclient_create(const char *base_path, sslConfig_t * ssl_config, lis
9394
}
9495

9596
wsc->ssl_config = ssl_config;
97+
wsc->api_tokens = api_tokens;
9698
wsc->log_mask = ws_log_mask;
9799

98100
return wsc;
99101

100-
error:
102+
error:
101103
if (wsc) {
102104
free(wsc);
103105
wsc = NULL;
@@ -121,6 +123,7 @@ void wsclient_free(wsclient_t * wsc)
121123
wsc->data_received_len = 0;
122124
wsc->data_callback_func = NULL;
123125
wsc->ssl_config = NULL;
126+
wsc->api_tokens = NULL;
124127

125128
free(wsc);
126129
}
@@ -143,7 +146,7 @@ static void connect_client(lws_sorted_usec_list_t * sul)
143146
i.host = i.address;
144147
i.origin = i.address;
145148
i.ssl_connection = wsc->ssl_config ? LCCSCF_USE_SSL : 0;
146-
149+
147150
//i.protocol = pro;
148151
//i.local_protocol_name = "websocket-client";
149152
i.pwsi = &wsc->wsi;
@@ -175,6 +178,25 @@ static int callback_wsclient(struct lws *wsi, enum lws_callback_reasons reason,
175178
goto do_retry;
176179
break;
177180

181+
case LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER:
182+
if (!wsc->api_tokens) {
183+
return 0;
184+
}
185+
lwsl_user("%s: LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER\n", __func__);
186+
unsigned char **p = (unsigned char **) in, *end = (*p) + len;
187+
listEntry_t *listEntry = NULL;
188+
keyValuePair_t *token = NULL;
189+
list_ForEach(listEntry, wsc->api_tokens) {
190+
token = listEntry->data;
191+
if (token && token->key && token->value && 0 == strcmp(token->key, AUTH_TOKEN_KEY)) {
192+
lwsl_user("%s: token->key==%s, token->value=%s\n", __func__, token->key, (char *) token->value);
193+
if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_AUTHORIZATION, (unsigned char *) token->value, (int) strlen(token->value), p, end)) {
194+
return -1;
195+
}
196+
}
197+
}
198+
break;
199+
178200
case LWS_CALLBACK_CLIENT_ESTABLISHED:
179201
lwsl_user("%s: wsclient connection established\n", __func__);
180202
lws_callback_on_writable(wsi);
@@ -196,7 +218,7 @@ static int callback_wsclient(struct lws *wsi, enum lws_callback_reasons reason,
196218
lwsl_user("%s: The content of data received is empty.\n", __func__);
197219
return 0;
198220
}
199-
char *valid_data = (char *)in + 1 ;
221+
char *valid_data = (char *) in + 1;
200222

201223
if (wsc->data_received_len > 0 && wsc->data_received) {
202224
free(wsc->data_received);
@@ -308,8 +330,11 @@ int wsclient_run(wsclient_t * wsc, wsc_mode_t mode)
308330
info.fd_limit_per_thread = 1 + 1 + 1;
309331

310332
if (wsc->ssl_config) {
333+
lwsl_user("%s: CACertFile=%s\n", __func__, wsc->ssl_config->CACertFile);
311334
info.client_ssl_ca_filepath = wsc->ssl_config->CACertFile;
335+
lwsl_user("%s: clientKeyFile=%s\n", __func__, wsc->ssl_config->clientKeyFile);
312336
info.client_ssl_private_key_filepath = wsc->ssl_config->clientKeyFile;
337+
lwsl_user("%s: clientCertFile=%s\n", __func__, wsc->ssl_config->clientCertFile);
313338
info.client_ssl_cert_filepath = wsc->ssl_config->clientCertFile;
314339
}
315340

kubernetes/websocket/wsclient.h

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ typedef struct wsclient_t {
2929
struct lws *wsi; /* related wsi if any */
3030
uint16_t retry_count; /* count of consequetive retries */
3131
sslConfig_t *ssl_config;
32+
list_t *api_tokens;
3233
} wsclient_t;
3334

3435
wsclient_t *wsclient_create(const char *, sslConfig_t *, list_t *, int);

0 commit comments

Comments
 (0)