Fix the issue of parsing json string of kubernetes secret

This commit is contained in:
Hui Yu
2021-07-21 22:41:29 +08:00
parent d83ca5a13a
commit 63d8c15564
33 changed files with 194 additions and 81 deletions

View File

@@ -9,6 +9,7 @@ all:
cd watch_list_pod; make
cd multi_thread; make
cd exec_pod; make
cd list_secret; make
clean:
cd create_pod; make clean
@@ -21,11 +22,13 @@ clean:
cd watch_list_pod; make clean
cd multi_thread; make clean
cd exec_pod; make clean
cd list_secret; make clean
test:
cd create_pod; make test; sleep 10
cd list_pod; make test
cd delete_pod; make test
cd list_secret; make test
cd generic; make test
cd multi_thread; make test; sleep 10
kubectl describe po test-pod-8

1
examples/list_secret/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
list_secret_bin

View File

@@ -0,0 +1,11 @@
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config
LIBS:=-L../../kubernetes/build -lkubernetes -lyaml -lwebsockets -L/usr/local/lib
CFLAGS:=-g
BIN:=list_secret_bin
all:
gcc main.c $(CFLAGS) $(INCLUDE) $(LIBS) -o $(BIN)
clean:
rm ./$(BIN)
test:
./$(BIN)

View File

@@ -0,0 +1,80 @@
#include <kube_config.h>
#include <apiClient.h>
#include <CoreV1API.h>
#include <malloc.h>
#include <stdio.h>
#include <errno.h>
void list_secret(apiClient_t * apiClient)
{
v1_secret_list_t *secret_list = CoreV1API_listNamespacedSecret(apiClient,
"default", // char *namespace
"true", // char *pretty
0, // int allowWatchBookmarks
NULL, // char * _continue
NULL, // char * fieldSelector
NULL, // char * labelSelector
0, // int limit
NULL, // char * resourceVersion
0, // int timeoutSeconds
0 //int watch
);
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
if (200 == apiClient->response_code) {
printf("List the secrets successfully.\n");
} else {
fprintf(stderr, "Failed to list the secrets.\n");
return;
}
if (secret_list && secret_list->items) {
listEntry_t *secret_list_entry = NULL;
v1_secret_t *secret = NULL;
list_ForEach(secret_list_entry, secret_list->items) {
secret = secret_list_entry->data;
printf("\tThe secret name: %s\n", secret->metadata->name);
listEntry_t *data_entry = NULL;
keyValuePair_t *pair = NULL;
list_ForEach(data_entry, secret->data) {
pair = data_entry->data;
printf("\tkey=%s, value=%s\n", pair->key, (char *) pair->value);
}
}
v1_secret_list_free(secret_list);
secret_list = NULL;
} else {
fprintf(stderr, "The secret list is invalid.\n");
}
}
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) {
fprintf(stderr, "Cannot load kubernetes configuration.\n");
return -1;
}
apiClient_t *apiClient = apiClient_create_with_base_path(basePath, sslConfig, apiKeys);
if (!apiClient) {
fprintf(stderr, "Cannot create a kubernetes client.\n");
return -1;
}
list_secret(apiClient);
apiClient_free(apiClient);
apiClient = NULL;
free_client_config(basePath, sslConfig, apiKeys);
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();
return 0;
}