2020-08-14 10:56:05 -07:00
|
|
|
#include <kube_config.h>
|
|
|
|
|
#include <apiClient.h>
|
|
|
|
|
#include <generic.h>
|
|
|
|
|
#include <malloc.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
char *basePath = NULL;
|
|
|
|
|
sslConfig_t *sslConfig = NULL;
|
|
|
|
|
list_t *apiKeys = NULL;
|
|
|
|
|
int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */
|
|
|
|
|
if (rc != 0) {
|
|
|
|
|
printf("Cannot load kubernetes configuration.\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
|
|
|
|
|
if (!apiClient) {
|
|
|
|
|
printf("Cannot create a kubernetes client.\n");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
genericClient_t *genericClient = genericClient_create(apiClient, "apps", "v1", "deployments");
|
|
|
|
|
|
|
|
|
|
char *list = Generic_listNamespaced(genericClient, "default");
|
|
|
|
|
printf("%s\n", list);
|
|
|
|
|
free(list);
|
|
|
|
|
|
|
|
|
|
char *result = Generic_readNamespacedResource(genericClient, "default", "camera-gc");
|
|
|
|
|
printf("%s\n", result);
|
|
|
|
|
free(result);
|
|
|
|
|
|
|
|
|
|
genericClient_free(genericClient);
|
|
|
|
|
genericClient = NULL;
|
2020-08-14 12:24:40 -07:00
|
|
|
|
|
|
|
|
genericClient = genericClient_create(apiClient, "", "v1", "namespaces");
|
|
|
|
|
|
|
|
|
|
const char *body = "{\"apiVersion\": \"v1\", \"kind\": \"Namespace\", \"metadata\": { \"name\": \"test\" } }";
|
|
|
|
|
char *create = Generic_createResource(genericClient, body);
|
|
|
|
|
printf("%s\n", create);
|
|
|
|
|
free(create);
|
|
|
|
|
|
|
|
|
|
const char *updateBody = "{\"apiVersion\": \"v1\", \"kind\": \"Namespace\", \"metadata\": { \"name\": \"test\", \"labels\": { \"foo\": \"bar\" } } }";
|
|
|
|
|
char *update = Generic_replaceResource(genericClient, "test", updateBody);
|
|
|
|
|
printf("%s\n", update);
|
|
|
|
|
free(update);
|
|
|
|
|
|
2021-02-18 16:48:49 +08:00
|
|
|
const char *patchBody = "[{\"op\": \"replace\", \"path\": \"/metadata/labels/foo\", \"value\": \"qux\" }]";
|
2022-03-09 14:09:10 +08:00
|
|
|
list_t *contentType = list_createList();
|
2021-02-18 16:48:49 +08:00
|
|
|
// Kubernetes supports multiple content types:
|
|
|
|
|
list_addElement(contentType, "application/json-patch+json");
|
|
|
|
|
// list_addElement(contentType, "application/merge-patch+json");
|
|
|
|
|
// list_addElement(contentType, "application/strategic-merge-patch+json");
|
|
|
|
|
// list_addElement(contentType, "application/apply-patch+yaml");
|
|
|
|
|
char *patch = Generic_patchResource(genericClient, "test", patchBody, NULL, NULL, NULL, NULL, contentType);
|
|
|
|
|
printf("%s\n", patch);
|
2022-03-09 14:09:10 +08:00
|
|
|
list_freeList(contentType);
|
2021-02-18 16:48:49 +08:00
|
|
|
free(patch);
|
|
|
|
|
|
2020-08-14 12:24:40 -07:00
|
|
|
char *del = Generic_deleteResource(genericClient, "test");
|
|
|
|
|
printf("%s\n", del);
|
|
|
|
|
free(del);
|
|
|
|
|
|
|
|
|
|
genericClient_free(genericClient);
|
|
|
|
|
genericClient = NULL;
|
|
|
|
|
|
2020-08-14 10:56:05 -07:00
|
|
|
apiClient_free(apiClient);
|
|
|
|
|
apiClient = NULL;
|
|
|
|
|
free_client_config(basePath, sslConfig, apiKeys);
|
|
|
|
|
basePath = NULL;
|
|
|
|
|
sslConfig = NULL;
|
|
|
|
|
apiKeys = NULL;
|
2020-10-27 20:33:38 +08:00
|
|
|
apiClient_unsetupGlobalEnv();
|
2020-08-14 10:56:05 -07:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|