Skip to content

Commit

Permalink
[Watch] Support watch
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuhui committed Sep 9, 2020
1 parent c57bb44 commit e983e6e
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ all:
cd exec_provider; make
cd generic; make
cd auth_provider; make
cd watch_list_pod; make

clean:
cd create_pod; make clean
Expand All @@ -13,3 +14,4 @@ clean:
cd exec_provider; make clean
cd generic; make clean
cd auth_provider; make clean
cd watch_list_pod; make clean
1 change: 1 addition & 0 deletions examples/watch_list_pod/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
watch_list_pod_bin
8 changes: 8 additions & 0 deletions examples/watch_list_pod/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config -I../../kubernetes/watch
LIBS:=-L../../kubernetes/build -lkubernetes -lcurl -lyaml -lpthread -lssl -lz
CFLAGS:=-g

all:
gcc main.c $(CFLAGS) $(INCLUDE) $(LIBS) -o watch_list_pod_bin
clean:
rm ./watch_list_pod_bin
110 changes: 110 additions & 0 deletions examples/watch_list_pod/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
#include <kube_config.h>
#include <apiClient.h>
#include <CoreV1API.h>
#include <malloc.h>
#include <stdio.h>
#include <errno.h>
#include <watch_util.h>

#define WATCH_EVENT_KEY_TYPE "type"
#define WATCH_EVENT_KEY_OBJECT "object"

void on_pod_event_comes(const char *event_string)
{
static char fname[] = "process_one_watch_event()";

if (!event_string) {
return;
}
printf("\nwatch event raw string:\n%s\n\n", event_string);

cJSON *event_json_obj = cJSON_Parse(event_string);
if (!event_json_obj) {
fprintf(stderr, "%s: Cannot create JSON from string.[%s].\n", fname, cJSON_GetErrorPtr());
goto end;
}

cJSON *json_value_type = cJSON_GetObjectItem(event_json_obj, WATCH_EVENT_KEY_TYPE);
if (!json_value_type || json_value_type->type != cJSON_String) {
fprintf(stderr, "%s: Cannot get type in watch event.\n", fname);
goto end;
}
char *type = strdup(json_value_type->valuestring);
printf("type: %s\n", type);

cJSON *json_value_object = cJSON_GetObjectItem(event_json_obj, WATCH_EVENT_KEY_OBJECT);
if (!json_value_object || json_value_object->type != cJSON_Object) {
fprintf(stderr, "%s: Cannot get object in watch event.\n", fname);
goto end;
}
v1_pod_t *pod = v1_pod_parseFromJSON(json_value_object);
if (!pod) {
fprintf(stderr, "%s: Cannot get pod from watch event object.\n", fname);
goto end;
}
printf("pod:\n\tname: %s\n", pod->metadata->name);

end:
if (pod) {
v1_pod_free(pod);
pod = NULL;
}
if (type) {
free(type);
type = NULL;
}
if (event_json_obj) {
cJSON_Delete(event_json_obj);
event_json_obj = NULL;
}
}

void my_pod_watch_handler(void** pData, long* pDataLen)
{
kubernets_watch_handler(pData, pDataLen, on_pod_event_comes);
}

void watch_list_pod(apiClient_t * apiClient)
{
apiClient->watch_func = my_pod_watch_handler;

CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */
NULL, /* pretty */
0, /* allowWatchBookmarks */
NULL, /* continue */
NULL, /* fieldSelector */
NULL, /* labelSelector */
0, /* limit */
NULL, /* resourceVersion */
0, /* timeoutSeconds */
1 /* watch */
);
}

