Merge pull request #19 from ityuhui/yhupdreadme

[Readme] Update the usage example in README.md about loading configuration.
This commit is contained in:
Kubernetes Prow Robot
2020-07-14 11:01:21 -07:00
committed by GitHub
4 changed files with 95 additions and 47 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,5 @@
kubernetes/build/ kubernetes/build/
.vs/
# Prerequisites # Prerequisites
*.d *.d

View File

@@ -31,23 +31,28 @@ make
``` ```
## Running the example ## Running the example
For now, you need to use `kubectl proxy` to handle authentication.
```bash ```bash
kubectl proxy
./list_pod_bin ./list_pod_bin
``` ```
## Usage example ## Usage example
```c list all pods:
list_t *apiKeys;
apiKeys = list_create();
keyValuePair_t *keyPairToken = keyValuePair_create(keyToken, valueToken);
list_addElement(apiKeys, keyPairToken);
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; v1_pod_list_t *pod_list = NULL;
pod_list = CoreV1API_listNamespacedPod(apiClient, pod_list = CoreV1API_listNamespacedPod(apiClient,
@@ -66,8 +71,59 @@ kubectl proxy
if (pod_list) { 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 ## Community, discussion, contribution, and support
Learn how to engage with the Kubernetes community on the [community page](http://kubernetes.io/community/). Learn how to engage with the Kubernetes community on the [community page](http://kubernetes.io/community/).

View File

@@ -37,32 +37,28 @@ void list_pod(apiClient_t * apiClient)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int rc = 0; char *basePath = NULL;
char *baseName = NULL;
sslConfig_t *sslConfig = NULL; sslConfig_t *sslConfig = NULL;
list_t *apiKeys = NULL; list_t *apiKeys = NULL;
apiClient_t *k8sApiClient = NULL; int rc = load_kube_config(&basePath, &sslConfig, &apiKeys, NULL); /* NULL means loading configuration from $HOME/.kube/config */
if (rc != 0) {
rc = load_kube_config(&baseName, &sslConfig, &apiKeys, NULL);
if (0 == rc) {
k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys);
} else {
printf("Cannot load kubernetes configuration.\n"); printf("Cannot load kubernetes configuration.\n");
return -1; return -1;
} }
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
if (k8sApiClient) { if (!apiClient) {
list_pod(k8sApiClient); printf("Cannot create a kubernetes client.\n");
return -1;
} }
free_client_config(baseName, sslConfig, apiKeys); list_pod(apiClient);
baseName = NULL;
apiClient_free(apiClient);
apiClient = NULL;
free_client_config(basePath, sslConfig, apiKeys);
basePath = NULL;
sslConfig = NULL; sslConfig = NULL;
apiKeys = NULL; apiKeys = NULL;
apiClient_free(k8sApiClient); return 0;
k8sApiClient = NULL;
return rc;
} }

View File

@@ -38,32 +38,27 @@ void list_pod(apiClient_t * apiClient)
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
int rc = 0; char *basePath = NULL;
char *baseName = NULL;
sslConfig_t *sslConfig = NULL; sslConfig_t *sslConfig = NULL;
list_t *apiKeys = NULL; list_t *apiKeys = NULL;
apiClient_t *k8sApiClient = NULL; int rc = load_incluster_config(&basePath, &sslConfig, &apiKeys);
if (rc != 0) {
rc = load_incluster_config(&baseName, &sslConfig, &apiKeys); printf("Cannot load kubernetes configuration in cluster.\n");
if (0 == rc) {
k8sApiClient = apiClient_create_with_base_path(baseName, sslConfig, apiKeys);
} else {
printf("Cannot load kubernetes configuration.\n");
return -1; return -1;
} }
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
if (k8sApiClient) { if (!apiClient) {
list_pod(k8sApiClient); printf("Cannot create a kubernetes client.\n");
return -1;
} }
list_pod(apiClient);
free_client_config(baseName, sslConfig, apiKeys); apiClient_free(apiClient);
baseName = NULL; apiClient = NULL;
free_client_config(basePath, sslConfig, apiKeys);
basePath = NULL;
sslConfig = NULL; sslConfig = NULL;
apiKeys = NULL; apiKeys = NULL;
apiClient_free(k8sApiClient); return 0;
k8sApiClient = NULL;
return rc;
} }