From cf3729b6115e836af8a2636c46f14e34e2f01bf6 Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Thu, 19 Mar 2020 09:56:19 +0800 Subject: [PATCH] Add the examples to demo listing/creating pods --- examples/create_pod/Makefile | 8 +++ examples/create_pod/main.c | 118 +++++++++++++++++++++++++++++++++++ examples/list_pod/Makefile | 8 +++ examples/list_pod/main.c | 111 ++++++++++++++++++++++++++++++++ 4 files changed, 245 insertions(+) create mode 100644 examples/create_pod/Makefile create mode 100644 examples/create_pod/main.c create mode 100644 examples/list_pod/Makefile create mode 100644 examples/list_pod/main.c diff --git a/examples/create_pod/Makefile b/examples/create_pod/Makefile new file mode 100644 index 0000000..6387394 --- /dev/null +++ b/examples/create_pod/Makefile @@ -0,0 +1,8 @@ +INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api +LIBS:=-L../../kubernetes/build -lkubernetes -lcurl -lpthread -lssl -lz +CFLAGS:=-g + +all: + gcc main.c $(CFLAGS) $(INCLUDE) $(LIBS) -o create_pod_bin +clean: + rm ./create_pod_bin diff --git a/examples/create_pod/main.c b/examples/create_pod/main.c new file mode 100644 index 0000000..a9bc9c1 --- /dev/null +++ b/examples/create_pod/main.c @@ -0,0 +1,118 @@ +#include +#include +#include +#include +#include + +#define K8S_APISERVER_BASEPATH "https://9.111.254.254:6443" +#define K8S_TOKEN_FILE_IN_CLUSTER "/var/run/secrets/kubernetes.io/serviceaccount/token" +#define K8S_TOKEN_BUF_SIZE 1024 +#define K8S_AUTH_KEY "Authorization" +#define K8S_AUTH_VALUE_TEMPLATE "Bearer %s" + +apiClient_t *g_k8sAPIConnector; + +void create_a_pod(apiClient_t *apiClient) +{ + char *namespace = "default"; + + v1_pod_t * podinfo = calloc(1, sizeof(v1_pod_t)); + podinfo->api_version = strdup("v1"); + podinfo->kind = strdup("Pod"); + podinfo->spec = calloc(1, sizeof(v1_pod_spec_t)); + + podinfo->metadata = calloc(1, sizeof(v1_object_meta_t)); + podinfo->metadata->name = strdup("test-pod-6"); + + list_t *containerlist = list_create(); + v1_container_t *con = calloc(1, sizeof(v1_container_t)); + con->name = strdup("my-container"); + con->image = strdup("ubuntu:16.04"); + con->image_pull_policy = strdup("IfNotPresent"); + + list_t *commandlist = list_create(); + char *cmd = strdup("sleep"); + list_addElement(commandlist, cmd); + con->command = commandlist; + + list_t *arglist = list_create(); + char *arg1 = strdup("3600"); + list_addElement(arglist, arg1); + con->args = arglist; + + list_addElement(containerlist, con); + podinfo->spec->containers = containerlist; + + v1_pod_t* apod = CoreV1API_createNamespacedPod(apiClient, namespace, podinfo, NULL, NULL, NULL); + printf("code=%ld\n", apiClient->response_code); + + v1_pod_free(apod); +} + +int +loadK8sConfigInCluster(char *token, int token_buf_size) +{ + static char fname[] = "loadK8sConfigInCluster()"; + + FILE *fp; + fp = fopen(K8S_TOKEN_FILE_IN_CLUSTER, "r"); + + if (fp == NULL) { + if (errno == ENOENT) { + printf("\ +%s: The file %s does not exist.", + fname, K8S_TOKEN_FILE_IN_CLUSTER); + return (-1); + } else { + printf("\ +%s: Failed to open file %s (%m).", + fname, K8S_TOKEN_FILE_IN_CLUSTER); + return (-1); + } + } + + while (fgets(token, token_buf_size, fp) != NULL) { + ; + } + + printf("%s\n", token); + + fclose(fp); + + return 0; +} + +int +init_k8s_connector(const char *token_out_of_cluster) +{ + list_t *apiKeys; + apiKeys = list_create(); + + char *keyToken = strdup(K8S_AUTH_KEY); + char token_in_cluster[K8S_TOKEN_BUF_SIZE]; + memset(token_in_cluster, 0, sizeof(token_in_cluster)); + + //loadK8sConfigInCluster(token_in_cluster, K8S_TOKEN_BUF_SIZE); // in cluster + + char valueToken[K8S_TOKEN_BUF_SIZE]; + memset(valueToken, 0, sizeof(valueToken)); + //sprintf(valueToken, K8S_AUTH_VALUE_TEMPLATE, token); // in cluster + + sprintf(valueToken, K8S_AUTH_VALUE_TEMPLATE, token_out_of_cluster); // out of cluster + + keyValuePair_t *keyPairToken = keyValuePair_create(keyToken, valueToken); + list_addElement(apiKeys, keyPairToken); + + g_k8sAPIConnector = apiClient_create_with_base_path(K8S_APISERVER_BASEPATH, NULL, apiKeys); +} + +int main(int argc, char *argv[]) +{ + + init_k8s_connector(argv[1]); + + create_a_pod(g_k8sAPIConnector); + + apiClient_free(g_k8sAPIConnector); +} + diff --git a/examples/list_pod/Makefile b/examples/list_pod/Makefile new file mode 100644 index 0000000..477785f --- /dev/null +++ b/examples/list_pod/Makefile @@ -0,0 +1,8 @@ +INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api +LIBS:=-L../../kubernetes/build -lkubernetes -lcurl -lpthread -lssl -lz +CFLAGS:=-g + +all: + gcc main.c $(CFLAGS) $(INCLUDE) $(LIBS) -o list_pod_bin +clean: + rm ./list_pod_bin diff --git a/examples/list_pod/main.c b/examples/list_pod/main.c new file mode 100644 index 0000000..490f910 --- /dev/null +++ b/examples/list_pod/main.c @@ -0,0 +1,111 @@ +#include +#include +#include +#include +#include + +#define K8S_APISERVER_BASEPATH "https://9.111.254.254:6443" +#define K8S_TOKEN_FILE_IN_CLUSTER "/var/run/secrets/kubernetes.io/serviceaccount/token" +#define K8S_TOKEN_BUF_SIZE 1024 +#define K8S_AUTH_KEY "Authorization" +#define K8S_AUTH_VALUE_TEMPLATE "Bearer %s" + +apiClient_t *g_k8sAPIConnector; + +void list_pod(apiClient_t *apiClient) +{ + v1_pod_list_t *pod_list = NULL; + pod_list = CoreV1API_listNamespacedPod( apiClient , + "default", /*namespace*/ + NULL, /* pretty */ + 0, /* allowWatchBookmarks */ + NULL, /* continue */ + NULL, /* fieldSelector */ + NULL, /* labelSelector */ + 0, /* limit */ + NULL, /* resourceVersion */ + 0, /* timeoutSeconds */ + 0 /* watch */ + ); + printf("return code=%ld\n", apiClient->response_code); + if(pod_list){ + printf("Get pod list.\n"); + listEntry_t *listEntry = NULL; + v1_pod_t *pod = NULL; + list_ForEach(listEntry, pod_list->items){ + pod = listEntry->data; + printf("pod name=%s\n", pod->metadata->name); + } + + }else{ + printf("Cannot list any pod.\n"); + } +} + +int +loadK8sConfigInCluster(char *token, int token_buf_size) +{ + static char fname[] = "loadK8sConfigInCluster()"; + + FILE *fp; + fp = fopen(K8S_TOKEN_FILE_IN_CLUSTER, "r"); + + if (fp == NULL) { + if (errno == ENOENT) { + printf("\ +%s: The file %s does not exist.", +fname, K8S_TOKEN_FILE_IN_CLUSTER); + return (-1); + } else { + printf("\ +%s: Failed to open file %s (%m).", +fname, K8S_TOKEN_FILE_IN_CLUSTER); + return (-1); + } + } + + while (fgets(token, token_buf_size, fp) != NULL) { + ; + } + + printf("%s\n", token); + + fclose(fp); + + return 0; +} + +int +init_k8s_connector(const char *token_out_of_cluster) +{ + list_t *apiKeys; + apiKeys = list_create(); + + char *keyToken = strdup(K8S_AUTH_KEY); + char token_in_cluster[K8S_TOKEN_BUF_SIZE]; + memset(token_in_cluster, 0, sizeof(token_in_cluster)); + + //loadK8sConfigInCluster(token_in_cluster, K8S_TOKEN_BUF_SIZE); // in cluster + + char valueToken[K8S_TOKEN_BUF_SIZE]; + memset(valueToken, 0, sizeof(valueToken)); + //sprintf(valueToken, K8S_AUTH_VALUE_TEMPLATE, token); // in cluster + + sprintf(valueToken, K8S_AUTH_VALUE_TEMPLATE, token_out_of_cluster); // out of cluster + + keyValuePair_t *keyPairToken = keyValuePair_create(keyToken, valueToken); + list_addElement(apiKeys, keyPairToken); + + g_k8sAPIConnector = apiClient_create_with_base_path(K8S_APISERVER_BASEPATH, NULL, apiKeys); +} + +int main(int argc, char *argv[]) +{ + + init_k8s_connector(argv[1]); + + list_pod(g_k8sAPIConnector); + + apiClient_free(g_k8sAPIConnector); +} +