int main(int argc, char *argv[])
{
char *basePath = NULL;
sslConfig_t *sslConfig = NULL;
list_t *apiKeys = NULL;
int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */
if (rc != 0) {
printf("Cannot load kubernetes configuration.\n");
return -1;
}
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
if (!apiClient) {
printf("Cannot create a kubernetes client.\n");
return -1;
}

watch_list_pod(apiClient);

apiClient_free(apiClient);
apiClient = NULL;
free_client_config(basePath, sslConfig, apiKeys);
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;

return 0;
}
2 changes: 2 additions & 0 deletions kubernetes/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ set(SRCS
config/exec_provider.c
config/authn_plugin/authn_plugin_util.c
config/authn_plugin/authn_plugin.c
watch/watch_util.c
src/list.c
src/apiKey.c
src/apiClient.c
Expand Down Expand Up @@ -802,6 +803,7 @@ set(HDRS
config/exec_provider.h
config/authn_plugin/authn_plugin_util.h
config/authn_plugin/authn_plugin.h
watch/watch_util.h
include/apiClient.h
include/list.h
include/binary.h
Expand Down
1 change: 1 addition & 0 deletions kubernetes/include/apiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef struct apiClient_t {
long dataReceivedLen;
long response_code;
list_t *apiKeys_BearerToken;
void (*watch_func)(void **, long *);
} apiClient_t;

apiClient_t* apiClient_create();
Expand Down
8 changes: 8 additions & 0 deletions kubernetes/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ apiClient_t *apiClient_create() {
apiClient->dataReceivedLen = 0;
apiClient->response_code = 0;
apiClient->apiKeys_BearerToken = NULL;
apiClient->watch_func = NULL;

return apiClient;
}
Expand Down Expand Up @@ -51,6 +52,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
}else{
apiClient->apiKeys_BearerToken = NULL;
}
apiClient->watch_func = NULL;

return apiClient;
}
Expand All @@ -73,6 +75,8 @@ void apiClient_free(apiClient_t *apiClient) {
}
list_free(apiClient->apiKeys_BearerToken);
}
apiClient->watch_func = NULL;

free(apiClient);
curl_global_cleanup();
}
Expand Down Expand Up @@ -452,6 +456,10 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp) {
apiClient->dataReceived = (char *)realloc( apiClient->dataReceived, apiClient->dataReceivedLen + size_this_time + 1);
memcpy(apiClient->dataReceived + apiClient->dataReceivedLen, buffer, size_this_time);
apiClient->dataReceivedLen += size_this_time;
((char*)apiClient->dataReceived)[apiClient->dataReceivedLen] = '\0'; // the space size of (apiClient->dataReceived) = dataReceivedLen + 1
if (apiClient->watch_func) {
apiClient->watch_func(&apiClient->dataReceived, &apiClient->dataReceivedLen);
}
return size_this_time;
}

Expand Down
65 changes: 65 additions & 0 deletions kubernetes/watch/watch_util.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include "../include/list.h"

#define JSON_ARRAY_DELIM "\r\n"

static int wu_convert_to_json_array(list_t* json_array, const char* json_string)
{
if (!json_string || '\0' == json_string[0] || !json_array) {
return -1;
}

int rc = 0;
char* json_string_dup = strdup(json_string);

char* token = NULL;
token = strtok(json_string_dup, JSON_ARRAY_DELIM);
while (token) {
cJSON* cjson = cJSON_Parse(token);
if (cjson == NULL) {
rc = -1;
goto end;
}
list_addElement(json_array, strdup(token));
token = strtok(NULL, JSON_ARRAY_DELIM);
}

end:
if (json_string_dup) {
free(json_string_dup);
json_string_dup = NULL;
}
return rc;
}

void kubernets_watch_handler(void** pData, long* pDataLen, void (*event_hander)(const char*))
{
char* data = *(char**)pData;
//printf("dataLen=%ld, strlen(data)=%ld\n", *pDataLen, strlen(data));

list_t* watch_event_list = list_create();
if (!watch_event_list) {
fprintf(stderr, "Cannot create a list for watch events.\n");
return;
}
int rc = wu_convert_to_json_array(watch_event_list, data);
if (0 == rc) {

listEntry_t* listEntry = NULL;
list_ForEach(listEntry, watch_event_list) {
char* list_item = listEntry->data;
event_hander(list_item);
}

free(data);
*pData = NULL;
*pDataLen = 0;
}

clear_and_free_string_list(watch_event_list);
watch_event_list = NULL;
}
13 changes: 13 additions & 0 deletions kubernetes/watch/watch_util.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#ifndef _WATCH_UTIL_H
#define _WATCH_UTIL_H

#ifdef __cplusplus
extern "C" {
#endif

void kubernets_watch_handler(void** pData, long* pDataLen, void (*event_hander)(const char*));

#ifdef __cplusplus
}
#endif
#endif /* _WATCH_UTIL_H */

0 comments on commit e983e6e

Please sign in to comment.