2020-03-22 21:56:55 -07:00
# Kubernetes Client Library for C
2022-03-18 17:56:31 +08:00
[](https://github.com/kubernetes/design-proposals-archive/blob/main/api-machinery/csi-new-client-library-procedure.md#client -capabilities)
[](https://github.com/kubernetes/design-proposals-archive/blob/main/api-machinery/csi-new-client-library-procedure.md#client -support-level)
2020-12-18 16:36:25 +08:00
[](https://github.com/kubernetes-client/c/actions?query=workflow%3A%22Code+Check%22)
[](https://github.com/kubernetes-client/c/actions?query=workflow%3ABuild)
2020-03-22 21:56:55 -07:00
This is the official Kubernetes client library for the C programming language.
## Building the library
```bash
# Clone the repo
git clone https://github.com/kubernetes-client/c
CLIENT_REPO_ROOT=${PWD}/c
# Install pre-requisites
2021-09-02 13:56:14 -03:00
sudo apt-get install libssl-dev libcurl4-openssl-dev uncrustify
2020-03-22 21:56:55 -07:00
2021-06-06 13:06:50 +08:00
# Build pre-requisite: libwebsockets
git clone https://libwebsockets.org/repo/libwebsockets --depth 1 --branch v4.2-stable
cd libwebsockets
mkdir build
cd build
2021-09-02 13:56:14 -03:00
cmake -DLWS_WITHOUT_TESTAPPS=ON -DLWS_WITHOUT_TEST_SERVER=ON-DLWS_WITHOUT_TEST_SERVER_EXTPOLL=ON \
-DLWS_WITHOUT_TEST_PING=ON -DLWS_WITHOUT_TEST_CLIENT=ON -DCMAKE_C_FLAGS="-fpic" -DCMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install
# Build pre-requisite: libyaml
git clone https://github.com/yaml/libyaml --depth 1 --branch release/0.2.5
cd libyaml
mkdir build
cd build
2021-09-10 14:04:41 -03:00
cmake -DCMAKE_INSTALL_PREFIX=/usr/local -DBUILD_TESTING=OFF -DBUILD_SHARED_LIBS=ON ..
2021-06-06 13:06:50 +08:00
make
sudo make install
2020-03-22 21:56:55 -07:00
# Move into the Kubernetes directory
cd ${CLIENT_REPO_ROOT}/kubernetes
# Build
mkdir build
cd build
2021-11-10 16:39:59 +08:00
cmake -DCMAKE_INSTALL_PREFIX=/usr/local ..
2020-03-22 21:56:55 -07:00
make
sudo make install
```
2021-09-24 14:02:44 +08:00
## (Optional) Installing using vcpkg on Windows
If you want to install the C client using vcpkg, please refer to [vcpkg.md ](https://github.com/kubernetes-client/c/blob/master/docs/vcpkg.md )
2021-09-23 11:58:27 -03:00
2020-03-22 21:56:55 -07:00
## Building an example
```bash
cd ${CLIENT_REPO_ROOT}/examples/list_pod
make
```
## Running the example
```bash
./list_pod_bin
```
## Usage example
2020-06-28 12:24:08 +08:00
list all pods:
2020-03-22 21:56:55 -07:00
```c
2020-06-28 12:24:08 +08:00
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;
}
v1_pod_list_t *pod_list = NULL;
pod_list = CoreV1API_listNamespacedPod(apiClient,
"default", /*namespace */
NULL, /* pretty */
0, /* allowWatchBookmarks */
NULL, /* continue */
NULL, /* fieldSelector */
NULL, /* labelSelector */
0, /* limit */
NULL, /* resourceVersion */
2021-09-15 14:23:19 +08:00
NULL, /* resourceVersionMatch */
2020-06-28 12:24:08 +08:00
0, /* timeoutSeconds */
0 /* watch */
);
printf("return code=%ld\n", apiClient->response_code);
if (pod_list) {
...
}
2020-03-22 21:56:55 -07:00
2020-06-28 12:24:08 +08: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-06-28 12:24:08 +08:00
```
list all pods in cluster:
```c
char *basePath = NULL;
sslConfig_t *sslConfig = NULL;
list_t *apiKeys = NULL;
int rc = load_incluster_config(& basePath, & sslConfig, &apiKeys);
if (rc != 0) {
printf("Cannot load kubernetes configuration in cluster.\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;
}
2020-03-22 21:56:55 -07:00
v1_pod_list_t *pod_list = NULL;
pod_list = CoreV1API_listNamespacedPod(apiClient,
"default", /*namespace */
NULL, /* pretty */
0, /* allowWatchBookmarks */
NULL, /* continue */
NULL, /* fieldSelector */
NULL, /* labelSelector */
0, /* limit */
NULL, /* resourceVersion */
2021-09-15 14:23:19 +08:00
NULL, /* resourceVersionMatch */
2020-03-22 21:56:55 -07:00
0, /* timeoutSeconds */
0 /* watch */
);
printf("return code=%ld\n", apiClient->response_code);
if (pod_list) {
...
}
2020-06-28 12:24:08 +08: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-03-22 21:56:55 -07:00
```
2020-03-17 07:59:06 -04:00
2022-07-25 09:29:09 +08:00
## Aggregated APIs and CRD-based APIs
If you want to implement a client for aggregated APIs (such as the metrics server API `apis/metrics.k8s.io` ) or CRD-based APIs, use the [generic client ](./kubernetes/src/generic.c ). See [example ](./examples/generic/main.c ).
2020-11-02 21:38:52 +08:00
## Multi-threaded Usage
2020-12-01 14:17:21 +08:00
If the C client library is used in multi-threaded program, the following 2 actions are required:
2020-11-02 21:38:52 +08:00
1. After the program starts up, main thread must call the function ```apiClient_setupGlobalEnv()` `` before any worker thread is created.
2. If the C client is no longer used, main thread must call the function ```apiClient_unsetupGlobalEnv()` `` after all worker threads end.
Refer to the [example ](https://github.com/kubernetes-client/c/tree/master/examples/multi_thread/ ) for detail.
2020-06-28 12:24:08 +08:00
2021-03-17 16:35:19 +08:00
## Documentation
All APIs and Models' documentation can be found at the [Generated client's README file ](https://github.com/kubernetes-client/c/blob/master/kubernetes/README.md#documentation-for-api-endpoints )
2020-03-17 07:59:06 -04:00
## Community, discussion, contribution, and support
Learn how to engage with the Kubernetes community on the [community page ](http://kubernetes.io/community/ ).
You can reach the maintainers of this project at:
- [Slack ](http://slack.k8s.io/ )
2022-07-25 09:29:09 +08:00
- [Mailing List ](https://groups.google.com/a/kubernetes.io/g/dev )
2020-03-17 07:59:06 -04:00
### Code of conduct
Participation in the Kubernetes community is governed by the [Kubernetes Code of Conduct ](code-of-conduct.md ).
[owners]: https://git.k8s.io/community/contributors/guide/owners.md
[Creative Commons 4.0]: https://git.k8s.io/website/LICENSE