generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 48
/
main.c
77 lines (64 loc) · 2.69 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#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;
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);
const char *patchBody = "[{\"op\": \"replace\", \"path\": \"/metadata/labels/foo\", \"value\": \"qux\" }]";
list_t *contentType = list_createList();
// 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);
list_freeList(contentType);
free(patch);
char *del = Generic_deleteResource(genericClient, "test");
printf("%s\n", del);
free(del);
genericClient_free(genericClient);
genericClient = NULL;
apiClient_free(apiClient);
apiClient = NULL;
free_client_config(basePath, sslConfig, apiKeys);
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();
return 0;
}