|
| 1 | +#include <config/kube_config.h> |
| 2 | +#include <api/CoreV1API.h> |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +#define ENV_KUBECONFIG "KUBECONFIG" |
| 6 | +#ifndef _WIN32 |
| 7 | +#define ENV_HOME "HOME" |
| 8 | +#else |
| 9 | +#define ENV_HOME "USERPROFILE" |
| 10 | +#endif |
| 11 | + |
| 12 | +#define KUBE_CONFIG_DEFAULT_LOCATION "%s/.kube/config" |
| 13 | + |
| 14 | +static char *getWorkingConfigFile(const char *configFileNamePassedIn) |
| 15 | +{ |
| 16 | + char *configFileName = NULL; |
| 17 | + const char *kubeconfig_env = NULL; |
| 18 | + const char *homedir_env = NULL; |
| 19 | + |
| 20 | + if (configFileNamePassedIn) { |
| 21 | + configFileName = strdup(configFileNamePassedIn); |
| 22 | + } else { |
| 23 | +#if defined(HAVE_SECURE_GETENV) |
| 24 | + kubeconfig_env = secure_getenv(ENV_KUBECONFIG); |
| 25 | +#elif defined(HAVE_GETENV) |
| 26 | + kubeconfig_env = getenv(ENV_KUBECONFIG); |
| 27 | +#endif |
| 28 | + if (kubeconfig_env) { |
| 29 | + configFileName = strdup(kubeconfig_env); |
| 30 | + } else { |
| 31 | +#if defined(HAVE_SECURE_GETENV) |
| 32 | + homedir_env = secure_getenv(ENV_HOME); |
| 33 | +#elif defined(HAVE_GETENV) |
| 34 | + homedir_env = getenv(ENV_HOME); |
| 35 | +#endif |
| 36 | + if (homedir_env) { |
| 37 | + int configFileNameSize = strlen(homedir_env) + strlen(KUBE_CONFIG_DEFAULT_LOCATION) + 1; |
| 38 | + configFileName = calloc(configFileNameSize, sizeof(char)); |
| 39 | + if (configFileName) { |
| 40 | + snprintf(configFileName, configFileNameSize, KUBE_CONFIG_DEFAULT_LOCATION, homedir_env); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return configFileName; |
| 47 | +} |
| 48 | + |
| 49 | +static char *getFileData(const char *filePath) |
| 50 | +{ |
| 51 | + char *data = NULL; |
| 52 | + char *kubeConfigFile = getWorkingConfigFile(filePath); |
| 53 | + if (kubeConfigFile) { |
| 54 | + FILE *kubeFile = fopen(kubeConfigFile, "r"); |
| 55 | + if (kubeFile) { |
| 56 | + fseek(kubeFile, 0, SEEK_END); |
| 57 | + long fsize = ftell(kubeFile); |
| 58 | + fseek(kubeFile, 0, SEEK_SET); |
| 59 | + |
| 60 | + data = calloc(1, fsize + 1); |
| 61 | + if (data) { |
| 62 | + fread(data, 1, fsize, kubeFile); |
| 63 | + } |
| 64 | + |
| 65 | + fclose(kubeFile); |
| 66 | + } |
| 67 | + free(kubeConfigFile); |
| 68 | + } |
| 69 | + |
| 70 | + return data; |
| 71 | +} |
| 72 | + |
| 73 | +void list_pod(apiClient_t * apiClient) |
| 74 | +{ |
| 75 | + v1_pod_list_t *pod_list = NULL; |
| 76 | + pod_list = CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */ |
| 77 | + NULL, /* pretty */ |
| 78 | + NULL, /* allowWatchBookmarks */ |
| 79 | + NULL, /* continue */ |
| 80 | + NULL, /* fieldSelector */ |
| 81 | + NULL, /* labelSelector */ |
| 82 | + NULL, /* limit */ |
| 83 | + NULL, /* resourceVersion */ |
| 84 | + NULL, /* resourceVersionMatch */ |
| 85 | + NULL, /* sendInitialEvents */ |
| 86 | + NULL, /* timeoutSeconds */ |
| 87 | + NULL /* watch */ |
| 88 | + ); |
| 89 | + printf("The return code of HTTP request=%ld\n", apiClient->response_code); |
| 90 | + if (pod_list) { |
| 91 | + printf("Get pod list:\n"); |
| 92 | + listEntry_t *listEntry = NULL; |
| 93 | + v1_pod_t *pod = NULL; |
| 94 | + list_ForEach(listEntry, pod_list->items) { |
| 95 | + pod = listEntry->data; |
| 96 | + printf("\tThe pod name: %s\n", pod->metadata->name); |
| 97 | + } |
| 98 | + v1_pod_list_free(pod_list); |
| 99 | + pod_list = NULL; |
| 100 | + } else { |
| 101 | + printf("Cannot get any pod.\n"); |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +int main() |
| 106 | +{ |
| 107 | + char *basePath = NULL; |
| 108 | + sslConfig_t *sslConfig = NULL; |
| 109 | + list_t *apiKeys = NULL; |
| 110 | + |
| 111 | + char *dataBuffer = getFileData(NULL); /* NULL means loading configuration from $HOME/.kube/config */ |
| 112 | + if (dataBuffer == NULL) { |
| 113 | + printf("Cannot get kubernetes configuration from file.\n"); |
| 114 | + return -1; |
| 115 | + } |
| 116 | + |
| 117 | + int rc = load_kube_config_buffer(&basePath, &sslConfig, &apiKeys, dataBuffer); |
| 118 | + if (rc != 0) { |
| 119 | + printf("Cannot load kubernetes configuration.\n"); |
| 120 | + return -1; |
| 121 | + } |
| 122 | + apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys); |
| 123 | + if (!apiClient) { |
| 124 | + printf("Cannot create a kubernetes client.\n"); |
| 125 | + return -1; |
| 126 | + } |
| 127 | + |
| 128 | + list_pod(apiClient); |
| 129 | + |
| 130 | + apiClient_free(apiClient); |
| 131 | + apiClient = NULL; |
| 132 | + free_client_config(basePath, sslConfig, apiKeys); |
| 133 | + basePath = NULL; |
| 134 | + sslConfig = NULL; |
| 135 | + apiKeys = NULL; |
| 136 | + apiClient_unsetupGlobalEnv(); |
| 137 | + free(dataBuffer); |
| 138 | + dataBuffer = NULL; |
| 139 | + |
| 140 | + return 0; |
| 141 | +} |
0 commit comments