1
+
2
+ #include "kubernetes/config/kube_config.h"
3
+ #include "kubernetes/include/apiClient.h"
4
+ #include "kubernetes/api/CoreV1API.h"
5
+ #include <malloc.h>
6
+ #include <stdio.h>
7
+ #include <errno.h>
8
+
9
+ void create_a_pod (apiClient_t * apiClient )
10
+ {
11
+ char * namespace = "default" ;
12
+
13
+ v1_pod_t * podinfo = calloc (1 , sizeof (v1_pod_t ));
14
+ podinfo -> api_version = strdup ("v1" );
15
+ podinfo -> kind = strdup ("Pod" );
16
+ podinfo -> spec = calloc (1 , sizeof (v1_pod_spec_t ));
17
+
18
+ podinfo -> metadata = calloc (1 , sizeof (v1_object_meta_t ));
19
+ /* set pod name */
20
+ podinfo -> metadata -> name = strdup ("test-pod-6" );
21
+
22
+ /* set containers for pod */
23
+ list_t * containerlist = list_createList ();
24
+ v1_container_t * con = calloc (1 , sizeof (v1_container_t ));
25
+ con -> name = strdup ("my-container" );
26
+ con -> image = strdup ("ubuntu:latest" );
27
+ con -> image_pull_policy = strdup ("IfNotPresent" );
28
+
29
+ /* set command for container */
30
+ list_t * commandlist = list_createList ();
31
+ char * cmd = strdup ("sleep" );
32
+ list_addElement (commandlist , cmd );
33
+ con -> command = commandlist ;
34
+
35
+ list_t * arglist = list_createList ();
36
+ char * arg1 = strdup ("3600" );
37
+ list_addElement (arglist , arg1 );
38
+ con -> args = arglist ;
39
+
40
+ /* set volume mounts for container */
41
+ list_t * volumemounts = list_createList ();
42
+ v1_volume_mount_t * volmou = calloc (1 , sizeof (v1_volume_mount_t ));
43
+ volmou -> mount_path = strdup ("/test" );
44
+ volmou -> name = strdup ("test" );
45
+ list_addElement (volumemounts , volmou );
46
+ con -> volume_mounts = volumemounts ;
47
+
48
+ list_addElement (containerlist , con );
49
+ podinfo -> spec -> containers = containerlist ;
50
+
51
+ /* set volumes for pod */
52
+ list_t * volumelist = list_createList ();
53
+ v1_volume_t * volume = calloc (1 , sizeof (v1_volume_t ));
54
+ volume -> name = strdup ("test" );
55
+
56
+ v1_host_path_volume_source_t * hostPath = calloc (1 , sizeof (v1_host_path_volume_source_t ));
57
+ hostPath -> path = strdup ("/test" );
58
+ volume -> host_path = hostPath ;
59
+
60
+ list_addElement (volumelist , volume );
61
+ podinfo -> spec -> volumes = volumelist ;
62
+
63
+ /* call API in libkubernetes to create pod */
64
+ v1_pod_t * apod = CoreV1API_createNamespacedPod (apiClient , namespace , podinfo , NULL , NULL , NULL , NULL );
65
+ printf ("code=%ld\n" , apiClient -> response_code );
66
+
67
+ v1_pod_free (apod );
68
+ v1_pod_free (podinfo );
69
+ }
70
+
71
+ int main (int argc , char * argv [])
72
+ {
73
+
74
+ int rc = 0 ;
75
+
76
+ char * baseName = NULL ;
77
+ sslConfig_t * sslConfig = NULL ;
78
+ list_t * apiKeys = NULL ;
79
+ apiClient_t * k8sApiClient = NULL ;
80
+
81
+ rc = load_kube_config (& baseName , & sslConfig , & apiKeys , NULL );
82
+ if (0 == rc ) {
83
+ k8sApiClient = apiClient_create_with_base_path (baseName , sslConfig , apiKeys );
84
+ } else {
85
+ printf ("Cannot load kubernetes configuration.\n" );
86
+ return -1 ;
87
+ }
88
+
89
+ if (k8sApiClient ) {
90
+ create_a_pod (k8sApiClient );
91
+ }
92
+
93
+ free_client_config (baseName , sslConfig , apiKeys );
94
+ baseName = NULL ;
95
+ sslConfig = NULL ;
96
+ apiKeys = NULL ;
97
+
98
+ apiClient_free (k8sApiClient );
99
+ k8sApiClient = NULL ;
100
+ apiClient_unsetupGlobalEnv ();
101
+
102
+ return 0 ;
103
+ }
0 commit comments