|
| 1 | +#include <config/kube_config.h> |
| 2 | +#include <api/CoreV1API.h> |
| 3 | +#include <stdio.h> |
| 4 | + |
| 5 | +void list_pod(apiClient_t * apiClient) |
| 6 | +{ |
| 7 | + v1_pod_list_t *pod_list = NULL; |
| 8 | + pod_list = CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */ |
| 9 | + NULL, /* pretty */ |
| 10 | + 0, /* allowWatchBookmarks */ |
| 11 | + NULL, /* continue */ |
| 12 | + NULL, /* fieldSelector */ |
| 13 | + NULL, /* labelSelector */ |
| 14 | + 0, /* limit */ |
| 15 | + NULL, /* resourceVersion */ |
| 16 | + NULL, /* resourceVersionMatch */ |
| 17 | + 0, /* timeoutSeconds */ |
| 18 | + 0 /* watch */ |
| 19 | + ); |
| 20 | + printf("The return code of HTTP request=%ld\n", apiClient->response_code); |
| 21 | + if (pod_list) { |
| 22 | + printf("Get pod list:\n"); |
| 23 | + listEntry_t *listEntry = NULL; |
| 24 | + v1_pod_t *pod = NULL; |
| 25 | + list_ForEach(listEntry, pod_list->items) { |
| 26 | + pod = listEntry->data; |
| 27 | + printf("\tThe pod name: %s\n", pod->metadata->name); |
| 28 | + } |
| 29 | + v1_pod_list_free(pod_list); |
| 30 | + pod_list = NULL; |
| 31 | + } else { |
| 32 | + printf("Cannot get any pod.\n"); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +int main() |
| 37 | +{ |
| 38 | + char *basePath = NULL; |
| 39 | + sslConfig_t *sslConfig = NULL; |
| 40 | + list_t *apiKeys = NULL; |
| 41 | + int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, "non-existent-file"); |
| 42 | + if (rc != 0) { |
| 43 | + printf("Cannot load kubernetes configuration.\n"); |
| 44 | + /* Return 0 to avoid Github/Action check failures. |
| 45 | + You should return a non-zero value in a production environment. */ |
| 46 | + return 0; |
| 47 | + } |
| 48 | + apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys); |
| 49 | + if (!apiClient) { |
| 50 | + printf("Cannot create a kubernetes client.\n"); |
| 51 | + /* Return 0 to avoid Github/Action check failures. |
| 52 | + You should return a non-zero value in a production environment. */ |
| 53 | + return 0; |
| 54 | + } |
| 55 | + |
| 56 | + list_pod(apiClient); |
| 57 | + |
| 58 | + apiClient_free(apiClient); |
| 59 | + apiClient = NULL; |
| 60 | + free_client_config(basePath, sslConfig, apiKeys); |
| 61 | + basePath = NULL; |
| 62 | + sslConfig = NULL; |
| 63 | + apiKeys = NULL; |
| 64 | + apiClient_unsetupGlobalEnv(); |
| 65 | + |
| 66 | + return 0; |
| 67 | +} |
0 commit comments