|
| 1 | +#include <kube_config.h> |
| 2 | +#include <apiClient.h> |
| 3 | +#include <CoreV1API.h> |
| 4 | +#include <malloc.h> |
| 5 | +#include <stdio.h> |
| 6 | +#include <errno.h> |
| 7 | +#include <unistd.h> |
| 8 | + |
| 9 | +void create_configmap(apiClient_t * apiClient, const char *name, const char *namespace_) |
| 10 | +{ |
| 11 | + char *api_version = strdup("v1"); |
| 12 | + char *kind = strdup("ConfigMap"); |
| 13 | + |
| 14 | + list_t *data = list_create(); |
| 15 | + keyValuePair_t *kv = keyValuePair_create(strdup("worker1"), strdup("1")); |
| 16 | + list_addElement(data, kv); |
| 17 | + kv = keyValuePair_create(strdup("worker2"), strdup("2")); |
| 18 | + list_addElement(data, kv); |
| 19 | + |
| 20 | + v1_object_meta_t *meta = v1_object_meta_create(NULL, |
| 21 | + NULL, |
| 22 | + NULL, |
| 23 | + 0, |
| 24 | + NULL, |
| 25 | + NULL, |
| 26 | + NULL, |
| 27 | + 0, |
| 28 | + NULL, |
| 29 | + NULL, |
| 30 | + strdup(name), |
| 31 | + strdup(namespace_), |
| 32 | + NULL, |
| 33 | + NULL, |
| 34 | + NULL, |
| 35 | + NULL); |
| 36 | + |
| 37 | + v1_config_map_t *body = v1_config_map_create(api_version, |
| 38 | + NULL, |
| 39 | + data, |
| 40 | + kind, |
| 41 | + meta); |
| 42 | + |
| 43 | + v1_config_map_t *ret_config_map = CoreV1API_createNamespacedConfigMap(apiClient, |
| 44 | + namespace_, |
| 45 | + body, |
| 46 | + NULL, |
| 47 | + NULL, |
| 48 | + NULL); |
| 49 | + |
| 50 | + printf("%s: The return code of HTTP request=%ld\n", __func__, apiClient->response_code); |
| 51 | + |
| 52 | + if (201 == apiClient->response_code) { |
| 53 | + printf("%s: Create the config map successfully.\n", __func__); |
| 54 | + } else { |
| 55 | + fprintf(stderr, "%s: Failed to create the config map.\n", __func__); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + if (ret_config_map) { |
| 60 | + v1_config_map_free(ret_config_map); |
| 61 | + ret_config_map = NULL; |
| 62 | + } |
| 63 | + if (body) { |
| 64 | + v1_config_map_free(body); |
| 65 | + body = NULL; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void list_configmap(apiClient_t * apiClient, const char *namespace_) |
| 70 | +{ |
| 71 | + v1_config_map_list_t *config_map_list = CoreV1API_listNamespacedConfigMap(apiClient, |
| 72 | + namespace_, // char *namespace |
| 73 | + "true", // char *pretty |
| 74 | + 0, // int allowWatchBookmarks |
| 75 | + NULL, // char * _continue |
| 76 | + NULL, // char * fieldSelector |
| 77 | + NULL, // char * labelSelector |
| 78 | + 0, // int limit |
| 79 | + NULL, // char * resourceVersion |
| 80 | + 0, // int timeoutSeconds |
| 81 | + 0 //int watch |
| 82 | + ); |
| 83 | + |
| 84 | + printf("%s: The return code of HTTP request=%ld\n", __func__, apiClient->response_code); |
| 85 | + |
| 86 | + if (200 == apiClient->response_code) { |
| 87 | + printf("%s: List the config maps successfully.\n", __func__); |
| 88 | + } else { |
| 89 | + fprintf(stderr, "%s: Failed to list the config maps.\n", __func__); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + if (config_map_list && config_map_list->items) { |
| 94 | + listEntry_t *config_map_list_entry = NULL; |
| 95 | + v1_config_map_t *config_map = NULL; |
| 96 | + list_ForEach(config_map_list_entry, config_map_list->items) { |
| 97 | + config_map = config_map_list_entry->data; |
| 98 | + printf("\tThe config map name: %s\n", config_map->metadata->name); |
| 99 | + |
| 100 | + listEntry_t *data_entry = NULL; |
| 101 | + keyValuePair_t *pair = NULL; |
| 102 | + list_ForEach(data_entry, config_map->data) { |
| 103 | + pair = data_entry->data; |
| 104 | + printf("\tkey=%s, value=%s\n", pair->key, (char *) pair->value); |
| 105 | + } |
| 106 | + } |
| 107 | + v1_config_map_list_free(config_map_list); |
| 108 | + config_map_list = NULL; |
| 109 | + } else { |
| 110 | + fprintf(stderr, "%s: The config map list is invalid.\n", __func__); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +void delete_configmap(apiClient_t * apiClient, const char *name, const char *namespace_) |
| 115 | +{ |
| 116 | + v1_status_t *status = CoreV1API_deleteNamespacedConfigMap(apiClient, |
| 117 | + name, // char *name |
| 118 | + namespace_, // char *namespace |
| 119 | + NULL, // char *pretty |
| 120 | + NULL, // char *dryRun |
| 121 | + 0, // int gracePeriodSeconds |
| 122 | + 0, // int orphanDependents |
| 123 | + NULL, // char *propagationPolicy |
| 124 | + NULL // v1_delete_options_t *body |
| 125 | + ); |
| 126 | + |
| 127 | + printf("The return code of HTTP request=%ld\n", apiClient->response_code); |
| 128 | + |
| 129 | + if (200 == apiClient->response_code || 202 == apiClient->response_code) { |
| 130 | + printf("The config map is deleted successfully.\n"); |
| 131 | + } else { |
| 132 | + if (status && status->message) { |
| 133 | + printf("Failed to delete the config map. The error message: %s\n", status->message); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (status) { |
| 138 | + v1_status_free(status); |
| 139 | + status = NULL; |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +int main(int argc, char *argv[]) |
| 144 | +{ |
| 145 | + char *basePath = NULL; |
| 146 | + sslConfig_t *sslConfig = NULL; |
| 147 | + list_t *apiKeys = NULL; |
| 148 | + int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */ |
| 149 | + if (rc != 0) { |
| 150 | + fprintf(stderr, "Cannot load kubernetes configuration.\n"); |
| 151 | + return -1; |
| 152 | + } |
| 153 | + apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys); |
| 154 | + if (!apiClient) { |
| 155 | + fprintf(stderr, "Cannot create a kubernetes client.\n"); |
| 156 | + return -1; |
| 157 | + } |
| 158 | + |
| 159 | + char *config_map_name = "cm1"; |
| 160 | + char *namespace_ = "default"; |
| 161 | + create_configmap(apiClient, config_map_name, namespace_); |
| 162 | + sleep(5); |
| 163 | + list_configmap(apiClient, namespace_); |
| 164 | + sleep(5); |
| 165 | + delete_configmap(apiClient, config_map_name, namespace_); |
| 166 | + |
| 167 | + apiClient_free(apiClient); |
| 168 | + apiClient = NULL; |
| 169 | + free_client_config(basePath, sslConfig, apiKeys); |
| 170 | + basePath = NULL; |
| 171 | + sslConfig = NULL; |
| 172 | + apiKeys = NULL; |
| 173 | + apiClient_unsetupGlobalEnv(); |
| 174 | + |
| 175 | + return 0; |
| 176 | +} |
0 commit comments