Skip to content

Commit d83ca5a

Browse files
authored
Merge pull request #65 from ityuhui/yh-websocket-exec
[Websocket] Support exec
2 parents 8eb6593 + 5d5e323 commit d83ca5a

File tree

20 files changed

+645
-12
lines changed

20 files changed

+645
-12
lines changed

.github/workflows/build.yml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ jobs:
1616
run: |
1717
sudo apt-get update
1818
sudo apt-get install -y libssl-dev libcurl4-openssl-dev uncrustify libyaml-dev
19+
- name: Prepare libwebsockets
20+
run: |
21+
git clone https://libwebsockets.org/repo/libwebsockets --depth 1 --branch v4.2-stable
22+
cd libwebsockets
23+
mkdir build
24+
cd build
25+
cmake ..
26+
make
27+
sudo make install
1928
- name: Build client library
2029
run: |
2130
cd kubernetes
@@ -35,8 +44,10 @@ jobs:
3544
cd examples/
3645
make
3746
- name: Create k8s kind cluster
38-
uses: helm/kind-action@v1.1.0
47+
uses: helm/kind-action@v1.2.0
3948
- name: Test examples
4049
run: |
50+
kubectl cluster-info --context kind-chart-testing
51+
kubectl describe node
4152
cd examples/
42-
LD_LIBRARY_PATH=$GITHUB_WORKSPACE/kubernetes/build/ make test
53+
LD_LIBRARY_PATH=$GITHUB_WORKSPACE/kubernetes/build/:/usr/local/lib make test

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ CLIENT_REPO_ROOT=${PWD}/c
1616
# Install pre-requisites
1717
sudo apt-get install libssl-dev libcurl4-openssl-dev uncrustify libyaml-dev
1818

19+
# Build pre-requisite: libwebsockets
20+
git clone https://libwebsockets.org/repo/libwebsockets --depth 1 --branch v4.2-stable
21+
cd libwebsockets
22+
mkdir build
23+
cd build
24+
cmake -DCMAKE_INSTALL_PREFIX=/usr/local/lib ..
25+
make
26+
sudo make install
27+
1928
# Move into the Kubernetes directory
2029
cd ${CLIENT_REPO_ROOT}/kubernetes
2130

examples/Makefile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ all:
88
cd auth_provider; make
99
cd watch_list_pod; make
1010
cd multi_thread; make
11+
cd exec_pod; make
1112

1213
clean:
1314
cd create_pod; make clean
@@ -19,10 +20,13 @@ clean:
1920
cd auth_provider; make clean
2021
cd watch_list_pod; make clean
2122
cd multi_thread; make clean
23+
cd exec_pod; make clean
2224

2325
test:
2426
cd create_pod; make test; sleep 10
2527
cd list_pod; make test
2628
cd delete_pod; make test
2729
cd generic; make test
28-
cd multi_thread; make test
30+
cd multi_thread; make test; sleep 10
31+
kubectl describe po test-pod-8
32+
cd exec_pod; make test

examples/auth_provider/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config
2-
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml
2+
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml -lwebsockets -L/usr/local/lib
33
CFLAGS:=-g
44

55
all:

examples/create_pod/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config
2-
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml
2+
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml -lwebsockets -L/usr/local/lib
33
CFLAGS:=-g
44
BIN:=create_pod_bin
55

examples/delete_pod/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config
2-
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml
2+
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml -lwebsockets -L/usr/local/lib
33
CFLAGS:=-g
44
BIN:=delete_pod_bin
55

examples/exec_pod/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exec_pod_bin

examples/exec_pod/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config -I../../kubernetes/websocket
2+
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml -lwebsockets -L/usr/local/lib
3+
CFLAGS:=-g
4+
BIN:=exec_pod_bin
5+
6+
all:
7+
gcc main.c $(CFLAGS) $(INCLUDE) $(LIBS) -o $(BIN)
8+
clean:
9+
rm ./$(BIN)
10+
test:
11+
./$(BIN) only-run-case-1

examples/exec_pod/main.c

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <kube_config.h>
2+
#include <malloc.h>
3+
#include <stdio.h>
4+
#include <kube_exec.h>
5+
6+
/*
7+
* An example of call back function:
8+
*/
9+
void my_exec_data_callback(void **p_data_received, long *p_data_received_len)
10+
{
11+
printf("%s: Received %ld bytes:\n%s", __func__, *p_data_received_len, (char *) (*p_data_received));
12+
}
13+
14+
int main(int argc, char *argv[])
15+
{
16+
char *base_path = NULL;
17+
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 */
20+
if (rc != 0) {
21+
printf("Cannot load kubernetes configuration.\n");
22+
return -1;
23+
}
24+
25+
/* The log level mask for libwebsokets */
26+
int wsc_log_mask = LLL_ERR | LLL_WARN;
27+
/*
28+
* If you need a detail log:*/
29+
//int wsc_log_mask = LLL_ERR | LLL_WARN | LLL_USER | LLL_NOTICE;
30+
31+
wsclient_t *wsc = wsclient_create(base_path, ssl_config, api_keys, wsc_log_mask);
32+
if (!wsc) {
33+
fprintf(stderr, "Cannot create a websocket client.\n");
34+
return -1;
35+
}
36+
37+
/*
38+
* Case #1
39+
* Normal mode (tty = 0)
40+
*/
41+
kube_exec(wsc, /* websocket client */
42+
"default", /* namespace */
43+
"test-pod-8", /* pod name */
44+
"my-container", /* container name, NULL means the default container in the pod */
45+
1, /* stdin */
46+
1, /* stdout */
47+
0, /* tty */
48+
"ls /" /* command */
49+
);
50+
51+
printf("Received %ld bytes:\n%s\n", wsc->data_received_len, (char *) (wsc->data_received));
52+
53+
if (argc > 1) { // skip the case #2 in the automation test
54+
goto end;
55+
}
56+
57+
/*
58+
* Case #2
59+
* Interactive and tty mode (tty = 1)
60+
*/
61+
/* Use the default callback function provided by libkubernetes */
62+
wsc->data_callback_func = NULL;
63+
/* If you want to use your call back function:
64+
* wsc->data_callback_func = my_callback_function;
65+
*/
66+
kube_exec(wsc, /* websocket client */
67+
"default", /* namespace */
68+
"test-pod-8", /* pod name */
69+
NULL, /* container name, NULL means the default container in the pod */
70+
1, /* stdin */
71+
1, /* stdout */
72+
1, /* tty */
73+
"bash" /* command */
74+
);
75+
76+
end:
77+
/* Clean up */
78+
wsclient_free(wsc);
79+
free_client_config(base_path, ssl_config, api_keys);
80+
base_path = NULL;
81+
ssl_config = NULL;
82+
api_keys = NULL;
83+
84+
return 0;
85+
}

examples/exec_provider/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config
2-
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml
2+
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml -lwebsockets -L/usr/local/lib
33
CFLAGS:=-g
44

55
all: my_exec_provider_bin list_pod_by_exec_provider_bin

0 commit comments

Comments
 (0)