[Readme & Examples]
* Update the usage example in README.md because kubernetes-client/c has supported loading cluster configuration to authentication. * Update some examples to keep consistence with README.md
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
||||
kubernetes/build/
|
||||
.vs/
|
||||
|
||||
# Prerequisites
|
||||
*.d
|
||||
|
||||
76
README.md
76
README.md
@@ -31,23 +31,28 @@ make
|
||||
```
|
||||
|
||||
## Running the example
|
||||
For now, you need to use `kubectl proxy` to handle authentication.
|
||||
|
||||
```bash
|
||||
kubectl proxy
|
||||
./list_pod_bin
|
||||
```
|
||||
|
||||
## Usage example
|
||||
|
||||
```c
|
||||
list_t *apiKeys;
|
||||
apiKeys = list_create();
|
||||
|
||||
keyValuePair_t *keyPairToken = keyValuePair_create(keyToken, valueToken);
|
||||
list_addElement(apiKeys, keyPairToken);
|
||||
list all pods:
|
||||
|
||||
g_k8sAPIConnector = apiClient_create_with_base_path(K8S_APISERVER_BASEPATH, NULL, apiKeys);
|
||||
```c
|
||||
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,
|
||||
@@ -66,8 +71,59 @@ kubectl proxy
|
||||
if (pod_list) {
|
||||
...
|
||||
}
|
||||
|
||||
apiClient_free(apiClient);
|
||||
apiClient = NULL;
|
||||
free_client_config(basePath, sslConfig, apiKeys);
|
||||
basePath = NULL;
|
||||
sslConfig = NULL;
|
||||
apiKeys = NULL;
|
||||
```
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 */
|
||||
0, /* timeoutSeconds */
|
||||
0 /* watch */
|
||||
);
|
||||
printf("return code=%ld\n", apiClient->response_code);
|
||||
if (pod_list) {
|
||||
...
|
||||
}
|
||||
|
||||
apiClient_free(apiClient);
|
||||
apiClient = NULL;
|
||||
free_client_config(basePath, sslConfig, apiKeys);
|
||||
basePath = NULL;
|
||||
sslConfig = NULL;
|
||||
apiKeys = NULL;
|
||||
```
|
||||
|
||||
|
||||
## Community, discussion, contribution, and support
|
||||
|
||||
Learn how to engage with the Kubernetes community on the [community page](http://kubernetes.io/community/).
|
||||
|
||||
@@ -37,32 +37,28 @@ void list_pod(apiClient_t * apiClient)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
char *baseName = NULL;
|
||||
char *basePath = NULL;
|
||||
sslConfig_t *sslConfig = NULL;
|
||||
list_t *apiKeys = NULL;
|
||||
apiClient_t *k8sApiClient = NULL;
|
||||
|
||||
rc = load_kube_config(&baseName, &sslConfig, &apiKeys, NULL);
|
||||
if (0 == rc) {
|
||||
k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys);
|
||||
} else {
|
||||
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;
|
||||
}
|
||||
|
||||
if (k8sApiClient) {
|
||||
list_pod(k8sApiClient);
|
||||
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
|
||||
if (!apiClient) {
|
||||
printf("Cannot create a kubernetes client.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
free_client_config(baseName, sslConfig, apiKeys);
|
||||
baseName = NULL;
|
||||
list_pod(apiClient);
|
||||
|
||||
apiClient_free(apiClient);
|
||||
apiClient = NULL;
|
||||
free_client_config(basePath, sslConfig, apiKeys);
|
||||
basePath = NULL;
|
||||
sslConfig = NULL;
|
||||
apiKeys = NULL;
|
||||
|
||||
apiClient_free(k8sApiClient);
|
||||
k8sApiClient = NULL;
|
||||
|
||||
return rc;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -38,32 +38,27 @@ void list_pod(apiClient_t * apiClient)
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
char *baseName = NULL;
|
||||
char *basePath = NULL;
|
||||
sslConfig_t *sslConfig = NULL;
|
||||
list_t *apiKeys = NULL;
|
||||
apiClient_t *k8sApiClient = NULL;
|
||||
|
||||
rc = load_incluster_config(&baseName, &sslConfig, &apiKeys);
|
||||
if (0 == rc) {
|
||||
k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys);
|
||||
} else {
|
||||
printf("Cannot load kubernetes configuration.\n");
|
||||
int rc = load_incluster_config(&basePath, &sslConfig, &apiKeys);
|
||||
if (rc != 0) {
|
||||
printf("Cannot load kubernetes configuration in cluster.\n");
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (k8sApiClient) {
|
||||
list_pod(k8sApiClient);
|
||||
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
|
||||
if (!apiClient) {
|
||||
printf("Cannot create a kubernetes client.\n");
|
||||
return -1;
|
||||
}
|
||||
list_pod(apiClient);
|
||||
|
||||
free_client_config(baseName, sslConfig, apiKeys);
|
||||
baseName = NULL;
|
||||
apiClient_free(apiClient);
|
||||
apiClient = NULL;
|
||||
free_client_config(basePath, sslConfig, apiKeys);
|
||||
basePath = NULL;
|
||||
sslConfig = NULL;
|
||||
apiKeys = NULL;
|
||||
|
||||
apiClient_free(k8sApiClient);
|
||||
k8sApiClient = NULL;
|
||||
|
||||
return rc;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user