[Example] An example demonstrates deleting a pod

This commit is contained in:
Hui Yu
2020-10-07 14:05:51 +08:00
parent d8d4bc87da
commit 87ee1eeea7
4 changed files with 75 additions and 0 deletions

View File

@@ -2,6 +2,7 @@ all:
cd create_pod; make
cd list_pod; make
cd list_pod_incluster; make
cd delete_pod; make
cd exec_provider; make
cd generic; make
cd auth_provider; make
@@ -11,6 +12,7 @@ clean:
cd create_pod; make clean
cd list_pod; make clean
cd list_pod_incluster; make clean
cd delete_pod; make clean
cd exec_provider; make clean
cd generic; make clean
cd auth_provider; make clean

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

@@ -0,0 +1 @@
delete_pod_bin

View File

@@ -0,0 +1,9 @@
INCLUDE:=-I../../kubernetes/include -I../../kubernetes/model -I../../kubernetes/api -I../../kubernetes/config
LIBS:=-L../../kubernetes/build -lkubernetes -lcurl -lyaml -lpthread -lssl -lz
CFLAGS:=-g
BIN:=delete_pod_bin
all:
gcc main.c $(CFLAGS) $(INCLUDE) $(LIBS) -o $(BIN)
clean:
rm ./$(BIN)

View File

@@ -0,0 +1,63 @@
#include <kube_config.h>
#include <apiClient.h>
#include <CoreV1API.h>
#include <malloc.h>
#include <stdio.h>
#include <errno.h>
void delete_a_pod(apiClient_t * apiClient)
{
v1_status_t *status = CoreV1API_deleteNamespacedPod(apiClient,
"test-pod-6", // char *name
"default", // char *namespace
NULL, // char *pretty
NULL, // char *dryRun
0, // int gracePeriodSeconds
0, // int orphanDependents
NULL, // char *propagationPolicy
NULL // v1_delete_options_t *body
);
printf("The return code of HTTP request=%ld\n", apiClient->response_code);
if (200 == apiClient->response_code || 202 == apiClient->response_code) {
printf("The pod is deleted successfully.\n");
} else {
if (status && status->message) {
printf("Failed to delete the pod. The error message: %s\n", status->message);
}
}
if (status) {
v1_status_free(status);
status = NULL;
}
}
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) {
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;
}
delete_a_pod(apiClient);
apiClient_free(apiClient);
apiClient = NULL;
free_client_config(basePath, sslConfig, apiKeys);
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
return 0;
}