generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add examples that uses bazel c kubernetes library #255
Merged
k8s-ci-robot
merged 2 commits into
kubernetes-client:master
from
joyanta55:example_bazel
Nov 23, 2024
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
|
||
#include "kubernetes/config/kube_config.h" | ||
#include "kubernetes/include/apiClient.h" | ||
#include "kubernetes/api/CoreV1API.h" | ||
#include <malloc.h> | ||
#include <stdio.h> | ||
#include <errno.h> | ||
|
||
void create_a_pod(apiClient_t * apiClient) | ||
{ | ||
char *namespace = "default"; | ||
|
||
v1_pod_t *podinfo = calloc(1, sizeof(v1_pod_t)); | ||
podinfo->api_version = strdup("v1"); | ||
podinfo->kind = strdup("Pod"); | ||
podinfo->spec = calloc(1, sizeof(v1_pod_spec_t)); | ||
|
||
podinfo->metadata = calloc(1, sizeof(v1_object_meta_t)); | ||
/* set pod name */ | ||
podinfo->metadata->name = strdup("test-pod-6"); | ||
|
||
/* set containers for pod */ | ||
list_t *containerlist = list_createList(); | ||
v1_container_t *con = calloc(1, sizeof(v1_container_t)); | ||
con->name = strdup("my-container"); | ||
con->image = strdup("ubuntu:latest"); | ||
con->image_pull_policy = strdup("IfNotPresent"); | ||
|
||
/* set command for container */ | ||
list_t *commandlist = list_createList(); | ||
char *cmd = strdup("sleep"); | ||
list_addElement(commandlist, cmd); | ||
con->command = commandlist; | ||
|
||
list_t *arglist = list_createList(); | ||
char *arg1 = strdup("3600"); | ||
list_addElement(arglist, arg1); | ||
con->args = arglist; | ||
|
||
/* set volume mounts for container */ | ||
list_t *volumemounts = list_createList(); | ||
v1_volume_mount_t *volmou = calloc(1, sizeof(v1_volume_mount_t)); | ||
volmou->mount_path = strdup("/test"); | ||
volmou->name = strdup("test"); | ||
list_addElement(volumemounts, volmou); | ||
con->volume_mounts = volumemounts; | ||
|
||
list_addElement(containerlist, con); | ||
podinfo->spec->containers = containerlist; | ||
|
||
/* set volumes for pod */ | ||
list_t *volumelist = list_createList(); | ||
v1_volume_t *volume = calloc(1, sizeof(v1_volume_t)); | ||
volume->name = strdup("test"); | ||
|
||
v1_host_path_volume_source_t *hostPath = calloc(1, sizeof(v1_host_path_volume_source_t)); | ||
hostPath->path = strdup("/test"); | ||
volume->host_path = hostPath; | ||
|
||
list_addElement(volumelist, volume); | ||
podinfo->spec->volumes = volumelist; | ||
|
||
/* call API in libkubernetes to create pod */ | ||
v1_pod_t *apod = CoreV1API_createNamespacedPod(apiClient, namespace, podinfo, NULL, NULL, NULL, NULL); | ||
printf("code=%ld\n", apiClient->response_code); | ||
|
||
v1_pod_free(apod); | ||
v1_pod_free(podinfo); | ||
} | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
|
||
int rc = 0; | ||
|
||
char *baseName = 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 { | ||
printf("Cannot load kubernetes configuration.\n"); | ||
return -1; | ||
} | ||
|
||
if (k8sApiClient) { | ||
create_a_pod(k8sApiClient); | ||
} | ||
|
||
free_client_config(baseName, sslConfig, apiKeys); | ||
baseName = NULL; | ||
sslConfig = NULL; | ||
apiKeys = NULL; | ||
|
||
apiClient_free(k8sApiClient); | ||
k8sApiClient = NULL; | ||
apiClient_unsetupGlobalEnv(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include "kubernetes/config/kube_config.h" | ||
#include "kubernetes/api/CoreV1API.h" | ||
#include "stdio.h" | ||
|
||
void list_pod(apiClient_t * apiClient) | ||
{ | ||
v1_pod_list_t *pod_list = NULL; | ||
pod_list = CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */ | ||
NULL, /* pretty */ | ||
NULL, /* allowWatchBookmarks */ | ||
NULL, /* continue */ | ||
NULL, /* fieldSelector */ | ||
NULL, /* labelSelector */ | ||
NULL, /* limit */ | ||
NULL, /* resourceVersion */ | ||
NULL, /* resourceVersionMatch */ | ||
NULL, /* sendInitialEvents */ | ||
NULL, /* timeoutSeconds */ | ||
NULL /* watch */ | ||
); | ||
printf("The return code of HTTP request=%ld\n", apiClient->response_code); | ||
if (pod_list) { | ||
printf("Get pod list:\n"); | ||
listEntry_t *listEntry = NULL; | ||
v1_pod_t *pod = NULL; | ||
list_ForEach(listEntry, pod_list->items) { | ||
pod = listEntry->data; | ||
printf("\tThe pod name: %s\n", pod->metadata->name); | ||
} | ||
v1_pod_list_free(pod_list); | ||
pod_list = NULL; | ||
} else { | ||
printf("Cannot get any pod.\n"); | ||
} | ||
} | ||
|
||
int main() | ||
{ | ||
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; | ||
} | ||
|
||
list_pod(apiClient); | ||
|
||
apiClient_free(apiClient); | ||
apiClient = NULL; | ||
free_client_config(basePath, sslConfig, apiKeys); | ||
basePath = NULL; | ||
sslConfig = NULL; | ||
apiKeys = NULL; | ||
apiClient_unsetupGlobalEnv(); | ||
|
||
return 0; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to have separate examples for bazel? Can we just use the existing example code in
create_pod/main.c
for example?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point @brendandburns , will take care of that.
Thanks!