|
| 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 | + |
| 8 | +void list_secret(apiClient_t * apiClient) |
| 9 | +{ |
| 10 | + v1_secret_list_t *secret_list = CoreV1API_listNamespacedSecret(apiClient, |
| 11 | + "default", // char *namespace |
| 12 | + "true", // char *pretty |
| 13 | + 0, // int allowWatchBookmarks |
| 14 | + NULL, // char * _continue |
| 15 | + NULL, // char * fieldSelector |
| 16 | + NULL, // char * labelSelector |
| 17 | + 0, // int limit |
| 18 | + NULL, // char * resourceVersion |
| 19 | + 0, // int timeoutSeconds |
| 20 | + 0 //int watch |
| 21 | + ); |
| 22 | + |
| 23 | + printf("The return code of HTTP request=%ld\n", apiClient->response_code); |
| 24 | + |
| 25 | + if (200 == apiClient->response_code) { |
| 26 | + printf("List the secrets successfully.\n"); |
| 27 | + } else { |
| 28 | + fprintf(stderr, "Failed to list the secrets.\n"); |
| 29 | + return; |
| 30 | + } |
| 31 | + |
| 32 | + if (secret_list && secret_list->items) { |
| 33 | + listEntry_t *secret_list_entry = NULL; |
| 34 | + v1_secret_t *secret = NULL; |
| 35 | + list_ForEach(secret_list_entry, secret_list->items) { |
| 36 | + secret = secret_list_entry->data; |
| 37 | + printf("\tThe secret name: %s\n", secret->metadata->name); |
| 38 | + |
| 39 | + listEntry_t *data_entry = NULL; |
| 40 | + keyValuePair_t *pair = NULL; |
| 41 | + list_ForEach(data_entry, secret->data) { |
| 42 | + pair = data_entry->data; |
| 43 | + printf("\tkey=%s, value=%s\n", pair->key, (char *) pair->value); |
| 44 | + } |
| 45 | + } |
| 46 | + v1_secret_list_free(secret_list); |
| 47 | + secret_list = NULL; |
| 48 | + } else { |
| 49 | + fprintf(stderr, "The secret list is invalid.\n"); |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +int main(int argc, char *argv[]) |
| 54 | +{ |
| 55 | + char *basePath = NULL; |
| 56 | + sslConfig_t *sslConfig = NULL; |
| 57 | + list_t *apiKeys = NULL; |
| 58 | + int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */ |
| 59 | + if (rc != 0) { |
| 60 | + fprintf(stderr, "Cannot load kubernetes configuration.\n"); |
| 61 | + return -1; |
| 62 | + } |
| 63 | + apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys); |
| 64 | + if (!apiClient) { |
| 65 | + fprintf(stderr, "Cannot create a kubernetes client.\n"); |
| 66 | + return -1; |
| 67 | + } |
| 68 | + |
| 69 | + list_secret(apiClient); |
| 70 | + |
| 71 | + apiClient_free(apiClient); |
| 72 | + apiClient = NULL; |
| 73 | + free_client_config(basePath, sslConfig, apiKeys); |
| 74 | + basePath = NULL; |
| 75 | + sslConfig = NULL; |
| 76 | + apiKeys = NULL; |
| 77 | + apiClient_unsetupGlobalEnv(); |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
0 commit comments