|
| 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 | +} |
0 commit comments